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 @@ | |||
1 | #include <config.h> | ||
1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
2 | #include <stdio.h> | 3 | #include <stdio.h> |
3 | #include <errno.h> | 4 | #include <errno.h> |
4 | #include <unistd.h> | 5 | #include <unistd.h> |
5 | #include <sys/types.h> | 6 | #include <sys/types.h> |
6 | #include <sys/wait.h> | 7 | #include <sys/wait.h> |
8 | #include <signal.h> | ||
9 | |||
10 | int volatile got_sigchld, got_sigalrm; | ||
11 | |||
12 | void | ||
13 | sighan (int sig) | ||
14 | { | ||
15 | switch (sig) | ||
16 | { | ||
17 | case SIGCHLD: | ||
18 | got_sigchld = 1; | ||
19 | break; | ||
20 | |||
21 | case SIGALRM: | ||
22 | got_sigalrm = 1; | ||
23 | break; | ||
24 | } | ||
25 | } | ||
7 | 26 | ||
8 | int | 27 | int |
9 | main (int argc, char **argv) | 28 | main (int argc, char **argv) |
@@ -13,7 +32,7 @@ main (int argc, char **argv) | |||
13 | char *p; | 32 | char *p; |
14 | pid_t pid, ret; | 33 | pid_t pid, ret; |
15 | int status; | 34 | int status; |
16 | 35 | ||
17 | if (argc < 3) | 36 | if (argc < 3) |
18 | { | 37 | { |
19 | fprintf (stderr, "usage: %s TIMEOUT COMMAND ...\n", progname); | 38 | fprintf (stderr, "usage: %s TIMEOUT COMMAND ...\n", progname); |
@@ -30,13 +49,16 @@ main (int argc, char **argv) | |||
30 | argc -= 2; | 49 | argc -= 2; |
31 | argv += 2; | 50 | argv += 2; |
32 | 51 | ||
52 | signal (SIGALRM, sighan); | ||
53 | signal (SIGCHLD, sighan); | ||
54 | |||
33 | pid = fork (); | 55 | pid = fork (); |
34 | if (pid == -1) | 56 | if (pid == -1) |
35 | { | 57 | { |
36 | perror ("fork"); | 58 | perror ("fork"); |
37 | exit (127); | 59 | exit (127); |
38 | } | 60 | } |
39 | 61 | ||
40 | if (pid == 0) | 62 | if (pid == 0) |
41 | { | 63 | { |
42 | execvp (argv[0], argv); | 64 | execvp (argv[0], argv); |
@@ -45,8 +67,23 @@ main (int argc, char **argv) | |||
45 | } | 67 | } |
46 | 68 | ||
47 | alarm (n); | 69 | alarm (n); |
70 | while (1) | ||
71 | { | ||
72 | pause (); | ||
73 | if (got_sigchld) | ||
74 | { | ||
75 | alarm (0); | ||
76 | break; | ||
77 | } | ||
78 | if (got_sigalrm) | ||
79 | { | ||
80 | fprintf (stderr, "%s: timed out\n", progname); | ||
81 | kill (pid, SIGKILL); | ||
82 | exit (127); | ||
83 | } | ||
84 | } | ||
85 | |||
48 | ret = wait (&status); | 86 | ret = wait (&status); |
49 | alarm (0); | ||
50 | 87 | ||
51 | if (ret != pid) | 88 | if (ret != pid) |
52 | { | 89 | { |
@@ -56,7 +93,7 @@ main (int argc, char **argv) | |||
56 | 93 | ||
57 | if (WIFEXITED (status)) | 94 | if (WIFEXITED (status)) |
58 | return WEXITSTATUS (status); | 95 | return WEXITSTATUS (status); |
59 | 96 | ||
60 | if (WIFSIGNALED (status)) | 97 | if (WIFSIGNALED (status)) |
61 | fprintf (stderr, "%s: %s terminated on signal %d\n", progname, argv[0], | 98 | fprintf (stderr, "%s: %s terminated on signal %d\n", progname, argv[0], |
62 | WTERMSIG (status)); | 99 | WTERMSIG (status)); |
@@ -67,8 +104,3 @@ main (int argc, char **argv) | |||
67 | progname, argv[0], status); | 104 | progname, argv[0], status); |
68 | return 127; | 105 | return 127; |
69 | } | 106 | } |
70 | |||
71 | |||
72 | |||
73 | |||
74 | |||