aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-timeout-w32.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-timeout-w32.c')
-rw-r--r--src/util/gnunet-timeout-w32.c182
1 files changed, 91 insertions, 91 deletions
diff --git a/src/util/gnunet-timeout-w32.c b/src/util/gnunet-timeout-w32.c
index 6f92b172f..3e579714c 100644
--- a/src/util/gnunet-timeout-w32.c
+++ b/src/util/gnunet-timeout-w32.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/util/gnunet-timeout-w32.c 22 * @file src/util/gnunet-timeout-w32.c
@@ -29,7 +29,7 @@
29#include <stdio.h> 29#include <stdio.h>
30 30
31int 31int
32main (int argc, char *argv[]) 32main(int argc, char *argv[])
33{ 33{
34 int i; 34 int i;
35 DWORD wait_result; 35 DWORD wait_result;
@@ -56,138 +56,138 @@ main (int argc, char *argv[])
56 if (argc < 3) 56 if (argc < 3)
57 { 57 {
58 printf 58 printf
59 ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n"); 59 ("arg 1: timeout in sec., arg 2: executable, arg<n> arguments\n");
60 exit (1); 60 exit(1);
61 } 61 }
62 62
63 timeout = atoi (argv[1]); 63 timeout = atoi(argv[1]);
64 64
65 if (timeout == 0) 65 if (timeout == 0)
66 timeout = 600; 66 timeout = 600;
67 67
68 commandline = GetCommandLineW (); 68 commandline = GetCommandLineW();
69 if (commandline == NULL) 69 if (commandline == NULL)
70 { 70 {
71 printf ("Failed to get commandline: %lu\n", GetLastError ()); 71 printf("Failed to get commandline: %lu\n", GetLastError());
72 exit (2); 72 exit(2);
73 } 73 }
74 74
75 wargv = CommandLineToArgvW (commandline, &wargc); 75 wargv = CommandLineToArgvW(commandline, &wargc);
76 if (wargv == NULL || wargc <= 1) 76 if (wargv == NULL || wargc <= 1)
77 { 77 {
78 printf ("Failed to get parse commandline: %lu\n", GetLastError ()); 78 printf("Failed to get parse commandline: %lu\n", GetLastError());
79 exit (3); 79 exit(3);
80 } 80 }
81 81
82 job = CreateJobObject (NULL, NULL); 82 job = CreateJobObject(NULL, NULL);
83 if (job == NULL) 83 if (job == NULL)
84 { 84 {
85 printf ("Failed to create a job: %lu\n", GetLastError ()); 85 printf("Failed to create a job: %lu\n", GetLastError());
86 exit (4); 86 exit(4);
87 } 87 }
88 88
89 pathbuf_len = GetEnvironmentVariableW (L"PATH", (wchar_t *) &pathbuf, 0); 89 pathbuf_len = GetEnvironmentVariableW(L"PATH", (wchar_t *)&pathbuf, 0);
90 90
91 alloc_len = pathbuf_len + 1; 91 alloc_len = pathbuf_len + 1;
92 92
93 pathbuf = malloc (alloc_len * sizeof (wchar_t)); 93 pathbuf = malloc(alloc_len * sizeof(wchar_t));
94 94
95 ptr = pathbuf; 95 ptr = pathbuf;
96 96
97 alloc_len = GetEnvironmentVariableW (L"PATH", ptr, pathbuf_len); 97 alloc_len = GetEnvironmentVariableW(L"PATH", ptr, pathbuf_len);
98 98
99 cmdlen = wcslen (wargv[2]); 99 cmdlen = wcslen(wargv[2]);
100 if (cmdlen < 5 || wcscmp (&wargv[2][cmdlen - 4], L".exe") != 0) 100 if (cmdlen < 5 || wcscmp(&wargv[2][cmdlen - 4], L".exe") != 0)
101 { 101 {
102 non_const_filename = malloc (sizeof (wchar_t) * (cmdlen + 5)); 102 non_const_filename = malloc(sizeof(wchar_t) * (cmdlen + 5));
103 swprintf (non_const_filename, cmdlen + 5, L"%S.exe", wargv[2]); 103 swprintf(non_const_filename, cmdlen + 5, L"%S.exe", wargv[2]);
104 } 104 }
105 else 105 else
106 { 106 {
107 non_const_filename = wcsdup (wargv[2]); 107 non_const_filename = wcsdup(wargv[2]);
108 } 108 }
109 109
110 /* Check that this is the full path. If it isn't, search. */ 110 /* Check that this is the full path. If it isn't, search. */
111 if (non_const_filename[1] == L':') 111 if (non_const_filename[1] == L':')
112 swprintf (wpath, sizeof (wpath) / sizeof (wchar_t), L"%S", non_const_filename); 112 swprintf(wpath, sizeof(wpath) / sizeof(wchar_t), L"%S", non_const_filename);
113 else if (!SearchPathW 113 else if (!SearchPathW
114 (pathbuf, non_const_filename, NULL, sizeof (wpath) / sizeof (wchar_t), 114 (pathbuf, non_const_filename, NULL, sizeof(wpath) / sizeof(wchar_t),
115 wpath, NULL)) 115 wpath, NULL))
116 { 116 {
117 printf ("Failed to get find executable: %lu\n", GetLastError ()); 117 printf("Failed to get find executable: %lu\n", GetLastError());
118 exit (5); 118 exit(5);
119 } 119 }
120 free (pathbuf); 120 free(pathbuf);
121 free (non_const_filename); 121 free(non_const_filename);
122 122
123 cmdlen = wcslen (wpath) + 4; 123 cmdlen = wcslen(wpath) + 4;
124 i = 3; 124 i = 3;
125 while (NULL != (arg = wargv[i++])) 125 while (NULL != (arg = wargv[i++]))
126 cmdlen += wcslen (arg) + 4; 126 cmdlen += wcslen(arg) + 4;
127 127
128 wcmd = malloc (sizeof (wchar_t) * (cmdlen + 1)); 128 wcmd = malloc(sizeof(wchar_t) * (cmdlen + 1));
129 wrote = 0; 129 wrote = 0;
130 i = 2; 130 i = 2;
131 while (NULL != (arg = wargv[i++])) 131 while (NULL != (arg = wargv[i++]))
132 {
133 /* This is to escape trailing slash */
134 wchar_t arg_lastchar = arg[wcslen (arg) - 1];
135 if (wrote == 0)
136 { 132 {
137 wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\" ", wpath, 133 /* This is to escape trailing slash */
138 arg_lastchar == L'\\' ? L"\\" : L""); 134 wchar_t arg_lastchar = arg[wcslen(arg) - 1];
139 } 135 if (wrote == 0)
140 else 136 {
141 { 137 wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\" ", wpath,
142 if (wcschr (arg, L' ') != NULL) 138 arg_lastchar == L'\\' ? L"\\" : L"");
143 wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\"%S", arg, 139 }
144 arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" ");
145 else 140 else
146 wrote += swprintf (&wcmd[wrote], cmdlen + 1 - wrote, L"%S%S%S", arg, 141 {
147 arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" "); 142 if (wcschr(arg, L' ') != NULL)
143 wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"\"%S%S\"%S", arg,
144 arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" ");
145 else
146 wrote += swprintf(&wcmd[wrote], cmdlen + 1 - wrote, L"%S%S%S", arg,
147 arg_lastchar == L'\\' ? L"\\" : L"", i == wargc ? L"" : L" ");
148 }
148 } 149 }
149 }
150 150
151 LocalFree (wargv); 151 LocalFree(wargv);
152 152
153 memset (&start, 0, sizeof (start)); 153 memset(&start, 0, sizeof(start));
154 start.cb = sizeof (start); 154 start.cb = sizeof(start);
155 155
156 if (!CreateProcessW (wpath, wcmd, NULL, NULL, TRUE, CREATE_SUSPENDED, 156 if (!CreateProcessW(wpath, wcmd, NULL, NULL, TRUE, CREATE_SUSPENDED,
157 NULL, NULL, &start, &proc)) 157 NULL, NULL, &start, &proc))
158 { 158 {
159 wprintf (L"Failed to get spawn process `%S' with arguments `%S': %lu\n", wpath, wcmd, GetLastError ()); 159 wprintf(L"Failed to get spawn process `%S' with arguments `%S': %lu\n", wpath, wcmd, GetLastError());
160 exit (6); 160 exit(6);
161 } 161 }
162 162
163 AssignProcessToJobObject (job, proc.hProcess); 163 AssignProcessToJobObject(job, proc.hProcess);
164 164
165 ResumeThread (proc.hThread); 165 ResumeThread(proc.hThread);
166 CloseHandle (proc.hThread); 166 CloseHandle(proc.hThread);
167 167
168 free (wcmd); 168 free(wcmd);
169 169
170 wait_result = WaitForSingleObject (proc.hProcess, timeout * 1000); 170 wait_result = WaitForSingleObject(proc.hProcess, timeout * 1000);
171 if (wait_result == WAIT_OBJECT_0) 171 if (wait_result == WAIT_OBJECT_0)
172 {
173 DWORD status;
174 wait_result = GetExitCodeProcess (proc.hProcess, &status);
175 CloseHandle (proc.hProcess);
176 if (wait_result != 0)
177 { 172 {
178 printf ("Test process exited with result %lu\n", status); 173 DWORD status;
179 TerminateJobObject (job, status); 174 wait_result = GetExitCodeProcess(proc.hProcess, &status);
180 exit (status); 175 CloseHandle(proc.hProcess);
176 if (wait_result != 0)
177 {
178 printf("Test process exited with result %lu\n", status);
179 TerminateJobObject(job, status);
180 exit(status);
181 }
182 printf("Test process exited (failed to obtain exit status)\n");
183 TerminateJobObject(job, 0);
184 exit(0);
181 } 185 }
182 printf ("Test process exited (failed to obtain exit status)\n"); 186 printf("Child processes were killed after timeout of %u seconds\n",
183 TerminateJobObject (job, 0); 187 timeout);
184 exit (0); 188 TerminateJobObject(job, 1);
185 } 189 CloseHandle(proc.hProcess);
186 printf ("Child processes were killed after timeout of %u seconds\n", 190 exit(1);
187 timeout);
188 TerminateJobObject (job, 1);
189 CloseHandle (proc.hProcess);
190 exit (1);
191} 191}
192 192
193/* end of timeout_watchdog_w32.c */ 193/* end of timeout_watchdog_w32.c */