tests-autotest/timeout.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <unistd.h>
00004 #include <signal.h>
00005 
00024 int _pid = -1;
00025 
00030 void family_assassination (int ignored)
00031 {
00032     (void) printf ("%s\n", "timeout");
00033     if (-1 != _pid) kill (_pid, SIGTERM);
00034     if (-1 != _pid) sleep(1);
00035     if (-1 != _pid) kill (_pid, SIGKILL);
00036     exit(1);
00037 }
00038 
00052 int main (int argc, char *argv[])
00053 {
00054     char opt;
00055     char testing = 1;
00056     char background = 1;
00057     unsigned sleeper = 1;
00058     struct sigaction timeout_sigaction =
00059     {
00060         .sa_handler = family_assassination
00061     };
00062 
00063     (void) sigaction (SIGALRM, &timeout_sigaction, NULL);
00064 
00065     while (-1 != (opt = getopt(argc, argv, "Bt:")))
00066     switch (opt)
00067     {
00068         case 'B': /* background for the daemon */
00069             background = 1;
00070             break;
00071         case 't': /* timeout */
00072             sleeper = atoi(optarg);
00073             testing = 0;
00074             break;
00075         case '?': /* unknown; ignored */
00076             fprintf (stderr, "unknown option\n");
00077             return -2;
00078     }
00079 
00080     if (optind >= argc)
00081        return 1;
00082     /*
00083      * parent returns immediately with a good return to allow backgrounding daemons (themselves
00084      * non-backgrounding) so that the parent test-runner can continue, letting the test daemon be
00085      * killed on timeout
00086      */
00087     else if (0 < background)
00088     {
00089         switch (fork())
00090         {
00091             case 0: /* child */
00092                 break;
00093             default: /* parent */
00094                 return 0;
00095         }
00096     }
00097 
00098     /* else */
00099     switch (_pid = fork())
00100     {
00101         case 0: /* child */
00102             if (testing)
00103             {
00104                 printf ("argc %d ; optind %d; argv[optind] [%s]\n", argc, optind, argv[optind]);
00105                 sleep (5+sleeper);
00106             }
00107             else
00108             {
00109                 execvp (argv[optind], &argv[optind]);
00110                 return 1;
00111             }
00112             
00113             return 0;
00114 
00115         default: /* parent */
00116             /* set the morning wakeup */
00117             alarm (sleeper);
00118 
00119             /* and snooze for the child forked pid */
00120             //(void) wait (NULL);
00121             /* and wait for SIGCHLD or SIGALRM */
00122             (void) pause();
00123 
00124             /* if we got here, the pid finished */
00125             return 0;
00126     }
00127 }

Generated on Sun Jul 12 20:25:26 2009 for mysqlfs by  doxygen 1.4.7