forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_win32.c
412 lines (371 loc) · 12.5 KB
/
run_win32.c
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Sebastien Hinderer, projet Gallium, INRIA Paris */
/* */
/* Copyright 2016 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* Run programs with rediretions and timeouts under Windows */
#include <stdio.h>
#include <stdlib.h>
#include <wtypes.h>
#include <winbase.h>
#include <windows.h>
#include <process.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include "caml/osdeps.h"
#include "run.h"
#include "run_common.h"
static void report_error(
const char *file, int line,
const command_settings *settings,
const char *message, const WCHAR *argument)
{
WCHAR windows_error_message[1024];
DWORD error = GetLastError();
char *caml_error_message, buf[256];
if (FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, 0, windows_error_message,
sizeof(windows_error_message)/sizeof(WCHAR), NULL) ) {
caml_error_message = caml_stat_strdup_of_utf16(windows_error_message);
} else {
caml_error_message = caml_stat_alloc(256);
sprintf(caml_error_message, "unknown Windows error #%lu", error);
}
if ( is_defined(argument) )
error_with_location(file, line,
settings, "%s %s: %s", message, argument, caml_error_message);
else
error_with_location(file, line,
settings, "%s: %s", message, caml_error_message);
caml_stat_free(caml_error_message);
}
static WCHAR *find_program(const WCHAR *program_name)
{
int max_path_length = 512;
DWORD result;
LPCWSTR searchpath = NULL, extension = L".exe";
WCHAR **filepart = NULL;
WCHAR *fullpath = malloc(max_path_length*sizeof(WCHAR));
if (fullpath == NULL) return NULL;
result = SearchPath
(
searchpath,
program_name,
extension,
max_path_length,
fullpath,
filepart
);
if (result == 0)
{
/* It may be an absolute path, return a copy of it */
int l = wcslen(program_name) + 1;
free(fullpath);
fullpath = malloc(l*sizeof(WCHAR));
if (fullpath != NULL) wcscpy(fullpath, program_name);
return fullpath;
}
if (result <= max_path_length) return fullpath;
/* fullpath was too small, allocate a bigger one */
free(fullpath);
result++; /* Take '\0' into account */
fullpath = malloc(result*sizeof(WCHAR));
if (fullpath == NULL) return NULL;
SearchPath
(
searchpath,
program_name,
extension,
result,
fullpath,
filepart
);
return fullpath;
}
static WCHAR *commandline_of_arguments(WCHAR **arguments)
{
WCHAR *commandline = NULL, **arguments_p, *commandline_p;
int args = 0; /* Number of arguments */
int commandline_length = 0;
if (*arguments == NULL) return NULL;
/* From here we know there is at least one argument */
/* First compute number of arguments and commandline length */
for (arguments_p = arguments; *arguments_p != NULL; arguments_p++)
{
args++;
commandline_length += wcslen(*arguments_p);
}
commandline_length += args; /* args-1 ' ' between arguments + final '\0' */
/* Allocate memory and accumulate arguments separated by spaces */
commandline = malloc(commandline_length*sizeof(WCHAR));
if (commandline == NULL) return NULL;
commandline_p = commandline;
for (arguments_p = arguments; *arguments_p!=NULL; arguments_p++)
{
int l = wcslen(*arguments_p);
memcpy(commandline_p, *arguments_p, l*sizeof(WCHAR));
commandline_p += l;
*commandline_p = L' ';
commandline_p++;
}
commandline[commandline_length-1] = 0;
return commandline;
}
static LPVOID prepare_environment(WCHAR **localenv)
{
LPTCH p, r, env, process_env = NULL;
WCHAR **q;
int l, process_env_length, localenv_length, env_length;
if (localenv == NULL) return NULL;
process_env = GetEnvironmentStrings();
if (process_env == NULL) return NULL;
/* Compute length of process environment */
process_env_length = 0;
p = process_env;
while (*p != L'\0') {
l = wcslen(p) + 1; /* also count terminating '\0' */
process_env_length += l;
p += l;
}
/* Compute length of local environment */
localenv_length = 0;
q = localenv;
while (*q != NULL) {
localenv_length += wcslen(*q) + 1;
q++;
}
/* Build new env that contains both process and local env */
env_length = process_env_length + localenv_length + 1;
env = malloc(env_length * sizeof(WCHAR));
if (env == NULL) {
FreeEnvironmentStrings(process_env);
return NULL;
}
r = env;
p = process_env;
while (*p != L'\0') {
l = wcslen(p) + 1; /* also count terminating '\0' */
memcpy(r, p, l * sizeof(WCHAR));
p += l;
r += l;
}
FreeEnvironmentStrings(process_env);
q = localenv;
while (*q != NULL) {
l = wcslen(*q) + 1;
memcpy(r, *q, l * sizeof(WCHAR));
r += l;
q++;
}
*r = L'\0';
return env;
}
static SECURITY_ATTRIBUTES security_attributes = {
sizeof(SECURITY_ATTRIBUTES), /* nLength */
NULL, /* lpSecurityDescriptor */
TRUE /* bInheritHandle */
};
static HANDLE create_input_handle(const WCHAR *filename)
{
return CreateFile
(
filename,
GENERIC_READ, /* DWORD desired_access */
FILE_SHARE_READ, /* DWORD share_mode */
&security_attributes,
OPEN_EXISTING, /* DWORD creation_disposition */
FILE_ATTRIBUTE_NORMAL, /* DWORD flags_and_attributes */
NULL /* HANDLE template_file */
);
}
static HANDLE create_output_handle(const WCHAR *filename, int append)
{
DWORD desired_access = append ? FILE_APPEND_DATA : GENERIC_WRITE;
DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
DWORD creation_disposition = append ? OPEN_ALWAYS : CREATE_ALWAYS;
return CreateFile
(
filename,
desired_access,
share_mode,
&security_attributes,
creation_disposition,
FILE_ATTRIBUTE_NORMAL, /* DWORD flags_and_attributes */
NULL /* HANDLE template_file */
);
}
#define checkerr(condition, message, argument) \
if ( (condition) ) \
{ \
report_error(__FILE__, __LINE__, settings, message, argument); \
status = -1; \
goto cleanup; \
} else { }
static WCHAR *translate_finename(WCHAR *filename)
{
if (!wcscmp(filename, L"/dev/null")) return L"NUL"; else return filename;
}
int run_command(const command_settings *settings)
{
BOOL process_created = FALSE;
int stdin_redirected = 0, stdout_redirected = 0, stderr_redirected = 0;
int combined = 0; /* 1 if stdout and stderr are redirected to the same file */
int wait_again = 0;
WCHAR *program = NULL;
WCHAR *commandline = NULL;
LPVOID environment = NULL;
LPCWSTR current_directory = NULL;
STARTUPINFO startup_info;
PROCESS_INFORMATION process_info;
BOOL wait_result;
DWORD status, stamp, cur;
DWORD timeout = (settings->timeout > 0) ? settings->timeout * 1000 : INFINITE;
JOBOBJECT_ASSOCIATE_COMPLETION_PORT port = {NULL, NULL};
HANDLE hJob = NULL;
DWORD completion_code;
ULONG_PTR completion_key;
LPOVERLAPPED pOverlapped;
ZeroMemory(&startup_info, sizeof(STARTUPINFO));
startup_info.cb = sizeof(STARTUPINFO);
startup_info.dwFlags = STARTF_USESTDHANDLES;
program = find_program(settings->program);
checkerr(
(program == NULL),
"Could not find program to execute",
settings->program
);
commandline = commandline_of_arguments(settings->argv);
environment = prepare_environment(settings->envp);
if (is_defined(settings->stdin_filename))
{
WCHAR *stdin_filename = translate_finename(settings->stdin_filename);
startup_info.hStdInput = create_input_handle(stdin_filename);
checkerr( (startup_info.hStdInput == INVALID_HANDLE_VALUE),
"Could not redirect standard input",
stdin_filename);
stdin_redirected = 1;
} else startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
if (is_defined(settings->stdout_filename))
{
WCHAR *stdout_filename = translate_finename(settings->stdout_filename);
startup_info.hStdOutput = create_output_handle(
stdout_filename, settings->append
);
checkerr( (startup_info.hStdOutput == INVALID_HANDLE_VALUE),
"Could not redirect standard output",
stdout_filename);
stdout_redirected = 1;
} else startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (is_defined(settings->stderr_filename))
{
if (stdout_redirected)
{
if (wcscmp(settings->stdout_filename, settings->stderr_filename) == 0)
{
startup_info.hStdError = startup_info.hStdOutput;
stderr_redirected = 1;
combined = 1;
}
}
if (! stderr_redirected)
{
WCHAR *stderr_filename = translate_finename(settings->stderr_filename);
startup_info.hStdError = create_output_handle
(
stderr_filename, settings->append
);
checkerr( (startup_info.hStdError == INVALID_HANDLE_VALUE),
"Could not redirect standard error",
stderr_filename);
stderr_redirected = 1;
}
} else startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
process_created = CreateProcess(
program,
commandline,
NULL, /* SECURITY_ATTRIBUTES process_attributes */
NULL, /* SECURITY_ATTRIBUTES thread_attributes */
TRUE, /* BOOL inherit_handles */
CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT, /* DWORD creation_flags */
environment,
NULL, /* LPCSTR current_directory */
&startup_info,
&process_info
);
checkerr( (! process_created), "CreateProcess failed", NULL);
hJob = CreateJobObject(NULL, NULL);
checkerr( (hJob == NULL), "CreateJobObject failed", NULL);
checkerr( !AssignProcessToJobObject(hJob, process_info.hProcess),
"AssignProcessToJob failed", NULL);
port.CompletionPort =
CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
checkerr( (port.CompletionPort == NULL),
"CreateIoCompletionPort failed", NULL);
checkerr( !SetInformationJobObject(
hJob,
JobObjectAssociateCompletionPortInformation,
&port, sizeof(port)), "SetInformationJobObject failed", NULL);
ResumeThread(process_info.hThread);
CloseHandle(process_info.hThread);
stamp = GetTickCount();
while ((wait_result = GetQueuedCompletionStatus(port.CompletionPort,
&completion_code,
&completion_key,
&pOverlapped,
timeout))
&& completion_code != JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
{
if (timeout != INFINITE)
{
cur = GetTickCount();
stamp = (cur > stamp ? cur - stamp : MAXDWORD - stamp + cur);
timeout = (timeout > stamp ? timeout - stamp : 0);
stamp = cur;
}
}
if (wait_result)
{
/* The child has terminated before the timeout has expired */
checkerr( (! GetExitCodeProcess(process_info.hProcess, &status)),
"GetExitCodeProcess failed", NULL);
} else if (pOverlapped == NULL) {
/* The timeout has expired, terminate the process */
checkerr( (! TerminateJobObject(hJob, 0)),
"TerminateJob failed", NULL);
status = -1;
wait_again = 1;
} else {
error_with_location(__FILE__, __LINE__, settings,
"GetQueuedCompletionStatus failed\n");
report_error(__FILE__, __LINE__,
settings, "Failure while waiting for process termination", NULL);
status = -1;
}
cleanup:
free(program);
free(commandline);
if (stdin_redirected) CloseHandle(startup_info.hStdInput);
if (stdout_redirected) CloseHandle(startup_info.hStdOutput);
if (stderr_redirected && !combined) CloseHandle(startup_info.hStdError);
if (wait_again)
{
/* Wait again but this time just 1sec to avoid being blocked */
WaitForSingleObject(process_info.hProcess, 1000);
}
if (process_created) CloseHandle(process_info.hProcess);
if (hJob != NULL) CloseHandle(hJob);
if (port.CompletionPort != NULL) CloseHandle(port.CompletionPort);
return status;
}