summaryrefslogtreecommitdiff
path: root/src/util/win32/process.c
blob: d29969dd3d1f15f2db6318743954eea5d4897c5a (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <stdio.h>
#include <git2.h>

#include "git2_util.h"
#include "process.h"
#include "git2/strarray.h"

#define ENV_MAX 32767

struct git_process {
	wchar_t *appname;
	wchar_t *cmdline;
	wchar_t *env;

	wchar_t *cwd;

	unsigned int capture_in  : 1,
	             capture_out : 1,
	             capture_err : 1;

	PROCESS_INFORMATION process_info;

	HANDLE child_in;
	HANDLE child_out;
	HANDLE child_err;

	git_process_result_status status;
};

/*
 * Windows processes have a single command-line that is split by the
 * invoked application into arguments (instead of an array of
 * command-line arguments).  This command-line is split by space or
 * tab delimiters, unless that whitespace is within a double quote.
 * Literal double-quotes themselves can be escaped by a backslash,
 * but only when not within double quotes.  Literal backslashes can
 * be escaped by a backslash.
 *
 * Effectively, this means that instead of thinking about quoting
 * individual strings, think about double quotes as an escaping
 * mechanism for whitespace.
 *
 * In other words (using ` as a string boundary):
 * [ `foo`, `bar` ] => `foo bar`
 * [ `foo bar` ] => `foo" "bar`
 * [ `foo bar`, `foo bar` ] => `foo" "bar foo" "bar`
 * [ `foo "bar" foo` ] => `foo" "\"bar\"" "foo`
 */
int git_process__cmdline(
	git_str *out,
	const char **in,
	size_t in_len)
{
	bool quoted = false;
	const char *c;
	size_t i;

	for (i = 0; i < in_len; i++) {
		/* Arguments are delimited by an unquoted space */
		if (i)
			git_str_putc(out, ' ');

		for (c = in[i]; *c; c++) {
			/* Start or stop quoting spaces within an argument */
			if ((*c == ' ' || *c == '\t') && !quoted) {
				git_str_putc(out, '"');
				quoted = true;
			} else if (*c != ' ' && *c != '\t' && quoted) {
				git_str_putc(out, '"');
				quoted = false;
			}

			/* Escape double-quotes and backslashes */
			if (*c == '"' || *c == '\\')
				git_str_putc(out, '\\');

			git_str_putc(out, *c);
		}
	}

	return git_str_oom(out) ? -1 : 0;
}

GIT_INLINE(bool) is_delete_env(const char *env)
{
	char *c = strchr(env, '=');

	if (c == NULL)
		return false;

	return *(c+1) == '\0';
}

static int merge_env(wchar_t **out, const char **in, size_t in_len, bool exclude_env)
{
	git_str merged = GIT_BUF_INIT;
	wchar_t *in16 = NULL, *env = NULL, *e;
	char *e8 = NULL;
	size_t e_len;
	int ret = 0;
	size_t i;

	*out = NULL;

	in16 = git__malloc(ENV_MAX * sizeof(wchar_t));
	GIT_ERROR_CHECK_ALLOC(in16);

	e8 = git__malloc(ENV_MAX);
	GIT_ERROR_CHECK_ALLOC(e8);

	for (i = 0; in && i < in_len; i++) {
		if (is_delete_env(in[i]))
			continue;

		if ((ret = git_utf8_to_16(in16, ENV_MAX, in[i])) < 0)
			goto done;

		git_str_put(&merged, (const char *)in16, ret * 2);
		git_str_put(&merged, "\0\0", 2);
	}

	if (!exclude_env) {
		env = GetEnvironmentStringsW();

		for (e = env; *e; e += (e_len + 1)) {
			e_len = wcslen(e);

			if ((ret = git_utf8_from_16(e8, ENV_MAX, e)) < 0)
				goto done;

			if (git_strlist_contains_key(in, in_len, e8, '='))
				continue;

			git_str_put(&merged, (const char *)e, e_len * 2);
			git_str_put(&merged, "\0\0", 2);
		}
	}

	git_str_put(&merged, "\0\0", 2);

	*out = (wchar_t *)git_str_detach(&merged);

done:
	if (env)
		FreeEnvironmentStringsW(env);

	git_str_dispose(&merged);
	git__free(e8);
	git__free(in16);

	return ret < 0 ? -1 : 0;
}

int git_process_new(
	git_process **out,
	const char **args,
	size_t args_len,
	const char **env,
	size_t env_len,
	git_process_options *opts)
{
	git_process *process;
	git_str cmdline = GIT_BUF_INIT;
	int error;

	GIT_ASSERT_ARG(out && args && args_len > 0);

	*out = NULL;

	process = git__calloc(1, sizeof(git_process));
	GIT_ERROR_CHECK_ALLOC(process);

	if ((error = git_process__cmdline(&cmdline, args, args_len)) < 0)
		goto done;

	if (git_utf8_to_16_alloc(&process->appname, args[0]) < 0 ||
	    git_utf8_to_16_alloc(&process->cmdline, cmdline.ptr) < 0) {
		error = -1;
		goto done;
	}

	if (opts && opts->cwd &&
	    git_utf8_to_16_alloc(&process->cwd, opts->cwd) < 0) {
		error = -1;
		goto done;
	}

	if (env && (error = merge_env(&process->env, env, env_len, opts && opts->exclude_env) < 0))
		goto done;

	if (opts) {
		process->capture_in = opts->capture_in;
		process->capture_out = opts->capture_out;
		process->capture_err = opts->capture_err;
	}

done:
	if (error)
		git_process_free(process);
	else
		*out = process;

	git_str_dispose(&cmdline);
	return error;
}

#define CLOSE_HANDLE(h) do { if ((h) != NULL) CloseHandle(h); } while(0)

int git_process_start(git_process *process)
{
	STARTUPINFOW startup_info;
	SECURITY_ATTRIBUTES security_attrs;
	DWORD flags = CREATE_UNICODE_ENVIRONMENT;
	HANDLE in[2]  = { NULL, NULL },
	       out[2] = { NULL, NULL },
	       err[2] = { NULL, NULL };

	memset(&security_attrs, 0, sizeof(SECURITY_ATTRIBUTES));
	security_attrs.bInheritHandle = TRUE;

	memset(&startup_info, 0, sizeof(STARTUPINFOW));
	startup_info.cb = sizeof(STARTUPINFOW);
	startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
	startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);

	if (process->capture_in) {
		if (!CreatePipe(&in[0], &in[1], &security_attrs, 0) ||
		    !SetHandleInformation(in[1], HANDLE_FLAG_INHERIT, 0)) {
			git_error_set(GIT_ERROR_OS, "could not create pipe");
			goto on_error;
		}

		startup_info.hStdInput = in[0];
		startup_info.dwFlags |= STARTF_USESTDHANDLES;
	}

	if (process->capture_out) {
		if (!CreatePipe(&out[0], &out[1], &security_attrs, 0) ||
		    !SetHandleInformation(out[0], HANDLE_FLAG_INHERIT, 0)) {
			git_error_set(GIT_ERROR_OS, "could not create pipe");
			goto on_error;
		}

		startup_info.hStdOutput = out[1];
		startup_info.dwFlags |= STARTF_USESTDHANDLES;
	}

	if (process->capture_err) {
		if (!CreatePipe(&err[0], &err[1], &security_attrs, 0) ||
		    !SetHandleInformation(err[0], HANDLE_FLAG_INHERIT, 0)) {
			git_error_set(GIT_ERROR_OS, "could not create pipe");
			goto on_error;
		}

		startup_info.hStdError = err[1];
		startup_info.dwFlags |= STARTF_USESTDHANDLES;
	}

	memset(&process->process_info, 0, sizeof(PROCESS_INFORMATION));

	if (!CreateProcessW(process->appname, process->cmdline,
	                    NULL, NULL, TRUE, flags, process->env,
	                    process->cwd,
	                    &startup_info,
	                    &process->process_info)) {
		git_error_set(GIT_ERROR_OS, "could not create process");
		goto on_error;
	}

	CLOSE_HANDLE(in[0]);  process->child_in  = in[1];
	CLOSE_HANDLE(out[1]); process->child_out = out[0];
	CLOSE_HANDLE(err[1]); process->child_err = err[0];

	return 0;

on_error:
	CLOSE_HANDLE(in[0]);  CLOSE_HANDLE(in[1]);
	CLOSE_HANDLE(out[0]); CLOSE_HANDLE(out[1]);
	CLOSE_HANDLE(err[0]); CLOSE_HANDLE(err[1]);
	return -1;
}

