]> git.ipfire.org Git - thirdparty/glibc.git/blob - posix/tst-waitid.c
* posix/tst-waitid.c (do_test): Add tests for waitpid with WCONTINUED.
[thirdparty/glibc.git] / posix / tst-waitid.c
1 /* Tests for waitid.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <error.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/wait.h>
25 #include <signal.h>
26
27 #define TIMEOUT 15
28
29 static void
30 test_child (void)
31 {
32 /* Wait a second to be sure the parent set his variables before we
33 produce a SIGCHLD. */
34 sleep (1);
35
36 /* First thing, we stop ourselves. */
37 raise (SIGSTOP);
38
39 /* Hey, we got continued! */
40 while (1)
41 pause ();
42 }
43
44 #ifndef WEXITED
45 # define WEXITED 0
46 # define WCONTINUED 0
47 # define WSTOPPED WUNTRACED
48 #endif
49
50 static sig_atomic_t expecting_sigchld, spurious_sigchld;
51 #ifdef SA_SIGINFO
52 static siginfo_t sigchld_info;
53
54 static void
55 sigchld (int signo, siginfo_t *info, void *ctx)
56 {
57 if (signo != SIGCHLD)
58 {
59 error (0, 0, "SIGCHLD handler got signal %d instead!", signo);
60 _exit (EXIT_FAILURE);
61 }
62
63 if (! expecting_sigchld)
64 {
65 spurious_sigchld = 1;
66 error (0, 0,
67 "spurious SIGCHLD: signo %d code %d status %d pid %d\n",
68 info->si_signo, info->si_code, info->si_status, info->si_pid);
69 }
70 else
71 {
72 sigchld_info = *info;
73 expecting_sigchld = 0;
74 }
75 }
76
77 static void
78 check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid)
79 {
80 if (expecting_sigchld)
81 {
82 error (0, 0, "missing SIGCHLD on %s", phase);
83 *ok = EXIT_FAILURE;
84 expecting_sigchld = 0;
85 return;
86 }
87
88 if (sigchld_info.si_signo != SIGCHLD)
89 {
90 error (0, 0, "SIGCHLD for %s signal %d", phase, sigchld_info.si_signo);
91 *ok = EXIT_FAILURE;
92 }
93 if (sigchld_info.si_code != code)
94 {
95 error (0, 0, "SIGCHLD for %s code %d", phase, sigchld_info.si_code);
96 *ok = EXIT_FAILURE;
97 }
98 if (sigchld_info.si_status != status)
99 {
100 error (0, 0, "SIGCHLD for %s status %d", phase, sigchld_info.si_status);
101 *ok = EXIT_FAILURE;
102 }
103 if (sigchld_info.si_pid != pid)
104 {
105 error (0, 0, "SIGCHLD for %s pid %d", phase, sigchld_info.si_pid);
106 *ok = EXIT_FAILURE;
107 }
108 }
109 # define CHECK_SIGCHLD(phase, code_check, status_check) \
110 check_sigchld ((phase), &status, (code_check), (status_check), pid)
111 #else
112 # define CHECK_SIGCHLD(phase, code, status) ((void) 0)
113 #endif
114
115 static int
116 do_test (int argc, char *argv[])
117 {
118 #ifdef SA_SIGINFO
119 struct sigaction sa;
120 sa.sa_flags = SA_SIGINFO|SA_RESTART;
121 sa.sa_sigaction = &sigchld;
122 if (sigemptyset (&sa.sa_mask) < 0 || sigaction (SIGCHLD, &sa, NULL) < 0)
123 {
124 error (0, errno, "setting SIGCHLD handler");
125 return EXIT_FAILURE;
126 }
127 #endif
128
129 expecting_sigchld = 1;
130
131 pid_t pid = fork ();
132 if (pid < 0)
133 {
134 error (0, errno, "fork");
135 return EXIT_FAILURE;
136 }
137 else if (pid == 0)
138 {
139 test_child ();
140 _exit (127);
141 }
142
143 int status = EXIT_SUCCESS;
144 #define RETURN(ok) \
145 do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
146
147 /* Give the child a chance to stop. */
148 sleep (3);
149
150 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
151
152 /* Now try a wait that should not succeed. */
153 siginfo_t info;
154 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
155 int fail = waitid (P_PID, pid, &info, WEXITED|WCONTINUED|WNOHANG);
156 switch (fail)
157 {
158 default:
159 error (0, 0, "waitid returned bogus value %d\n", fail);
160 RETURN (EXIT_FAILURE);
161 case -1:
162 error (0, errno, "waitid WNOHANG on stopped");
163 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
164 case 0:
165 if (info.si_signo == 0)
166 break;
167 if (info.si_signo == SIGCHLD)
168 error (0, 0, "waitid WNOHANG on stopped status %d\n", info.si_status);
169 else
170 error (0, 0, "waitid WNOHANG on stopped signal %d\n", info.si_signo);
171 RETURN (EXIT_FAILURE);
172 }
173
174 /* Next the wait that should succeed right away. */
175 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
176 info.si_pid = -1;
177 info.si_status = -1;
178 fail = waitid (P_PID, pid, &info, WSTOPPED|WNOHANG);
179 switch (fail)
180 {
181 default:
182 error (0, 0, "waitid WSTOPPED|WNOHANG returned bogus value %d\n", fail);
183 RETURN (EXIT_FAILURE);
184 case -1:
185 error (0, errno, "waitid WSTOPPED|WNOHANG on stopped");
186 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
187 case 0:
188 if (info.si_signo != SIGCHLD)
189 {
190 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped signal %d\n",
191 info.si_signo);
192 RETURN (EXIT_FAILURE);
193 }
194 if (info.si_code != CLD_STOPPED)
195 {
196 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped code %d\n",
197 info.si_code);
198 RETURN (EXIT_FAILURE);
199 }
200 if (info.si_status != SIGSTOP)
201 {
202 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped status %d\n",
203 info.si_status);
204 RETURN (EXIT_FAILURE);
205 }
206 if (info.si_pid != pid)
207 {
208 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
209 info.si_pid, pid);
210 RETURN (EXIT_FAILURE);
211 }
212 }
213
214 expecting_sigchld = WCONTINUED != 0;
215
216 if (kill (pid, SIGCONT) != 0)
217 {
218 error (0, errno, "kill (%d, SIGCONT)", pid);
219 RETURN (EXIT_FAILURE);
220 }
221
222 /* Wait for the child to have continued. */
223 sleep (2);
224
225 #if WCONTINUED != 0
226 if (expecting_sigchld)
227 {
228 error (0, 0, "no SIGCHLD seen for SIGCONT (optional)");
229 expecting_sigchld = 0;
230 }
231 else
232 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
233
234 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
235 info.si_pid = -1;
236 info.si_status = -1;
237 fail = waitid (P_PID, pid, &info, WCONTINUED|WNOWAIT);
238 switch (fail)
239 {
240 default:
241 error (0, 0,
242 "waitid WCONTINUED|WNOWAIT returned bogus value %d\n", fail);
243 RETURN (EXIT_FAILURE);
244 case -1:
245 error (0, errno, "waitid WCONTINUED|WNOWAIT on continued");
246 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
247 case 0:
248 if (info.si_signo != SIGCHLD)
249 {
250 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued signal %d\n",
251 info.si_signo);
252 RETURN (EXIT_FAILURE);
253 }
254 if (info.si_code != CLD_CONTINUED)
255 {
256 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued code %d\n",
257 info.si_code);
258 RETURN (EXIT_FAILURE);
259 }
260 if (info.si_status != SIGCONT)
261 {
262 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued status %d\n",
263 info.si_status);
264 RETURN (EXIT_FAILURE);
265 }
266 if (info.si_pid != pid)
267 {
268 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
269 info.si_pid, pid);
270 RETURN (EXIT_FAILURE);
271 }
272 }
273
274 /* That should leave the CLD_CONTINUED state waiting to be seen again. */
275 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
276 info.si_pid = -1;
277 info.si_status = -1;
278 fail = waitid (P_PID, pid, &info, WCONTINUED);
279 switch (fail)
280 {
281 default:
282 error (0, 0, "waitid WCONTINUED returned bogus value %d\n", fail);
283 RETURN (EXIT_FAILURE);
284 case -1:
285 error (0, errno, "waitid WCONTINUED on continued");
286 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
287 case 0:
288 if (info.si_signo != SIGCHLD)
289 {
290 error (0, 0, "waitid WCONTINUED on continued signal %d\n",
291 info.si_signo);
292 RETURN (EXIT_FAILURE);
293 }
294 if (info.si_code != CLD_CONTINUED)
295 {
296 error (0, 0, "waitid WCONTINUED on continued code %d\n",
297 info.si_code);
298 RETURN (EXIT_FAILURE);
299 }
300 if (info.si_status != SIGCONT)
301 {
302 error (0, 0, "waitid WCONTINUED on continued status %d\n",
303 info.si_status);
304 RETURN (EXIT_FAILURE);
305 }
306 if (info.si_pid != pid)
307 {
308 error (0, 0, "waitid WCONTINUED on continued pid %d != %d\n",
309 info.si_pid, pid);
310 RETURN (EXIT_FAILURE);
311 }
312 }
313
314 /* Now try a wait that should not succeed. */
315 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
316 fail = waitid (P_PID, pid, &info, WCONTINUED|WNOHANG);
317 switch (fail)
318 {
319 default:
320 error (0, 0, "waitid returned bogus value %d\n", fail);
321 RETURN (EXIT_FAILURE);
322 case -1:
323 error (0, errno, "waitid WCONTINUED|WNOHANG on waited continued");
324 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
325 case 0:
326 if (info.si_signo == 0)
327 break;
328 if (info.si_signo == SIGCHLD)
329 error (0, 0,
330 "waitid WCONTINUED|WNOHANG on waited continued status %d\n",
331 info.si_status);
332 else
333 error (0, 0,
334 "waitid WCONTINUED|WNOHANG on waited continued signal %d\n",
335 info.si_signo);
336 RETURN (EXIT_FAILURE);
337 }
338
339 /* Now stop him again and test waitpid with WCONTINUED. */
340 expecting_sigchld = 1;
341 if (kill (pid, SIGSTOP) != 0)
342 {
343 error (0, errno, "kill (%d, SIGSTOP)", pid);
344 RETURN (EXIT_FAILURE);
345 }
346 pid_t wpid = waitpid (pid, &fail, WUNTRACED);
347 if (wpid < 0)
348 {
349 error (0, errno, "waitpid WUNTRACED on stopped");
350 RETURN (EXIT_FAILURE);
351 }
352 else if (wpid != pid)
353 {
354 error (0, 0,
355 "waitpid WUNTRACED on stopped returned %d != %d (status %x)",
356 wpid, pid, fail);
357 RETURN (EXIT_FAILURE);
358 }
359 else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
360 || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP)
361 {
362 error (0, 0, "waitpid WUNTRACED on stopped: status %x", fail);
363 RETURN (EXIT_FAILURE);
364 }
365 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
366
367 expecting_sigchld = 1;
368 if (kill (pid, SIGCONT) != 0)
369 {
370 error (0, errno, "kill (%d, SIGCONT)", pid);
371 RETURN (EXIT_FAILURE);
372 }
373
374 /* Wait for the child to have continued. */
375 sleep (2);
376
377 if (expecting_sigchld)
378 {
379 error (0, 0, "no SIGCHLD seen for SIGCONT (optional)");
380 expecting_sigchld = 0;
381 }
382 else
383 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
384
385 wpid = waitpid (pid, &fail, WCONTINUED);
386 if (wpid < 0)
387 {
388 if (errno == EINVAL)
389 error (0, 0, "waitpid does not support WCONTINUED");
390 else
391 {
392 error (0, errno, "waitpid WCONTINUED on continued");
393 RETURN (EXIT_FAILURE);
394 }
395 }
396 else if (wpid != pid)
397 {
398 error (0, 0,
399 "waitpid WCONTINUED on continued returned %d != %d (status %x)",
400 wpid, pid, fail);
401 RETURN (EXIT_FAILURE);
402 }
403 else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
404 || !WIFCONTINUED (fail))
405 {
406 error (0, 0, "waitpid WCONTINUED on continued: status %x", fail);
407 RETURN (EXIT_FAILURE);
408 }
409 #endif
410
411 expecting_sigchld = 1;
412
413 /* Die, child, die! */
414 if (kill (pid, SIGKILL) != 0)
415 {
416 error (0, errno, "kill (%d, SIGKILL)", pid);
417 RETURN (EXIT_FAILURE);
418 }
419
420 #ifdef WNOWAIT
421 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
422 info.si_pid = -1;
423 info.si_status = -1;
424 fail = waitid (P_PID, pid, &info, WEXITED|WNOWAIT);
425 switch (fail)
426 {
427 default:
428 error (0, 0, "waitid WNOWAIT returned bogus value %d\n", fail);
429 RETURN (EXIT_FAILURE);
430 case -1:
431 error (0, errno, "waitid WNOWAIT on killed");
432 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
433 case 0:
434 if (info.si_signo != SIGCHLD)
435 {
436 error (0, 0, "waitid WNOWAIT on killed signal %d\n",
437 info.si_signo);
438 RETURN (EXIT_FAILURE);
439 }
440 if (info.si_code != CLD_KILLED)
441 {
442 error (0, 0, "waitid WNOWAIT on killed code %d\n",
443 info.si_code);
444 RETURN (EXIT_FAILURE);
445 }
446 if (info.si_status != SIGKILL)
447 {
448 error (0, 0, "waitid WNOWAIT on killed status %d\n",
449 info.si_status);
450 RETURN (EXIT_FAILURE);
451 }
452 if (info.si_pid != pid)
453 {
454 error (0, 0, "waitid WNOWAIT on killed pid %d != %d\n",
455 info.si_pid, pid);
456 RETURN (EXIT_FAILURE);
457 }
458 }
459 #else
460 /* Allow enough time to be sure the child died; we didn't synchronize. */
461 sleep (2);
462 #endif
463
464 CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
465
466 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
467 info.si_pid = -1;
468 info.si_status = -1;
469 fail = waitid (P_PID, pid, &info, WEXITED|WNOHANG);
470 switch (fail)
471 {
472 default:
473 error (0, 0, "waitid WNOHANG returned bogus value %d\n", fail);
474 RETURN (EXIT_FAILURE);
475 case -1:
476 error (0, errno, "waitid WNOHANG on killed");
477 RETURN (EXIT_FAILURE);
478 case 0:
479 if (info.si_signo != SIGCHLD)
480 {
481 error (0, 0, "waitid WNOHANG on killed signal %d\n",
482 info.si_signo);
483 RETURN (EXIT_FAILURE);
484 }
485 if (info.si_code != CLD_KILLED)
486 {
487 error (0, 0, "waitid WNOHANG on killed code %d\n",
488 info.si_code);
489 RETURN (EXIT_FAILURE);
490 }
491 if (info.si_status != SIGKILL)
492 {
493 error (0, 0, "waitid WNOHANG on killed status %d\n",
494 info.si_status);
495 RETURN (EXIT_FAILURE);
496 }
497 if (info.si_pid != pid)
498 {
499 error (0, 0, "waitid WNOHANG on killed pid %d != %d\n",
500 info.si_pid, pid);
501 RETURN (EXIT_FAILURE);
502 }
503 }
504
505 fail = waitid (P_PID, pid, &info, WEXITED);
506 if (fail == -1)
507 {
508 if (errno != ECHILD)
509 {
510 error (0, errno, "waitid WEXITED on killed");
511 RETURN (EXIT_FAILURE);
512 }
513 }
514 else
515 {
516 error (0, 0, "waitid WEXITED returned bogus value %d\n", fail);
517 RETURN (EXIT_FAILURE);
518 }
519
520 #undef RETURN
521 out:
522 if (spurious_sigchld)
523 status = EXIT_FAILURE;
524 signal (SIGCHLD, SIG_IGN);
525 kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */
526 return status;
527 }
528
529 #include "../test-skeleton.c"