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
32 /* Wait a second to be sure the parent set his variables before we
36 /* First thing, we stop ourselves. */
39 /* Hey, we got continued! */
47 # define WSTOPPED WUNTRACED
50 static sig_atomic_t expecting_sigchld
, spurious_sigchld
;
52 static siginfo_t sigchld_info
;
55 sigchld (int signo
, siginfo_t
*info
, void *ctx
)
59 printf ("SIGCHLD handler got signal %d instead!\n", signo
);
63 if (! expecting_sigchld
)
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
);
72 expecting_sigchld
= 0;
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
);
83 expecting_sigchld
= 0;
87 if (sigchld_info
.si_signo
!= SIGCHLD
)
89 printf ("SIGCHLD for %s signal %d\n", phase
, sigchld_info
.si_signo
);
92 if (sigchld_info
.si_code
!= code
)
94 printf ("SIGCHLD for %s code %d\n", phase
, sigchld_info
.si_code
);
97 if (sigchld_info
.si_status
!= status
)
99 printf ("SIGCHLD for %s status %d\n", phase
, sigchld_info
.si_status
);
102 if (sigchld_info
.si_pid
!= pid
)
104 printf ("SIGCHLD for %s pid %d\n", phase
, sigchld_info
.si_pid
);
108 # define CHECK_SIGCHLD(phase, code_check, status_check) \
109 check_sigchld ((phase), &status, (code_check), (status_check), pid)
111 # define CHECK_SIGCHLD(phase, code, status) ((void) 0)
115 do_test (int argc
, char *argv
[])
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");
128 expecting_sigchld
= 1;
133 printf ("fork: %m\n");
142 int status
= EXIT_SUCCESS
;
144 do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
146 /* Give the child a chance to stop. */
149 CHECK_SIGCHLD ("stopped", CLD_STOPPED
, SIGSTOP
);
151 /* Now try a wait that should not succeed. */
153 info
.si_signo
= 0; /* A successful call sets it to SIGCHLD. */
154 int fail
= waitid (P_PID
, pid
, &info
, WEXITED
|WCONTINUED
|WNOHANG
);
158 printf ("waitid returned bogus value %d\n", fail
);
159 RETURN (EXIT_FAILURE
);
161 printf ("waitid WNOHANG on stopped: %m\n");
162 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
164 if (info
.si_signo
== 0)
166 if (info
.si_signo
== SIGCHLD
)
167 printf ("waitid WNOHANG on stopped status %d\n", info
.si_status
);
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. */
177 fail
= waitid (P_PID
, pid
, &info
, WSTOPPED
|WNOHANG
);
181 printf ("waitid WSTOPPED|WNOHANG returned bogus value %d\n", fail
);
182 RETURN (EXIT_FAILURE
);
184 printf ("waitid WSTOPPED|WNOHANG on stopped: %m\n");
185 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
187 if (info
.si_signo
!= SIGCHLD
)
189 printf ("waitid WSTOPPED|WNOHANG on stopped signal %d\n",
191 RETURN (EXIT_FAILURE
);
193 if (info
.si_code
!= CLD_STOPPED
)
195 printf ("waitid WSTOPPED|WNOHANG on stopped code %d\n",
197 RETURN (EXIT_FAILURE
);
199 if (info
.si_status
!= SIGSTOP
)
201 printf ("waitid WSTOPPED|WNOHANG on stopped status %d\n",
203 RETURN (EXIT_FAILURE
);
205 if (info
.si_pid
!= pid
)
207 printf ("waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
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. */
225 if (expecting_sigchld
)
227 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
228 expecting_sigchld
= 0;
231 CHECK_SIGCHLD ("continued", CLD_CONTINUED
, SIGCONT
);
233 info
.si_signo
= 0; /* A successful call sets it to SIGCHLD. */
236 fail
= waitid (P_PID
, pid
, &info
, WCONTINUED
|WNOWAIT
);
240 printf ("waitid WCONTINUED|WNOWAIT returned bogus value %d\n", fail
);
241 RETURN (EXIT_FAILURE
);
243 printf ("waitid WCONTINUED|WNOWAIT on continued: %m\n");
244 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
246 if (info
.si_signo
!= SIGCHLD
)
248 printf ("waitid WCONTINUED|WNOWAIT on continued signal %d\n",
250 RETURN (EXIT_FAILURE
);
252 if (info
.si_code
!= CLD_CONTINUED
)
254 printf ("waitid WCONTINUED|WNOWAIT on continued code %d\n",
256 RETURN (EXIT_FAILURE
);
258 if (info
.si_status
!= SIGCONT
)
260 printf ("waitid WCONTINUED|WNOWAIT on continued status %d\n",
262 RETURN (EXIT_FAILURE
);
264 if (info
.si_pid
!= pid
)
266 printf ("waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
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. */
276 fail
= waitid (P_PID
, pid
, &info
, WCONTINUED
);
280 printf ("waitid WCONTINUED returned bogus value %d\n", fail
);
281 RETURN (EXIT_FAILURE
);
283 printf ("waitid WCONTINUED on continued: %m\n");
284 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
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",
300 RETURN (EXIT_FAILURE
);
302 if (info
.si_pid
!= pid
)
304 printf ("waitid WCONTINUED on continued pid %d != %d\n",
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
);
316 printf ("waitid returned bogus value %d\n", fail
);
317 RETURN (EXIT_FAILURE
);
319 printf ("waitid WCONTINUED|WNOHANG on waited continued: %m\n");
320 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
322 if (info
.si_signo
== 0)
324 if (info
.si_signo
== SIGCHLD
)
325 printf ("waitid WCONTINUED|WNOHANG on waited continued status %d\n",
328 printf ("waitid WCONTINUED|WNOHANG on waited continued signal %d\n",
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
);
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",
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. */
370 if (expecting_sigchld
)
372 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
373 expecting_sigchld
= 0;
376 CHECK_SIGCHLD ("continued", CLD_CONTINUED
, SIGCONT
);
378 wpid
= waitpid (pid
, &fail
, WCONTINUED
);
382 printf ("waitpid does not support WCONTINUED\n");
385 printf ("waitpid WCONTINUED on continued: %m\n");
386 RETURN (EXIT_FAILURE
);
389 else if (wpid
!= pid
)
392 waitpid WCONTINUED on continued returned %d != %d (status %x)\n",
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
);
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
);
414 info
.si_signo
= 0; /* A successful call sets it to SIGCHLD. */
417 fail
= waitid (P_PID
, pid
, &info
, WEXITED
|WNOWAIT
);
421 printf ("waitid WNOWAIT returned bogus value %d\n", fail
);
422 RETURN (EXIT_FAILURE
);
424 printf ("waitid WNOWAIT on killed: %m\n");
425 RETURN (errno
== ENOTSUP
? EXIT_SUCCESS
: EXIT_FAILURE
);
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
);
449 /* Allow enough time to be sure the child died; we didn't synchronize. */
453 CHECK_SIGCHLD ("killed", CLD_KILLED
, SIGKILL
);
455 info
.si_signo
= 0; /* A successful call sets it to SIGCHLD. */
458 fail
= waitid (P_PID
, pid
, &info
, WEXITED
|WNOHANG
);
462 printf ("waitid WNOHANG returned bogus value %d\n", fail
);
463 RETURN (EXIT_FAILURE
);
465 printf ("waitid WNOHANG on killed: %m\n");
466 RETURN (EXIT_FAILURE
);
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
);
495 printf ("waitid WEXITED on killed: %m\n");
496 RETURN (EXIT_FAILURE
);
501 printf ("waitid WEXITED returned bogus value %d\n", fail
);
502 RETURN (EXIT_FAILURE
);
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. */
514 #include "../test-skeleton.c"