really disable upnp
[oweals/gnunet.git] / contrib / timeout_watchdog.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 static void sigchld_handler(int val)
37 {
38   int status = 0;
39   int ret = 0;
40   
41   waitpid (child, &status, 0);
42   if (WIFEXITED(status) != 0)
43   {
44     ret = WEXITSTATUS(status);
45     printf("Test process exited with result %u\n", ret);
46   }
47   if (WIFSIGNALED(status) != 0)
48   {
49         ret = WTERMSIG(status);
50         printf("Test process was signaled %u\n", ret);
51   }   
52   exit(ret);  
53 }
54
55 static void sigint_handler(int val)
56
57   kill(0, val);
58   exit(val);
59 }
60
61 int main(int argc, char *argv[])
62 {
63 int timeout = 0;
64 pid_t gpid =0;
65
66 if (argc < 3)
67 {  
68   printf("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");     
69   exit(1);
70 }
71
72 timeout = atoi(argv[1]);
73
74 if (timeout == 0)
75   timeout = 600;   
76
77 /* with getpgid() it does not compile, but getpgrp is the BSD version and working */
78 gpid = getpgrp();
79
80 signal(SIGCHLD, sigchld_handler);
81 signal(SIGABRT, sigint_handler);
82 signal(SIGFPE,  sigint_handler);
83 signal(SIGILL,  sigint_handler);
84 signal(SIGINT,  sigint_handler);
85 signal(SIGSEGV, sigint_handler);
86 signal(SIGTERM, sigint_handler);
87
88 child = fork();
89 if (child==0)
90 {
91   /*  int setpgrp(pid_t pid, pid_t pgid); is not working on this machine*/
92    //setpgrp (0, pid_t gpid);
93    setpgid(0,gpid);
94    execvp(argv[2],&argv[2]);
95    exit(1);
96 }
97 if (child > 0)
98 {
99   sleep(timeout);
100   printf("Child processes were killed after timeout of %u seconds\n",timeout);
101   kill(0,SIGTERM);
102   exit(1);
103 }  
104 exit(1);
105 }
106
107 /* end of timeout_watchdog.c */