tst-gettext6.c 2.1 KB

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