Debian Codegraph @

lib/roken/simple_exec_w32.c

raw
1 /***********************************************************************
2 * Copyright (c) 2009, Secure Endpoints Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 **********************************************************************/
31
32 #include <config.h>
33
34 #include <roken.h>
35 #include <strsafe.h>
36
37 #ifndef _WIN32
38 #error This implementation is Windows specific.
39 #endif
40
41 /**
42 * wait_for_process_timed waits for a process to terminate or until a
43 * specified timeout occurs.
44 *
45 * @param[in] pid Process id for the monitored process
46
47 * @param[in] func Timeout callback function. When the wait times out,
48 * the callback function is called. The possible return values
49 * from the callback function are:
50 *
51 * - ((time_t) -2) Exit loop without killing child and return SE_E_EXECTIMEOUT.
52 * - ((time_t) -1) Kill child with SIGTERM and wait for child to exit.
53 * - 0 Don't timeout again
54 * - n Seconds to next timeout
55 *
56 * @param[in] ptr Optional parameter for func()
57 *
58 * @param[in] timeout Seconds to first timeout.
59 *
60 * @retval SE_E_UNSPECIFIED Unspecified system error
61 * @retval SE_E_FORKFAILED Fork failure (not applicable for _WIN32 targets)
62 * @retval SE_E_WAITPIDFAILED waitpid errors
63 * @retval SE_E_EXECTIMEOUT exec timeout
64 * @retval 0 <= Return value from subprocess
65 * @retval SE_E_NOTFOUND The program coudln't be found
66 * @retval 128- The signal that killed the subprocess +128.
67 */
68 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
69 wait_for_process_timed(pid_t pid, time_t (*func)(void *),
70 void *ptr, time_t timeout)
71 {
72 HANDLE hProcess;
73 DWORD wrv = 0;
74 DWORD dtimeout;
75 int rv = 0;
76
77 hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);
78
79 if (hProcess == NULL) {
80 return SE_E_WAITPIDFAILED;
81 }
82
83 dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
84
85 do {
86 wrv = WaitForSingleObject(hProcess, dtimeout);
87
88 if (wrv == WAIT_OBJECT_0) {
89
90 DWORD prv = 0;
91
92 GetExitCodeProcess(hProcess, &prv);
93 rv = (int) prv;
94 break;
95
96 } else if (wrv == WAIT_TIMEOUT) {
97
98 if (func == NULL)
99 continue;
100
101 timeout = (*func)(ptr);
102
103 if (timeout == (time_t)-1) {
104
105 if (TerminateProcess(hProcess, 128 + 9)) {
106 dtimeout = INFINITE;
107 continue;
108 }
109 rv = SE_E_UNSPECIFIED;
110 break;
111
112 } else if (timeout == (time_t) -2) {
113
114 rv = SE_E_EXECTIMEOUT;
115 break;
116
117 } else {
118
119 dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
120 continue;
121
122 }
123
124 } else {
125
126 rv = SE_E_UNSPECIFIED;
127 break;
128
129 }
130
131 } while(TRUE);
132
133 CloseHandle(hProcess);
134
135 return rv;
136 }
137
138 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
139 wait_for_process(pid_t pid)
140 {
141 return wait_for_process_timed(pid, NULL, NULL, 0);
142 }
143
144 static char *
145 collect_commandline(const char * fn, va_list * ap)
146 {
147 size_t len = 0;
148 size_t alloc_len = 0;
149 const char * s;
150 char * cmd = NULL;
151
152 for (s = fn; s; s = (char *) va_arg(*ap, char *)) {
153 size_t cmp_len;
154 int need_quote = FALSE;
155
156 if (FAILED(StringCchLength(s, MAX_PATH, &cmp_len))) {
157 if (cmd)
158 free(cmd);
159 return NULL;
160 }
161
162 if (cmp_len == 0)
163 continue;
164
165 if (strchr(s, ' ') && /* need to quote any component that
166 has embedded spaces, but not if
167 they are already quoted. */
168 s[0] != '"' &&
169 s[cmp_len - 1] != '"') {
170 need_quote = TRUE;
171 cmp_len += 2 * sizeof(char);
172 }
173
174 if (s != fn)
175 cmp_len += 1 * sizeof(char);
176
177 if (alloc_len < len + cmp_len + 1) {
178 char * nc;
179
180 alloc_len += ((len + cmp_len - alloc_len) / MAX_PATH + 1) * MAX_PATH;
181 nc = (char *) realloc(cmd, alloc_len * sizeof(char));
182 if (nc == NULL) {
183 if (cmd)
184 free(cmd);
185 return NULL;
186 }
187 cmd = nc;
188 }
189
190 if (cmd == NULL)
191 return NULL;
192
193 if (s != fn)
194 cmd[len++] = ' ';
195
196 if (need_quote) {
197 StringCchPrintf(cmd + len, alloc_len - len, "\"%s\"", s);
198 } else {
199 StringCchCopy(cmd + len, alloc_len - len, s);
200 }
201
202 len += cmp_len;
203 }
204
205 return cmd;
206 }
207
208 ROKEN_LIB_FUNCTION pid_t ROKEN_LIB_CALL
209 pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
210 const char *file, ...)
211 {
212 HANDLE hOut_r = NULL;
213 HANDLE hOut_w = NULL;
214 HANDLE hIn_r = NULL;
215 HANDLE hIn_w = NULL;
216 HANDLE hErr_r = NULL;
217 HANDLE hErr_w = NULL;
218
219 SECURITY_ATTRIBUTES sa;
220 STARTUPINFO si;
221 PROCESS_INFORMATION pi;
222
223 char * commandline = NULL;
224
225 pid_t rv = (pid_t) -1;
226
227 {
228 va_list ap;
229
230 va_start(ap, file);
231 commandline = collect_commandline(file, &ap);
232
233 if (commandline == NULL)
234 return rv;
235 }
236
237 ZeroMemory(&si, sizeof(si));
238 ZeroMemory(&pi, sizeof(pi));
239 ZeroMemory(&sa, sizeof(sa));
240
241 pi.hProcess = NULL;
242 pi.hThread = NULL;
243
244 sa.nLength = sizeof(sa);
245 sa.bInheritHandle = TRUE;
246 sa.lpSecurityDescriptor = NULL;
247
248 if ((stdout_fd && !CreatePipe(&hOut_r, &hOut_w, &sa, 0 /* Use default */)) ||
249
250 (stdin_fd && !CreatePipe(&hIn_r, &hIn_w, &sa, 0)) ||
251
252 (stderr_fd && !CreatePipe(&hErr_r, &hErr_w, &sa, 0)) ||
253
254 (!stdout_fd && (hOut_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
255 &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||
256
257 (!stdin_fd && (hIn_r = CreateFile("CON",GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
258 &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||
259
260 (!stderr_fd && (hErr_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
261 &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE))
262
263 goto _exit;
264
265 /* We don't want the child processes inheriting these */
266 if (hOut_r)
267 SetHandleInformation(hOut_r, HANDLE_FLAG_INHERIT, FALSE);
268
269 if (hIn_w)
270 SetHandleInformation(hIn_w, HANDLE_FLAG_INHERIT, FALSE);
271
272 if (hErr_r)
273 SetHandleInformation(hErr_r, HANDLE_FLAG_INHERIT, FALSE);
274
275 si.cb = sizeof(si);
276 si.lpReserved = NULL;
277 si.lpDesktop = NULL;
278 si.lpTitle = NULL;
279 si.dwFlags = STARTF_USESTDHANDLES;
280 si.hStdInput = hIn_r;
281 si.hStdOutput = hOut_w;
282 si.hStdError = hErr_w;
283
284 if (!CreateProcess(file, commandline, NULL, NULL,
285 TRUE, /* bInheritHandles */
286 CREATE_NO_WINDOW, /* dwCreationFlags */
287 NULL, /* lpEnvironment */
288 NULL, /* lpCurrentDirectory */
289 &si,
290 &pi)) {
291
292 rv = (pid_t) (GetLastError() == ERROR_FILE_NOT_FOUND)? 127 : -1;
293 goto _exit;
294 }
295
296 if (stdin_fd) {
297 *stdin_fd = _fdopen(_open_osfhandle((intptr_t) hIn_w, 0), "wb");
298 if (*stdin_fd)
299 hIn_w = NULL;
300 }
301
302 if (stdout_fd) {
303 *stdout_fd = _fdopen(_open_osfhandle((intptr_t) hOut_r, _O_RDONLY), "rb");
304 if (*stdout_fd)
305 hOut_r = NULL;
306 }
307
308 if (stderr_fd) {
309 *stderr_fd = _fdopen(_open_osfhandle((intptr_t) hErr_r, _O_RDONLY), "rb");
310 if (*stderr_fd)
311 hErr_r = NULL;
312 }
313
314 rv = (pid_t) pi.dwProcessId;
315
316 _exit:
317
318 if (pi.hProcess) CloseHandle(pi.hProcess);
319
320 if (pi.hThread) CloseHandle(pi.hThread);
321
322 if (hIn_r) CloseHandle(hIn_r);
323
324 if (hIn_w) CloseHandle(hIn_w);
325
326 if (hOut_r) CloseHandle(hOut_r);
327
328 if (hOut_w) CloseHandle(hOut_w);
329
330 if (hErr_r) CloseHandle(hErr_r);
331
332 if (hErr_w) CloseHandle(hErr_w);
333
334 return rv;
335 }
336
337
338 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
339 simple_execvp_timed(const char *file, char *const args[],
340 time_t (*func)(void *), void *ptr, time_t timeout)
341 {
342 intptr_t hp;
343 int rv;
344
345 hp = spawnvp(_P_NOWAIT, file, args);
346
347 if (hp == -1)
348 return (errno == ENOENT)? 127: 126;
349 else if (hp == 0)
350 return 0;
351
352 rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);
353
354 CloseHandle((HANDLE) hp);
355
356 return rv;
357 }
358
359
360 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
361 simple_execvp(const char *file, char *const args[])
362 {
363 return simple_execvp_timed(file, args, NULL, NULL, 0);
364 }
365
366
367 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
368 simple_execve_timed(const char *file, char *const args[], char *const envp[],
369 time_t (*func)(void *), void *ptr, time_t timeout)
370 {
371 intptr_t hp;
372 int rv;
373
374 hp = spawnve(_P_NOWAIT, file, args, envp);
375
376 if (hp == -1)
377 return (errno == ENOENT)? 127: 126;
378 else if (hp == 0)
379 return 0;
380
381 rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);
382
383 CloseHandle((HANDLE) hp);
384
385 return rv;
386 }
387
388
389 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
390 simple_execve(const char *file, char *const args[], char *const envp[])
391 {
392 return simple_execve_timed(file, args, envp, NULL, NULL, 0);
393 }
394
395
396 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
397 simple_execlp(const char *file, ...)
398 {
399 va_list ap;
400 char **argv;
401 int ret;
402
403 va_start(ap, file);
404 argv = vstrcollect(&ap);
405 va_end(ap);
406 if(argv == NULL)
407 return SE_E_UNSPECIFIED;
408 ret = simple_execvp(file, argv);
409 free(argv);
410 return ret;
411 }
412
413
414 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
415 simple_execle(const char *file, ... /* ,char *const envp[] */)
416 {
417 va_list ap;
418 char **argv;
419 char *const* envp;
420 int ret;
421
422 va_start(ap, file);
423 argv = vstrcollect(&ap);
424 envp = va_arg(ap, char **);
425 va_end(ap);
426 if(argv == NULL)
427 return SE_E_UNSPECIFIED;
428 ret = simple_execve(file, argv, envp);
429 free(argv);
430 return ret;
431 }