Skip to content

Latest commit

 

History

History
1120 lines (931 loc) · 30.7 KB

process.c

File metadata and controls

1120 lines (931 loc) · 30.7 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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.
*/
Jun 1, 2012
Jun 1, 2012
22
#include <assert.h>
May 28, 2012
May 28, 2012
23
#include <io.h>
Jul 30, 2011
Jul 30, 2011
24
25
#include <stdio.h>
#include <stdlib.h>
Nov 2, 2011
Nov 2, 2011
26
#include <signal.h>
Aug 1, 2013
Aug 1, 2013
27
#include <limits.h>
Jun 2, 2012
Jun 2, 2012
28
29
30
#include "uv.h"
#include "internal.h"
Jun 2, 2012
Jun 2, 2012
31
#include "handle-inl.h"
Jun 2, 2012
Jun 2, 2012
32
#include "req-inl.h"
Jul 30, 2011
Jul 30, 2011
33
Jun 1, 2012
Jun 1, 2012
34
Nov 2, 2011
Nov 2, 2011
35
36
#define SIGKILL 9
Jun 1, 2012
Jun 1, 2012
37
Aug 9, 2011
Aug 9, 2011
38
39
typedef struct env_var {
const char* narrow;
Aug 13, 2012
Aug 13, 2012
40
const WCHAR* wide;
Aug 28, 2012
Aug 28, 2012
41
42
size_t len; /* including null or '=' */
DWORD value_len;
Aug 9, 2011
Aug 9, 2011
43
44
45
int supplied;
} env_var_t;
Aug 10, 2011
Aug 10, 2011
46
#define E_V(str) { str "=", L##str, sizeof(str), 0, 0 }
Aug 9, 2011
Aug 9, 2011
47
Jun 1, 2012
Jun 1, 2012
48
May 14, 2013
May 14, 2013
49
50
51
52
static HANDLE uv_global_job_handle_;
static uv_once_t uv_global_job_handle_init_guard_ = UV_ONCE_INIT;
May 16, 2013
May 16, 2013
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static void uv__init_global_job_handle(void) {
/* Create a job object and set it up to kill all contained processes when
* it's closed. Since this handle is made non-inheritable and we're not
* giving it to anyone, we're the only process holding a reference to it.
* That means that if this process exits it is closed and all the processes
* it contains are killed. All processes created with uv_spawn that are not
* spawned with the UV_PROCESS_DETACHED flag are assigned to this job.
*
* We're setting the JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK flag so only the
* processes that we explicitly add are affected, and *their* subprocesses
* are not. This ensures that our child processes are not limited in their
* ability to use job control on Windows versions that don't deal with
* nested jobs (prior to Windows 8 / Server 2012). It also lets our child
* processes created detached processes without explicitly breaking away
* from job control (which uv_spawn doesn't, either).
*/
May 14, 2013
May 14, 2013
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
SECURITY_ATTRIBUTES attr;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
memset(&attr, 0, sizeof attr);
attr.bInheritHandle = FALSE;
memset(&info, 0, sizeof info);
info.BasicLimitInformation.LimitFlags =
JOB_OBJECT_LIMIT_BREAKAWAY_OK |
JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK |
JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION |
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
uv_global_job_handle_ = CreateJobObjectW(&attr, NULL);
if (uv_global_job_handle_ == NULL)
uv_fatal_error(GetLastError(), "CreateJobObjectW");
if (!SetInformationJobObject(uv_global_job_handle_,
JobObjectExtendedLimitInformation,
&info,
sizeof info))
uv_fatal_error(GetLastError(), "SetInformationJobObject");
}
Jul 7, 2013
Jul 7, 2013
94
static int uv_utf8_to_utf16_alloc(const char* s, WCHAR** ws_ptr) {
Aug 13, 2012
Aug 13, 2012
95
96
97
98
99
100
101
102
103
104
int ws_len, r;
WCHAR* ws;
ws_len = MultiByteToWideChar(CP_UTF8,
0,
s,
-1,
NULL,
0);
if (ws_len <= 0) {
Jul 7, 2013
Jul 7, 2013
105
return GetLastError();
Aug 13, 2012
Aug 13, 2012
106
107
108
109
}
ws = (WCHAR*) malloc(ws_len * sizeof(WCHAR));
if (ws == NULL) {
Jul 7, 2013
Jul 7, 2013
110
return ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
111
112
113
114
115
116
117
118
119
120
121
}
r = MultiByteToWideChar(CP_UTF8,
0,
s,
-1,
ws,
ws_len);
assert(r == ws_len);
*ws_ptr = ws;
Jul 7, 2013
Jul 7, 2013
122
return 0;
Aug 13, 2012
Aug 13, 2012
123
}
Jul 30, 2011
Jul 30, 2011
124
125
Aug 31, 2011
Aug 31, 2011
126
static void uv_process_init(uv_loop_t* loop, uv_process_t* handle) {
Jun 12, 2012
Jun 12, 2012
127
uv__handle_init(loop, (uv_handle_t*) handle, UV_PROCESS);
Jul 30, 2011
Jul 30, 2011
128
129
130
131
132
handle->exit_cb = NULL;
handle->pid = 0;
handle->exit_signal = 0;
handle->wait_handle = INVALID_HANDLE_VALUE;
handle->process_handle = INVALID_HANDLE_VALUE;
Jun 1, 2012
Jun 1, 2012
133
handle->child_stdio_buffer = NULL;
Aug 13, 2012
Aug 13, 2012
134
handle->exit_cb_pending = 0;
Jul 30, 2011
Jul 30, 2011
135
Aug 31, 2011
Aug 31, 2011
136
uv_req_init(loop, (uv_req_t*)&handle->exit_req);
Jul 30, 2011
Jul 30, 2011
137
138
139
140
141
142
143
144
145
146
147
148
handle->exit_req.type = UV_PROCESS_EXIT;
handle->exit_req.data = handle;
}
/*
* Path search functions
*/
/*
* Helper function for search_path
*/
Aug 13, 2012
Aug 13, 2012
149
static WCHAR* search_path_join_test(const WCHAR* dir,
Aug 28, 2012
Aug 28, 2012
150
151
152
153
154
155
156
size_t dir_len,
const WCHAR* name,
size_t name_len,
const WCHAR* ext,
size_t ext_len,
const WCHAR* cwd,
size_t cwd_len) {
Aug 13, 2012
Aug 13, 2012
157
WCHAR *result, *result_pos;
Jul 30, 2011
Jul 30, 2011
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
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 13, 2012
Aug 13, 2012
183
result = result_pos = (WCHAR*)malloc(sizeof(WCHAR) *
Aug 31, 2011
Aug 31, 2011
184
(cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
Jul 30, 2011
Jul 30, 2011
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
/* 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
211
212
213
214
215
216
217
/* 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
218
219
220
221
222
223
224
225
226
227
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 &&
Jun 18, 2013
Jun 18, 2013
228
!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
Jul 30, 2011
Jul 30, 2011
229
230
231
232
233
234
235
236
237
238
239
return result;
}
free(result);
return NULL;
}
/*
* Helper function for search_path
*/
Aug 13, 2012
Aug 13, 2012
240
static WCHAR* path_search_walk_ext(const WCHAR *dir,
Aug 28, 2012
Aug 28, 2012
241
242
243
244
245
246
size_t dir_len,
const WCHAR *name,
size_t name_len,
WCHAR *cwd,
size_t cwd_len,
int name_has_ext) {
Aug 13, 2012
Aug 13, 2012
247
WCHAR* result;
Jul 30, 2011
Jul 30, 2011
248
Aug 18, 2011
Aug 18, 2011
249
/* If the name itself has a nonempty extension, try this extension first */
Jul 30, 2011
Jul 30, 2011
250
251
252
253
254
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
255
256
if (result != NULL) {
return result;
Jul 30, 2011
Jul 30, 2011
257
}
Aug 18, 2011
Aug 18, 2011
258
}
Jul 30, 2011
Jul 30, 2011
259
Aug 18, 2011
Aug 18, 2011
260
261
262
263
264
265
266
267
/* 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
268
Aug 18, 2011
Aug 18, 2011
269
270
271
272
273
274
275
/* 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
276
277
}
Aug 18, 2011
Aug 18, 2011
278
return NULL;
Jul 30, 2011
Jul 30, 2011
279
280
281
282
283
284
285
286
287
288
}
/*
* 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
289
290
291
292
* 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
293
294
295
296
297
298
299
*
* - 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
300
* - If filename specified has *any* extension, search for the file with the
Jul 30, 2011
Jul 30, 2011
301
302
303
* specified extension first.
*
* - If the literal filename is not found in a directory, try *appending*
Aug 18, 2011
Aug 18, 2011
304
* (not replacing) .com first and then .exe.
Jul 30, 2011
Jul 30, 2011
305
306
307
308
309
310
311
312
313
*
* - 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
314
* - When cmd.exe cannot read a directory, it will just skip it and go on
Jul 30, 2011
Jul 30, 2011
315
316
317
318
319
320
* 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
*/
Aug 13, 2012
Aug 13, 2012
321
322
323
static WCHAR* search_path(const WCHAR *file,
WCHAR *cwd,
const WCHAR *path) {
Jul 30, 2011
Jul 30, 2011
324
int file_has_dir;
Aug 13, 2012
Aug 13, 2012
325
326
327
328
WCHAR* result = NULL;
WCHAR *file_name_start;
WCHAR *dot;
const WCHAR *dir_start, *dir_end, *dir_path;
Aug 28, 2012
Aug 28, 2012
329
size_t dir_len;
Jul 30, 2011
Jul 30, 2011
330
331
int name_has_ext;
Aug 28, 2012
Aug 28, 2012
332
333
size_t file_len = wcslen(file);
size_t cwd_len = wcslen(cwd);
Jul 30, 2011
Jul 30, 2011
334
335
336
337
338
339
340
341
342
/* 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
343
344
/* Find the start of the filename so we can split the directory from the */
/* name. */
Aug 13, 2012
Aug 13, 2012
345
for (file_name_start = (WCHAR*)file + file_len;
Jul 30, 2011
Jul 30, 2011
346
347
348
349
350
351
352
353
354
355
356
357
358
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
359
/* The file has a path inside, don't use path */
Jul 30, 2011
Jul 30, 2011
360
361
362
363
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
364
name_has_ext);
Jul 30, 2011
Jul 30, 2011
365
366
} else {
Nov 4, 2011
Nov 4, 2011
367
dir_end = path;
Jul 30, 2011
Jul 30, 2011
368
369
370
371
372
/* 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
373
name_has_ext);
Jul 30, 2011
Jul 30, 2011
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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
413
414
file, file_len,
cwd, cwd_len,
Aug 18, 2011
Aug 18, 2011
415
name_has_ext);
Jul 30, 2011
Jul 30, 2011
416
417
418
419
420
421
}
}
return result;
}
Aug 9, 2011
Aug 9, 2011
422
Aug 5, 2011
Aug 5, 2011
423
424
425
426
/*
* Quotes command line arguments
* Returns a pointer to the end (next char to be written) of the buffer
*/
Aug 13, 2012
Aug 13, 2012
427
WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target) {
Aug 28, 2012
Aug 28, 2012
428
429
430
size_t len = wcslen(source);
size_t i;
int quote_hit;
Aug 13, 2012
Aug 13, 2012
431
WCHAR* start;
Aug 5, 2011
Aug 5, 2011
432
Aug 18, 2011
Aug 18, 2011
433
/*
Aug 5, 2011
Aug 5, 2011
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
* 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
449
/*
Aug 5, 2011
Aug 5, 2011
450
451
452
453
454
455
456
457
458
459
460
* 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
461
* Expected input/output:
Aug 5, 2011
Aug 5, 2011
462
463
464
465
466
* input : hello"world
* output: "hello\"world"
* input : hello""world
* output: "hello\"\"world"
* input : hello\world
Aug 5, 2011
Aug 5, 2011
467
* output: hello\world
Aug 5, 2011
Aug 5, 2011
468
* input : hello\\world
Aug 5, 2011
Aug 5, 2011
469
* output: hello\\world
Aug 5, 2011
Aug 5, 2011
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
* 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
499
Aug 9, 2011
Aug 9, 2011
500
Jul 7, 2013
Jul 7, 2013
501
int make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr) {
Jul 30, 2011
Jul 30, 2011
502
char** arg;
Aug 13, 2012
Aug 13, 2012
503
504
505
506
507
WCHAR* dst = NULL;
WCHAR* temp_buffer = NULL;
size_t dst_len = 0;
size_t temp_buffer_len = 0;
WCHAR* pos;
Jul 30, 2011
Jul 30, 2011
508
int arg_count = 0;
Jul 7, 2013
Jul 7, 2013
509
int err = 0;
Jul 30, 2011
Jul 30, 2011
510
511
512
/* Count the required size. */
for (arg = args; *arg; arg++) {
Aug 13, 2012
Aug 13, 2012
513
514
515
516
517
518
519
520
521
DWORD arg_len;
arg_len = MultiByteToWideChar(CP_UTF8,
0,
*arg,
-1,
NULL,
0);
if (arg_len == 0) {
Jul 7, 2013
Jul 7, 2013
522
return GetLastError();
Aug 13, 2012
Aug 13, 2012
523
524
525
526
527
528
529
}
dst_len += arg_len;
if (arg_len > temp_buffer_len)
temp_buffer_len = arg_len;
Jul 30, 2011
Jul 30, 2011
530
531
532
arg_count++;
}
Aug 13, 2012
Aug 13, 2012
533
/* Adjust for potential quotes. Also assume the worst-case scenario */
Aug 3, 2011
Aug 3, 2011
534
/* that every character needs escaping, so we need twice as much space. */
Aug 13, 2012
Aug 13, 2012
535
dst_len = dst_len * 2 + arg_count * 2;
Jul 30, 2011
Jul 30, 2011
536
Aug 13, 2012
Aug 13, 2012
537
538
539
/* Allocate buffer for the final command line. */
dst = (WCHAR*) malloc(dst_len * sizeof(WCHAR));
if (dst == NULL) {
Jul 7, 2013
Jul 7, 2013
540
err = ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
541
goto error;
Jul 30, 2011
Jul 30, 2011
542
543
}
Aug 13, 2012
Aug 13, 2012
544
545
546
/* Allocate temporary working buffer. */
temp_buffer = (WCHAR*) malloc(temp_buffer_len * sizeof(WCHAR));
if (temp_buffer == NULL) {
Jul 7, 2013
Jul 7, 2013
547
err = ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
548
goto error;
Aug 1, 2011
Aug 1, 2011
549
550
}
Aug 13, 2012
Aug 13, 2012
551
pos = dst;
Aug 1, 2011
Aug 1, 2011
552
for (arg = args; *arg; arg++) {
Aug 13, 2012
Aug 13, 2012
553
554
555
556
557
558
559
560
DWORD arg_len;
/* Convert argument to wide char. */
arg_len = MultiByteToWideChar(CP_UTF8,
0,
*arg,
-1,
temp_buffer,
Aug 28, 2012
Aug 28, 2012
561
(int) (dst + dst_len - pos));
Aug 13, 2012
Aug 13, 2012
562
if (arg_len == 0) {
Jul 7, 2013
Jul 7, 2013
563
err = GetLastError();
Aug 1, 2011
Aug 1, 2011
564
goto error;
Jul 30, 2011
Jul 30, 2011
565
}
Aug 13, 2012
Aug 13, 2012
566
Aug 5, 2011
Aug 5, 2011
567
if (verbatim_arguments) {
Aug 13, 2012
Aug 13, 2012
568
569
570
/* Copy verbatim. */
wcscpy(pos, temp_buffer);
pos += arg_len - 1;
Aug 5, 2011
Aug 5, 2011
571
} else {
Aug 13, 2012
Aug 13, 2012
572
573
/* Quote/escape, if needed. */
pos = quote_cmd_arg(temp_buffer, pos);
Aug 5, 2011
Aug 5, 2011
574
}
Aug 13, 2012
Aug 13, 2012
575
576
*pos++ = *(arg + 1) ? L' ' : L'\0';
Jul 30, 2011
Jul 30, 2011
577
578
}
Aug 13, 2012
Aug 13, 2012
579
580
581
free(temp_buffer);
*dst_ptr = dst;
Jul 7, 2013
Jul 7, 2013
582
return 0;
Aug 1, 2011
Aug 1, 2011
583
584
585
error:
free(dst);
Aug 13, 2012
Aug 13, 2012
586
587
free(temp_buffer);
return err;
Jul 30, 2011
Jul 30, 2011
588
589
}
Aug 9, 2011
Aug 9, 2011
590
591
592
593
594
/*
* 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
595
* issues associated with that solution; this is the caller's
Aug 9, 2011
Aug 9, 2011
596
597
* char**, and modifying it is rude.
*/
Aug 28, 2012
Aug 28, 2012
598
static void check_required_vars_contains_var(env_var_t* required, int count,
Aug 31, 2011
Aug 31, 2011
599
const char* var) {
Aug 9, 2011
Aug 9, 2011
600
int i;
Aug 28, 2012
Aug 28, 2012
601
for (i = 0; i < count; ++i) {
Aug 9, 2011
Aug 9, 2011
602
603
604
605
606
607
608
609
if (_strnicmp(required[i].narrow, var, required[i].len) == 0) {
required[i].supplied = 1;
return;
}
}
}
Jul 30, 2011
Jul 30, 2011
610
/*
Aug 9, 2011
Aug 9, 2011
611
612
613
* 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
614
*
Aug 9, 2011
Aug 9, 2011
615
616
617
618
619
620
* 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.
*/
Jul 7, 2013
Jul 7, 2013
621
int make_program_env(char* env_block[], WCHAR** dst_ptr) {
Aug 13, 2012
Aug 13, 2012
622
623
WCHAR* dst;
WCHAR* ptr;
Jul 30, 2011
Jul 30, 2011
624
char** env;
Aug 28, 2012
Aug 28, 2012
625
size_t env_len = 1; /* room for closing null */
Jul 30, 2011
Jul 30, 2011
626
int len;
Aug 9, 2011
Aug 9, 2011
627
628
629
630
631
632
633
634
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
635
636
for (env = env_block; *env; env++) {
Aug 28, 2012
Aug 28, 2012
637
int len;
Aug 31, 2011
Aug 31, 2011
638
check_required_vars_contains_var(required_vars,
Jan 18, 2012
Jan 18, 2012
639
ARRAY_SIZE(required_vars),
Aug 31, 2011
Aug 31, 2011
640
*env);
Aug 28, 2012
Aug 28, 2012
641
642
643
644
645
646
647
648
len = MultiByteToWideChar(CP_UTF8,
0,
*env,
-1,
NULL,
0);
if (len <= 0) {
Jul 7, 2013
Jul 7, 2013
649
return GetLastError();
Aug 28, 2012
Aug 28, 2012
650
651
652
}
env_len += len;
Jul 30, 2011
Jul 30, 2011
653
654
}
Jan 18, 2012
Jan 18, 2012
655
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
Aug 9, 2011
Aug 9, 2011
656
if (!required_vars[i].supplied) {
Aug 28, 2012
Aug 28, 2012
657
env_len += required_vars[i].len;
Aug 9, 2011
Aug 9, 2011
658
659
var_size = GetEnvironmentVariableW(required_vars[i].wide, NULL, 0);
if (var_size == 0) {
Jul 7, 2013
Jul 7, 2013
660
return GetLastError();
Aug 9, 2011
Aug 9, 2011
661
}
Aug 28, 2012
Aug 28, 2012
662
663
required_vars[i].value_len = var_size;
env_len += var_size;
Aug 9, 2011
Aug 9, 2011
664
665
666
}
}
Aug 28, 2012
Aug 28, 2012
667
dst = malloc(env_len * sizeof(WCHAR));
Jul 30, 2011
Jul 30, 2011
668
if (!dst) {
Jul 7, 2013
Jul 7, 2013
669
return ERROR_OUTOFMEMORY;
Jul 30, 2011
Jul 30, 2011
670
671
672
673
674
}
ptr = dst;
for (env = env_block; *env; env++, ptr += len) {
Aug 28, 2012
Aug 28, 2012
675
676
677
678
679
680
681
len = MultiByteToWideChar(CP_UTF8,
0,
*env,
-1,
ptr,
(int) (env_len - (ptr - dst)));
if (len <= 0) {
Jul 30, 2011
Jul 30, 2011
682
free(dst);
Jul 7, 2013
Jul 7, 2013
683
return GetLastError();
Jul 30, 2011
Jul 30, 2011
684
685
686
}
}
Jan 18, 2012
Jan 18, 2012
687
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
Aug 9, 2011
Aug 9, 2011
688
689
690
691
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
692
693
694
var_size = GetEnvironmentVariableW(required_vars[i].wide,
ptr,
required_vars[i].value_len);
Aug 9, 2011
Aug 9, 2011
695
696
697
698
699
700
701
if (var_size == 0) {
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
}
ptr += required_vars[i].value_len;
}
}
Aug 28, 2012
Aug 28, 2012
702
/* Terminate with an extra NULL. */
Jul 30, 2011
Jul 30, 2011
703
*ptr = L'\0';
Aug 28, 2012
Aug 28, 2012
704
705
*dst_ptr = dst;
Jul 7, 2013
Jul 7, 2013
706
return 0;
Jul 30, 2011
Jul 30, 2011
707
708
709
}
Aug 2, 2011
Aug 2, 2011
710
711
712
/*
* Called on Windows thread-pool thread to indicate that
* a child process has exited.
Jul 30, 2011
Jul 30, 2011
713
714
*/
static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
Aug 13, 2012
Aug 13, 2012
715
uv_process_t* process = (uv_process_t*) data;
Aug 31, 2011
Aug 31, 2011
716
uv_loop_t* loop = process->loop;
Aug 2, 2011
Aug 2, 2011
717
Jul 30, 2011
Jul 30, 2011
718
719
assert(didTimeout == FALSE);
assert(process);
Aug 13, 2012
Aug 13, 2012
720
assert(!process->exit_cb_pending);
Jul 30, 2011
Jul 30, 2011
721
Aug 13, 2012
Aug 13, 2012
722
process->exit_cb_pending = 1;
Aug 2, 2011
Aug 2, 2011
723
724
/* Post completed */
Aug 31, 2011
Aug 31, 2011
725
POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
Aug 2, 2011
Aug 2, 2011
726
727
728
}
Sep 6, 2011
Sep 6, 2011
729
730
/* Called on main thread after a child process has exited. */
void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle) {
Aug 24, 2013
Aug 24, 2013
731
int64_t exit_code;
Jul 7, 2013
Jul 7, 2013
732
DWORD status;
Jul 30, 2011
Jul 30, 2011
733
Aug 13, 2012
Aug 13, 2012
734
735
736
737
738
assert(handle->exit_cb_pending);
handle->exit_cb_pending = 0;
/* If we're closing, don't call the exit callback. Just schedule a close */
/* callback now. */
Sep 13, 2012
Sep 13, 2012
739
if (handle->flags & UV__HANDLE_CLOSING) {
Aug 13, 2012
Aug 13, 2012
740
uv_want_endgame(loop, (uv_handle_t*) handle);
May 17, 2012
May 17, 2012
741
742
743
return;
}
Jul 30, 2011
Jul 30, 2011
744
745
746
747
748
749
/* Unregister from process notification. */
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
UnregisterWait(handle->wait_handle);
handle->wait_handle = INVALID_HANDLE_VALUE;
}
May 17, 2012
May 17, 2012
750
751
752
753
/* Set the handle to inactive: no callbacks will be made after the exit */
/* callback.*/
uv__handle_stop(handle);
Nov 7, 2013
Nov 7, 2013
754
if (GetExitCodeProcess(handle->process_handle, &status)) {
Aug 24, 2013
Aug 24, 2013
755
756
exit_code = status;
} else {
Aug 13, 2012
Aug 13, 2012
757
/* Unable to to obtain the exit code. This should never happen. */
Jul 7, 2013
Jul 7, 2013
758
exit_code = uv_translate_sys_error(GetLastError());
Aug 13, 2012
Aug 13, 2012
759
760
}
Jul 30, 2011
Jul 30, 2011
761
762
763
764
765
766
767
/* Fire the exit callback. */
if (handle->exit_cb) {
handle->exit_cb(handle, exit_code, handle->exit_signal);
}
}
Aug 31, 2011
Aug 31, 2011
768
void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
Aug 27, 2012
Aug 27, 2012
769
uv__handle_closing(handle);
May 17, 2012
May 17, 2012
770
Jul 30, 2011
Jul 30, 2011
771
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
Aug 13, 2012
Aug 13, 2012
772
773
774
775
776
777
778
/* This blocks until either the wait was cancelled, or the callback has */
/* completed. */
BOOL r = UnregisterWaitEx(handle->wait_handle, INVALID_HANDLE_VALUE);
if (!r) {
/* This should never happen, and if it happens, we can't recover... */
uv_fatal_error(GetLastError(), "UnregisterWaitEx");
}
Jul 30, 2011
Jul 30, 2011
779
Aug 13, 2012
Aug 13, 2012
780
781
782
783
handle->wait_handle = INVALID_HANDLE_VALUE;
}
if (!handle->exit_cb_pending) {
Aug 31, 2011
Aug 31, 2011
784
uv_want_endgame(loop, (uv_handle_t*)handle);
Jul 30, 2011
Jul 30, 2011
785
786
787
788
}
}
May 17, 2012
May 17, 2012
789
void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
Aug 13, 2012
Aug 13, 2012
790
assert(!handle->exit_cb_pending);
Sep 13, 2012
Sep 13, 2012
791
assert(handle->flags & UV__HANDLE_CLOSING);
Aug 13, 2012
Aug 13, 2012
792
assert(!(handle->flags & UV_HANDLE_CLOSED));
May 17, 2012
May 17, 2012
793
Aug 13, 2012
Aug 13, 2012
794
795
/* Clean-up the process handle. */
CloseHandle(handle->process_handle);
Jun 1, 2012
Jun 1, 2012
796
Aug 13, 2012
Aug 13, 2012
797
uv__handle_close(handle);
May 17, 2012
May 17, 2012
798
799
800
}
Sep 1, 2013
Sep 1, 2013
801
802
803
int uv_spawn(uv_loop_t* loop,
uv_process_t* process,
const uv_process_options_t* options) {
Aug 13, 2012
Aug 13, 2012
804
int i;
Jul 7, 2013
Jul 7, 2013
805
int err = 0;
Aug 13, 2012
Aug 13, 2012
806
WCHAR* path = NULL;
Aug 31, 2011
Aug 31, 2011
807
BOOL result;
Aug 13, 2012
Aug 13, 2012
808
WCHAR* application_path = NULL, *application = NULL, *arguments = NULL,
Aug 28, 2012
Aug 28, 2012
809
*env = NULL, *cwd = NULL;
Jul 30, 2011
Jul 30, 2011
810
811
STARTUPINFOW startup;
PROCESS_INFORMATION info;
Jun 1, 2012
Jun 1, 2012
812
DWORD process_flags;
Aug 2, 2011
Aug 2, 2011
813
Feb 16, 2014
Feb 16, 2014
814
815
816
uv_process_init(loop, process);
process->exit_cb = options->exit_cb;
Sep 1, 2013
Sep 1, 2013
817
if (options->flags & (UV_PROCESS_SETGID | UV_PROCESS_SETUID)) {
Jul 7, 2013
Jul 7, 2013
818
return UV_ENOTSUP;
Sep 19, 2011
Sep 19, 2011
819
820
}
Sep 1, 2013
Sep 1, 2013
821
822
if (options->file == NULL ||
options->args == NULL) {
Jul 7, 2013
Jul 7, 2013
823
return UV_EINVAL;
Aug 13, 2012
Aug 13, 2012
824
825
}
Sep 1, 2013
Sep 1, 2013
826
827
828
829
830
831
assert(options->file != NULL);
assert(!(options->flags & ~(UV_PROCESS_DETACHED |
UV_PROCESS_SETGID |
UV_PROCESS_SETUID |
UV_PROCESS_WINDOWS_HIDE |
UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS)));
Apr 28, 2012
Apr 28, 2012
832
Sep 1, 2013
Sep 1, 2013
833
err = uv_utf8_to_utf16_alloc(options->file, &application);
Jul 7, 2013
Jul 7, 2013
834
if (err)
Dec 12, 2013
Dec 12, 2013
835
goto done;
Aug 13, 2012
Aug 13, 2012
836
Sep 1, 2013
Sep 1, 2013
837
838
839
840
err = make_program_args(
options->args,
options->flags & UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
&arguments);
Jul 7, 2013
Jul 7, 2013
841
if (err)
Dec 12, 2013
Dec 12, 2013
842
goto done;
Jul 30, 2011
Jul 30, 2011
843
Sep 1, 2013
Sep 1, 2013
844
845
if (options->env) {
err = make_program_env(options->env, &env);
Aug 23, 2013
Aug 23, 2013
846
if (err)
Dec 12, 2013
Dec 12, 2013
847
goto done;
Aug 28, 2012
Aug 28, 2012
848
849
}
Sep 1, 2013
Sep 1, 2013
850
if (options->cwd) {
Aug 13, 2012
Aug 13, 2012
851
/* Explicit cwd */
Sep 1, 2013
Sep 1, 2013
852
err = uv_utf8_to_utf16_alloc(options->cwd, &cwd);
Jul 7, 2013
Jul 7, 2013
853
if (err)
Dec 12, 2013
Dec 12, 2013
854
goto done;
Aug 13, 2012
Aug 13, 2012
855
Jul 30, 2011
Jul 30, 2011
856
} else {
Aug 13, 2012
Aug 13, 2012
857
858
/* Inherit cwd */
DWORD cwd_len, r;
Jun 1, 2012
Jun 1, 2012
859
Aug 13, 2012
Aug 13, 2012
860
861
cwd_len = GetCurrentDirectoryW(0, NULL);
if (!cwd_len) {
Jul 7, 2013
Jul 7, 2013
862
err = GetLastError();
Dec 12, 2013
Dec 12, 2013
863
goto done;
Aug 13, 2012
Aug 13, 2012
864
865
866
867
}
cwd = (WCHAR*) malloc(cwd_len * sizeof(WCHAR));
if (cwd == NULL) {
Jul 7, 2013
Jul 7, 2013
868
err = ERROR_OUTOFMEMORY;
Dec 12, 2013
Dec 12, 2013
869
goto done;
Aug 13, 2012
Aug 13, 2012
870
871
872
873
}
r = GetCurrentDirectoryW(cwd_len, cwd);
if (r == 0 || r >= cwd_len) {
Jul 7, 2013
Jul 7, 2013
874
err = GetLastError();
Dec 12, 2013
Dec 12, 2013
875
goto done;
Jul 30, 2011
Jul 30, 2011
876
877
878
}
}
Aug 23, 2013
Aug 23, 2013
879
/* Get PATH environment variable. */
Aug 13, 2012
Aug 13, 2012
880
881
882
883
884
{
DWORD path_len, r;
path_len = GetEnvironmentVariableW(L"PATH", NULL, 0);
if (path_len == 0) {
Jul 7, 2013
Jul 7, 2013
885
err = GetLastError();
Dec 12, 2013
Dec 12, 2013
886
goto done;
Aug 13, 2012
Aug 13, 2012
887
888
889
890
}
path = (WCHAR*) malloc(path_len * sizeof(WCHAR));
if (path == NULL) {
Jul 7, 2013
Jul 7, 2013
891
err = ERROR_OUTOFMEMORY;
Dec 12, 2013
Dec 12, 2013
892
goto done;
Aug 13, 2012
Aug 13, 2012
893
894
895
896
}
r = GetEnvironmentVariableW(L"PATH", path, path_len);
if (r == 0 || r >= path_len) {
Jul 7, 2013
Jul 7, 2013
897
err = GetLastError();
Dec 12, 2013
Dec 12, 2013
898
goto done;
Aug 13, 2012
Aug 13, 2012
899
}
Aug 1, 2011
Aug 1, 2011
900
901
}
Sep 1, 2013
Sep 1, 2013
902
err = uv__stdio_create(loop, options, &process->child_stdio_buffer);
Aug 23, 2013
Aug 23, 2013
903
if (err)
Dec 12, 2013
Dec 12, 2013
904
goto done;
Aug 23, 2013
Aug 23, 2013
905
Aug 2, 2011
Aug 2, 2011
906
application_path = search_path(application,
Jul 30, 2011
Jul 30, 2011
907
cwd,
Aug 18, 2011
Aug 18, 2011
908
path);
Aug 13, 2012
Aug 13, 2012
909
910
if (application_path == NULL) {
/* Not found. */
Jul 7, 2013
Jul 7, 2013
911
err = ERROR_FILE_NOT_FOUND;
Dec 12, 2013
Dec 12, 2013
912
goto done;
Jul 30, 2011
Jul 30, 2011
913
914
915
916
917
918
}
startup.cb = sizeof(startup);
startup.lpReserved = NULL;
startup.lpDesktop = NULL;
startup.lpTitle = NULL;
Nov 27, 2012
Nov 27, 2012
919
920
startup.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Jun 13, 2012
Jun 13, 2012
921
922
startup.cbReserved2 = uv__stdio_size(process->child_stdio_buffer);
startup.lpReserved2 = (BYTE*) process->child_stdio_buffer;
Nov 27, 2012
Nov 27, 2012
923
Jun 13, 2012
Jun 13, 2012
924
925
926
startup.hStdInput = uv__stdio_handle(process->child_stdio_buffer, 0);
startup.hStdOutput = uv__stdio_handle(process->child_stdio_buffer, 1);
startup.hStdError = uv__stdio_handle(process->child_stdio_buffer, 2);
Jul 30, 2011
Jul 30, 2011
927
Sep 1, 2013
Sep 1, 2013
928
if (options->flags & UV_PROCESS_WINDOWS_HIDE) {
Nov 27, 2012
Nov 27, 2012
929
930
931
932
933
934
/* Use SW_HIDE to avoid any potential process window. */
startup.wShowWindow = SW_HIDE;
} else {
startup.wShowWindow = SW_SHOWDEFAULT;
}
Jun 1, 2012
Jun 1, 2012
935
process_flags = CREATE_UNICODE_ENVIRONMENT;
May 16, 2013
May 16, 2013
936
Sep 1, 2013
Sep 1, 2013
937
if (options->flags & UV_PROCESS_DETACHED) {
May 16, 2013
May 16, 2013
938
939
940
941
942
943
944
945
946
947
/* Note that we're not setting the CREATE_BREAKAWAY_FROM_JOB flag. That
* means that libuv might not let you create a fully deamonized process
* when run under job control. However the type of job control that libuv
* itself creates doesn't trickle down to subprocesses so they can still
* daemonize.
*
* A reason to not do this is that CREATE_BREAKAWAY_FROM_JOB makes the
* CreateProcess call fail if we're under job control that doesn't allow
* breakaway.
*/
Jun 1, 2012
Jun 1, 2012
948
949
950
process_flags |= DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP;
}
Dec 12, 2013
Dec 12, 2013
951
if (!CreateProcessW(application_path,
Aug 2, 2011
Aug 2, 2011
952
953
954
arguments,
NULL,
NULL,
Feb 28, 2012
Feb 28, 2012
955
1,
Jun 1, 2012
Jun 1, 2012
956
process_flags,
Aug 2, 2011
Aug 2, 2011
957
958
959
960
env,
cwd,
&startup,
&info)) {
Dec 12, 2013
Dec 12, 2013
961
962
963
964
/* CreateProcessW failed. */
err = GetLastError();
goto done;
}
May 14, 2013
May 14, 2013
965
Dec 12, 2013
Dec 12, 2013
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/* Spawn succeeded */
/* Beyond this point, failure is reported asynchronously. */
process->process_handle = info.hProcess;
process->pid = info.dwProcessId;
/* If the process isn't spawned as detached, assign to the global job */
/* object so windows will kill it when the parent process dies. */
if (!(options->flags & UV_PROCESS_DETACHED)) {
uv_once(&uv_global_job_handle_init_guard_, uv__init_global_job_handle);
if (!AssignProcessToJobObject(uv_global_job_handle_, info.hProcess)) {
/* AssignProcessToJobObject might fail if this process is under job
* control and the job doesn't have the
* JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK flag set, on a Windows version
* that doesn't support nested jobs.
*
* When that happens we just swallow the error and continue without
* establishing a kill-child-on-parent-exit relationship, otherwise
* there would be no way for libuv applications run under job control
* to spawn processes at all.
*/
DWORD err = GetLastError();
if (err != ERROR_ACCESS_DENIED)
uv_fatal_error(err, "AssignProcessToJobObject");
Oct 6, 2011
Oct 6, 2011
991
}
Dec 12, 2013
Dec 12, 2013
992
}
Oct 6, 2011
Oct 6, 2011
993
Dec 12, 2013
Dec 12, 2013
994
995
996
997
998
999
1000
/* Set IPC pid to all IPC pipes. */
for (i = 0; i < options->stdio_count; i++) {
const uv_stdio_container_t* fdopt = &options->stdio[i];
if (fdopt->flags & UV_CREATE_PIPE &&
fdopt->data.stream->type == UV_NAMED_PIPE &&
((uv_pipe_t*) fdopt->data.stream)->ipc) {
((uv_pipe_t*) fdopt->data.stream)->ipc_pid = info.dwProcessId;