int git_process_id(p_pid_t *out, git_process *process)
{
	GIT_ASSERT(out && process);

	if (!process->process_info.dwProcessId) {
		git_error_set(GIT_ERROR_INVALID, "process not running");
		return -1;
	}

	*out = process->process_info.dwProcessId;
	return 0;
}

ssize_t git_process_read(git_process *process, void *buf, size_t count)
{
	DWORD ret;

	if (count > DWORD_MAX)
		count = DWORD_MAX;
	if (count > SSIZE_MAX)
		count = SSIZE_MAX;

	if (!ReadFile(process->child_out, buf, (DWORD)count, &ret, NULL)) {
		if (GetLastError() == ERROR_BROKEN_PIPE)
			return 0;

		git_error_set(GIT_ERROR_OS, "could not read");
		return -1;
	}

	return ret;
}

ssize_t git_process_write(git_process *process, const void *buf, size_t count)
{
	DWORD ret;

	if (count > DWORD_MAX)
		count = DWORD_MAX;
	if (count > SSIZE_MAX)
		count = SSIZE_MAX;

	if (!WriteFile(process->child_in, buf, (DWORD)count, &ret, NULL)) {
		git_error_set(GIT_ERROR_OS, "could not write");
		return -1;
	}

	return ret;
}

int git_process_close_in(git_process *process)
{
	if (!process->capture_in) {
		git_error_set(GIT_ERROR_INVALID, "input is not open");
		return -1;
	}

	if (process->child_in) {
		CloseHandle(process->child_in);
		process->child_in = NULL;
	}

	return 0;
}

