Makeconfig: sandwich gnulib-tests between libc/ld linking of tests
[glibc.git] / nptl / tst-signal1.c
blob2779213dc2182c2e65f46469e2bebea78a1f8d23
1 /* Copyright (C) 2002-2020 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 <https://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 <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
30 static sigset_t ss;
31 static pthread_barrier_t *b;
34 static void *
35 tf (void *arg)
37 sigdelset (&ss, SIGINT);
39 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
41 puts ("2nd pthread_sigmask failed");
42 exit (1);
45 pthread_barrier_wait (b);
47 int sig;
48 int res = sigwait (&ss, &sig);
49 if (res == 0)
51 printf ("sigwait returned successfully with signal %d\n", sig);
52 exit (1);
55 printf ("sigwait returned with %s (%d)\n", strerror (res), res);
57 return NULL;
61 static void
62 receiver (void)
64 pthread_t th;
66 /* Make sure the process doesn't run forever. */
67 alarm (10);
69 sigfillset (&ss);
71 if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
73 puts ("1st pthread_sigmask failed");
74 exit (1);
77 if (pthread_create (&th, NULL, tf, NULL) != 0)
79 puts ("pthread_create failed");
80 exit (1);
83 if (pthread_join (th, NULL) == 0)
85 puts ("thread joined?!");
86 exit (1);
89 _exit (0);
93 static int
94 do_test (void)
96 char tmp[] = "/tmp/tst-signal1-XXXXXX";
98 int fd = mkstemp (tmp);
99 if (fd == -1)
101 puts ("mkstemp failed");
102 exit (1);
105 unlink (tmp);
107 int i;
108 for (i = 0; i < 20; ++i)
109 write (fd, "foobar xyzzy", 12);
111 b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
112 MAP_SHARED, fd, 0);
113 if (b == MAP_FAILED)
115 puts ("mmap failed");
116 exit (1);
119 pthread_barrierattr_t ba;
120 if (pthread_barrierattr_init (&ba) != 0)
122 puts ("barrierattr_init failed");
123 exit (1);
126 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
128 puts ("barrierattr_setpshared failed");
129 exit (1);
132 if (pthread_barrier_init (b, &ba, 2) != 0)
134 puts ("barrier_init failed");
135 exit (1);
138 if (pthread_barrierattr_destroy (&ba) != 0)
140 puts ("barrierattr_destroy failed");
141 exit (1);
144 pid_t pid = fork ();
145 if (pid == -1)
147 puts ("fork failed");
148 exit (1);
151 if (pid == 0)
152 receiver ();
154 pthread_barrier_wait (b);
156 /* Wait a bit more. */
157 struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
158 nanosleep (&ts, NULL);
160 /* Send the signal. */
161 puts ("sending the signal now");
162 kill (pid, SIGINT);
164 /* Wait for the process to terminate. */
165 int status;
166 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
168 puts ("wrong child reported terminated");
169 exit (1);
172 if (!WIFSIGNALED (status))
174 puts ("child wasn't signalled");
175 exit (1);
178 if (WTERMSIG (status) != SIGINT)
180 puts ("child not terminated with SIGINT");
181 exit (1);
184 return 0;
187 #define TEST_FUNCTION do_test ()
188 #include "../test-skeleton.c"