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':
00069 background = 1;
00070 break;
00071 case 't':
00072 sleeper = atoi(optarg);
00073 testing = 0;
00074 break;
00075 case '?':
00076 fprintf (stderr, "unknown option\n");
00077 return -2;
00078 }
00079
00080 if (optind >= argc)
00081 return 1;
00082
00083
00084
00085
00086
00087 else if (0 < background)
00088 {
00089 switch (fork())
00090 {
00091 case 0:
00092 break;
00093 default:
00094 return 0;
00095 }
00096 }
00097
00098
00099 switch (_pid = fork())
00100 {
00101 case 0:
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:
00116
00117 alarm (sleeper);
00118
00119
00120
00121
00122 (void) pause();
00123
00124
00125 return 0;
00126 }
00127 }