summaryrefslogtreecommitdiff
path: root/third_party/heimdal/lib/roken/simple_exec_w32.c
blob: 264cdb2e9d2425b993b9e15fd7a19b7f93182390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/***********************************************************************
 * Copyright (c) 2009, Secure Endpoints Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **********************************************************************/

#include <config.h>

#include <roken.h>
#include <strsafe.h>

#ifndef _WIN32
#error This implementation is Windows specific.
#endif

/**
 * wait_for_process_timed waits for a process to terminate or until a
 * specified timeout occurs.
 *
 * @param[in] pid Process id for the monitored process

 * @param[in] func Timeout callback function.  When the wait times out,
 *     the callback function is called.  The possible return values
 *     from the callback function are:
 *
 * - ((time_t) -2) Exit loop without killing child and return SE_E_EXECTIMEOUT.
 * - ((time_t) -1) Kill child with SIGTERM and wait for child to exit.
 * - 0             Don't timeout again
 * - n             Seconds to next timeout
 *
 * @param[in] ptr Optional parameter for func()
 *
 * @param[in] timeout Seconds to first timeout.
 *
 * @retval SE_E_UNSPECIFIED   Unspecified system error
 * @retval SE_E_FORKFAILED    Fork failure (not applicable for _WIN32 targets)
 * @retval SE_E_WAITPIDFAILED waitpid errors
 * @retval SE_E_EXECTIMEOUT   exec timeout
 * @retval 0 <= Return value  from subprocess
 * @retval SE_E_NOTFOUND      The program coudln't be found
 * @retval 128- The signal that killed the subprocess +128.
 */
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process_timed(pid_t pid, time_t (*func)(void *),
		       void *ptr, time_t timeout)
{
    HANDLE hProcess;
    DWORD wrv = 0;
    DWORD dtimeout;
    int rv = 0;

    hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);

    if (hProcess == NULL) {
        return SE_E_WAITPIDFAILED;
    }

    dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);

    do {
	wrv = WaitForSingleObject(hProcess, dtimeout);

	if (wrv == WAIT_OBJECT_0) {

	    DWORD prv = 0;

	    GetExitCodeProcess(hProcess, &prv);
	    rv = (int) prv;
	    break;

	} else if (wrv == WAIT_TIMEOUT) {

	    if (func == NULL)
		continue;

	    timeout = (*func)(ptr);

	    if (timeout == (time_t)-1) {

		if (TerminateProcess(hProcess, 128 + 9)) {
		    dtimeout = INFINITE;
		    continue;
		}
		rv = SE_E_UNSPECIFIED;
		break;

	    } else if (timeout == (time_t) -2) {

		rv = SE_E_EXECTIMEOUT;
		break;

	    } else {

		dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
		continue;

	    }

	} else {

	    rv = SE_E_UNSPECIFIED;
	    break;

	}

    } while(TRUE);

    CloseHandle(hProcess);

    return rv;
}

ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process(pid_t pid)
{
    return wait_for_process_timed(pid, NULL, NULL, 0);
}

static char *
collect_commandline(const char * fn, va_list * ap)
{
    size_t len = 0;
    size_t alloc_len = 0;
    const char * s;
    char * cmd = NULL;

    for (s = fn; s; s = (char *) va_arg(*ap, char *)) {
	size_t cmp_len;
	int need_quote = FALSE;

	if (FAILED(StringCchLength(s, MAX_PATH, &cmp_len))) {
	    if (cmd)
		free(cmd);
	    return NULL;
	}

	if (cmp_len == 0)
	    continue;

	if (strchr(s, ' ') &&	/* need to quote any component that
				   has embedded spaces, but not if
				   they are already quoted. */
	    s[0] != '"' &&
	    s[cmp_len - 1] != '"') {
	    need_quote = TRUE;
	    cmp_len += 2 * sizeof(char);
	}

	if (s != fn)
	    cmp_len += 1 * sizeof(char);

	if (alloc_len < len + cmp_len + 1) {
	    char * nc;

	    alloc_len += ((len + cmp_len - alloc_len) / MAX_PATH + 1) * MAX_PATH;
	    nc = (char *) realloc(cmd, alloc_len * sizeof(char));
	    if (nc == NULL) {
		if (cmd)
		    free(cmd);
		return NULL;
	    }
	    cmd = nc;
	}

	if (cmd == NULL)
	    return NULL;

	if (s != fn)
	    cmd[len++] = ' ';

	if (need_quote) {
	    StringCchPrintf(cmd + len, alloc_len - len, "\"%s\"", s);
	} else {
	    StringCchCopy(cmd + len, alloc_len - len, s);
	}

	len += cmp_len;
    }

    return cmd;
}

