source: branches/libc-0.6/src/libctests/glibc/posix/tst-waitid.c

Last change on this file was 2196, checked in by bird, 20 years ago

fixed waitid testcase.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
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 <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <sys/wait.h>
25#include <signal.h>
26
27#define TIMEOUT 15
28
29static void
30test_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
50static sig_atomic_t expecting_sigchld, spurious_sigchld;
51#ifdef SA_SIGINFO
52static siginfo_t sigchld_info;
53
54static void
55sigchld (int signo, siginfo_t *info, void *ctx)
56{
57 if (signo != SIGCHLD)
58 {
59 printf ("SIGCHLD handler got signal %d instead!\n", signo);
60 _exit (EXIT_FAILURE);
61 }
62
63 if (! expecting_sigchld)
64 {
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);
68 }
69 else
70 {
71 sigchld_info = *info;
72 expecting_sigchld = 0;
73 }
74}
75
76static void
77check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid)
78{
79 if (expecting_sigchld)
80 {
81 printf ("missing SIGCHLD on %s\n", phase);
82 *ok = EXIT_FAILURE;
83 expecting_sigchld = 0;
84 return;
85 }
86
87 if (sigchld_info.si_signo != SIGCHLD)
88 {
89 printf ("SIGCHLD for %s signal %d\n", phase, sigchld_info.si_signo);
90 *ok = EXIT_FAILURE;
91 }
92 if (sigchld_info.si_code != code)
93 {
94 printf ("SIGCHLD for %s code %d\n", phase, sigchld_info.si_code);
95 *ok = EXIT_FAILURE;
96 }
97 if (sigchld_info.si_status != status)
98 {
99 printf ("SIGCHLD for %s status %d\n", phase, sigchld_info.si_status);
100 *ok = EXIT_FAILURE;
101 }
102 if (sigchld_info.si_pid != pid)
103 {
104 printf ("SIGCHLD for %s pid %d\n", phase, sigchld_info.si_pid);
105 *ok = EXIT_FAILURE;
106 }
107}
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
113
114static int
115do_test (int argc, char *argv[])
116{
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)
122 {
123 printf ("setting SIGCHLD handler: %m\n");
124 return EXIT_FAILURE;
125 }
126#endif
127
128 expecting_sigchld = 1;
129
130 pid_t pid = fork ();
131 if (pid < 0)
132 {
133 printf ("fork: %m\n");
134 return EXIT_FAILURE;
135 }
136 else if (pid == 0)
137 {
138 test_child ();
139 _exit (127);
140 }
141
142 int status = EXIT_SUCCESS;
143#define RETURN(ok) \
144 do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
145
146 /* Give the child a chance to stop. */
147 sleep (3);
148
149 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
150
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)
156 {
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);
171 }
172
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)
179 {
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)
188 {
189 printf ("waitid WSTOPPED|WNOHANG on stopped signal %d\n",
190 info.si_signo);
191 RETURN (EXIT_FAILURE);
192 }
193 if (info.si_code != CLD_STOPPED)
194 {
195 printf ("waitid WSTOPPED|WNOHANG on stopped code %d\n",
196 info.si_code);
197 RETURN (EXIT_FAILURE);
198 }
199 if (info.si_status != SIGSTOP)
200 {
201 printf ("waitid WSTOPPED|WNOHANG on stopped status %d\n",
202 info.si_status);
203 RETURN (EXIT_FAILURE);
204 }
205 if (info.si_pid != pid)
206 {
207 printf ("waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
208 info.si_pid, pid);
209 RETURN (EXIT_FAILURE);
210 }
211 }
212
213 expecting_sigchld = WCONTINUED != 0;
214
215 if (kill (pid, SIGCONT) != 0)
216 {
217 printf ("kill (%d, SIGCONT): %m\n", pid);
218 RETURN (EXIT_FAILURE);
219 }
220
221 /* Wait for the child to have continued. */
222 sleep (2);
223
224#if WCONTINUED != 0
225 if (expecting_sigchld)
226 {
227 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
228 expecting_sigchld = 0;
229 }
230 else
231 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
232
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)
238 {
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)
247 {
248 printf ("waitid WCONTINUED|WNOWAIT on continued signal %d\n",
249 info.si_signo);
250 RETURN (EXIT_FAILURE);
251 }
252 if (info.si_code != CLD_CONTINUED)
253 {
254 printf ("waitid WCONTINUED|WNOWAIT on continued code %d\n",
255 info.si_code);
256 RETURN (EXIT_FAILURE);
257 }
258 if (info.si_status != SIGCONT)
259 {
260 printf ("waitid WCONTINUED|WNOWAIT on continued status %d\n",
261 info.si_status);
262 RETURN (EXIT_FAILURE);
263 }
264 if (info.si_pid != pid)
265 {
266 printf ("waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
267 info.si_pid, pid);
268 RETURN (EXIT_FAILURE);
269 }
270 }
271
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)
278 {
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)
287 {
288 printf ("waitid WCONTINUED on continued signal %d\n", info.si_signo);
289 RETURN (EXIT_FAILURE);
290 }
291 if (info.si_code != CLD_CONTINUED)
292 {
293 printf ("waitid WCONTINUED on continued code %d\n", info.si_code);
294 RETURN (EXIT_FAILURE);
295 }
296 if (info.si_status != SIGCONT)
297 {
298 printf ("waitid WCONTINUED on continued status %d\n",
299 info.si_status);
300 RETURN (EXIT_FAILURE);
301 }
302 if (info.si_pid != pid)
303 {
304 printf ("waitid WCONTINUED on continued pid %d != %d\n",
305 info.si_pid, pid);
306 RETURN (EXIT_FAILURE);
307 }
308 }
309
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)
314 {
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);
331 }
332
333 /* Now stop him again and test waitpid with WCONTINUED. */
334 expecting_sigchld = 1;
335 if (kill (pid, SIGSTOP) != 0)
336 {
337 printf ("kill (%d, SIGSTOP): %m\n", pid);
338 RETURN (EXIT_FAILURE);
339 }
340 pid_t wpid = waitpid (pid, &fail, WUNTRACED);
341 if (wpid < 0)
342 {
343 printf ("waitpid WUNTRACED on stopped: %m\n");
344 RETURN (EXIT_FAILURE);
345 }
346 else if (wpid != pid)
347 {
348 printf ("waitpid WUNTRACED on stopped returned %d != %d (status %x)\n",
349 wpid, pid, fail);
350 RETURN (EXIT_FAILURE);
351 }
352 else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
353 || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP)
354 {
355 printf ("waitpid WUNTRACED on stopped: status %x\n", fail);
356 RETURN (EXIT_FAILURE);
357 }
358 CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
359
360 expecting_sigchld = 1;
361 if (kill (pid, SIGCONT) != 0)
362 {
363 printf ("kill (%d, SIGCONT): %m\n", pid);
364 RETURN (EXIT_FAILURE);
365 }
366
367 /* Wait for the child to have continued. */
368 sleep (2);
369
370 if (expecting_sigchld)
371 {
372 printf ("no SIGCHLD seen for SIGCONT (optional)\n");
373 expecting_sigchld = 0;
374 }
375 else
376 CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
377
378 wpid = waitpid (pid, &fail, WCONTINUED);
379 if (wpid < 0)
380 {
381 if (errno == EINVAL)
382 printf ("waitpid does not support WCONTINUED\n");
383 else
384 {
385 printf ("waitpid WCONTINUED on continued: %m\n");
386 RETURN (EXIT_FAILURE);
387 }
388 }
389 else if (wpid != pid)
390 {
391 printf ("\
392waitpid WCONTINUED on continued returned %d != %d (status %x)\n",
393 wpid, pid, fail);
394 RETURN (EXIT_FAILURE);
395 }
396 else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
397 || !WIFCONTINUED (fail))
398 {
399 printf ("waitpid WCONTINUED on continued: status %x (stopped=%d,signaled=%d,exited=%d,continued=%d)\n",
400 fail, WIFSTOPPED (fail), WIFSIGNALED (fail), WIFEXITED (fail), WIFCONTINUED (fail));
401 RETURN (EXIT_FAILURE);
402 }
403#endif
404
405 expecting_sigchld = 1;
406
407 /* Die, child, die! */
408 if (kill (pid, SIGKILL) != 0)
409 {
410 printf ("kill (%d, SIGKILL): %m\n", pid);
411 RETURN (EXIT_FAILURE);
412 }
413
414#ifdef WNOWAIT
415 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
416 info.si_pid = -1;
417 info.si_status = -1;
418 fail = waitid (P_PID, pid, &info, WEXITED|WNOWAIT);
419 switch (fail)
420 {
421 default:
422 printf ("waitid WNOWAIT returned bogus value %d\n", fail);
423 RETURN (EXIT_FAILURE);
424 case -1:
425 printf ("waitid WNOWAIT on killed: %m\n");
426 RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
427 case 0:
428 if (info.si_signo != SIGCHLD)
429 {
430 printf ("waitid WNOWAIT on killed signal %d\n", info.si_signo);
431 RETURN (EXIT_FAILURE);
432 }
433 if (info.si_code != CLD_KILLED)
434 {
435 printf ("waitid WNOWAIT on killed code %d\n", info.si_code);
436 RETURN (EXIT_FAILURE);
437 }
438 if (info.si_status != SIGKILL)
439 {
440 printf ("waitid WNOWAIT on killed status %d\n", info.si_status);
441 RETURN (EXIT_FAILURE);
442 }
443 if (info.si_pid != pid)
444 {
445 printf ("waitid WNOWAIT on killed pid %d != %d\n", info.si_pid, pid);
446 RETURN (EXIT_FAILURE);
447 }
448 }
449#else
450 /* Allow enough time to be sure the child died; we didn't synchronize. */
451 sleep (2);
452#endif
453
454 CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
455
456 info.si_signo = 0; /* A successful call sets it to SIGCHLD. */
457 info.si_pid = -1;
458 info.si_status = -1;
459 fail = waitid (P_PID, pid, &info, WEXITED|WNOHANG);
460 switch (fail)
461 {
462 default:
463 printf ("waitid WNOHANG returned bogus value %d\n", fail);
464 RETURN (EXIT_FAILURE);
465 case -1:
466 printf ("waitid WNOHANG on killed: %m\n");
467 RETURN (EXIT_FAILURE);
468 case 0:
469 if (info.si_signo != SIGCHLD)
470 {
471 printf ("waitid WNOHANG on killed signal %d\n", info.si_signo);
472 RETURN (EXIT_FAILURE);
473 }
474 if (info.si_code != CLD_KILLED)
475 {
476 printf ("waitid WNOHANG on killed code %d\n", info.si_code);
477 RETURN (EXIT_FAILURE);
478 }
479 if (info.si_status != SIGKILL)
480 {
481 printf ("waitid WNOHANG on killed status %d\n", info.si_status);
482 RETURN (EXIT_FAILURE);
483 }
484 if (info.si_pid != pid)
485 {
486 printf ("waitid WNOHANG on killed pid %d != %d\n", info.si_pid, pid);
487 RETURN (EXIT_FAILURE);
488 }
489 }
490
491 fail = waitid (P_PID, pid, &info, WEXITED);
492 if (fail == -1)
493 {
494 if (errno != ECHILD)
495 {
496 printf ("waitid WEXITED on killed: %m\n");
497 RETURN (EXIT_FAILURE);
498 }
499 }
500 else
501 {
502 printf ("waitid WEXITED returned bogus value %d\n", fail);
503 RETURN (EXIT_FAILURE);
504 }
505
506#undef RETURN
507 out:
508 if (spurious_sigchld)
509 status = EXIT_FAILURE;
510 signal (SIGCHLD, SIG_IGN);
511 kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */
512 return status;
513}
514
515#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.