Skip to content

Latest commit

 

History

History
1145 lines (969 loc) · 31.5 KB

process.c

File metadata and controls

1145 lines (969 loc) · 31.5 KB
 
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
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "../uv-common.h"
#include "internal.h"
Jul 30, 2011
Jul 30, 2011
26
27
28
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
Nov 2, 2011
Nov 2, 2011
29
#include <signal.h>
Jul 30, 2011
Jul 30, 2011
30
31
#include <windows.h>
Nov 2, 2011
Nov 2, 2011
32
33
#define SIGKILL 9
Aug 9, 2011
Aug 9, 2011
34
35
36
37
38
39
40
41
typedef struct env_var {
const char* narrow;
const wchar_t* wide;
int len; /* including null or '=' */
int supplied;
int value_len;
} env_var_t;
Aug 10, 2011
Aug 10, 2011
42
#define E_V(str) { str "=", L##str, sizeof(str), 0, 0 }
Aug 9, 2011
Aug 9, 2011
43
Jul 30, 2011
Jul 30, 2011
44
45
46
47
48
49
50
#define UTF8_TO_UTF16(s, t) \
size = uv_utf8_to_utf16(s, NULL, 0) * sizeof(wchar_t); \
t = (wchar_t*)malloc(size); \
if (!t) { \
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); \
} \
if (!uv_utf8_to_utf16(s, t, size / sizeof(wchar_t))) { \
Oct 6, 2011
Oct 6, 2011
51
uv__set_sys_error(loop, GetLastError()); \
Jul 30, 2011
Jul 30, 2011
52
53
54
55
56
err = -1; \
goto done; \
}
Aug 31, 2011
Aug 31, 2011
57
static void uv_process_init(uv_loop_t* loop, uv_process_t* handle) {
Jul 30, 2011
Jul 30, 2011
58
handle->type = UV_PROCESS;
Aug 31, 2011
Aug 31, 2011
59
handle->loop = loop;
Jul 30, 2011
Jul 30, 2011
60
61
62
63
64
65
66
handle->flags = 0;
handle->exit_cb = NULL;
handle->pid = 0;
handle->exit_signal = 0;
handle->wait_handle = INVALID_HANDLE_VALUE;
handle->process_handle = INVALID_HANDLE_VALUE;
handle->close_handle = INVALID_HANDLE_VALUE;
Sep 6, 2011
Sep 6, 2011
67
68
69
handle->child_stdio[0] = INVALID_HANDLE_VALUE;
handle->child_stdio[1] = INVALID_HANDLE_VALUE;
handle->child_stdio[2] = INVALID_HANDLE_VALUE;
Jul 30, 2011
Jul 30, 2011
70
Aug 31, 2011
Aug 31, 2011
71
uv_req_init(loop, (uv_req_t*)&handle->exit_req);
Jul 30, 2011
Jul 30, 2011
72
73
handle->exit_req.type = UV_PROCESS_EXIT;
handle->exit_req.data = handle;
Aug 31, 2011
Aug 31, 2011
74
uv_req_init(loop, (uv_req_t*)&handle->close_req);
Jul 30, 2011
Jul 30, 2011
75
76
77
handle->close_req.type = UV_PROCESS_CLOSE;
handle->close_req.data = handle;
Aug 31, 2011
Aug 31, 2011
78
79
loop->counters.handle_init++;
loop->counters.process_init++;
Jul 30, 2011
Jul 30, 2011
80
Aug 31, 2011
Aug 31, 2011
81
uv_ref(loop);
Jul 30, 2011
Jul 30, 2011
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
}
/*
* Path search functions
*/
/*
* Helper function for search_path
*/
static wchar_t* search_path_join_test(const wchar_t* dir,
int dir_len,
const wchar_t* name,
int name_len,
const wchar_t* ext,
int ext_len,
const wchar_t* cwd,
int cwd_len) {
wchar_t *result, *result_pos;
DWORD attrs;
if (dir_len >= 1 && (dir[0] == L'/' || dir[0] == L'\\')) {
/* It's a full path without drive letter, use cwd's drive letter only */
cwd_len = 2;
} else if (dir_len >= 2 && dir[1] == L':' &&
(dir_len < 3 || (dir[2] != L'/' && dir[2] != L'\\'))) {
/* It's a relative path with drive letter (ext.g. D:../some/file)
* Replace drive letter in dir by full cwd if it points to the same drive,
* otherwise use the dir only.
*/
if (cwd_len < 2 || _wcsnicmp(cwd, dir, 2) != 0) {
cwd_len = 0;
} else {
dir += 2;
dir_len -= 2;
}
} else if (dir_len > 2 && dir[1] == L':') {
/* It's an absolute path with drive letter
* Don't use the cwd at all
*/
cwd_len = 0;
}
/* Allocate buffer for output */
Aug 31, 2011
Aug 31, 2011
126
127
result = result_pos = (wchar_t*)malloc(sizeof(wchar_t) *
(cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
Jul 30, 2011
Jul 30, 2011
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
/* Copy cwd */
wcsncpy(result_pos, cwd, cwd_len);
result_pos += cwd_len;
/* Add a path separator if cwd didn't end with one */
if (cwd_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
result_pos[0] = L'\\';
result_pos++;
}
/* Copy dir */
wcsncpy(result_pos, dir, dir_len);
result_pos += dir_len;
/* Add a separator if the dir didn't end with one */
if (dir_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
result_pos[0] = L'\\';
result_pos++;
}
/* Copy filename */
wcsncpy(result_pos, name, name_len);
result_pos += name_len;
if (ext_len) {
Aug 18, 2011
Aug 18, 2011
154
155
156
157
158
159
160
/* Add a dot if the filename didn't end with one */
if (name_len && result_pos[-1] != '.') {
result_pos[0] = L'.';
result_pos++;
}
/* Copy extension */
Jul 30, 2011
Jul 30, 2011
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
wcsncpy(result_pos, ext, ext_len);
result_pos += ext_len;
}
/* Null terminator */
result_pos[0] = L'\0';
attrs = GetFileAttributesW(result);
if (attrs != INVALID_FILE_ATTRIBUTES &&
!(attrs & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) {
return result;
}
free(result);
return NULL;
}
/*
* Helper function for search_path
*/
static wchar_t* path_search_walk_ext(const wchar_t *dir,
int dir_len,
const wchar_t *name,
int name_len,
wchar_t *cwd,
int cwd_len,
int name_has_ext) {
Aug 18, 2011
Aug 18, 2011
190
wchar_t* result;
Jul 30, 2011
Jul 30, 2011
191
Aug 18, 2011
Aug 18, 2011
192
/* If the name itself has a nonempty extension, try this extension first */
Jul 30, 2011
Jul 30, 2011
193
194
195
196
197
if (name_has_ext) {
result = search_path_join_test(dir, dir_len,
name, name_len,
L"", 0,
cwd, cwd_len);
Aug 18, 2011
Aug 18, 2011
198
199
if (result != NULL) {
return result;
Jul 30, 2011
Jul 30, 2011
200
}
Aug 18, 2011
Aug 18, 2011
201
}
Jul 30, 2011
Jul 30, 2011
202
Aug 18, 2011
Aug 18, 2011
203
204
205
206
207
208
209
210
/* Try .com extension */
result = search_path_join_test(dir, dir_len,
name, name_len,
L"com", 3,
cwd, cwd_len);
if (result != NULL) {
return result;
}
Jul 30, 2011
Jul 30, 2011
211
Aug 18, 2011
Aug 18, 2011
212
213
214
215
216
217
218
/* Try .exe extension */
result = search_path_join_test(dir, dir_len,
name, name_len,
L"exe", 3,
cwd, cwd_len);
if (result != NULL) {
return result;
Jul 30, 2011
Jul 30, 2011
219
220
}
Aug 18, 2011
Aug 18, 2011
221
return NULL;
Jul 30, 2011
Jul 30, 2011
222
223
224
225
226
227
228
229
230
231
}
/*
* search_path searches the system path for an executable filename -
* the windows API doesn't provide this as a standalone function nor as an
* option to CreateProcess.
*
* It tries to return an absolute filename.
*
Aug 18, 2011
Aug 18, 2011
232
233
234
235
* Furthermore, it tries to follow the semantics that cmd.exe, with this
* exception that PATHEXT environment variable isn't used. Since CreateProcess
* can start only .com and .exe files, only those extensions are tried. This
* behavior equals that of msvcrt's spawn functions.
Jul 30, 2011
Jul 30, 2011
236
237
238
239
240
241
242
*
* - Do not search the path if the filename already contains a path (either
* relative or absolute).
*
* - If there's really only a filename, check the current directory for file,
* then search all path directories.
*
Aug 18, 2011
Aug 18, 2011
243
* - If filename specified has *any* extension, search for the file with the
Jul 30, 2011
Jul 30, 2011
244
245
246
* specified extension first.
*
* - If the literal filename is not found in a directory, try *appending*
Aug 18, 2011
Aug 18, 2011
247
* (not replacing) .com first and then .exe.
Jul 30, 2011
Jul 30, 2011
248
249
250
251
252
253
254
255
256
*
* - The path variable may contain relative paths; relative paths are relative
* to the cwd.
*
* - Directories in path may or may not end with a trailing backslash.
*
* - CMD does not trim leading/trailing whitespace from path/pathex entries
* nor from the environment variables as a whole.
*
Nov 30, 2011
Nov 30, 2011
257
* - When cmd.exe cannot read a directory, it will just skip it and go on
Jul 30, 2011
Jul 30, 2011
258
259
260
261
262
263
264
265
* searching. However, unlike posix-y systems, it will happily try to run a
* file that is not readable/executable; if the spawn fails it will not
* continue searching.
*
* TODO: correctly interpret UNC paths
*/
static wchar_t* search_path(const wchar_t *file,
wchar_t *cwd,
Aug 18, 2011
Aug 18, 2011
266
const wchar_t *path) {
Jul 30, 2011
Jul 30, 2011
267
268
269
270
int file_has_dir;
wchar_t* result = NULL;
wchar_t *file_name_start;
wchar_t *dot;
Nov 4, 2011
Nov 4, 2011
271
272
const wchar_t *dir_start, *dir_end, *dir_path;
int dir_len;
Jul 30, 2011
Jul 30, 2011
273
274
275
276
277
278
279
280
281
282
283
284
285
int name_has_ext;
int file_len = wcslen(file);
int cwd_len = wcslen(cwd);
/* If the caller supplies an empty filename,
* we're not gonna return c:\windows\.exe -- GFY!
*/
if (file_len == 0
|| (file_len == 1 && file[0] == L'.')) {
return NULL;
}
Aug 31, 2011
Aug 31, 2011
286
287
/* Find the start of the filename so we can split the directory from the */
/* name. */
Jul 30, 2011
Jul 30, 2011
288
289
290
291
292
293
294
295
296
297
298
299
300
301
for (file_name_start = (wchar_t*)file + file_len;
file_name_start > file
&& file_name_start[-1] != L'\\'
&& file_name_start[-1] != L'/'
&& file_name_start[-1] != L':';
file_name_start--);
file_has_dir = file_name_start != file;
/* Check if the filename includes an extension */
dot = wcschr(file_name_start, L'.');
name_has_ext = (dot != NULL && dot[1] != L'\0');
if (file_has_dir) {
Aug 18, 2011
Aug 18, 2011
302
/* The file has a path inside, don't use path */
Jul 30, 2011
Jul 30, 2011
303
304
305
306
result = path_search_walk_ext(
file, file_name_start - file,
file_name_start, file_len - (file_name_start - file),
cwd, cwd_len,
Aug 18, 2011
Aug 18, 2011
307
name_has_ext);
Jul 30, 2011
Jul 30, 2011
308
309
} else {
Nov 4, 2011
Nov 4, 2011
310
dir_end = path;
Jul 30, 2011
Jul 30, 2011
311
312
313
314
315
/* The file is really only a name; look in cwd first, then scan path */
result = path_search_walk_ext(L"", 0,
file, file_len,
cwd, cwd_len,
Aug 18, 2011
Aug 18, 2011
316
name_has_ext);
Jul 30, 2011
Jul 30, 2011
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
while (result == NULL) {
if (*dir_end == L'\0') {
break;
}
/* Skip the separator that dir_end now points to */
if (dir_end != path) {
dir_end++;
}
/* Next slice starts just after where the previous one ended */
dir_start = dir_end;
/* Slice until the next ; or \0 is found */
dir_end = wcschr(dir_start, L';');
if (dir_end == NULL) {
dir_end = wcschr(dir_start, L'\0');
}
/* If the slice is zero-length, don't bother */
if (dir_end - dir_start == 0) {
continue;
}
Nov 4, 2011
Nov 4, 2011
342
343
344
345
346
347
348
349
350
351
352
353
354
355
dir_path = dir_start;
dir_len = dir_end - dir_start;
/* Adjust if the path is quoted. */
if (dir_path[0] == '"' || dir_path[0] == '\'') {
++dir_path;
--dir_len;
}
if (dir_path[dir_len - 1] == '"' || dir_path[dir_len - 1] == '\'') {
--dir_len;
}
result = path_search_walk_ext(dir_path, dir_len,
Jul 30, 2011
Jul 30, 2011
356
357
file, file_len,
cwd, cwd_len,
Aug 18, 2011
Aug 18, 2011
358
name_has_ext);
Jul 30, 2011
Jul 30, 2011
359
360
361
362
363
364
}
}
return result;
}
Aug 9, 2011
Aug 9, 2011
365
Aug 5, 2011
Aug 5, 2011
366
367
368
369
370
371
372
373
374
/*
* Quotes command line arguments
* Returns a pointer to the end (next char to be written) of the buffer
*/
wchar_t* quote_cmd_arg(const wchar_t *source, wchar_t *target) {
int len = wcslen(source),
i, quote_hit;
wchar_t* start;
Aug 18, 2011
Aug 18, 2011
375
/*
Aug 5, 2011
Aug 5, 2011
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
* Check if the string must be quoted;
* if unnecessary, don't do it, it may only confuse older programs.
*/
if (len == 0) {
return target;
}
if (NULL == wcspbrk(source, L" \t\"")) {
/* No quotation needed */
wcsncpy(target, source, len);
target += len;
return target;
}
if (NULL == wcspbrk(source, L"\"\\")) {
Aug 18, 2011
Aug 18, 2011
391
/*
Aug 5, 2011
Aug 5, 2011
392
393
394
395
396
397
398
399
400
401
402
* No embedded double quotes or backlashes, so I can just wrap
* quote marks around the whole thing.
*/
*(target++) = L'"';
wcsncpy(target, source, len);
target += len;
*(target++) = L'"';
return target;
}
/*
Nov 30, 2011
Nov 30, 2011
403
* Expected input/output:
Aug 5, 2011
Aug 5, 2011
404
405
406
407
408
* input : hello"world
* output: "hello\"world"
* input : hello""world
* output: "hello\"\"world"
* input : hello\world
Aug 5, 2011
Aug 5, 2011
409
* output: hello\world
Aug 5, 2011
Aug 5, 2011
410
* input : hello\\world
Aug 5, 2011
Aug 5, 2011
411
* output: hello\\world
Aug 5, 2011
Aug 5, 2011
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
* input : hello\"world
* output: "hello\\\"world"
* input : hello\\"world
* output: "hello\\\\\"world"
* input : hello world\
* output: "hello world\"
*/
*(target++) = L'"';
start = target;
quote_hit = 1;
for (i = len; i > 0; --i) {
*(target++) = source[i - 1];
if (quote_hit && source[i - 1] == L'\\') {
*(target++) = L'\\';
} else if(source[i - 1] == L'"') {
quote_hit = 1;
*(target++) = L'\\';
} else {
quote_hit = 0;
}
}
target[0] = L'\0';
wcsrev(start);
*(target++) = L'"';
return target;
}
Jul 30, 2011
Jul 30, 2011
441
Aug 9, 2011
Aug 9, 2011
442
Aug 5, 2011
Aug 5, 2011
443
wchar_t* make_program_args(char** args, int verbatim_arguments) {
Jul 30, 2011
Jul 30, 2011
444
445
446
447
448
449
wchar_t* dst;
wchar_t* ptr;
char** arg;
size_t size = 0;
size_t len;
int arg_count = 0;
Aug 1, 2011
Aug 1, 2011
450
451
452
wchar_t* buffer;
int arg_size;
int buffer_size = 0;
Jul 30, 2011
Jul 30, 2011
453
454
455
/* Count the required size. */
for (arg = args; *arg; arg++) {
Aug 1, 2011
Aug 1, 2011
456
457
458
arg_size = uv_utf8_to_utf16(*arg, NULL, 0) * sizeof(wchar_t);
size += arg_size;
buffer_size = arg_size > buffer_size ? arg_size : buffer_size;
Jul 30, 2011
Jul 30, 2011
459
460
461
arg_count++;
}
Aug 18, 2011
Aug 18, 2011
462
/* Adjust for potential quotes. Also assume the worst-case scenario
Aug 3, 2011
Aug 3, 2011
463
464
/* that every character needs escaping, so we need twice as much space. */
size = size * 2 + arg_count * 2;
Jul 30, 2011
Jul 30, 2011
465
466
467
468
469
470
dst = (wchar_t*)malloc(size);
if (!dst) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
Aug 1, 2011
Aug 1, 2011
471
472
473
474
475
buffer = (wchar_t*)malloc(buffer_size);
if (!buffer) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
Jul 30, 2011
Jul 30, 2011
476
ptr = dst;
Aug 1, 2011
Aug 1, 2011
477
478
for (arg = args; *arg; arg++) {
len = uv_utf8_to_utf16(*arg, buffer, (size_t)(size - (ptr - dst)));
Jul 30, 2011
Jul 30, 2011
479
if (!len) {
Aug 1, 2011
Aug 1, 2011
480
goto error;
Jul 30, 2011
Jul 30, 2011
481
}
Aug 5, 2011
Aug 5, 2011
482
483
484
485
486
487
488
if (verbatim_arguments) {
wcscpy(ptr, buffer);
ptr += len - 1;
} else {
ptr = quote_cmd_arg(buffer, ptr);
}
*ptr++ = *(arg + 1) ? L' ' : L'\0';
Jul 30, 2011
Jul 30, 2011
489
490
}
Aug 1, 2011
Aug 1, 2011
491
free(buffer);
Jul 30, 2011
Jul 30, 2011
492
return dst;
Aug 1, 2011
Aug 1, 2011
493
494
495
496
497
error:
free(dst);
free(buffer);
return NULL;
Jul 30, 2011
Jul 30, 2011
498
499
}
Aug 9, 2011
Aug 9, 2011
500
501
502
503
504
/*
* If we learn that people are passing in huge environment blocks
* then we should probably qsort() the array and then bsearch()
* to see if it contains this variable. But there are ownership
Aug 18, 2011
Aug 18, 2011
505
* issues associated with that solution; this is the caller's
Aug 9, 2011
Aug 9, 2011
506
507
* char**, and modifying it is rude.
*/
Aug 31, 2011
Aug 31, 2011
508
509
static void check_required_vars_contains_var(env_var_t* required, int size,
const char* var) {
Aug 9, 2011
Aug 9, 2011
510
511
512
513
514
515
516
517
518
519
int i;
for (i = 0; i < size; ++i) {
if (_strnicmp(required[i].narrow, var, required[i].len) == 0) {
required[i].supplied = 1;
return;
}
}
}
Jul 30, 2011
Jul 30, 2011
520
/*
Aug 9, 2011
Aug 9, 2011
521
522
523
* The way windows takes environment variables is different than what C does;
* Windows wants a contiguous block of null-terminated strings, terminated
* with an additional null.
Aug 18, 2011
Aug 18, 2011
524
*
Aug 9, 2011
Aug 9, 2011
525
526
527
528
529
530
* Windows has a few "essential" environment variables. winsock will fail
* to initialize if SYSTEMROOT is not defined; some APIs make reference to
* TEMP. SYSTEMDRIVE is probably also important. We therefore ensure that
* these get defined if the input environment block does not contain any
* values for them.
*/
Aug 1, 2011
Aug 1, 2011
531
wchar_t* make_program_env(char** env_block) {
Jul 30, 2011
Jul 30, 2011
532
533
534
535
536
wchar_t* dst;
wchar_t* ptr;
char** env;
int env_len = 1 * sizeof(wchar_t); /* room for closing null */
int len;
Aug 9, 2011
Aug 9, 2011
537
538
539
540
541
542
543
544
int i;
DWORD var_size;
env_var_t required_vars[] = {
E_V("SYSTEMROOT"),
E_V("SYSTEMDRIVE"),
E_V("TEMP"),
};
Jul 30, 2011
Jul 30, 2011
545
546
for (env = env_block; *env; env++) {
Aug 31, 2011
Aug 31, 2011
547
check_required_vars_contains_var(required_vars,
Jan 18, 2012
Jan 18, 2012
548
ARRAY_SIZE(required_vars),
Aug 31, 2011
Aug 31, 2011
549
*env);
Jul 30, 2011
Jul 30, 2011
550
551
552
env_len += (uv_utf8_to_utf16(*env, NULL, 0) * sizeof(wchar_t));
}
Jan 18, 2012
Jan 18, 2012
553
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
Aug 9, 2011
Aug 9, 2011
554
555
556
557
558
559
560
561
562
563
564
565
if (!required_vars[i].supplied) {
env_len += required_vars[i].len * sizeof(wchar_t);
var_size = GetEnvironmentVariableW(required_vars[i].wide, NULL, 0);
if (var_size == 0) {
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
}
required_vars[i].value_len = (int)var_size;
env_len += (int)var_size * sizeof(wchar_t);
}
}
dst = malloc(env_len);
Jul 30, 2011
Jul 30, 2011
566
567
568
569
570
571
572
573
574
575
576
577
578
579
if (!dst) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
ptr = dst;
for (env = env_block; *env; env++, ptr += len) {
len = uv_utf8_to_utf16(*env, ptr, (size_t)(env_len - (ptr - dst)));
if (!len) {
free(dst);
return NULL;
}
}
Jan 18, 2012
Jan 18, 2012
580
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
Aug 9, 2011
Aug 9, 2011
581
582
583
584
if (!required_vars[i].supplied) {
wcscpy(ptr, required_vars[i].wide);
ptr += required_vars[i].len - 1;
*ptr++ = L'=';
Aug 31, 2011
Aug 31, 2011
585
586
587
var_size = GetEnvironmentVariableW(required_vars[i].wide,
ptr,
required_vars[i].value_len);
Aug 9, 2011
Aug 9, 2011
588
589
590
591
592
593
594
if (var_size == 0) {
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
}
ptr += required_vars[i].value_len;
}
}
Jul 30, 2011
Jul 30, 2011
595
596
597
598
599
*ptr = L'\0';
return dst;
}
Aug 2, 2011
Aug 2, 2011
600
601
602
/*
* Called on Windows thread-pool thread to indicate that
* a child process has exited.
Jul 30, 2011
Jul 30, 2011
603
604
605
*/
static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
uv_process_t* process = (uv_process_t*)data;
Aug 31, 2011
Aug 31, 2011
606
uv_loop_t* loop = process->loop;
Aug 2, 2011
Aug 2, 2011
607
Jul 30, 2011
Jul 30, 2011
608
609
assert(didTimeout == FALSE);
assert(process);
Aug 2, 2011
Aug 2, 2011
610
Jul 30, 2011
Jul 30, 2011
611
/* Post completed */
Aug 31, 2011
Aug 31, 2011
612
POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
Jul 30, 2011
Jul 30, 2011
613
614
615
}
Aug 2, 2011
Aug 2, 2011
616
617
618
/*
* Called on Windows thread-pool thread to indicate that
* UnregisterWaitEx has completed.
Jul 30, 2011
Jul 30, 2011
619
620
621
*/
static void CALLBACK close_wait_callback(void* data, BOOLEAN didTimeout) {
uv_process_t* process = (uv_process_t*)data;
Aug 31, 2011
Aug 31, 2011
622
uv_loop_t* loop = process->loop;
Aug 2, 2011
Aug 2, 2011
623
Jul 30, 2011
Jul 30, 2011
624
625
626
627
assert(didTimeout == FALSE);
assert(process);
/* Post completed */
Aug 31, 2011
Aug 31, 2011
628
POST_COMPLETION_FOR_REQ(loop, &process->close_req);
Jul 30, 2011
Jul 30, 2011
629
630
631
}
Aug 2, 2011
Aug 2, 2011
632
633
634
635
636
637
638
639
640
/*
* Called on windows thread pool when CreateProcess failed. It writes an error
* message to the process' intended stderr and then posts a PROCESS_EXIT
* packet to the completion port.
*/
static DWORD WINAPI spawn_failure(void* data) {
char syscall[] = "CreateProcessW: ";
char unknown[] = "unknown error\n";
uv_process_t* process = (uv_process_t*) data;
Aug 31, 2011
Aug 31, 2011
641
uv_loop_t* loop = process->loop;
Sep 6, 2011
Sep 6, 2011
642
HANDLE child_stderr = process->child_stdio[2];
Aug 2, 2011
Aug 2, 2011
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
char* buf = NULL;
DWORD count, written;
WriteFile(child_stderr, syscall, sizeof(syscall) - 1, &written, NULL);
count = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
process->spawn_errno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &buf,
0,
NULL);
if (buf != NULL && count > 0) {
WriteFile(child_stderr, buf, count, &written, NULL);
LocalFree(buf);
} else {
WriteFile(child_stderr, unknown, sizeof(unknown) - 1, &written, NULL);
}
FlushFileBuffers(child_stderr);
/* Post completed */
Aug 31, 2011
Aug 31, 2011
668
POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
Aug 2, 2011
Aug 2, 2011
669
670
671
672
673
return 0;
}
Sep 6, 2011
Sep 6, 2011
674
static void close_child_stdio(uv_process_t* process) {
Jul 30, 2011
Jul 30, 2011
675
int i;
Sep 6, 2011
Sep 6, 2011
676
HANDLE handle;
Jul 30, 2011
Jul 30, 2011
677
Jan 18, 2012
Jan 18, 2012
678
for (i = 0; i < ARRAY_SIZE(process->child_stdio); i++) {
Sep 6, 2011
Sep 6, 2011
679
680
681
682
handle = process->child_stdio[i];
if (handle != NULL && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
process->child_stdio[i] = INVALID_HANDLE_VALUE;
Jul 30, 2011
Jul 30, 2011
683
684
}
}
Sep 6, 2011
Sep 6, 2011
685
686
687
688
689
690
}
/* Called on main thread after a child process has exited. */
void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle) {
DWORD exit_code;
Jul 30, 2011
Jul 30, 2011
691
692
693
694
695
696
697
698
699
700
/* Unregister from process notification. */
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
UnregisterWait(handle->wait_handle);
handle->wait_handle = INVALID_HANDLE_VALUE;
}
if (handle->process_handle != INVALID_HANDLE_VALUE) {
/* Get the exit code. */
if (!GetExitCodeProcess(handle->process_handle, &exit_code)) {
Aug 2, 2011
Aug 2, 2011
701
exit_code = 127;
Jul 30, 2011
Jul 30, 2011
702
703
}
Aug 2, 2011
Aug 2, 2011
704
/* Clean-up the process handle. */
Jul 30, 2011
Jul 30, 2011
705
706
CloseHandle(handle->process_handle);
handle->process_handle = INVALID_HANDLE_VALUE;
Aug 2, 2011
Aug 2, 2011
707
} else {
Sep 6, 2011
Sep 6, 2011
708
709
710
711
/* We probably left the child stdio handles open to report the error */
/* asynchronously, so close them now. */
close_child_stdio(handle);
Aug 2, 2011
Aug 2, 2011
712
713
/* The process never even started in the first place. */
exit_code = 127;
Jul 30, 2011
Jul 30, 2011
714
715
716
717
718
719
720
721
722
723
}
/* Fire the exit callback. */
if (handle->exit_cb) {
handle->exit_cb(handle, exit_code, handle->exit_signal);
}
}
/* Called on main thread after UnregisterWaitEx finishes. */
Aug 31, 2011
Aug 31, 2011
724
725
void uv_process_proc_close(uv_loop_t* loop, uv_process_t* handle) {
uv_want_endgame(loop, (uv_handle_t*)handle);
Jul 30, 2011
Jul 30, 2011
726
727
728
}
Aug 31, 2011
Aug 31, 2011
729
void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
Jul 30, 2011
Jul 30, 2011
730
731
732
733
734
735
736
737
if (handle->flags & UV_HANDLE_CLOSING) {
assert(!(handle->flags & UV_HANDLE_CLOSED));
handle->flags |= UV_HANDLE_CLOSED;
if (handle->close_cb) {
handle->close_cb((uv_handle_t*)handle);
}
Aug 31, 2011
Aug 31, 2011
738
uv_unref(loop);
Jul 30, 2011
Jul 30, 2011
739
740
741
742
}
}
Aug 31, 2011
Aug 31, 2011
743
void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
Jul 30, 2011
Jul 30, 2011
744
745
746
747
748
749
750
751
752
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
handle->close_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
UnregisterWaitEx(handle->wait_handle, handle->close_handle);
handle->wait_handle = NULL;
RegisterWaitForSingleObject(&handle->wait_handle, handle->close_handle,
close_wait_callback, (void*)handle, INFINITE,
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE);
} else {
Aug 31, 2011
Aug 31, 2011
753
uv_want_endgame(loop, (uv_handle_t*)handle);
Jul 30, 2011
Jul 30, 2011
754
755
756
757
}
}
Aug 31, 2011
Aug 31, 2011
758
static int uv_create_stdio_pipe_pair(uv_loop_t* loop, uv_pipe_t* server_pipe,
Oct 6, 2011
Oct 6, 2011
759
760
HANDLE* child_pipe, DWORD server_access, DWORD child_access,
int overlapped) {
Jul 30, 2011
Jul 30, 2011
761
762
763
int err;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
char pipe_name[64];
Jul 30, 2011
Jul 30, 2011
764
DWORD mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT;
Jul 30, 2011
Jul 30, 2011
765
Aug 2, 2011
Aug 2, 2011
766
if (server_pipe->type != UV_NAMED_PIPE) {
Sep 28, 2011
Sep 28, 2011
767
uv__set_artificial_error(loop, UV_EINVAL);
Jul 30, 2011
Jul 30, 2011
768
err = -1;
Aug 2, 2011
Aug 2, 2011
769
goto done;
Jul 30, 2011
Jul 30, 2011
770
771
772
}
/* Create server pipe handle. */
Aug 31, 2011
Aug 31, 2011
773
774
775
776
777
err = uv_stdio_pipe_server(loop,
server_pipe,
server_access,
pipe_name,
sizeof(pipe_name));
Jul 30, 2011
Jul 30, 2011
778
779
780
781
782
783
784
785
786
787
if (err) {
goto done;
}
/* Create child pipe handle. */
*child_pipe = CreateFileA(pipe_name,
child_access,
0,
&sa,
OPEN_EXISTING,
Oct 6, 2011
Oct 6, 2011
788
overlapped ? FILE_FLAG_OVERLAPPED : 0,
Jul 30, 2011
Jul 30, 2011
789
790
791
NULL);
if (*child_pipe == INVALID_HANDLE_VALUE) {
Sep 28, 2011
Sep 28, 2011
792
uv__set_sys_error(loop, GetLastError());
Jul 30, 2011
Jul 30, 2011
793
794
795
796
err = -1;
goto done;
}
Jul 30, 2011
Jul 30, 2011
797
if (!SetNamedPipeHandleState(*child_pipe, &mode, NULL, NULL)) {
Sep 28, 2011
Sep 28, 2011
798
uv__set_sys_error(loop, GetLastError());
Jul 30, 2011
Jul 30, 2011
799
800
801
802
err = -1;
goto done;
}
Jul 30, 2011
Jul 30, 2011
803
804
805
806
807
/* Do a blocking ConnectNamedPipe. This should not block because
* we have both ends of the pipe created.
*/
if (!ConnectNamedPipe(server_pipe->handle, NULL)) {
if (GetLastError() != ERROR_PIPE_CONNECTED) {
Sep 28, 2011
Sep 28, 2011
808
uv__set_sys_error(loop, GetLastError());
Jul 30, 2011
Jul 30, 2011
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
err = -1;
goto done;
}
}
err = 0;
done:
if (err) {
if (server_pipe->handle != INVALID_HANDLE_VALUE) {
close_pipe(server_pipe, NULL, NULL);
}
if (*child_pipe != INVALID_HANDLE_VALUE) {
CloseHandle(*child_pipe);
*child_pipe = INVALID_HANDLE_VALUE;
}
}
return err;
}
Sep 6, 2011
Sep 6, 2011
832
833
834
static int duplicate_std_handle(uv_loop_t* loop, DWORD id, HANDLE* dup) {
HANDLE handle;
HANDLE current_process = GetCurrentProcess();
Mar 9, 2012
Mar 9, 2012
835
Sep 6, 2011
Sep 6, 2011
836
837
838
839
840
841
842
handle = GetStdHandle(id);
if (handle == NULL) {
*dup = NULL;
return 0;
} else if (handle == INVALID_HANDLE_VALUE) {
*dup = INVALID_HANDLE_VALUE;
Sep 28, 2011
Sep 28, 2011
843
uv__set_sys_error(loop, GetLastError());
Sep 6, 2011
Sep 6, 2011
844
845
846
847
848
849
850
851
852
853
854
return -1;
}
if (!DuplicateHandle(current_process,
handle,
current_process,
dup,
0,
TRUE,
DUPLICATE_SAME_ACCESS)) {
*dup = INVALID_HANDLE_VALUE;
Sep 28, 2011
Sep 28, 2011
855
uv__set_sys_error(loop, GetLastError());
Sep 6, 2011
Sep 6, 2011
856
857
858
859
860
861
862
return -1;
}
return 0;
}
Aug 31, 2011
Aug 31, 2011
863
864
int uv_spawn(uv_loop_t* loop, uv_process_t* process,
uv_process_options_t options) {
Sep 6, 2011
Sep 6, 2011
865
int err = 0, keep_child_stdio_open = 0;
Sep 19, 2011
Sep 19, 2011
866
wchar_t* path = NULL;
Jul 30, 2011
Jul 30, 2011
867
int size;
Aug 31, 2011
Aug 31, 2011
868
BOOL result;
Oct 6, 2011
Oct 6, 2011
869
870
wchar_t* application_path = NULL, *application = NULL, *arguments = NULL,
*env = NULL, *cwd = NULL;
Sep 6, 2011
Sep 6, 2011
871
HANDLE* child_stdio = process->child_stdio;
Jul 30, 2011
Jul 30, 2011
872
873
STARTUPINFOW startup;
PROCESS_INFORMATION info;
Aug 2, 2011
Aug 2, 2011
874
Apr 28, 2012
Apr 28, 2012
875
876
if (options.flags & (UV_PROCESS_SETGID | UV_PROCESS_SETUID)) {
uv__set_artificial_error(loop, UV_ENOTSUP);
Sep 19, 2011
Sep 19, 2011
877
878
879
return -1;
}
Apr 28, 2012
Apr 28, 2012
880
881
882
883
884
assert(options.file != NULL);
assert(!(options.flags & ~(UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS |
UV_PROCESS_SETGID |
UV_PROCESS_SETUID)));
Aug 31, 2011
Aug 31, 2011
885
uv_process_init(loop, process);
Jul 30, 2011
Jul 30, 2011
886
887
888
process->exit_cb = options.exit_cb;
UTF8_TO_UTF16(options.file, application);
Aug 31, 2011
Aug 31, 2011
889
arguments = options.args ? make_program_args(options.args,
Apr 28, 2012
Apr 28, 2012
890
options.flags & UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS) : NULL;
Aug 1, 2011
Aug 1, 2011
891
env = options.env ? make_program_env(options.env) : NULL;
Jul 30, 2011
Jul 30, 2011
892
893
894
895
896
897
898
899
900
901
902
903
if (options.cwd) {
UTF8_TO_UTF16(options.cwd, cwd);
} else {
size = GetCurrentDirectoryW(0, NULL) * sizeof(wchar_t);
if (size) {
cwd = (wchar_t*)malloc(size);
if (!cwd) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
GetCurrentDirectoryW(size, cwd);
} else {
Sep 28, 2011
Sep 28, 2011
904
uv__set_sys_error(loop, GetLastError());
Jul 30, 2011
Jul 30, 2011
905
906
907
908
909
err = -1;
goto done;
}
}
Aug 1, 2011
Aug 1, 2011
910
911
912
/* Get PATH env. variable. */
size = GetEnvironmentVariableW(L"PATH", NULL, 0) + 1;
path = (wchar_t*)malloc(size * sizeof(wchar_t));
Aug 1, 2011
Aug 1, 2011
913
if (!path) {
Aug 1, 2011
Aug 1, 2011
914
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
Aug 1, 2011
Aug 1, 2011
915
}
Aug 1, 2011
Aug 1, 2011
916
917
GetEnvironmentVariableW(L"PATH", path, size * sizeof(wchar_t));
path[size - 1] = L'\0';
Aug 1, 2011
Aug 1, 2011
918
Aug 2, 2011
Aug 2, 2011
919
application_path = search_path(application,
Jul 30, 2011
Jul 30, 2011
920
cwd,
Aug 18, 2011
Aug 18, 2011
921
path);
Jul 30, 2011
Jul 30, 2011
922
923
if (!application_path) {
Aug 2, 2011
Aug 2, 2011
924
925
926
/* CreateProcess will fail, but this allows us to pass this error to */
/* the user asynchronously. */
application_path = application;
Jul 30, 2011
Jul 30, 2011
927
928
929
930
}
/* Create stdio pipes. */
if (options.stdin_stream) {
Oct 7, 2011
Oct 7, 2011
931
if (options.stdin_stream->ipc) {
Oct 6, 2011
Oct 6, 2011
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
err = uv_create_stdio_pipe_pair(
loop,
options.stdin_stream,
&child_stdio[0],
PIPE_ACCESS_DUPLEX,
GENERIC_READ | FILE_WRITE_ATTRIBUTES | GENERIC_WRITE,
1);
} else {
err = uv_create_stdio_pipe_pair(
loop,
options.stdin_stream,
&child_stdio[0],
PIPE_ACCESS_OUTBOUND,
GENERIC_READ | FILE_WRITE_ATTRIBUTES,
0);
}
Feb 28, 2012
Feb 28, 2012
948
} else {
Sep 6, 2011
Sep 6, 2011
949
950
951
952
err = duplicate_std_handle(loop, STD_INPUT_HANDLE, &child_stdio[0]);
}
if (err) {
goto done;
Jul 30, 2011
Jul 30, 2011
953
954
955
}
if (options.stdout_stream) {
Aug 31, 2011
Aug 31, 2011
956
957
err = uv_create_stdio_pipe_pair(
loop, options.stdout_stream,
Sep 6, 2011
Sep 6, 2011
958
&child_stdio[1],
Aug 31, 2011
Aug 31, 2011
959
PIPE_ACCESS_INBOUND,
Oct 6, 2011
Oct 6, 2011
960
961
GENERIC_WRITE,
0);
Feb 28, 2012
Feb 28, 2012
962
} else {
Sep 6, 2011
Sep 6, 2011
963
964
965
966
err = duplicate_std_handle(loop, STD_OUTPUT_HANDLE, &child_stdio[1]);
}
if (err) {
goto done;
Jul 30, 2011
Jul 30, 2011
967
968
969
}
if (options.stderr_stream) {
Aug 31, 2011
Aug 31, 2011
970
971
972
err = uv_create_stdio_pipe_pair(
loop,
options.stderr_stream,
Sep 6, 2011
Sep 6, 2011
973
&child_stdio[2],
Aug 31, 2011
Aug 31, 2011
974
PIPE_ACCESS_INBOUND,
Oct 6, 2011
Oct 6, 2011
975
976
GENERIC_WRITE,
0);
Feb 28, 2012
Feb 28, 2012
977
} else {
Sep 6, 2011
Sep 6, 2011
978
979
980
981
err = duplicate_std_handle(loop, STD_ERROR_HANDLE, &child_stdio[2]);
}
if (err) {
goto done;
Jul 30, 2011
Jul 30, 2011
982
983
984
985
986
987
}
startup.cb = sizeof(startup);
startup.lpReserved = NULL;
startup.lpDesktop = NULL;
startup.lpTitle = NULL;
Feb 28, 2012
Feb 28, 2012
988
startup.dwFlags = STARTF_USESTDHANDLES;
Jul 30, 2011
Jul 30, 2011
989
990
startup.cbReserved2 = 0;
startup.lpReserved2 = NULL;
Feb 28, 2012
Feb 28, 2012
991
992
993
startup.hStdInput = child_stdio[0];
startup.hStdOutput = child_stdio[1];
startup.hStdError = child_stdio[2];
Jul 30, 2011
Jul 30, 2011
994
Aug 2, 2011
Aug 2, 2011
995
996
997
998
if (CreateProcessW(application_path,
arguments,
NULL,
NULL,
Feb 28, 2012
Feb 28, 2012
999
1000
1,
CREATE_UNICODE_ENVIRONMENT,