diff options
Diffstat (limited to 'tests/to.c')
-rw-r--r-- | tests/to.c | 50 |
1 files changed, 41 insertions, 9 deletions
@@ -1,9 +1,28 @@ +#include <config.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> +#include <signal.h> + +int volatile got_sigchld, got_sigalrm; + +void +sighan (int sig) +{ + switch (sig) + { + case SIGCHLD: + got_sigchld = 1; + break; + + case SIGALRM: + got_sigalrm = 1; + break; + } +} int main (int argc, char **argv) @@ -13,7 +32,7 @@ main (int argc, char **argv) char *p; pid_t pid, ret; int status; - + if (argc < 3) { fprintf (stderr, "usage: %s TIMEOUT COMMAND ...\n", progname); @@ -30,13 +49,16 @@ main (int argc, char **argv) argc -= 2; argv += 2; + signal (SIGALRM, sighan); + signal (SIGCHLD, sighan); + pid = fork (); if (pid == -1) { perror ("fork"); exit (127); } - + if (pid == 0) { execvp (argv[0], argv); @@ -45,8 +67,23 @@ main (int argc, char **argv) } alarm (n); + while (1) + { + pause (); + if (got_sigchld) + { + alarm (0); + break; + } + if (got_sigalrm) + { + fprintf (stderr, "%s: timed out\n", progname); + kill (pid, SIGKILL); + exit (127); + } + } + ret = wait (&status); - alarm (0); if (ret != pid) { @@ -56,7 +93,7 @@ main (int argc, char **argv) if (WIFEXITED (status)) return WEXITSTATUS (status); - + if (WIFSIGNALED (status)) fprintf (stderr, "%s: %s terminated on signal %d\n", progname, argv[0], WTERMSIG (status)); @@ -67,8 +104,3 @@ main (int argc, char **argv) progname, argv[0], status); return 127; } - - - - - |