undo hostkey patch
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file contrib/timeout_watchdog.c
23  * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period
24  * @author Matthias Wachs
25  */
26
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 static pid_t child;
35
36
37 static void
38 sigchld_handler (int val)
39 {
40   int status = 0;
41   int ret = 0;
42
43   (void) val;
44   waitpid (child, &status, 0);
45   if (WIFEXITED (status) != 0)
46     {
47       ret = WEXITSTATUS (status);
48       printf ("Test process exited with result %u\n", ret);
49     }
50   if (WIFSIGNALED (status) != 0)
51     {
52       ret = WTERMSIG (status);
53       printf ("Test process was signaled %u\n", ret);
54     }
55   exit (ret);
56 }
57
58
59 static void
60 sigint_handler (int val)
61 {
62   kill (0, val);
63   exit (val);
64 }
65
66
67 int
68 main (int argc,
69       char *argv[])
70 {
71   int timeout = 0;
72   pid_t gpid = 0;
73
74   if (argc < 3)
75     {
76       printf
77         ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
78       exit (1);
79     }
80
81   timeout = atoi (argv[1]);
82
83   if (timeout == 0)
84     timeout = 600;
85
86 /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
87   gpid = getpgrp ();
88
89   signal (SIGCHLD, sigchld_handler);
90   signal (SIGABRT, sigint_handler);
91   signal (SIGFPE, sigint_handler);
92   signal (SIGILL, sigint_handler);
93   signal (SIGINT, sigint_handler);
94   signal (SIGSEGV, sigint_handler);
95   signal (SIGTERM, sigint_handler);
96
97   child = fork ();
98   if (child == 0)
99     {
100       /*  int setpgrp(pid_t pid, pid_t pgid); is not working on this machine */
101       //setpgrp (0, pid_t gpid);
102       if (-1 != gpid)
103         setpgid (0, gpid);
104       execvp (argv[2], &argv[2]);
105       exit (1);
106     }
107   if (child > 0)
108     {
109       sleep (timeout);
110       printf ("Child processes were killed after timeout of %u seconds\n",
111               timeout);
112       kill (0, SIGTERM);
113       exit (1);
114     }
115   exit (1);
116 }
117
118 /* end of timeout_watchdog.c */