chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.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 <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
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       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
76 static void
77 check_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
114 static int
115 do_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 ("\
392 waitpid 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\n", fail);
400       RETURN (EXIT_FAILURE);
401     }
402 #endif
403
404   expecting_sigchld = 1;
405
406   /* Die, child, die!  */
407   if (kill (pid, SIGKILL) != 0)
408     {
409       printf ("kill (%d, SIGKILL): %m\n", pid);
410       RETURN (EXIT_FAILURE);
411     }
412
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)
419     {
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)
428         {
429           printf ("waitid WNOWAIT on killed signal %d\n", info.si_signo);
430           RETURN (EXIT_FAILURE);
431         }
432       if (info.si_code != CLD_KILLED)
433         {
434           printf ("waitid WNOWAIT on killed code %d\n", info.si_code);
435           RETURN (EXIT_FAILURE);
436         }
437       if (info.si_status != SIGKILL)
438         {
439           printf ("waitid WNOWAIT on killed status %d\n", info.si_status);
440           RETURN (EXIT_FAILURE);
441         }
442       if (info.si_pid != pid)
443         {
444           printf ("waitid WNOWAIT on killed pid %d != %d\n", info.si_pid, pid);
445           RETURN (EXIT_FAILURE);
446         }
447     }
448 #else
449   /* Allow enough time to be sure the child died; we didn't synchronize.  */
450   sleep (2);
451 #endif
452
453   CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
454
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)
460     {
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)
469         {
470           printf ("waitid WNOHANG on killed signal %d\n", info.si_signo);
471           RETURN (EXIT_FAILURE);
472         }
473       if (info.si_code != CLD_KILLED)
474         {
475           printf ("waitid WNOHANG on killed code %d\n", info.si_code);
476           RETURN (EXIT_FAILURE);
477         }
478       if (info.si_status != SIGKILL)
479         {
480           printf ("waitid WNOHANG on killed status %d\n", info.si_status);
481           RETURN (EXIT_FAILURE);
482         }
483       if (info.si_pid != pid)
484         {
485           printf ("waitid WNOHANG on killed pid %d != %d\n", info.si_pid, pid);
486           RETURN (EXIT_FAILURE);
487         }
488     }
489
490   fail = waitid (P_PID, pid, &info, WEXITED);
491   if (fail == -1)
492     {
493       if (errno != ECHILD)
494         {
495           printf ("waitid WEXITED on killed: %m\n");
496           RETURN (EXIT_FAILURE);
497         }
498     }
499   else
500     {
501       printf ("waitid WEXITED returned bogus value %d\n", fail);
502       RETURN (EXIT_FAILURE);
503     }
504
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;
512 }
513
514 #include "../test-skeleton.c"