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