Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / contrib / timeout_watchdog.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License, or
8      (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file contrib/timeout_watchdog.c
21  * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period
22  * @author Matthias Wachs
23  */
24
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 static pid_t child;
33
34
35 static void
36 sigchld_handler (int val)
37 {
38   int status = 0;
39   int ret = 0;
40
41   (void) val;
42   waitpid (child, &status, 0);
43   if (WIFEXITED (status) != 0)
44     {
45       ret = WEXITSTATUS (status);
46       printf ("Test process exited with result %u\n", ret);
47     }
48   if (WIFSIGNALED (status) != 0)
49     {
50       ret = WTERMSIG (status);
51       printf ("Test process was signaled %u\n", ret);
52     }
53   exit (ret);
54 }
55
56
57 static void
58 sigint_handler (int val)
59 {
60   kill (0, val);
61   exit (val);
62 }
63
64
65 int
66 main (int argc,
67       char *argv[])
68 {
69   int timeout = 0;
70   pid_t gpid = 0;
71
72   if (argc < 3)
73     {
74       printf
75         ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
76       exit (1);
77     }
78
79   timeout = atoi (argv[1]);
80
81   if (timeout == 0)
82     timeout = 600;
83
84 /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
85   gpid = getpgrp ();
86
87   signal (SIGCHLD, sigchld_handler);
88   signal (SIGABRT, sigint_handler);
89   signal (SIGFPE, sigint_handler);
90   signal (SIGILL, sigint_handler);
91   signal (SIGINT, sigint_handler);
92   signal (SIGSEGV, sigint_handler);
93   signal (SIGTERM, sigint_handler);
94
95   child = fork ();
96   if (child == 0)
97     {
98       /*  int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
99       //setpgrp (0, pid_t gpid);
100       if (-1 != gpid)
101         setpgid (0, gpid);
102       execvp (argv[2], &argv[2]);
103       exit (1);
104     }
105   if (child > 0)
106     {
107       sleep (timeout);
108       printf ("Child processes were killed after timeout of %u seconds\n",
109               timeout);
110       kill (0, SIGTERM);
111       exit (1);
112     }
113   exit (1);
114 }
115
116 /* end of timeout_watchdog.c */