configure: Use -Wno-ignored-attributes if compiler warns about multiple aliases
[glibc.git] / sysdeps / pthread / tst-signal1.c
blob6a9e8a928aaca6feafc15086a7a35465b890af44
1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2 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.
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.
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/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
29 static sigset_t ss;
30 static pthread_barrier_t *b;
33 static void *
34 tf (void *arg)
36 sigdelset (&ss, SIGINT);
38 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
40 puts ("2nd pthread_sigmask failed");
41 exit (1);
44 pthread_barrier_wait (b);
46 int sig;
47 int res = sigwait (&ss, &sig);
48 if (res == 0)
50 printf ("sigwait returned successfully with signal %d\n", sig);
51 exit (1);
54 printf ("sigwait returned with %s (%d)\n", strerror (res), res);
56 return NULL;
60 static void
61 receiver (void)
63 pthread_t th;
65 /* Make sure the process doesn't run forever. */
66 alarm (10);
68 sigfillset (&ss);
70 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
72 puts ("1st pthread_sigmask failed");
73 exit (1);
76 if (pthread_create (&th, NULL, tf, NULL) != 0)
78 puts ("pthread_create failed");
79 exit (1);
82 if (pthread_join (th, NULL) == 0)
84 puts ("thread joined?!");
85 exit (1);
88 _exit (0);
92 static int
93 do_test (void)
95 char tmp[] = "/tmp/tst-signal1-XXXXXX";
97 int fd = mkstemp (tmp);
98 if (fd == -1)
100 puts ("mkstemp failed");
101 exit (1);
104 unlink (tmp);
106 int i;
107 for (i = 0; i < 20; ++i)
108 write (fd, "foobar xyzzy", 12);
110 b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
111 MAP_SHARED, fd, 0);
112 if (b == MAP_FAILED)
114 puts ("mmap failed");
115 exit (1);
118 pthread_barrierattr_t ba;
119 if (pthread_barrierattr_init (&ba) != 0)
121 puts ("barrierattr_init failed");
122 exit (1);
125 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
127 puts ("barrierattr_setpshared failed");
128 exit (1);
131 if (pthread_barrier_init (b, &ba, 2) != 0)
133 puts ("barrier_init failed");
134 exit (1);
137 if (pthread_barrierattr_destroy (&ba) != 0)
139 puts ("barrierattr_destroy failed");
140 exit (1);
143 pid_t pid = fork ();
144 if (pid == -1)
146 puts ("fork failed");
147 exit (1);
150 if (pid == 0)
151 receiver ();
153 pthread_barrier_wait (b);
155 /* Wait a bit more. */
156 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
157 nanosleep (&ts, NULL);
159 /* Send the signal. */
160 puts ("sending the signal now");
161 kill (pid, SIGINT);
163 /* Wait for the process to terminate. */
164 int status;
165 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
167 puts ("wrong child reported terminated");
168 exit (1);
171 if (!WIFSIGNALED (status))
173 puts ("child wasn't signalled");
174 exit (1);
177 if (WTERMSIG (status) != SIGINT)
179 puts ("child not terminated with SIGINT");
180 exit (1);
183 return 0;
186 #define TEST_FUNCTION do_test ()
187 #include "../test-skeleton.c"