]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 2003-2015 Free Software Foundation, Inc. |
675620f7 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2003. | |
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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
675620f7 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <pthread.h> | |
21 | #include <signal.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <sys/wait.h> | |
25 | #include <unistd.h> | |
26 | ||
27 | ||
28 | static int fd[4]; | |
29 | static pthread_barrier_t b; | |
30 | volatile int in_sh_body; | |
31 | unsigned long cleanups; | |
32 | ||
33 | static void | |
34 | cl (void *arg) | |
35 | { | |
36 | cleanups = (cleanups << 4) | (long) arg; | |
37 | } | |
38 | ||
39 | ||
40 | static void __attribute__((noinline)) | |
41 | sh_body (void) | |
42 | { | |
43 | char c; | |
44 | ||
45 | pthread_cleanup_push (cl, (void *) 1L); | |
46 | ||
47 | in_sh_body = 1; | |
48 | if (read (fd[2], &c, 1) == 1) | |
49 | { | |
50 | puts ("read succeeded"); | |
51 | exit (1); | |
52 | } | |
53 | ||
54 | pthread_cleanup_pop (0); | |
55 | } | |
56 | ||
57 | ||
58 | static void | |
59 | sh (int sig) | |
60 | { | |
61 | pthread_cleanup_push (cl, (void *) 2L); | |
62 | sh_body (); | |
63 | in_sh_body = 0; | |
64 | ||
65 | pthread_cleanup_pop (0); | |
66 | } | |
67 | ||
68 | ||
69 | static void __attribute__((noinline)) | |
70 | tf_body (void) | |
71 | { | |
72 | char c; | |
73 | ||
74 | pthread_cleanup_push (cl, (void *) 3L); | |
75 | ||
76 | int r = pthread_barrier_wait (&b); | |
77 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) | |
78 | { | |
79 | puts ("child thread: barrier_wait failed"); | |
80 | exit (1); | |
81 | } | |
82 | ||
83 | if (read (fd[0], &c, 1) == 1) | |
84 | { | |
85 | puts ("read succeeded"); | |
86 | exit (1); | |
87 | } | |
88 | ||
89 | read (fd[0], &c, 1); | |
90 | ||
91 | pthread_cleanup_pop (0); | |
92 | } | |
93 | ||
94 | ||
95 | static void * | |
96 | tf (void *arg) | |
97 | { | |
98 | pthread_t th = (pthread_t) arg; | |
99 | ||
100 | int r = pthread_barrier_wait (&b); | |
101 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) | |
102 | { | |
103 | puts ("parent thread: barrier_wait failed"); | |
104 | exit (1); | |
105 | } | |
106 | ||
107 | sleep (1); | |
108 | ||
109 | r = pthread_kill (th, SIGHUP); | |
110 | if (r) | |
111 | { | |
112 | errno = r; | |
113 | printf ("pthread_kill failed %m\n"); | |
114 | exit (1); | |
115 | } | |
116 | ||
117 | while (in_sh_body == 0) | |
118 | sleep (1); | |
119 | ||
120 | if (pthread_cancel (th) != 0) | |
121 | { | |
122 | puts ("cancel failed"); | |
123 | exit (1); | |
124 | } | |
125 | ||
126 | /* This will cause the read in the initial thread to return. */ | |
127 | close (fd[0]); | |
128 | close (fd[1]); | |
129 | close (fd[2]); | |
130 | close (fd[3]); | |
131 | ||
132 | void *ret; | |
133 | if (pthread_join (th, &ret) != 0) | |
134 | { | |
135 | puts ("join failed"); | |
136 | exit (1); | |
137 | } | |
138 | ||
139 | if (ret != PTHREAD_CANCELED) | |
140 | { | |
141 | puts ("result is wrong"); | |
142 | exit (1); | |
143 | } | |
144 | ||
145 | if (cleanups != 0x1234L) | |
146 | { | |
147 | printf ("called cleanups %lx\n", cleanups); | |
148 | exit (1); | |
149 | } | |
150 | ||
151 | if (pthread_barrier_destroy (&b)) | |
152 | { | |
153 | puts ("barrier destroy failed"); | |
154 | exit (1); | |
155 | } | |
156 | ||
157 | exit (0); | |
158 | } | |
159 | ||
160 | ||
161 | static int | |
162 | do_one_test (void) | |
163 | { | |
f4c024d1 UD |
164 | in_sh_body = 0; |
165 | ||
675620f7 UD |
166 | pid_t pid = fork (); |
167 | ||
168 | if (pid == -1) | |
169 | { | |
170 | printf ("fork failed: %m\n"); | |
171 | return 1; | |
172 | } | |
173 | ||
174 | if (pid) | |
175 | { | |
176 | int status; | |
177 | if (waitpid (pid, &status, 0) < 0) | |
178 | { | |
179 | printf ("waitpid failed %m\n"); | |
180 | return 1; | |
181 | } | |
182 | ||
183 | return !WIFEXITED (status) || WEXITSTATUS (status); | |
184 | } | |
185 | ||
186 | if (pthread_barrier_init (&b, NULL, 2) != 0) | |
187 | { | |
188 | puts ("barrier_init failed"); | |
189 | exit (1); | |
190 | } | |
191 | ||
192 | cleanups = 0; | |
193 | if (pipe (fd) != 0 || pipe (fd + 2) != 0) | |
194 | { | |
195 | puts ("pipe failed"); | |
196 | exit (1); | |
197 | } | |
198 | ||
199 | pthread_t th; | |
200 | if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0) | |
201 | { | |
202 | puts ("create failed"); | |
203 | exit (1); | |
204 | } | |
205 | ||
206 | pthread_cleanup_push (cl, (void *) 4L); | |
207 | tf_body (); | |
208 | pthread_cleanup_pop (0); | |
209 | exit (1); | |
210 | } | |
211 | ||
212 | ||
213 | static int | |
214 | do_test (void) | |
215 | { | |
216 | stack_t ss; | |
217 | ss.ss_sp = malloc (2 * SIGSTKSZ); | |
218 | if (ss.ss_sp == NULL) | |
219 | { | |
220 | puts ("failed to allocate alternate stack"); | |
221 | return 1; | |
222 | } | |
223 | ss.ss_flags = 0; | |
224 | ss.ss_size = 2 * SIGSTKSZ; | |
225 | if (sigaltstack (&ss, NULL) < 0) | |
226 | { | |
227 | printf ("sigaltstack failed %m\n"); | |
228 | return 1; | |
229 | } | |
230 | ||
231 | struct sigaction sa; | |
232 | sa.sa_handler = sh; | |
233 | sigemptyset (&sa.sa_mask); | |
234 | sa.sa_flags = 0; | |
235 | ||
236 | if (sigaction (SIGHUP, &sa, NULL) != 0) | |
237 | { | |
238 | puts ("sigaction failed"); | |
239 | return 1; | |
240 | } | |
241 | ||
242 | puts ("sa_flags = 0 test"); | |
243 | if (do_one_test ()) | |
244 | return 1; | |
245 | ||
246 | sa.sa_handler = sh; | |
247 | sigemptyset (&sa.sa_mask); | |
248 | sa.sa_flags = SA_ONSTACK; | |
249 | ||
250 | if (sigaction (SIGHUP, &sa, NULL) != 0) | |
251 | { | |
252 | puts ("sigaction failed"); | |
253 | return 1; | |
254 | } | |
255 | ||
256 | puts ("sa_flags = SA_ONSTACK test"); | |
257 | if (do_one_test ()) | |
258 | return 1; | |
259 | ||
260 | sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh; | |
261 | sigemptyset (&sa.sa_mask); | |
262 | sa.sa_flags = SA_SIGINFO; | |
263 | ||
264 | if (sigaction (SIGHUP, &sa, NULL) != 0) | |
265 | { | |
266 | puts ("sigaction failed"); | |
267 | return 1; | |
268 | } | |
269 | ||
270 | puts ("sa_flags = SA_SIGINFO test"); | |
271 | if (do_one_test ()) | |
272 | return 1; | |
273 | ||
274 | sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh; | |
275 | sigemptyset (&sa.sa_mask); | |
276 | sa.sa_flags = SA_SIGINFO | SA_ONSTACK; | |
277 | ||
278 | if (sigaction (SIGHUP, &sa, NULL) != 0) | |
279 | { | |
280 | puts ("sigaction failed"); | |
281 | return 1; | |
282 | } | |
283 | ||
284 | puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test"); | |
285 | if (do_one_test ()) | |
286 | return 1; | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | #define TIMEOUT 40 | |
292 | #define TEST_FUNCTION do_test () | |
293 | #include "../test-skeleton.c" |