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