* sysdeps/generic/strcpy_chk.c (__strcpy_chk): Speed up by checking
[glibc.git] / posix / tst-waitid.c
blob642d529a9a965de1ea3fbc95435b8f915ac8a35f
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 <error.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 error (0, 0, "SIGCHLD handler got signal %d instead!", signo);
60 _exit (EXIT_FAILURE);
63 if (! expecting_sigchld)
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);
70 else
72 sigchld_info = *info;
73 expecting_sigchld = 0;
77 static void
78 check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid)
80 if (expecting_sigchld)
82 error (0, 0, "missing SIGCHLD on %s", phase);
83 *ok = EXIT_FAILURE;
84 expecting_sigchld = 0;
85 return;
88 if (sigchld_info.si_signo != SIGCHLD)
90 error (0, 0, "SIGCHLD for %s signal %d", phase, sigchld_info.si_signo);
91 *ok = EXIT_FAILURE;
93 if (sigchld_info.si_code != code)
95 error (0, 0, "SIGCHLD for %s code %d", phase, sigchld_info.si_code);
96 *ok = EXIT_FAILURE;
98 if (sigchld_info.si_status != status)
100 error (0, 0, "SIGCHLD for %s status %d", phase, sigchld_info.si_status);
101 *ok = EXIT_FAILURE;
103 if (sigchld_info.si_pid != pid)
105 error (0, 0, "SIGCHLD for %s pid %d", phase, sigchld_info.si_pid);
106 *ok = EXIT_FAILURE;
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
115 static int
116 do_test (int argc, char *argv[])
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)
124 error (0, errno, "setting SIGCHLD handler");
125 return EXIT_FAILURE;
127 #endif
129 expecting_sigchld = 1;
131 pid_t pid = fork ();
132 if (pid < 0)
134 error (0, errno, "fork");
135 return EXIT_FAILURE;
137 else if (pid == 0)
139 test_child ();
140 _exit (127);
143 int status = EXIT_SUCCESS;
144 #define RETURN(ok) \
145 do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
147 /* Give the child a chance to stop. */
148 sleep (3);
150 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
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)
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);
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)
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)
190 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped signal %d\n",
191 info.si_signo);
192 RETURN (EXIT_FAILURE);
194 if (info.si_code != CLD_STOPPED)
196 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped code %d\n",
197 info.si_code);
198 RETURN (EXIT_FAILURE);
200 if (info.si_status != SIGSTOP)
202 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped status %d\n",
203 info.si_status);
204 RETURN (EXIT_FAILURE);
206 if (info.si_pid != pid)
208 error (0, 0, "waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
209 info.si_pid, pid);
210 RETURN (EXIT_FAILURE);
214 expecting_sigchld = WCONTINUED != 0;
216 if (kill (pid, SIGCONT) != 0)
218 error (0, errno, "kill (%d, SIGCONT)", pid);
219 RETURN (EXIT_FAILURE);
222 /* Wait for the child to have continued. */
223 sleep (2);
225 #if WCONTINUED != 0
226 if (expecting_sigchld)
228 error (0, 0, "no SIGCHLD seen for SIGCONT (optional)");
229 expecting_sigchld = 0;
231 else
232 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
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)
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)
250 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued signal %d\n",
251 info.si_signo);
252 RETURN (EXIT_FAILURE);
254 if (info.si_code != CLD_CONTINUED)
256 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued code %d\n",
257 info.si_code);
258 RETURN (EXIT_FAILURE);
260 if (info.si_status != SIGCONT)
262 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued status %d\n",
263 info.si_status);
264 RETURN (EXIT_FAILURE);
266 if (info.si_pid != pid)
268 error (0, 0, "waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
269 info.si_pid, pid);
270 RETURN (EXIT_FAILURE);
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)
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)
290 error (0, 0, "waitid WCONTINUED on continued signal %d\n",
291 info.si_signo);
292 RETURN (EXIT_FAILURE);
294 if (info.si_code != CLD_CONTINUED)
296 error (0, 0, "waitid WCONTINUED on continued code %d\n",
297 info.si_code);
298 RETURN (EXIT_FAILURE);
300 if (info.si_status != SIGCONT)
302 error (0, 0, "waitid WCONTINUED on continued status %d\n",
303 info.si_status);
304 RETURN (EXIT_FAILURE);
306 if (info.si_pid != pid)
308 error (0, 0, "waitid WCONTINUED on continued pid %d != %d\n",
309 info.si_pid, pid);
310 RETURN (EXIT_FAILURE);
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)
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);
339 /* Now stop him again and test waitpid with WCONTINUED. */
340 expecting_sigchld = 1;
341 if (kill (pid, SIGSTOP) != 0)
343 error (0, errno, "kill (%d, SIGSTOP)", pid);
344 RETURN (EXIT_FAILURE);
346 pid_t wpid = waitpid (pid, &fail, WUNTRACED);
347 if (wpid < 0)
349 error (0, errno, "waitpid WUNTRACED on stopped");
350 RETURN (EXIT_FAILURE);
352 else if (wpid != pid)
354 error (0, 0,
355 "waitpid WUNTRACED on stopped returned %d != %d (status %x)",
356 wpid, pid, fail);
357 RETURN (EXIT_FAILURE);
359 else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
360 || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP)
362 error (0, 0, "waitpid WUNTRACED on stopped: status %x", fail);
363 RETURN (EXIT_FAILURE);
365 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
367 expecting_sigchld = 1;
368 if (kill (pid, SIGCONT) != 0)
370 error (0, errno, "kill (%d, SIGCONT)", pid);
371 RETURN (EXIT_FAILURE);
374 /* Wait for the child to have continued. */
375 sleep (2);
377 if (expecting_sigchld)
379 error (0, 0, "no SIGCHLD seen for SIGCONT (optional)");
380 expecting_sigchld = 0;
382 else
383 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
385 wpid = waitpid (pid, &fail, WCONTINUED);
386 if (wpid < 0)
388 if (errno == EINVAL)
389 error (0, 0, "waitpid does not support WCONTINUED");
390 else
392 error (0, errno, "waitpid WCONTINUED on continued");
393 RETURN (EXIT_FAILURE);
396 else if (wpid != pid)
398 error (0, 0,
399 "waitpid WCONTINUED on continued returned %d != %d (status %x)",
400 wpid, pid, fail);
401 RETURN (EXIT_FAILURE);
403 else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
404 || !WIFCONTINUED (fail))
406 error (0, 0, "waitpid WCONTINUED on continued: status %x", fail);
407 RETURN (EXIT_FAILURE);
409 #endif
411 expecting_sigchld = 1;
413 /* Die, child, die! */
414 if (kill (pid, SIGKILL) != 0)
416 error (0, errno, "kill (%d, SIGKILL)", pid);
417 RETURN (EXIT_FAILURE);
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)
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)
436 error (0, 0, "waitid WNOWAIT on killed signal %d\n",
437 info.si_signo);
438 RETURN (EXIT_FAILURE);
440 if (info.si_code != CLD_KILLED)
442 error (0, 0, "waitid WNOWAIT on killed code %d\n",
443 info.si_code);
444 RETURN (EXIT_FAILURE);
446 if (info.si_status != SIGKILL)
448 error (0, 0, "waitid WNOWAIT on killed status %d\n",
449 info.si_status);
450 RETURN (EXIT_FAILURE);
452 if (info.si_pid != pid)
454 error (0, 0, "waitid WNOWAIT on killed pid %d != %d\n",
455 info.si_pid, pid);
456 RETURN (EXIT_FAILURE);
459 #else
460 /* Allow enough time to be sure the child died; we didn't synchronize. */
461 sleep (2);
462 #endif
464 CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
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)
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)
481 error (0, 0, "waitid WNOHANG on killed signal %d\n",
482 info.si_signo);
483 RETURN (EXIT_FAILURE);
485 if (info.si_code != CLD_KILLED)
487 error (0, 0, "waitid WNOHANG on killed code %d\n",
488 info.si_code);
489 RETURN (EXIT_FAILURE);
491 if (info.si_status != SIGKILL)
493 error (0, 0, "waitid WNOHANG on killed status %d\n",
494 info.si_status);
495 RETURN (EXIT_FAILURE);
497 if (info.si_pid != pid)
499 error (0, 0, "waitid WNOHANG on killed pid %d != %d\n",
500 info.si_pid, pid);
501 RETURN (EXIT_FAILURE);
505 fail = waitid (P_PID, pid, &info, WEXITED);
506 if (fail == -1)
508 if (errno != ECHILD)
510 error (0, errno, "waitid WEXITED on killed");
511 RETURN (EXIT_FAILURE);
514 else
516 error (0, 0, "waitid WEXITED returned bogus value %d\n", fail);
517 RETURN (EXIT_FAILURE);
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;
529 #include "../test-skeleton.c"