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