10 static int caught
= 0;
12 static void caught_signal(int which
)
17 static int wait_for_process(pid_t pid
)
22 struct sigaction sig_handler
;
24 sig_handler
.sa_handler
= caught_signal
;
25 sig_handler
.sa_flags
= 0;
27 sigaction(SIGALRM
, &sig_handler
, NULL
);
28 sigaction(SIGHUP
, &sig_handler
, NULL
);
29 sigaction(SIGINT
, &sig_handler
, NULL
);
30 sigaction(SIGTERM
, &sig_handler
, NULL
);
31 sigaction(SIGPIPE
, &sig_handler
, NULL
);
33 /* Loop forever waiting for the process to quit */
35 pid_t p
= waitpid(pid
, &stats
, 0);
37 /* child exited. Let's get out of here */
38 rv
= WIFEXITED(stats
) ?
40 (0x80 | WTERMSIG(stats
));
46 /* On the first iteration, pass the signal through */
47 sig
= caught
> 0 ? caught
: SIGTERM
;
48 if (caught
== SIGALRM
) {
49 fprintf(stderr
, "Timeout.. killing the process\n");
59 if (kill(pid
, sig
) < 0) {
60 /* Kill failed. Must have lost the process. :/ */
61 perror("lost child when trying to kill");
63 /* Wait up to 5 seconds for the pid */
70 static int spawn_and_wait(char **argv
)
79 break; /* NOTREACHED */
81 execvp(argv
[0], argv
);
84 break; /* NOTREACHED */
86 rv
= wait_for_process(pid
);
91 int main(int argc
, char **argv
)
96 naptime
= atoi(argv
[1]);
97 assert(naptime
> 0 && naptime
< 1800);
101 return spawn_and_wait(argv
+2);