diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/timeout_watchdog.c | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/contrib/timeout_watchdog.c b/contrib/timeout_watchdog.c index 9a1fc006d..b28e8e833 100644 --- a/contrib/timeout_watchdog.c +++ b/contrib/timeout_watchdog.c | |||
@@ -33,75 +33,80 @@ | |||
33 | 33 | ||
34 | static pid_t child; | 34 | static pid_t child; |
35 | 35 | ||
36 | static void sigchld_handler(int val) | 36 | static void |
37 | sigchld_handler (int val) | ||
37 | { | 38 | { |
38 | int status = 0; | 39 | int status = 0; |
39 | int ret = 0; | 40 | int ret = 0; |
40 | 41 | ||
41 | waitpid (child, &status, 0); | 42 | waitpid (child, &status, 0); |
42 | if (WIFEXITED(status) != 0) | 43 | if (WIFEXITED (status) != 0) |
43 | { | 44 | { |
44 | ret = WEXITSTATUS(status); | 45 | ret = WEXITSTATUS (status); |
45 | printf("Test process exited with result %u\n", ret); | 46 | printf ("Test process exited with result %u\n", ret); |
46 | } | 47 | } |
47 | if (WIFSIGNALED(status) != 0) | 48 | if (WIFSIGNALED (status) != 0) |
48 | { | 49 | { |
49 | ret = WTERMSIG(status); | 50 | ret = WTERMSIG (status); |
50 | printf("Test process was signaled %u\n", ret); | 51 | printf ("Test process was signaled %u\n", ret); |
51 | } | 52 | } |
52 | exit(ret); | 53 | exit (ret); |
53 | } | 54 | } |
54 | 55 | ||
55 | static void sigint_handler(int val) | 56 | static void |
56 | { | 57 | sigint_handler (int val) |
57 | kill(0, val); | 58 | { |
58 | exit(val); | 59 | kill (0, val); |
60 | exit (val); | ||
59 | } | 61 | } |
60 | 62 | ||
61 | int main(int argc, char *argv[]) | 63 | int |
64 | main (int argc, char *argv[]) | ||
62 | { | 65 | { |
63 | int timeout = 0; | 66 | int timeout = 0; |
64 | pid_t gpid =0; | 67 | pid_t gpid = 0; |
65 | 68 | ||
66 | if (argc < 3) | 69 | if (argc < 3) |
67 | { | 70 | { |
68 | printf("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); | 71 | printf |
69 | exit(1); | 72 | ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); |
70 | } | 73 | exit (1); |
74 | } | ||
71 | 75 | ||
72 | timeout = atoi(argv[1]); | 76 | timeout = atoi (argv[1]); |
73 | 77 | ||
74 | if (timeout == 0) | 78 | if (timeout == 0) |
75 | timeout = 600; | 79 | timeout = 600; |
76 | 80 | ||
77 | /* with getpgid() it does not compile, but getpgrp is the BSD version and working */ | 81 | /* with getpgid() it does not compile, but getpgrp is the BSD version and working */ |
78 | gpid = getpgrp(); | 82 | gpid = getpgrp (); |
79 | 83 | ||
80 | signal(SIGCHLD, sigchld_handler); | 84 | signal (SIGCHLD, sigchld_handler); |
81 | signal(SIGABRT, sigint_handler); | 85 | signal (SIGABRT, sigint_handler); |
82 | signal(SIGFPE, sigint_handler); | 86 | signal (SIGFPE, sigint_handler); |
83 | signal(SIGILL, sigint_handler); | 87 | signal (SIGILL, sigint_handler); |
84 | signal(SIGINT, sigint_handler); | 88 | signal (SIGINT, sigint_handler); |
85 | signal(SIGSEGV, sigint_handler); | 89 | signal (SIGSEGV, sigint_handler); |
86 | signal(SIGTERM, sigint_handler); | 90 | signal (SIGTERM, sigint_handler); |
87 | 91 | ||
88 | child = fork(); | 92 | child = fork (); |
89 | if (child==0) | 93 | if (child == 0) |
90 | { | 94 | { |
91 | /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine*/ | 95 | /* int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */ |
92 | //setpgrp (0, pid_t gpid); | 96 | //setpgrp (0, pid_t gpid); |
93 | setpgid(0,gpid); | 97 | setpgid (0, gpid); |
94 | execvp(argv[2],&argv[2]); | 98 | execvp (argv[2], &argv[2]); |
95 | exit(1); | 99 | exit (1); |
96 | } | 100 | } |
97 | if (child > 0) | 101 | if (child > 0) |
98 | { | 102 | { |
99 | sleep(timeout); | 103 | sleep (timeout); |
100 | printf("Child processes were killed after timeout of %u seconds\n",timeout); | 104 | printf ("Child processes were killed after timeout of %u seconds\n", |
101 | kill(0,SIGTERM); | 105 | timeout); |
102 | exit(1); | 106 | kill (0, SIGTERM); |
103 | } | 107 | exit (1); |
104 | exit(1); | 108 | } |
109 | exit (1); | ||
105 | } | 110 | } |
106 | 111 | ||
107 | /* end of timeout_watchdog.c */ | 112 | /* end of timeout_watchdog.c */ |