Fix warning from latest GCC in tst-printf.c
[glibc.git] / nptl / tst-signal2.c
blobae5178fd183e9a1484799aa1339e61d2243d9e8c
1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
27 #include <string.h>
30 static sigset_t ss;
31 static pthread_barrier_t *b;
34 static void *
35 tf (void *arg)
37 pthread_barrier_wait (b);
39 puts ("child: calling sigwait now");
41 int sig;
42 int err;
43 err = sigwait (&ss, &sig);
44 if (err != 0)
46 printf ("sigwait returned unsuccessfully: %s (%d)\n",
47 strerror (err), err);
48 _exit (1);
51 puts ("sigwait returned");
53 if (sig != SIGINT)
55 printf ("caught signal %d, expected %d (SIGINT)\n", sig, SIGINT);
56 _exit (1);
59 puts ("child thread terminating now");
61 return NULL;
65 static void
66 receiver (void)
68 pthread_t th;
70 /* Make sure the process doesn't run forever. */
71 alarm (10);
73 sigfillset (&ss);
75 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
77 puts ("1st pthread_sigmask failed");
78 _exit (1);
81 if (pthread_create (&th, NULL, tf, NULL) != 0)
83 puts ("pthread_create failed");
84 _exit (1);
87 if (pthread_join (th, NULL) != 0)
89 puts ("thread didn't join");
90 _exit (1);
93 puts ("join succeeded");
95 _exit (0);
99 static int
100 do_test (void)
102 char tmp[] = "/tmp/tst-signal1-XXXXXX";
104 int fd = mkstemp (tmp);
105 if (fd == -1)
107 puts ("mkstemp failed");
108 exit (1);
111 unlink (tmp);
113 int i;
114 for (i = 0; i < 20; ++i)
115 write (fd, "foobar xyzzy", 12);
117 b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
118 MAP_SHARED, fd, 0);
119 if (b == MAP_FAILED)
121 puts ("mmap failed");
122 exit (1);
125 pthread_barrierattr_t ba;
126 if (pthread_barrierattr_init (&ba) != 0)
128 puts ("barrierattr_init failed");
129 exit (1);
132 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
134 puts ("barrierattr_setpshared failed");
135 exit (1);
138 if (pthread_barrier_init (b, &ba, 2) != 0)
140 puts ("barrier_init failed");
141 exit (1);
144 if (pthread_barrierattr_destroy (&ba) != 0)
146 puts ("barrierattr_destroy failed");
147 exit (1);
150 pid_t pid = fork ();
151 if (pid == -1)
153 puts ("fork failed");
154 exit (1);
157 if (pid == 0)
158 receiver ();
160 pthread_barrier_wait (b);
162 /* Wait a bit more. */
163 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
164 nanosleep (&ts, NULL);
166 /* Send the signal. */
167 puts ("sending the signal now");
168 kill (pid, SIGINT);
170 /* Wait for the process to terminate. */
171 int status;
172 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
174 puts ("wrong child reported terminated");
175 exit (1);
178 if (!WIFEXITED (status))
180 if (WIFSIGNALED (status))
181 printf ("child exited with signal %d\n", WTERMSIG (status));
182 else
183 puts ("child didn't exit normally");
184 exit (1);
187 if (WEXITSTATUS (status) != 0)
189 printf ("exit status %d != 0\n", WEXITSTATUS (status));
190 exit (1);
193 return 0;
196 #define TEST_FUNCTION do_test ()
197 #include "../test-skeleton.c"