ROKEN_LIB_FUNCTION pid_t ROKEN_LIB_CALL
pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
	   const char *file, ...)
{
    HANDLE  hOut_r = NULL;
    HANDLE  hOut_w = NULL;
    HANDLE  hIn_r  = NULL;
    HANDLE  hIn_w  = NULL;
    HANDLE  hErr_r = NULL;
    HANDLE  hErr_w = NULL;

    SECURITY_ATTRIBUTES sa;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    char * commandline = NULL;

    pid_t rv = (pid_t) -1;

    {
	va_list ap;

	va_start(ap, file);
	commandline = collect_commandline(file, &ap);

	if (commandline == NULL)
	    return rv;
    }

    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));
    ZeroMemory(&sa, sizeof(sa));

    pi.hProcess = NULL;
    pi.hThread = NULL;

    sa.nLength = sizeof(sa);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    if ((stdout_fd && !CreatePipe(&hOut_r, &hOut_w, &sa, 0 /* Use default */)) ||

	(stdin_fd && !CreatePipe(&hIn_r, &hIn_w, &sa, 0)) ||

	(stderr_fd && !CreatePipe(&hErr_r, &hErr_w, &sa, 0)) ||

	(!stdout_fd && (hOut_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
					    &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||

	(!stdin_fd && (hIn_r = CreateFile("CON",GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
					  &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||

	(!stderr_fd && (hErr_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
					    &sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE))

	goto _exit;

    /* We don't want the child processes inheriting these */
    if (hOut_r)
	SetHandleInformation(hOut_r, HANDLE_FLAG_INHERIT, FALSE);

    if (hIn_w)
	SetHandleInformation(hIn_w, HANDLE_FLAG_INHERIT, FALSE);

    if (hErr_r)
	SetHandleInformation(hErr_r, HANDLE_FLAG_INHERIT, FALSE);

    si.cb = sizeof(si);
    si.lpReserved = NULL;
    si.lpDesktop = NULL;
    si.lpTitle = NULL;
    si.dwFlags = STARTF_USESTDHANDLES;
    si.hStdInput = hIn_r;
    si.hStdOutput = hOut_w;
    si.hStdError = hErr_w;

    if (!CreateProcess(file, commandline, NULL, NULL,
		       TRUE,	/* bInheritHandles */
		       CREATE_NO_WINDOW, /* dwCreationFlags */
		       NULL,		 /* lpEnvironment */
		       NULL,		 /* lpCurrentDirectory */
		       &si,
		       &pi)) {

	rv = (pid_t) (GetLastError() == ERROR_FILE_NOT_FOUND)? 127 : -1;
	goto _exit;
    }

    if (stdin_fd) {
	*stdin_fd = _fdopen(_open_osfhandle((intptr_t) hIn_w, 0), "wb");
	if (*stdin_fd)
	    hIn_w = NULL;
    }

    if (stdout_fd) {
	*stdout_fd = _fdopen(_open_osfhandle((intptr_t) hOut_r, _O_RDONLY), "rb");
	if (*stdout_fd)
	    hOut_r = NULL;
    }

    if (stderr_fd) {
	*stderr_fd = _fdopen(_open_osfhandle((intptr_t) hErr_r, _O_RDONLY), "rb");
	if (*stderr_fd)
	    hErr_r = NULL;
    }

    rv = (pid_t) pi.dwProcessId;

 _exit:

    if (pi.hProcess) CloseHandle(pi.hProcess);

    if (pi.hThread) CloseHandle(pi.hThread);

    if (hIn_r) CloseHandle(hIn_r);

    if (hIn_w) CloseHandle(hIn_w);

    if (hOut_r) CloseHandle(hOut_r);

    if (hOut_w) CloseHandle(hOut_w);

    if (hErr_r) CloseHandle(hErr_r);

    if (hErr_w) CloseHandle(hErr_w);

    return rv;
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp_timed(const char *file, char *const args[],
		    time_t (*func)(void *), void *ptr, time_t timeout)
{
    intptr_t hp;
    int rv;

    hp = spawnvp(_P_NOWAIT, file, args);

    if (hp == -1)
	return (errno == ENOENT)? 127: 126;
    else if (hp == 0)
	return 0;

    rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);

    CloseHandle((HANDLE) hp);

    return rv;
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp(const char *file, char *const args[])
{
    return simple_execvp_timed(file, args, NULL, NULL, 0);
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve_timed(const char *file, char *const args[], char *const envp[],
		    time_t (*func)(void *), void *ptr, time_t timeout)
{
    intptr_t hp;
    int rv;

    hp = spawnve(_P_NOWAIT, file, args, envp);

    if (hp == -1)
	return (errno == ENOENT)? 127: 126;
    else if (hp == 0)
	return 0;

    rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);

    CloseHandle((HANDLE) hp);

    return rv;
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve(const char *file, char *const args[], char *const envp[])
{
    return simple_execve_timed(file, args, envp, NULL, NULL, 0);
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execlp(const char *file, ...)
{
    va_list ap;
    char **argv;
    int ret;

    va_start(ap, file);
    argv = vstrcollect(&ap);
    va_end(ap);
    if(argv == NULL)
	return SE_E_UNSPECIFIED;
    ret = simple_execvp(file, argv);
    free(argv);
    return ret;
}


ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execle(const char *file, ... /* ,char *const envp[] */)
{
    va_list ap;
    char **argv;
    char *const* envp;
    int ret;

    va_start(ap, file);
    argv = vstrcollect(&ap);
    envp = va_arg(ap, char **);
    va_end(ap);
    if(argv == NULL)
	return SE_E_UNSPECIFIED;
    ret = simple_execve(file, argv, envp);
    free(argv);
    return ret;
}