int git_process_close_out(git_process *process)
{
	if (!process->capture_out) {
		git_error_set(GIT_ERROR_INVALID, "output is not open");
		return -1;
	}

	if (process->child_out) {
		CloseHandle(process->child_out);
		process->child_out = NULL;
	}

	return 0;
}

int git_process_close_err(git_process *process)
{
	if (!process->capture_err) {
		git_error_set(GIT_ERROR_INVALID, "error is not open");
		return -1;
	}

	if (process->child_err) {
		CloseHandle(process->child_err);
		process->child_err = NULL;
	}

	return 0;
}

int git_process_close(git_process *process)
{
	if (process->child_in) {
		CloseHandle(process->child_in);
		process->child_in = NULL;
	}

	if (process->child_out) {
		CloseHandle(process->child_out);
		process->child_out = NULL;
	}

	if (process->child_err) {
		CloseHandle(process->child_err);
		process->child_err = NULL;
	}

	CloseHandle(process->process_info.hProcess);
	process->process_info.hProcess = NULL;

	CloseHandle(process->process_info.hThread);
	process->process_info.hThread = NULL;

	return 0;
}

int git_process_wait(git_process_result *result, git_process *process)
{
	DWORD exitcode;

	if (result)
		memset(result, 0, sizeof(git_process_result));

	if (!process->process_info.dwProcessId) {
		git_error_set(GIT_ERROR_INVALID, "process is stopped");
		return -1;
	}

	if (WaitForSingleObject(process->process_info.hProcess, INFINITE) == WAIT_FAILED) {
		git_error_set(GIT_ERROR_OS, "could not wait for process");
		return -1;
	}

	if (!GetExitCodeProcess(process->process_info.hProcess, &exitcode)) {
		git_error_set(GIT_ERROR_OS, "could not get process exit code");
		return -1;
	}

	result->status = GIT_PROCESS_STATUS_NORMAL;
	result->exitcode = exitcode;

	memset(&process->process_info, 0, sizeof(PROCESS_INFORMATION));
	return 0;
}

int git_process_result_msg(git_str *out, git_process_result *result)
{
	if (result->status == GIT_PROCESS_STATUS_NONE) {
		return git_str_puts(out, "process not started");
	} else if (result->status == GIT_PROCESS_STATUS_NORMAL) {
		return git_str_printf(out, "process exited with code %d",
		                      result->exitcode);
	} else if (result->signal) {
		return git_str_printf(out, "process exited on signal %d",
		                      result->signal);
	}

	return git_str_puts(out, "unknown error");
}

void git_process_free(git_process *process)
{
	if (!process)
		return;

	if (process->process_info.hProcess)
		git_process_close(process);

	git__free(process->env);
	git__free(process->cwd);
	git__free(process->cmdline);
	git__free(process->appname);
	git__free(process);
}