diff options
Diffstat (limited to 'tests/to.c')
-rw-r--r-- | tests/to.c | 44 |
1 files changed, 38 insertions, 6 deletions
@@ -1,12 +1,31 @@ +#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) { char *progname = argv[0]; unsigned long n; @@ -27,12 +46,15 @@ main (int argc, char **argv) exit (1); } argc -= 2; argv += 2; + signal (SIGALRM, sighan); + signal (SIGCHLD, sighan); + pid = fork (); if (pid == -1) { perror ("fork"); exit (127); } @@ -42,14 +64,29 @@ main (int argc, char **argv) execvp (argv[0], argv); perror (argv[0]); exit (127); } alarm (n); - ret = wait (&status); + 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); if (ret != pid) { perror ("wait"); exit (127); } @@ -64,11 +101,6 @@ main (int argc, char **argv) fprintf (stderr, "%s: %s stopped\n", progname, argv[0]); else fprintf (stderr, "%s: %s exited with unrecognized status %d\n", progname, argv[0], status); return 127; } - - - - - |