.
[glibc/history.git] / posix / tst-waitid.c
blobd810922f04ddc836799df30fbea696d27714455a
1 /* Tests for waitid.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/wait.h>
25 #include <signal.h>
27 #define TIMEOUT 15
29 static void
30 test_child (void)
32 /* Wait a second to be sure the parent set his variables before we
33 produce a SIGCHLD. */
34 sleep (1);
36 /* First thing, we stop ourselves. */
37 raise (SIGSTOP);
39 /* Hey, we got continued! */
40 while (1)
41 pause ();
44 #ifndef WEXITED
45 # define WEXITED 0
46 # define WCONTINUED 0
47 # define WSTOPPED WUNTRACED
48 #endif
50 static sig_atomic_t expecting_sigchld, spurious_sigchld;
51 #ifdef SA_SIGINFO
52 static siginfo_t sigchld_info;
54 static void
55 sigchld (int signo, siginfo_t *info, void *ctx)
57 if (signo != SIGCHLD)
59 printf ("SIGCHLD handler got signal %d instead!\n", signo);
60 _exit (EXIT_FAILURE);
63 if (! expecting_sigchld)
65 spurious_sigchld = 1;
66 printf ("spurious SIGCHLD: signo %d code %d status %d pid %d\n",
67 info->si_signo, info->si_code, info->si_status, info->si_pid);
69 else
71 sigchld_info = *info;
72 expecting_sigchld = 0;
76 static void
77 check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid)
79 if (expecting_sigchld)
81 printf ("missing SIGCHLD on %s\n", phase);
82 *ok = EXIT_FAILURE;
83 expecting_sigchld = 0;
84 return;
87 if (sigchld_info.si_signo != SIGCHLD)
89 printf ("SIGCHLD for %s signal %d\n", phase, sigchld_info.si_signo);
90 *ok = EXIT_FAILURE;
92 if (sigchld_info.si_code != code)
94 printf ("SIGCHLD for %s code %d\n", phase, sigchld_info.si_code);
95 *ok = EXIT_FAILURE;
97 if (sigchld_info.si_status != status)
99 printf ("SIGCHLD for %s status %d\n", phase, sigchld_info.si_status);
100 *ok = EXIT_FAILURE;
102 if (sigchld_info.si_pid != pid)
104 printf ("SIGCHLD for %s pid %d\n", phase, sigchld_info.si_pid);
105 *ok = EXIT_FAILURE;
108 # define CHECK_SIGCHLD(phase, code_check, status_check) \
109 check_sigchld ((phase), &status, (code_check), (status_check), pid)
110 #else
111 # define CHECK_SIGCHLD(phase, code, status) ((void) 0)
112 #endif
114 static int
115 do_test (int argc, char *argv[])
117 #ifdef SA_SIGINFO
118 struct sigaction sa;
119 sa.sa_flags = SA_SIGINFO|SA_RESTART;
120 sa.sa_sigaction = &sigchld;
121 if (sigemptyset (&sa.sa_mask) < 0 || sigaction (SIGCHLD, &sa, NULL) < 0)
123 printf ("setting SIGCHLD handler: %m\n");
124 return EXIT_FAILURE;
126 #endif
128 expecting_sigchld = 1;
130 pid_t pid = fork ();
131 if (pid < 0)
133 printf ("fork: %m\n");
134 return EXIT_FAILURE;
136 else if (pid == 0)
138 test_child ();
139 _exit (127);
142 int status = EXIT_SUCCESS;
143 #define RETURN(ok) \
144 do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
146 /* Give the child a chance to stop. */
147 sleep (3);
149 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
151 /* Now try a wait that should not succeed. */
152 siginfo_t info;
153 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
154 int fail = waitid (P_PID, pid, &info, WEXITED|WCONTINUED|WNOHANG);
155 switch (fail)
157 default:
158 printf ("waitid returned bogus value %d\n", fail);
159 RETURN (EXIT_FAILURE);
160 case -1:
161 printf ("waitid WNOHANG on stopped: %m\n");
162 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
163 case 0:
164 if (info.si_signo == 0)
165 break;
166 if (info.si_signo == SIGCHLD)
167 printf ("waitid WNOHANG on stopped status %d\n", info.si_status);
168 else
169 printf ("waitid WNOHANG on stopped signal %d\n", info.si_signo);
170 RETURN (EXIT_FAILURE);
173 /* Next the wait that should succeed right away. */
174 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
175 info.si_pid = -1;
176 info.si_status = -1;
177 fail = waitid (P_PID, pid, &info, WSTOPPED|WNOHANG);
178 switch (fail)
180 default:
181 printf ("waitid WSTOPPED|WNOHANG returned bogus value %d\n", fail);
182 RETURN (EXIT_FAILURE);
183 case -1:
184 printf ("waitid WSTOPPED|WNOHANG on stopped: %m\n");
185 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
186 case 0:
187 if (info.si_signo != SIGCHLD)
189 printf ("waitid WSTOPPED|WNOHANG on stopped signal %d\n",
190 info.si_signo);
191 RETURN (EXIT_FAILURE);
193 if (info.si_code != CLD_STOPPED)
195 printf ("waitid WSTOPPED|WNOHANG on stopped code %d\n",
196 info.si_code);
197 RETURN (EXIT_FAILURE);
199 if (info.si_status != SIGSTOP)
201 printf ("waitid WSTOPPED|WNOHANG on stopped status %d\n",
202 info.si_status);
203 RETURN (EXIT_FAILURE);
205 if (info.si_pid != pid)
207 printf ("waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
208 info.si_pid, pid);
209 RETURN (EXIT_FAILURE);
213 expecting_sigchld = WCONTINUED != 0;
215 if (kill (pid, SIGCONT) != 0)
217 printf ("kill (%d, SIGCONT): %m\n", pid);
218 RETURN (EXIT_FAILURE);
221 /* Wait for the child to have continued. */
222 sleep (2);
224 #if WCONTINUED != 0
225 if (expecting_sigchld)
227 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
228 expecting_sigchld = 0;
230 else
231 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
233 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
234 info.si_pid = -1;
235 info.si_status = -1;
236 fail = waitid (P_PID, pid, &info, WCONTINUED|WNOWAIT);
237 switch (fail)
239 default:
240 printf ("waitid WCONTINUED|WNOWAIT returned bogus value %d\n", fail);
241 RETURN (EXIT_FAILURE);
242 case -1:
243 printf ("waitid WCONTINUED|WNOWAIT on continued: %m\n");
244 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
245 case 0:
246 if (info.si_signo != SIGCHLD)
248 printf ("waitid WCONTINUED|WNOWAIT on continued signal %d\n",
249 info.si_signo);
250 RETURN (EXIT_FAILURE);
252 if (info.si_code != CLD_CONTINUED)
254 printf ("waitid WCONTINUED|WNOWAIT on continued code %d\n",
255 info.si_code);
256 RETURN (EXIT_FAILURE);
258 if (info.si_status != SIGCONT)
260 printf ("waitid WCONTINUED|WNOWAIT on continued status %d\n",
261 info.si_status);
262 RETURN (EXIT_FAILURE);
264 if (info.si_pid != pid)
266 printf ("waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
267 info.si_pid, pid);
268 RETURN (EXIT_FAILURE);
272 /* That should leave the CLD_CONTINUED state waiting to be seen again. */
273 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
274 info.si_pid = -1;
275 info.si_status = -1;
276 fail = waitid (P_PID, pid, &info, WCONTINUED);
277 switch (fail)
279 default:
280 printf ("waitid WCONTINUED returned bogus value %d\n", fail);
281 RETURN (EXIT_FAILURE);
282 case -1:
283 printf ("waitid WCONTINUED on continued: %m\n");
284 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
285 case 0:
286 if (info.si_signo != SIGCHLD)
288 printf ("waitid WCONTINUED on continued signal %d\n", info.si_signo);
289 RETURN (EXIT_FAILURE);
291 if (info.si_code != CLD_CONTINUED)
293 printf ("waitid WCONTINUED on continued code %d\n", info.si_code);
294 RETURN (EXIT_FAILURE);
296 if (info.si_status != SIGCONT)
298 printf ("waitid WCONTINUED on continued status %d\n",
299 info.si_status);
300 RETURN (EXIT_FAILURE);
302 if (info.si_pid != pid)
304 printf ("waitid WCONTINUED on continued pid %d != %d\n",
305 info.si_pid, pid);
306 RETURN (EXIT_FAILURE);
310 /* Now try a wait that should not succeed. */
311 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
312 fail = waitid (P_PID, pid, &info, WCONTINUED|WNOHANG);
313 switch (fail)
315 default:
316 printf ("waitid returned bogus value %d\n", fail);
317 RETURN (EXIT_FAILURE);
318 case -1:
319 printf ("waitid WCONTINUED|WNOHANG on waited continued: %m\n");
320 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
321 case 0:
322 if (info.si_signo == 0)
323 break;
324 if (info.si_signo == SIGCHLD)
325 printf ("waitid WCONTINUED|WNOHANG on waited continued status %d\n",
326 info.si_status);
327 else
328 printf ("waitid WCONTINUED|WNOHANG on waited continued signal %d\n",
329 info.si_signo);
330 RETURN (EXIT_FAILURE);
333 /* Now stop him again and test waitpid with WCONTINUED. */
334 expecting_sigchld = 1;
335 if (kill (pid, SIGSTOP) != 0)
337 printf ("kill (%d, SIGSTOP): %m\n", pid);
338 RETURN (EXIT_FAILURE);
340 pid_t wpid = waitpid (pid, &fail, WUNTRACED);
341 if (wpid < 0)
343 printf ("waitpid WUNTRACED on stopped: %m\n");
344 RETURN (EXIT_FAILURE);
346 else if (wpid != pid)
348 printf ("waitpid WUNTRACED on stopped returned %d != %d (status %x)\n",
349 wpid, pid, fail);
350 RETURN (EXIT_FAILURE);
352 else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
353 || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP)
355 printf ("waitpid WUNTRACED on stopped: status %x\n", fail);
356 RETURN (EXIT_FAILURE);
358 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
360 expecting_sigchld = 1;
361 if (kill (pid, SIGCONT) != 0)
363 printf ("kill (%d, SIGCONT): %m\n", pid);
364 RETURN (EXIT_FAILURE);
367 /* Wait for the child to have continued. */
368 sleep (2);
370 if (expecting_sigchld)
372 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
373 expecting_sigchld = 0;
375 else
376 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
378 wpid = waitpid (pid, &fail, WCONTINUED);
379 if (wpid < 0)
381 if (errno == EINVAL)
382 printf ("waitpid does not support WCONTINUED\n");
383 else
385 printf ("waitpid WCONTINUED on continued: %m\n");
386 RETURN (EXIT_FAILURE);
389 else if (wpid != pid)
391 printf ("\
392 waitpid WCONTINUED on continued returned %d != %d (status %x)\n",
393 wpid, pid, fail);
394 RETURN (EXIT_FAILURE);
396 else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
397 || !WIFCONTINUED (fail))
399 printf ("waitpid WCONTINUED on continued: status %x\n", fail);
400 RETURN (EXIT_FAILURE);
402 #endif
404 expecting_sigchld = 1;
406 /* Die, child, die! */
407 if (kill (pid, SIGKILL) != 0)
409 printf ("kill (%d, SIGKILL): %m\n", pid);
410 RETURN (EXIT_FAILURE);
413 #ifdef WNOWAIT
414 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
415 info.si_pid = -1;
416 info.si_status = -1;
417 fail = waitid (P_PID, pid, &info, WEXITED|WNOWAIT);
418 switch (fail)
420 default:
421 printf ("waitid WNOWAIT returned bogus value %d\n", fail);
422 RETURN (EXIT_FAILURE);
423 case -1:
424 printf ("waitid WNOWAIT on killed: %m\n");
425 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
426 case 0:
427 if (info.si_signo != SIGCHLD)
429 printf ("waitid WNOWAIT on killed signal %d\n", info.si_signo);
430 RETURN (EXIT_FAILURE);
432 if (info.si_code != CLD_KILLED)
434 printf ("waitid WNOWAIT on killed code %d\n", info.si_code);
435 RETURN (EXIT_FAILURE);
437 if (info.si_status != SIGKILL)
439 printf ("waitid WNOWAIT on killed status %d\n", info.si_status);
440 RETURN (EXIT_FAILURE);
442 if (info.si_pid != pid)
444 printf ("waitid WNOWAIT on killed pid %d != %d\n", info.si_pid, pid);
445 RETURN (EXIT_FAILURE);
448 #else
449 /* Allow enough time to be sure the child died; we didn't synchronize. */
450 sleep (2);
451 #endif
453 CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
455 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
456 info.si_pid = -1;
457 info.si_status = -1;
458 fail = waitid (P_PID, pid, &info, WEXITED|WNOHANG);
459 switch (fail)
461 default:
462 printf ("waitid WNOHANG returned bogus value %d\n", fail);
463 RETURN (EXIT_FAILURE);
464 case -1:
465 printf ("waitid WNOHANG on killed: %m\n");
466 RETURN (EXIT_FAILURE);
467 case 0:
468 if (info.si_signo != SIGCHLD)
470 printf ("waitid WNOHANG on killed signal %d\n", info.si_signo);
471 RETURN (EXIT_FAILURE);
473 if (info.si_code != CLD_KILLED)
475 printf ("waitid WNOHANG on killed code %d\n", info.si_code);
476 RETURN (EXIT_FAILURE);
478 if (info.si_status != SIGKILL)
480 printf ("waitid WNOHANG on killed status %d\n", info.si_status);
481 RETURN (EXIT_FAILURE);
483 if (info.si_pid != pid)
485 printf ("waitid WNOHANG on killed pid %d != %d\n", info.si_pid, pid);
486 RETURN (EXIT_FAILURE);
490 fail = waitid (P_PID, pid, &info, WEXITED);
491 if (fail == -1)
493 if (errno != ECHILD)
495 printf ("waitid WEXITED on killed: %m\n");
496 RETURN (EXIT_FAILURE);
499 else
501 printf ("waitid WEXITED returned bogus value %d\n", fail);
502 RETURN (EXIT_FAILURE);
505 #undef RETURN
506 out:
507 if (spurious_sigchld)
508 status = EXIT_FAILURE;
509 signal (SIGCHLD, SIG_IGN);
510 kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */
511 return status;
514 #include "../test-skeleton.c"