diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/Makefile.am | 11 | ||||
-rw-r--r-- | contrib/timeout_watchdog.c | 116 | ||||
-rw-r--r-- | contrib/timeout_watchdog_w32.c | 191 |
3 files changed, 0 insertions, 318 deletions
diff --git a/contrib/Makefile.am b/contrib/Makefile.am index 158e43998..eec3300b9 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am | |||
@@ -5,17 +5,6 @@ tap32dir = $(pkgdatadir)/openvpn-tap32/tapw32/ | |||
5 | 5 | ||
6 | tap64dir = $(pkgdatadir)/openvpn-tap32/tapw64/ | 6 | tap64dir = $(pkgdatadir)/openvpn-tap32/tapw64/ |
7 | 7 | ||
8 | noinst_PROGRAMS = \ | ||
9 | timeout_watchdog | ||
10 | |||
11 | if !MINGW | ||
12 | timeout_watchdog_SOURCES = \ | ||
13 | timeout_watchdog.c | ||
14 | else | ||
15 | timeout_watchdog_SOURCES = \ | ||
16 | timeout_watchdog_w32.c | ||
17 | endif | ||
18 | |||
19 | noinst_SCRIPTS = \ | 8 | noinst_SCRIPTS = \ |
20 | scripts/terminate.py \ | 9 | scripts/terminate.py \ |
21 | scripts/pydiffer.py \ | 10 | scripts/pydiffer.py \ |
diff --git a/contrib/timeout_watchdog.c b/contrib/timeout_watchdog.c deleted file mode 100644 index 70e840d55..000000000 --- a/contrib/timeout_watchdog.c +++ /dev/null | |||
@@ -1,116 +0,0 @@ | |||
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 */ | ||
diff --git a/contrib/timeout_watchdog_w32.c b/contrib/timeout_watchdog_w32.c deleted file mode 100644 index 901eb6207..000000000 --- a/contrib/timeout_watchdog_w32.c +++ /dev/null | |||
@@ -1,191 +0,0 @@ | |||
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_w32.c | ||
21 | * @brief small tool starting a child process, waiting that it terminates or killing it after a given timeout period | ||
22 | * @author LRN | ||
23 | */ | ||
24 | |||
25 | #include <windows.h> | ||
26 | #include <sys/types.h> | ||
27 | #include <stdio.h> | ||
28 | |||
29 | int | ||
30 | main (int argc, char *argv[]) | ||
31 | { | ||
32 | int i; | ||
33 | DWORD wait_result; | ||
34 | wchar_t *commandline; | ||
35 | wchar_t **wargv; | ||
36 | wchar_t *arg; | ||
37 | unsigned int cmdlen; | ||
38 | STARTUPINFOW start; | ||
39 | PROCESS_INFORMATION proc; | ||
40 | |||
41 | wchar_t wpath[MAX_PATH + 1]; | ||
42 | |||
43 | wchar_t *pathbuf; | ||
44 | DWORD pathbuf_len, alloc_len; | ||
45 | wchar_t *ptr; | ||
46 | wchar_t *non_const_filename; | ||
47 | wchar_t *wcmd; | ||
48 | int wargc; | ||
49 | int timeout = 0; | ||
50 | ssize_t wrote; | ||
51 | |||
52 | HANDLE job; | ||
53 | |||
54 | if (argc < 3) | ||
55 | { | ||
56 | printf | ||
57 | ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); | ||
58 | exit (1); | ||
59 | } | ||
60 | |||
61 | timeout = atoi (argv[1]); | ||
62 | |||
63 | if (timeout == 0) | ||
64 | timeout = 600; | ||
65 | |||
66 | commandline = GetCommandLineW (); | ||
67 | if (commandline == NULL) | ||
68 | { | ||
69 | printf ("Failed to get commandline: %lu\n", GetLastError ()); | ||
70 | exit (2); | ||
71 | } | ||
72 | |||
73 | wargv = CommandLineToArgvW (commandline, &wargc); | ||
74 | if (wargv == NULL || wargc <= 1) | ||
75 | { | ||
76 | printf ("Failed to get parse commandline: %lu\n", GetLastError ()); | ||
77 | exit (3); | ||
78 | } | ||
79 | |||
80 | job = CreateJobObject (NULL, NULL); | ||
81 | if (job == NULL) | ||
82 | { | ||
83 | printf ("Failed to create a job: %lu\n", GetLastError ()); | ||
84 | exit (4); | ||
85 | } | ||
86 | |||
87 | pathbuf_len = GetEnvironmentVariableW (L"PATH", (wchar_t *) &pathbuf, 0); | ||
88 | |||
89 | alloc_len = pathbuf_len + 1; | ||
90 | |||
91 | pathbuf = malloc (alloc_len * sizeof (wchar_t)); | ||
92 | |||
93 | ptr = pathbuf; | ||
94 | |||
95 | alloc_len = GetEnvironmentVariableW (L"PATH", ptr, pathbuf_len); | ||
96 | |||
97 | cmdlen = wcslen (wargv[2]); | ||
98 | if (cmdlen < 5 || wcscmp (&wargv[2][cmdlen - 4], L".exe") != 0) | ||
99 | { | ||
100 | non_const_filename = malloc (sizeof (wchar_t) * (cmdlen + 5)); | ||
101 | swprintf (non_const_filename, cmdlen + 5, L"%S.exe", wargv[2]); | ||
102 | } | ||
103 | else | ||
104 | { | ||
105 | non_const_filename = wcsdup (wargv[2]); | ||
106 | } | ||
107 | |||
108 | /* Check that this is the full path. If it isn't, search. */ | ||
109 | if (non_const_filename[1] == L':') | ||
110 | swprintf (wpath, sizeof (wpath) / sizeof (wchar_t), L"%S", non_const_filename); | ||
111 | else if (!SearchPathW | ||
112 | (pathbuf, non_const_filename, NULL, sizeof (wpath) / sizeof (wchar_t), | ||
113 | wpath, NULL)) | ||
114 | { | ||
115 | printf ("Failed to get find executable: %lu\n", GetLastError ()); | ||
116 | exit (5); | ||
117 | } | ||
118 | free (pathbuf); | ||
119 | free (non_const_filename); | ||
120 | |||
121 | cmdlen = wcslen (wpath) + 4; | ||
122 | i = 3; | ||
123 | while (NULL != (arg = wargv[i++])) | ||
124 | cmdlen += wcslen (arg) + 4; | ||
125 | |||
126 | wcmd = malloc (sizeof (wchar_t) * (cmdlen + 1)); | ||
127 | wrote = 0; | ||
128 | i = 2; | ||
129 | while (NULL != (arg = wargv[i++])) | ||
130 | { | ||
131 | /* This is to escape trailing slash */ | ||
132 | wchar_t arg_lastchar = arg[wcslen (arg) - 1]; | ||
133 | if (wrote == 0) | ||
134 | { | ||
135 | wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\" ", wpath, | ||
136 | arg_lastchar == L'\\' ? L"\\" : L""); | ||
137 | } | ||
138 | else | ||
139 | { | ||
140 | if (wcschr (arg, L' ') != NULL) | ||
141 | wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\"%S", arg, | ||
142 | arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" "); | ||
143 | else | ||
144 | wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"%S%S%S", arg, | ||
145 | arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" "); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | LocalFree (wargv); | ||
150 | |||
151 | memset (&start, 0, sizeof (start)); | ||
152 | start.cb = sizeof (start); | ||
153 | |||
154 | if (!CreateProcessW (wpath, wcmd, NULL, NULL, TRUE, CREATE_SUSPENDED, | ||
155 | NULL, NULL, &start, &proc)) | ||
156 | { | ||
157 | wprintf (L"Failed to get spawn process `%S' with arguments `%S': %lu\n", wpath, wcmd, GetLastError ()); | ||
158 | exit (6); | ||
159 | } | ||
160 | |||
161 | AssignProcessToJobObject (job, proc.hProcess); | ||
162 | |||
163 | ResumeThread (proc.hThread); | ||
164 | CloseHandle (proc.hThread); | ||
165 | |||
166 | free (wcmd); | ||
167 | |||
168 | wait_result = WaitForSingleObject (proc.hProcess, timeout * 1000); | ||
169 | if (wait_result == WAIT_OBJECT_0) | ||
170 | { | ||
171 | DWORD status; | ||
172 | wait_result = GetExitCodeProcess (proc.hProcess, &status); | ||
173 | CloseHandle (proc.hProcess); | ||
174 | if (wait_result != 0) | ||
175 | { | ||
176 | printf ("Test process exited with result %lu\n", status); | ||
177 | TerminateJobObject (job, status); | ||
178 | exit (status); | ||
179 | } | ||
180 | printf ("Test process exited (failed to obtain exit status)\n"); | ||
181 | TerminateJobObject (job, 0); | ||
182 | exit (0); | ||
183 | } | ||
184 | printf ("Child processes were killed after timeout of %u seconds\n", | ||
185 | timeout); | ||
186 | TerminateJobObject (job, 1); | ||
187 | CloseHandle (proc.hProcess); | ||
188 | exit (1); | ||
189 | } | ||
190 | |||
191 | /* end of timeout_watchdog_w32.c */ | ||