Back to index

glibc  2.9
tst-waitid.c
Go to the documentation of this file.
00001 /* Tests for waitid.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <sys/wait.h>
00025 #include <signal.h>
00026 
00027 #define TIMEOUT 15
00028 
00029 static void
00030 test_child (void)
00031 {
00032   /* Wait a second to be sure the parent set his variables before we
00033      produce a SIGCHLD.  */
00034   sleep (1);
00035 
00036   /* First thing, we stop ourselves.  */
00037   raise (SIGSTOP);
00038 
00039   /* Hey, we got continued!  */
00040   while (1)
00041     pause ();
00042 }
00043 
00044 #ifndef WEXITED
00045 # define WEXITED     0
00046 # define WCONTINUED  0
00047 # define WSTOPPED    WUNTRACED
00048 #endif
00049 
00050 static sig_atomic_t expecting_sigchld, spurious_sigchld;
00051 #ifdef SA_SIGINFO
00052 static siginfo_t sigchld_info;
00053 
00054 static void
00055 sigchld (int signo, siginfo_t *info, void *ctx)
00056 {
00057   if (signo != SIGCHLD)
00058     {
00059       printf ("SIGCHLD handler got signal %d instead!\n", signo);
00060       _exit (EXIT_FAILURE);
00061     }
00062 
00063   if (! expecting_sigchld)
00064     {
00065       spurious_sigchld = 1;
00066       printf ("spurious SIGCHLD: signo %d code %d status %d pid %d\n",
00067              info->si_signo, info->si_code, info->si_status, info->si_pid);
00068     }
00069   else
00070     {
00071       sigchld_info = *info;
00072       expecting_sigchld = 0;
00073     }
00074 }
00075 
00076 static void
00077 check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid)
00078 {
00079   if (expecting_sigchld)
00080     {
00081       printf ("missing SIGCHLD on %s\n", phase);
00082       *ok = EXIT_FAILURE;
00083       expecting_sigchld = 0;
00084       return;
00085     }
00086 
00087   if (sigchld_info.si_signo != SIGCHLD)
00088     {
00089       printf ("SIGCHLD for %s signal %d\n", phase, sigchld_info.si_signo);
00090       *ok = EXIT_FAILURE;
00091     }
00092   if (sigchld_info.si_code != code)
00093     {
00094       printf ("SIGCHLD for %s code %d\n", phase, sigchld_info.si_code);
00095       *ok = EXIT_FAILURE;
00096     }
00097   if (sigchld_info.si_status != status)
00098     {
00099       printf ("SIGCHLD for %s status %d\n", phase, sigchld_info.si_status);
00100       *ok = EXIT_FAILURE;
00101     }
00102   if (sigchld_info.si_pid != pid)
00103     {
00104       printf ("SIGCHLD for %s pid %d\n", phase, sigchld_info.si_pid);
00105       *ok = EXIT_FAILURE;
00106     }
00107 }
00108 # define CHECK_SIGCHLD(phase, code_check, status_check) \
00109   check_sigchld ((phase), &status, (code_check), (status_check), pid)
00110 #else
00111 # define CHECK_SIGCHLD(phase, code, status) ((void) 0)
00112 #endif
00113 
00114 static int
00115 do_test (int argc, char *argv[])
00116 {
00117 #ifdef SA_SIGINFO
00118   struct sigaction sa;
00119   sa.sa_flags = SA_SIGINFO|SA_RESTART;
00120   sa.sa_sigaction = &sigchld;
00121   if (sigemptyset (&sa.sa_mask) < 0 || sigaction (SIGCHLD, &sa, NULL) < 0)
00122     {
00123       printf ("setting SIGCHLD handler: %m\n");
00124       return EXIT_FAILURE;
00125     }
00126 #endif
00127 
00128   expecting_sigchld = 1;
00129 
00130   pid_t pid = fork ();
00131   if (pid < 0)
00132     {
00133       printf ("fork: %m\n");
00134       return EXIT_FAILURE;
00135     }
00136   else if (pid == 0)
00137     {
00138       test_child ();
00139       _exit (127);
00140     }
00141 
00142   int status = EXIT_SUCCESS;
00143 #define RETURN(ok) \
00144     do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0)
00145 
00146   /* Give the child a chance to stop.  */
00147   sleep (3);
00148 
00149   CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
00150 
00151   /* Now try a wait that should not succeed.  */
00152   siginfo_t info;
00153   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00154   int fail = waitid (P_PID, pid, &info, WEXITED|WCONTINUED|WNOHANG);
00155   switch (fail)
00156     {
00157     default:
00158       printf ("waitid returned bogus value %d\n", fail);
00159       RETURN (EXIT_FAILURE);
00160     case -1:
00161       printf ("waitid WNOHANG on stopped: %m\n");
00162       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00163     case 0:
00164       if (info.si_signo == 0)
00165        break;
00166       if (info.si_signo == SIGCHLD)
00167        printf ("waitid WNOHANG on stopped status %d\n", info.si_status);
00168       else
00169        printf ("waitid WNOHANG on stopped signal %d\n", info.si_signo);
00170       RETURN (EXIT_FAILURE);
00171     }
00172 
00173   /* Next the wait that should succeed right away.  */
00174   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00175   info.si_pid = -1;
00176   info.si_status = -1;
00177   fail = waitid (P_PID, pid, &info, WSTOPPED|WNOHANG);
00178   switch (fail)
00179     {
00180     default:
00181       printf ("waitid WSTOPPED|WNOHANG returned bogus value %d\n", fail);
00182       RETURN (EXIT_FAILURE);
00183     case -1:
00184       printf ("waitid WSTOPPED|WNOHANG on stopped: %m\n");
00185       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00186     case 0:
00187       if (info.si_signo != SIGCHLD)
00188        {
00189          printf ("waitid WSTOPPED|WNOHANG on stopped signal %d\n",
00190                 info.si_signo);
00191          RETURN (EXIT_FAILURE);
00192        }
00193       if (info.si_code != CLD_STOPPED)
00194        {
00195          printf ("waitid WSTOPPED|WNOHANG on stopped code %d\n",
00196                 info.si_code);
00197          RETURN (EXIT_FAILURE);
00198        }
00199       if (info.si_status != SIGSTOP)
00200        {
00201          printf ("waitid WSTOPPED|WNOHANG on stopped status %d\n",
00202                 info.si_status);
00203          RETURN (EXIT_FAILURE);
00204        }
00205       if (info.si_pid != pid)
00206        {
00207          printf ("waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n",
00208                 info.si_pid, pid);
00209          RETURN (EXIT_FAILURE);
00210        }
00211     }
00212 
00213   expecting_sigchld = WCONTINUED != 0;
00214 
00215   if (kill (pid, SIGCONT) != 0)
00216     {
00217       printf ("kill (%d, SIGCONT): %m\n", pid);
00218       RETURN (EXIT_FAILURE);
00219     }
00220 
00221   /* Wait for the child to have continued.  */
00222   sleep (2);
00223 
00224 #if WCONTINUED != 0
00225   if (expecting_sigchld)
00226     {
00227       printf ("no SIGCHLD seen for SIGCONT (optional)\n");
00228       expecting_sigchld = 0;
00229     }
00230   else
00231     CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
00232 
00233   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00234   info.si_pid = -1;
00235   info.si_status = -1;
00236   fail = waitid (P_PID, pid, &info, WCONTINUED|WNOWAIT);
00237   switch (fail)
00238     {
00239     default:
00240       printf ("waitid WCONTINUED|WNOWAIT returned bogus value %d\n", fail);
00241       RETURN (EXIT_FAILURE);
00242     case -1:
00243       printf ("waitid WCONTINUED|WNOWAIT on continued: %m\n");
00244       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00245     case 0:
00246       if (info.si_signo != SIGCHLD)
00247        {
00248          printf ("waitid WCONTINUED|WNOWAIT on continued signal %d\n",
00249                 info.si_signo);
00250          RETURN (EXIT_FAILURE);
00251        }
00252       if (info.si_code != CLD_CONTINUED)
00253        {
00254          printf ("waitid WCONTINUED|WNOWAIT on continued code %d\n",
00255                 info.si_code);
00256          RETURN (EXIT_FAILURE);
00257        }
00258       if (info.si_status != SIGCONT)
00259        {
00260          printf ("waitid WCONTINUED|WNOWAIT on continued status %d\n",
00261                 info.si_status);
00262          RETURN (EXIT_FAILURE);
00263        }
00264       if (info.si_pid != pid)
00265        {
00266          printf ("waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n",
00267                 info.si_pid, pid);
00268          RETURN (EXIT_FAILURE);
00269        }
00270     }
00271 
00272   /* That should leave the CLD_CONTINUED state waiting to be seen again.  */
00273   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00274   info.si_pid = -1;
00275   info.si_status = -1;
00276   fail = waitid (P_PID, pid, &info, WCONTINUED);
00277   switch (fail)
00278     {
00279     default:
00280       printf ("waitid WCONTINUED returned bogus value %d\n", fail);
00281       RETURN (EXIT_FAILURE);
00282     case -1:
00283       printf ("waitid WCONTINUED on continued: %m\n");
00284       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00285     case 0:
00286       if (info.si_signo != SIGCHLD)
00287        {
00288          printf ("waitid WCONTINUED on continued signal %d\n", info.si_signo);
00289          RETURN (EXIT_FAILURE);
00290        }
00291       if (info.si_code != CLD_CONTINUED)
00292        {
00293          printf ("waitid WCONTINUED on continued code %d\n", info.si_code);
00294          RETURN (EXIT_FAILURE);
00295        }
00296       if (info.si_status != SIGCONT)
00297        {
00298          printf ("waitid WCONTINUED on continued status %d\n",
00299                 info.si_status);
00300          RETURN (EXIT_FAILURE);
00301        }
00302       if (info.si_pid != pid)
00303        {
00304          printf ("waitid WCONTINUED on continued pid %d != %d\n",
00305                 info.si_pid, pid);
00306          RETURN (EXIT_FAILURE);
00307        }
00308     }
00309 
00310   /* Now try a wait that should not succeed.  */
00311   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00312   fail = waitid (P_PID, pid, &info, WCONTINUED|WNOHANG);
00313   switch (fail)
00314     {
00315     default:
00316       printf ("waitid returned bogus value %d\n", fail);
00317       RETURN (EXIT_FAILURE);
00318     case -1:
00319       printf ("waitid WCONTINUED|WNOHANG on waited continued: %m\n");
00320       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00321     case 0:
00322       if (info.si_signo == 0)
00323        break;
00324       if (info.si_signo == SIGCHLD)
00325        printf ("waitid WCONTINUED|WNOHANG on waited continued status %d\n",
00326               info.si_status);
00327       else
00328        printf ("waitid WCONTINUED|WNOHANG on waited continued signal %d\n",
00329               info.si_signo);
00330       RETURN (EXIT_FAILURE);
00331     }
00332 
00333   /* Now stop him again and test waitpid with WCONTINUED.  */
00334   expecting_sigchld = 1;
00335   if (kill (pid, SIGSTOP) != 0)
00336     {
00337       printf ("kill (%d, SIGSTOP): %m\n", pid);
00338       RETURN (EXIT_FAILURE);
00339     }
00340   pid_t wpid = waitpid (pid, &fail, WUNTRACED);
00341   if (wpid < 0)
00342     {
00343       printf ("waitpid WUNTRACED on stopped: %m\n");
00344       RETURN (EXIT_FAILURE);
00345     }
00346   else if (wpid != pid)
00347     {
00348       printf ("waitpid WUNTRACED on stopped returned %d != %d (status %x)\n",
00349              wpid, pid, fail);
00350       RETURN (EXIT_FAILURE);
00351     }
00352   else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
00353           || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP)
00354     {
00355       printf ("waitpid WUNTRACED on stopped: status %x\n", fail);
00356       RETURN (EXIT_FAILURE);
00357     }
00358   CHECK_SIGCHLD ("stopped", CLD_STOPPED, SIGSTOP);
00359 
00360   expecting_sigchld = 1;
00361   if (kill (pid, SIGCONT) != 0)
00362     {
00363       printf ("kill (%d, SIGCONT): %m\n", pid);
00364       RETURN (EXIT_FAILURE);
00365     }
00366 
00367   /* Wait for the child to have continued.  */
00368   sleep (2);
00369 
00370   if (expecting_sigchld)
00371     {
00372       printf ("no SIGCHLD seen for SIGCONT (optional)\n");
00373       expecting_sigchld = 0;
00374     }
00375   else
00376     CHECK_SIGCHLD ("continued", CLD_CONTINUED, SIGCONT);
00377 
00378   wpid = waitpid (pid, &fail, WCONTINUED);
00379   if (wpid < 0)
00380     {
00381       if (errno == EINVAL)
00382        printf ("waitpid does not support WCONTINUED\n");
00383       else
00384        {
00385          printf ("waitpid WCONTINUED on continued: %m\n");
00386          RETURN (EXIT_FAILURE);
00387        }
00388     }
00389   else if (wpid != pid)
00390     {
00391       printf ("\
00392 waitpid WCONTINUED on continued returned %d != %d (status %x)\n",
00393             wpid, pid, fail);
00394       RETURN (EXIT_FAILURE);
00395     }
00396   else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail)
00397           || !WIFCONTINUED (fail))
00398     {
00399       printf ("waitpid WCONTINUED on continued: status %x\n", fail);
00400       RETURN (EXIT_FAILURE);
00401     }
00402 #endif
00403 
00404   expecting_sigchld = 1;
00405 
00406   /* Die, child, die!  */
00407   if (kill (pid, SIGKILL) != 0)
00408     {
00409       printf ("kill (%d, SIGKILL): %m\n", pid);
00410       RETURN (EXIT_FAILURE);
00411     }
00412 
00413 #ifdef WNOWAIT
00414   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00415   info.si_pid = -1;
00416   info.si_status = -1;
00417   fail = waitid (P_PID, pid, &info, WEXITED|WNOWAIT);
00418   switch (fail)
00419     {
00420     default:
00421       printf ("waitid WNOWAIT returned bogus value %d\n", fail);
00422       RETURN (EXIT_FAILURE);
00423     case -1:
00424       printf ("waitid WNOWAIT on killed: %m\n");
00425       RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE);
00426     case 0:
00427       if (info.si_signo != SIGCHLD)
00428        {
00429          printf ("waitid WNOWAIT on killed signal %d\n", info.si_signo);
00430          RETURN (EXIT_FAILURE);
00431        }
00432       if (info.si_code != CLD_KILLED)
00433        {
00434          printf ("waitid WNOWAIT on killed code %d\n", info.si_code);
00435          RETURN (EXIT_FAILURE);
00436        }
00437       if (info.si_status != SIGKILL)
00438        {
00439          printf ("waitid WNOWAIT on killed status %d\n", info.si_status);
00440          RETURN (EXIT_FAILURE);
00441        }
00442       if (info.si_pid != pid)
00443        {
00444          printf ("waitid WNOWAIT on killed pid %d != %d\n", info.si_pid, pid);
00445          RETURN (EXIT_FAILURE);
00446        }
00447     }
00448 #else
00449   /* Allow enough time to be sure the child died; we didn't synchronize.  */
00450   sleep (2);
00451 #endif
00452 
00453   CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL);
00454 
00455   info.si_signo = 0;        /* A successful call sets it to SIGCHLD.  */
00456   info.si_pid = -1;
00457   info.si_status = -1;
00458   fail = waitid (P_PID, pid, &info, WEXITED|WNOHANG);
00459   switch (fail)
00460     {
00461     default:
00462       printf ("waitid WNOHANG returned bogus value %d\n", fail);
00463       RETURN (EXIT_FAILURE);
00464     case -1:
00465       printf ("waitid WNOHANG on killed: %m\n");
00466       RETURN (EXIT_FAILURE);
00467     case 0:
00468       if (info.si_signo != SIGCHLD)
00469        {
00470          printf ("waitid WNOHANG on killed signal %d\n", info.si_signo);
00471          RETURN (EXIT_FAILURE);
00472        }
00473       if (info.si_code != CLD_KILLED)
00474        {
00475          printf ("waitid WNOHANG on killed code %d\n", info.si_code);
00476          RETURN (EXIT_FAILURE);
00477        }
00478       if (info.si_status != SIGKILL)
00479        {
00480          printf ("waitid WNOHANG on killed status %d\n", info.si_status);
00481          RETURN (EXIT_FAILURE);
00482        }
00483       if (info.si_pid != pid)
00484        {
00485          printf ("waitid WNOHANG on killed pid %d != %d\n", info.si_pid, pid);
00486          RETURN (EXIT_FAILURE);
00487        }
00488     }
00489 
00490   fail = waitid (P_PID, pid, &info, WEXITED);
00491   if (fail == -1)
00492     {
00493       if (errno != ECHILD)
00494        {
00495          printf ("waitid WEXITED on killed: %m\n");
00496          RETURN (EXIT_FAILURE);
00497        }
00498     }
00499   else
00500     {
00501       printf ("waitid WEXITED returned bogus value %d\n", fail);
00502       RETURN (EXIT_FAILURE);
00503     }
00504 
00505 #undef RETURN
00506  out:
00507   if (spurious_sigchld)
00508     status = EXIT_FAILURE;
00509   signal (SIGCHLD, SIG_IGN);
00510   kill (pid, SIGKILL);             /* Make sure it's dead if we bailed early.  */
00511   return status;
00512 }
00513 
00514 #include "../test-skeleton.c"