1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
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 
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 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/wait.h>
26 #include <string.h>
27 
28 
29 static sigset_t ss;
30 static pthread_barrier_t *b;
31 
32 
33 static void *
tf(void * arg)34 tf (void *arg)
35 {
36   pthread_barrier_wait (b);
37 
38   puts ("child: calling sigwait now");
39 
40   int sig;
41   int err;
42   err = sigwait (&ss, &sig);
43   if (err != 0)
44     {
45       printf ("sigwait returned unsuccessfully: %s (%d)\n",
46 	      strerror (err), err);
47       _exit (1);
48     }
49 
50   puts ("sigwait returned");
51 
52   if (sig != SIGINT)
53     {
54       printf ("caught signal %d, expected %d (SIGINT)\n", sig, SIGINT);
55       _exit (1);
56     }
57 
58   puts ("child thread terminating now");
59 
60   return NULL;
61 }
62 
63 
64 static void
receiver(void)65 receiver (void)
66 {
67   pthread_t th;
68 
69   /* Make sure the process doesn't run forever.  */
70   alarm (10);
71 
72   sigfillset (&ss);
73 
74   if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
75     {
76       puts ("1st pthread_sigmask failed");
77       _exit (1);
78     }
79 
80   if (pthread_create (&th, NULL, tf, NULL) != 0)
81     {
82       puts ("pthread_create failed");
83       _exit (1);
84     }
85 
86   if (pthread_join (th, NULL) != 0)
87     {
88       puts ("thread didn't join");
89       _exit (1);
90     }
91 
92   puts ("join succeeded");
93 
94   _exit (0);
95 }
96 
97 
98 static int
do_test(void)99 do_test (void)
100 {
101   char tmp[] = "/tmp/tst-signal1-XXXXXX";
102 
103   int fd = mkstemp (tmp);
104   if (fd == -1)
105     {
106       puts ("mkstemp failed");
107       exit (1);
108     }
109 
110   unlink (tmp);
111 
112   int i;
113   for (i = 0; i < 20; ++i)
114     write (fd, "foobar xyzzy", 12);
115 
116   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
117 	    MAP_SHARED, fd, 0);
118   if (b == MAP_FAILED)
119     {
120       puts ("mmap failed");
121       exit (1);
122     }
123 
124   pthread_barrierattr_t ba;
125   if (pthread_barrierattr_init (&ba) != 0)
126     {
127       puts ("barrierattr_init failed");
128       exit (1);
129     }
130 
131   if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
132     {
133       puts ("barrierattr_setpshared failed");
134       exit (1);
135     }
136 
137   if (pthread_barrier_init (b, &ba, 2) != 0)
138     {
139       puts ("barrier_init failed");
140       exit (1);
141     }
142 
143   if (pthread_barrierattr_destroy (&ba) != 0)
144     {
145       puts ("barrierattr_destroy failed");
146       exit (1);
147     }
148 
149   pid_t pid = fork ();
150   if (pid == -1)
151     {
152       puts ("fork failed");
153       exit (1);
154     }
155 
156   if (pid == 0)
157     receiver ();
158 
159   pthread_barrier_wait (b);
160 
161   /* Wait a bit more.  */
162   struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
163   nanosleep (&ts, NULL);
164 
165   /* Send the signal.  */
166   puts ("sending the signal now");
167   kill (pid, SIGINT);
168 
169   /* Wait for the process to terminate.  */
170   int status;
171   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
172     {
173       puts ("wrong child reported terminated");
174       exit (1);
175     }
176 
177   if (!WIFEXITED (status))
178     {
179       if (WIFSIGNALED (status))
180 	printf ("child exited with signal %d\n", WTERMSIG (status));
181       else
182 	puts ("child didn't exit normally");
183       exit (1);
184     }
185 
186   if (WEXITSTATUS (status) != 0)
187     {
188       printf ("exit status %d != 0\n", WEXITSTATUS (status));
189       exit (1);
190     }
191 
192   return 0;
193 }
194 
195 #define TEST_FUNCTION do_test ()
196 #include "../test-skeleton.c"
197