diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2010-06-28 08:34:57 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2010-06-28 08:34:57 +0000 |
commit | 633c713f6a10312c0c627c858d85212590b47bf0 (patch) | |
tree | fc3ccb9932a44d558c814e54fff5bc2d69a4335a | |
parent | c6a2c9c1199b77cdd7f743c5a30174bb01db1870 (diff) | |
download | gnunet-633c713f6a10312c0c627c858d85212590b47bf0.tar.gz gnunet-633c713f6a10312c0c627c858d85212590b47bf0.zip |
Added timeout_watchdog, a small helper tool for the buildbots
-rw-r--r-- | contrib/timeout_watchdog.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/contrib/timeout_watchdog.c b/contrib/timeout_watchdog.c new file mode 100644 index 000000000..2fb4cd20b --- /dev/null +++ b/contrib/timeout_watchdog.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 2, 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 "signal.h" | ||
28 | #include "stdio.h" | ||
29 | #include "stdlib.h" | ||
30 | #include <unistd.h> | ||
31 | #include <wait.h> | ||
32 | |||
33 | static int child_died; | ||
34 | static pid_t child; | ||
35 | |||
36 | void sigchld_handler(int val) | ||
37 | { | ||
38 | int status = 0; | ||
39 | int ret = 0; | ||
40 | |||
41 | waitpid (child, &status, 0); | ||
42 | if (WIFEXITED(status) == 1) | ||
43 | { | ||
44 | ret = WEXITSTATUS(status); | ||
45 | printf("Test process exited with result %u\n", ret); | ||
46 | } | ||
47 | if (WIFSIGNALED(status) == 1) | ||
48 | { | ||
49 | printf("Test process was signaled %u\n", WTERMSIG(status)); | ||
50 | } | ||
51 | exit(ret); | ||
52 | } | ||
53 | |||
54 | void sigint_handler(int val) | ||
55 | { | ||
56 | printf("Killing test process\n"); | ||
57 | kill(0, SIGINT); | ||
58 | exit(0); | ||
59 | } | ||
60 | |||
61 | |||
62 | int main(int argc, char *argv[]) | ||
63 | { | ||
64 | int timeout = 0; | ||
65 | int remain = 0; | ||
66 | int ret = 0; | ||
67 | |||
68 | if (argc < 3) | ||
69 | { | ||
70 | printf("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); | ||
71 | exit(1); | ||
72 | } | ||
73 | |||
74 | timeout = atoi(argv[1]); | ||
75 | |||
76 | if (timeout == 0) | ||
77 | timeout = 600; | ||
78 | |||
79 | |||
80 | char ** arguments = &argv[3]; | ||
81 | |||
82 | pid_t gpid = getpgid(0); | ||
83 | |||
84 | child = fork(); | ||
85 | if (child > 0) | ||
86 | { | ||
87 | signal(SIGCHLD, sigchld_handler); | ||
88 | signal(SIGINT, sigint_handler); | ||
89 | } | ||
90 | for (;;) | ||
91 | { | ||
92 | if (child==0) | ||
93 | { | ||
94 | printf("Starting test process `%s'\n",argv[2],arguments); | ||
95 | setpgid(0,gpid); | ||
96 | execvp(argv[2],&argv[2]); | ||
97 | printf("Test process `%s' could not be started\n",argv[1]); | ||
98 | exit(1); | ||
99 | } | ||
100 | if (child > 0) | ||
101 | { | ||
102 | sleep(1); | ||
103 | remain++; | ||
104 | if (timeout == remain) | ||
105 | { | ||
106 | printf("Timeout, killing all test processes\n"); | ||
107 | kill(0,SIGABRT); | ||
108 | exit(1); | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | } | ||
114 | |||
115 | /* end of timeout_watchdog.c */ \ No newline at end of file | ||