tst-gettext6.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Test that gettext() in multithreaded applications works correctly.
  2. Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <libintl.h>
  17. #include <locale.h>
  18. #include <pthread.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/wait.h>
  22. #include <unistd.h>
  23. pthread_barrier_t b;
  24. static void *
  25. tf (void *arg)
  26. {
  27. pthread_barrier_wait (&b);
  28. return gettext ("Operation not permitted");
  29. }
  30. int
  31. test (void)
  32. {
  33. pthread_t th[4];
  34. unsetenv ("LANGUAGE");
  35. unsetenv ("OUTPUT_CHARSET");
  36. textdomain ("tstgettext6");
  37. bindtextdomain ("tstgettext6", OBJPFX "domaindir");
  38. setlocale (LC_ALL, "ja_JP.UTF-8");
  39. pthread_barrier_init (&b, NULL, 4);
  40. for (int i = 0; i < 4; i++)
  41. if (pthread_create (&th[i], NULL, tf, NULL))
  42. {
  43. puts ("pthread_create failed");
  44. return 1;
  45. }
  46. for (int i = 0; i < 4; i++)
  47. pthread_join (th[i], NULL);
  48. return 0;
  49. }
  50. int
  51. main (void)
  52. {
  53. for (int i = 0; i < 300; i++)
  54. {
  55. pid_t p = fork ();
  56. if (p == -1)
  57. {
  58. printf ("fork failed: %m\n");
  59. return 1;
  60. }
  61. if (p == 0)
  62. _exit (test ());
  63. int status;
  64. wait (&status);
  65. if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
  66. {
  67. printf ("child exited with %d\n", WEXITSTATUS (status));
  68. return 1;
  69. }
  70. else if (WIFSIGNALED (status))
  71. {
  72. printf ("child killed by signal %d\n", WTERMSIG (status));
  73. return 1;
  74. }
  75. }
  76. return 0;
  77. }