Skip to content

Latest commit

 

History

History
1272 lines (1075 loc) · 34.8 KB

process.c

File metadata and controls

1272 lines (1075 loc) · 34.8 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>
Aug 1, 2014
Aug 1, 2014
28
#include <wchar.h>
Jun 2, 2015
Jun 2, 2015
29
#include <malloc.h> /* alloca */
Jun 2, 2012
Jun 2, 2012
30
31
32
#include "uv.h"
#include "internal.h"
Jun 2, 2012
Jun 2, 2012
33
#include "handle-inl.h"
Jun 2, 2012
Jun 2, 2012
34
#include "req-inl.h"
Jul 30, 2011
Jul 30, 2011
35
Jun 1, 2012
Jun 1, 2012
36
Nov 2, 2011
Nov 2, 2011
37
38
#define SIGKILL 9
Jun 1, 2012
Jun 1, 2012
39
Aug 9, 2011
Aug 9, 2011
40
typedef struct env_var {
Aug 1, 2014
Aug 1, 2014
41
42
43
const WCHAR* const wide;
const WCHAR* const wide_eq;
const size_t len; /* including null or '=' */
Aug 9, 2011
Aug 9, 2011
44
45
} env_var_t;
Aug 1, 2014
Aug 1, 2014
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define E_V(str) { L##str, L##str L"=", sizeof(str) }
static const env_var_t required_vars[] = { /* keep me sorted */
E_V("HOMEDRIVE"),
E_V("HOMEPATH"),
E_V("LOGONSERVER"),
E_V("PATH"),
E_V("SYSTEMDRIVE"),
E_V("SYSTEMROOT"),
E_V("TEMP"),
E_V("USERDOMAIN"),
E_V("USERNAME"),
E_V("USERPROFILE"),
E_V("WINDIR"),
};
static size_t n_required_vars = ARRAY_SIZE(required_vars);
Aug 9, 2011
Aug 9, 2011
62
Jun 1, 2012
Jun 1, 2012
63
May 14, 2013
May 14, 2013
64
65
66
67
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
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
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
109
static int uv_utf8_to_utf16_alloc(const char* s, WCHAR** ws_ptr) {
Aug 13, 2012
Aug 13, 2012
110
111
112
113
114
115
116
117
118
119
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
120
return GetLastError();
Aug 13, 2012
Aug 13, 2012
121
122
}
Jun 2, 2015
Jun 2, 2015
123
ws = (WCHAR*) uv__malloc(ws_len * sizeof(WCHAR));
Aug 13, 2012
Aug 13, 2012
124
if (ws == NULL) {
Jul 7, 2013
Jul 7, 2013
125
return ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
126
127
128
129
130
131
132
133
134
135
136
}
r = MultiByteToWideChar(CP_UTF8,
0,
s,
-1,
ws,
ws_len);
assert(r == ws_len);
*ws_ptr = ws;
Jul 7, 2013
Jul 7, 2013
137
return 0;
Aug 13, 2012
Aug 13, 2012
138
}
Jul 30, 2011
Jul 30, 2011
139
140
Aug 31, 2011
Aug 31, 2011
141
static void uv_process_init(uv_loop_t* loop, uv_process_t* handle) {
Jun 12, 2012
Jun 12, 2012
142
uv__handle_init(loop, (uv_handle_t*) handle, UV_PROCESS);
Jul 30, 2011
Jul 30, 2011
143
144
145
146
147
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
148
handle->child_stdio_buffer = NULL;
Aug 13, 2012
Aug 13, 2012
149
handle->exit_cb_pending = 0;
Jul 30, 2011
Jul 30, 2011
150
Mar 21, 2017
Mar 21, 2017
151
UV_REQ_INIT(&handle->exit_req, UV_PROCESS_EXIT);
Jul 30, 2011
Jul 30, 2011
152
153
154
155
156
157
158
159
160
161
162
handle->exit_req.data = handle;
}
/*
* Path search functions
*/
/*
* Helper function for search_path
*/
Aug 13, 2012
Aug 13, 2012
163
static WCHAR* search_path_join_test(const WCHAR* dir,
Aug 28, 2012
Aug 28, 2012
164
165
166
167
168
169
170
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
171
WCHAR *result, *result_pos;
Jul 30, 2011
Jul 30, 2011
172
DWORD attrs;
Aug 10, 2014
Aug 10, 2014
173
174
175
176
if (dir_len > 2 && dir[0] == L'\\' && dir[1] == L'\\') {
/* It's a UNC path so ignore cwd */
cwd_len = 0;
} else if (dir_len >= 1 && (dir[0] == L'/' || dir[0] == L'\\')) {
Jul 30, 2011
Jul 30, 2011
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 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 */
Jun 2, 2015
Jun 2, 2015
199
result = result_pos = (WCHAR*)uv__malloc(sizeof(WCHAR) *
Aug 31, 2011
Aug 31, 2011
200
(cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
Jul 30, 2011
Jul 30, 2011
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
/* 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
227
228
229
230
231
232
233
/* 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
234
235
236
237
238
239
240
241
242
243
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
244
!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
Jul 30, 2011
Jul 30, 2011
245
246
247
return result;
}
Jun 2, 2015
Jun 2, 2015
248
uv__free(result);
Jul 30, 2011
Jul 30, 2011
249
250
251
252
253
254
255
return NULL;
}
/*
* Helper function for search_path
*/
Aug 13, 2012
Aug 13, 2012
256
static WCHAR* path_search_walk_ext(const WCHAR *dir,
Aug 28, 2012
Aug 28, 2012
257
258
259
260
261
262
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
263
WCHAR* result;
Jul 30, 2011
Jul 30, 2011
264
Aug 18, 2011
Aug 18, 2011
265
/* If the name itself has a nonempty extension, try this extension first */
Jul 30, 2011
Jul 30, 2011
266
267
268
269
270
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
271
272
if (result != NULL) {
return result;
Jul 30, 2011
Jul 30, 2011
273
}
Aug 18, 2011
Aug 18, 2011
274
}
Jul 30, 2011
Jul 30, 2011
275
Aug 18, 2011
Aug 18, 2011
276
277
278
279
280
281
282
283
/* 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
284
Aug 18, 2011
Aug 18, 2011
285
286
287
288
289
290
291
/* 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
292
293
}
Aug 18, 2011
Aug 18, 2011
294
return NULL;
Jul 30, 2011
Jul 30, 2011
295
296
297
298
299
300
301
302
303
304
}
/*
* 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
305
306
307
308
* 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
309
310
311
312
313
314
315
*
* - 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
316
* - If filename specified has *any* extension, search for the file with the
Jul 30, 2011
Jul 30, 2011
317
318
319
* specified extension first.
*
* - If the literal filename is not found in a directory, try *appending*
Aug 18, 2011
Aug 18, 2011
320
* (not replacing) .com first and then .exe.
Jul 30, 2011
Jul 30, 2011
321
322
323
324
325
326
327
328
329
*
* - 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
330
* - When cmd.exe cannot read a directory, it will just skip it and go on
Jul 30, 2011
Jul 30, 2011
331
332
333
334
* 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.
*
Aug 10, 2014
Aug 10, 2014
335
336
337
338
339
* UNC path support: we are dealing with UNC paths in both the path and the
* filename. This is a deviation from what cmd.exe does (it does not let you
* start a program by specifying an UNC path on the command line) but this is
* really a pointless restriction.
*
Jul 30, 2011
Jul 30, 2011
340
*/
Aug 13, 2012
Aug 13, 2012
341
342
343
static WCHAR* search_path(const WCHAR *file,
WCHAR *cwd,
const WCHAR *path) {
Jul 30, 2011
Jul 30, 2011
344
int file_has_dir;
Aug 13, 2012
Aug 13, 2012
345
346
347
348
WCHAR* result = NULL;
WCHAR *file_name_start;
WCHAR *dot;
const WCHAR *dir_start, *dir_end, *dir_path;
Aug 28, 2012
Aug 28, 2012
349
size_t dir_len;
Jul 30, 2011
Jul 30, 2011
350
351
int name_has_ext;
Aug 28, 2012
Aug 28, 2012
352
353
size_t file_len = wcslen(file);
size_t cwd_len = wcslen(cwd);
Jul 30, 2011
Jul 30, 2011
354
355
356
357
358
359
360
361
362
/* 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
363
364
/* Find the start of the filename so we can split the directory from the */
/* name. */
Aug 13, 2012
Aug 13, 2012
365
for (file_name_start = (WCHAR*)file + file_len;
Jul 30, 2011
Jul 30, 2011
366
367
368
369
370
371
372
373
374
375
376
377
378
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
379
/* The file has a path inside, don't use path */
Jul 30, 2011
Jul 30, 2011
380
381
382
383
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
384
name_has_ext);
Jul 30, 2011
Jul 30, 2011
385
386
} else {
Nov 4, 2011
Nov 4, 2011
387
dir_end = path;
Jul 30, 2011
Jul 30, 2011
388
389
390
391
392
/* 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
393
name_has_ext);
Jul 30, 2011
Jul 30, 2011
394
395
396
397
398
399
400
while (result == NULL) {
if (*dir_end == L'\0') {
break;
}
/* Skip the separator that dir_end now points to */
Apr 10, 2014
Apr 10, 2014
401
if (dir_end != path || *path == L';') {
Jul 30, 2011
Jul 30, 2011
402
403
404
405
406
407
dir_end++;
}
/* Next slice starts just after where the previous one ended */
dir_start = dir_end;
Jul 27, 2017
Jul 27, 2017
408
409
410
411
412
413
414
/* If path is quoted, find quote end */
if (*dir_start == L'"' || *dir_start == L'\'') {
dir_end = wcschr(dir_start + 1, *dir_start);
if (dir_end == NULL) {
dir_end = wcschr(dir_start, L'\0');
}
}
Jul 30, 2011
Jul 30, 2011
415
/* Slice until the next ; or \0 is found */
Jul 27, 2017
Jul 27, 2017
416
dir_end = wcschr(dir_end, L';');
Jul 30, 2011
Jul 30, 2011
417
418
419
420
421
422
423
424
425
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
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
440
441
file, file_len,
cwd, cwd_len,
Aug 18, 2011
Aug 18, 2011
442
name_has_ext);
Jul 30, 2011
Jul 30, 2011
443
444
445
446
447
448
}
}
return result;
}
Aug 9, 2011
Aug 9, 2011
449
Aug 5, 2011
Aug 5, 2011
450
451
452
453
/*
* Quotes command line arguments
* Returns a pointer to the end (next char to be written) of the buffer
*/
Aug 13, 2012
Aug 13, 2012
454
WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target) {
Aug 28, 2012
Aug 28, 2012
455
456
457
size_t len = wcslen(source);
size_t i;
int quote_hit;
Aug 13, 2012
Aug 13, 2012
458
WCHAR* start;
Aug 5, 2011
Aug 5, 2011
459
460
if (len == 0) {
May 12, 2014
May 12, 2014
461
462
463
/* Need double quotation for empty argument */
*(target++) = L'"';
*(target++) = L'"';
Aug 5, 2011
Aug 5, 2011
464
465
466
467
468
469
470
471
472
473
474
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
475
/*
Aug 5, 2011
Aug 5, 2011
476
477
478
479
480
481
482
483
484
485
486
* 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
487
* Expected input/output:
Aug 5, 2011
Aug 5, 2011
488
489
490
491
492
* input : hello"world
* output: "hello\"world"
* input : hello""world
* output: "hello\"\"world"
* input : hello\world
Aug 5, 2011
Aug 5, 2011
493
* output: hello\world
Aug 5, 2011
Aug 5, 2011
494
* input : hello\\world
Aug 5, 2011
Aug 5, 2011
495
* output: hello\\world
Aug 5, 2011
Aug 5, 2011
496
497
498
499
500
* input : hello\"world
* output: "hello\\\"world"
* input : hello\\"world
* output: "hello\\\\\"world"
* input : hello world\
Dec 1, 2016
Dec 1, 2016
501
* output: "hello world\\"
Aug 5, 2011
Aug 5, 2011
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
*/
*(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
525
Aug 9, 2011
Aug 9, 2011
526
Jul 7, 2013
Jul 7, 2013
527
int make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr) {
Jul 30, 2011
Jul 30, 2011
528
char** arg;
Aug 13, 2012
Aug 13, 2012
529
530
531
532
533
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
534
int arg_count = 0;
Jul 7, 2013
Jul 7, 2013
535
int err = 0;
Jul 30, 2011
Jul 30, 2011
536
537
538
/* Count the required size. */
for (arg = args; *arg; arg++) {
Aug 13, 2012
Aug 13, 2012
539
540
541
542
543
544
545
546
547
DWORD arg_len;
arg_len = MultiByteToWideChar(CP_UTF8,
0,
*arg,
-1,
NULL,
0);
if (arg_len == 0) {
Jul 7, 2013
Jul 7, 2013
548
return GetLastError();
Aug 13, 2012
Aug 13, 2012
549
550
551
552
553
554
555
}
dst_len += arg_len;
if (arg_len > temp_buffer_len)
temp_buffer_len = arg_len;
Jul 30, 2011
Jul 30, 2011
556
557
558
arg_count++;
}
Aug 13, 2012
Aug 13, 2012
559
/* Adjust for potential quotes. Also assume the worst-case scenario */
Aug 3, 2011
Aug 3, 2011
560
/* that every character needs escaping, so we need twice as much space. */
Aug 13, 2012
Aug 13, 2012
561
dst_len = dst_len * 2 + arg_count * 2;
Jul 30, 2011
Jul 30, 2011
562
Aug 13, 2012
Aug 13, 2012
563
/* Allocate buffer for the final command line. */
Jun 2, 2015
Jun 2, 2015
564
dst = (WCHAR*) uv__malloc(dst_len * sizeof(WCHAR));
Aug 13, 2012
Aug 13, 2012
565
if (dst == NULL) {
Jul 7, 2013
Jul 7, 2013
566
err = ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
567
goto error;
Jul 30, 2011
Jul 30, 2011
568
569
}
Aug 13, 2012
Aug 13, 2012
570
/* Allocate temporary working buffer. */
Jun 2, 2015
Jun 2, 2015
571
temp_buffer = (WCHAR*) uv__malloc(temp_buffer_len * sizeof(WCHAR));
Aug 13, 2012
Aug 13, 2012
572
if (temp_buffer == NULL) {
Jul 7, 2013
Jul 7, 2013
573
err = ERROR_OUTOFMEMORY;
Aug 13, 2012
Aug 13, 2012
574
goto error;
Aug 1, 2011
Aug 1, 2011
575
576
}
Aug 13, 2012
Aug 13, 2012
577
pos = dst;
Aug 1, 2011
Aug 1, 2011
578
for (arg = args; *arg; arg++) {
Aug 13, 2012
Aug 13, 2012
579
580
581
582
583
584
585
586
DWORD arg_len;
/* Convert argument to wide char. */
arg_len = MultiByteToWideChar(CP_UTF8,
0,
*arg,
-1,
temp_buffer,
Aug 28, 2012
Aug 28, 2012
587
(int) (dst + dst_len - pos));
Aug 13, 2012
Aug 13, 2012
588
if (arg_len == 0) {
Jul 7, 2013
Jul 7, 2013
589
err = GetLastError();
Aug 1, 2011
Aug 1, 2011
590
goto error;
Jul 30, 2011
Jul 30, 2011
591
}
Aug 13, 2012
Aug 13, 2012
592
Aug 5, 2011
Aug 5, 2011
593
if (verbatim_arguments) {
Aug 13, 2012
Aug 13, 2012
594
595
596
/* Copy verbatim. */
wcscpy(pos, temp_buffer);
pos += arg_len - 1;
Aug 5, 2011
Aug 5, 2011
597
} else {
Aug 13, 2012
Aug 13, 2012
598
599
/* Quote/escape, if needed. */
pos = quote_cmd_arg(temp_buffer, pos);
Aug 5, 2011
Aug 5, 2011
600
}
Aug 13, 2012
Aug 13, 2012
601
602
*pos++ = *(arg + 1) ? L' ' : L'\0';
Jul 30, 2011
Jul 30, 2011
603
604
}
Jun 2, 2015
Jun 2, 2015
605
uv__free(temp_buffer);
Aug 13, 2012
Aug 13, 2012
606
607
*dst_ptr = dst;
Jul 7, 2013
Jul 7, 2013
608
return 0;
Aug 1, 2011
Aug 1, 2011
609
610
error:
Jun 2, 2015
Jun 2, 2015
611
612
uv__free(dst);
uv__free(temp_buffer);
Aug 13, 2012
Aug 13, 2012
613
return err;
Jul 30, 2011
Jul 30, 2011
614
615
}
Aug 9, 2011
Aug 9, 2011
616
Aug 1, 2014
Aug 1, 2014
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
int env_strncmp(const wchar_t* a, int na, const wchar_t* b) {
wchar_t* a_eq;
wchar_t* b_eq;
wchar_t* A;
wchar_t* B;
int nb;
int r;
if (na < 0) {
a_eq = wcschr(a, L'=');
assert(a_eq);
na = (int)(long)(a_eq - a);
} else {
na--;
}
b_eq = wcschr(b, L'=');
assert(b_eq);
nb = b_eq - b;
A = alloca((na+1) * sizeof(wchar_t));
B = alloca((nb+1) * sizeof(wchar_t));
r = LCMapStringW(LOCALE_INVARIANT, LCMAP_UPPERCASE, a, na, A, na);
assert(r==na);
A[na] = L'\0';
r = LCMapStringW(LOCALE_INVARIANT, LCMAP_UPPERCASE, b, nb, B, nb);
assert(r==nb);
B[nb] = L'\0';
while (1) {
wchar_t AA = *A++;
wchar_t BB = *B++;
if (AA < BB) {
return -1;
} else if (AA > BB) {
return 1;
} else if (!AA && !BB) {
return 0;
Aug 9, 2011
Aug 9, 2011
655
656
657
658
659
}
}
}
Aug 1, 2014
Aug 1, 2014
660
661
662
663
664
665
666
static int qsort_wcscmp(const void *a, const void *b) {
wchar_t* astr = *(wchar_t* const*)a;
wchar_t* bstr = *(wchar_t* const*)b;
return env_strncmp(astr, -1, bstr);
}
Jul 30, 2011
Jul 30, 2011
667
/*
Aug 9, 2011
Aug 9, 2011
668
669
670
* 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
671
*
Aug 9, 2011
Aug 9, 2011
672
673
674
675
676
* 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, 2014
Aug 1, 2014
677
678
679
680
681
*
* Also add variables known to Cygwin to be required for correct
* subprocess operation in many cases:
* https://github.com/Alexpux/Cygwin/blob/b266b04fbbd3a595f02ea149e4306d3ab9b1fe3d/winsup/cygwin/environ.cc#L955
*
Aug 9, 2011
Aug 9, 2011
682
*/
Jul 7, 2013
Jul 7, 2013
683
int make_program_env(char* env_block[], WCHAR** dst_ptr) {
Aug 13, 2012
Aug 13, 2012
684
685
WCHAR* dst;
WCHAR* ptr;
Jul 30, 2011
Jul 30, 2011
686
char** env;
Aug 1, 2014
Aug 1, 2014
687
size_t env_len = 0;
Jul 30, 2011
Jul 30, 2011
688
int len;
Mar 7, 2014
Mar 7, 2014
689
size_t i;
Aug 9, 2011
Aug 9, 2011
690
DWORD var_size;
Aug 1, 2014
Aug 1, 2014
691
692
693
694
695
size_t env_block_count = 1; /* 1 for null-terminator */
WCHAR* dst_copy;
WCHAR** ptr_copy;
WCHAR** env_copy;
DWORD* required_vars_value_len = alloca(n_required_vars * sizeof(DWORD*));
Aug 9, 2011
Aug 9, 2011
696
Aug 1, 2014
Aug 1, 2014
697
/* first pass: determine size in UTF-16 */
Jul 30, 2011
Jul 30, 2011
698
for (env = env_block; *env; env++) {
Aug 28, 2012
Aug 28, 2012
699
int len;
Aug 1, 2014
Aug 1, 2014
700
701
702
703
704
705
706
707
708
709
710
711
if (strchr(*env, '=')) {
len = MultiByteToWideChar(CP_UTF8,
0,
*env,
-1,
NULL,
0);
if (len <= 0) {
return GetLastError();
}
env_len += len;
env_block_count++;
Aug 28, 2012
Aug 28, 2012
712
}
Aug 1, 2014
Aug 1, 2014
713
714
715
}
/* second pass: copy to UTF-16 environment block */
Jun 2, 2015
Jun 2, 2015
716
dst_copy = (WCHAR*)uv__malloc(env_len * sizeof(WCHAR));
Aug 1, 2014
Aug 1, 2014
717
718
719
720
if (!dst_copy) {
return ERROR_OUTOFMEMORY;
}
env_copy = alloca(env_block_count * sizeof(WCHAR*));
Aug 28, 2012
Aug 28, 2012
721
Aug 1, 2014
Aug 1, 2014
722
723
724
725
726
727
728
729
730
731
732
733
ptr = dst_copy;
ptr_copy = env_copy;
for (env = env_block; *env; env++) {
if (strchr(*env, '=')) {
len = MultiByteToWideChar(CP_UTF8,
0,
*env,
-1,
ptr,
(int) (env_len - (ptr - dst_copy)));
if (len <= 0) {
DWORD err = GetLastError();
Jun 2, 2015
Jun 2, 2015
734
uv__free(dst_copy);
Aug 1, 2014
Aug 1, 2014
735
736
737
738
739
return err;
}
*ptr_copy++ = ptr;
ptr += len;
}
Jul 30, 2011
Jul 30, 2011
740
}
Aug 1, 2014
Aug 1, 2014
741
742
743
744
745
*ptr_copy = NULL;
assert(env_len == ptr - dst_copy);
/* sort our (UTF-16) copy */
qsort(env_copy, env_block_count-1, sizeof(wchar_t*), qsort_wcscmp);
Jul 30, 2011
Jul 30, 2011
746
Aug 1, 2014
Aug 1, 2014
747
748
749
750
751
752
753
754
755
756
757
758
/* third pass: check for required variables */
for (ptr_copy = env_copy, i = 0; i < n_required_vars; ) {
int cmp;
if (!*ptr_copy) {
cmp = -1;
} else {
cmp = env_strncmp(required_vars[i].wide_eq,
required_vars[i].len,
*ptr_copy);
}
if (cmp < 0) {
/* missing required var */
Aug 9, 2011
Aug 9, 2011
759
var_size = GetEnvironmentVariableW(required_vars[i].wide, NULL, 0);
Aug 1, 2014
Aug 1, 2014
760
required_vars_value_len[i] = var_size;
Aug 1, 2014
Aug 1, 2014
761
762
763
764
if (var_size != 0) {
env_len += required_vars[i].len;
env_len += var_size;
}
Aug 1, 2014
Aug 1, 2014
765
766
767
768
769
i++;
} else {
ptr_copy++;
if (cmp == 0)
i++;
Aug 9, 2011
Aug 9, 2011
770
771
772
}
}
Aug 1, 2014
Aug 1, 2014
773
/* final pass: copy, in sort order, and inserting required variables */
Jun 2, 2015
Jun 2, 2015
774
dst = uv__malloc((1+env_len) * sizeof(WCHAR));
Jul 30, 2011
Jul 30, 2011
775
if (!dst) {
Jun 2, 2015
Jun 2, 2015
776
uv__free(dst_copy);
Jul 7, 2013
Jul 7, 2013
777
return ERROR_OUTOFMEMORY;
Jul 30, 2011
Jul 30, 2011
778
779
}
Aug 1, 2014
Aug 1, 2014
780
781
782
783
784
785
786
787
788
789
790
791
for (ptr = dst, ptr_copy = env_copy, i = 0;
*ptr_copy || i < n_required_vars;
ptr += len) {
int cmp;
if (i >= n_required_vars) {
cmp = 1;
} else if (!*ptr_copy) {
cmp = -1;
} else {
cmp = env_strncmp(required_vars[i].wide_eq,
required_vars[i].len,
*ptr_copy);
Jul 30, 2011
Jul 30, 2011
792
}
Aug 1, 2014
Aug 1, 2014
793
794
795
796
797
798
799
800
801
802
803
804
if (cmp < 0) {
/* missing required var */
len = required_vars_value_len[i];
if (len) {
wcscpy(ptr, required_vars[i].wide_eq);
ptr += required_vars[i].len;
var_size = GetEnvironmentVariableW(required_vars[i].wide,
ptr,
(int) (env_len - (ptr - dst)));
if (var_size != len-1) { /* race condition? */
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
}
Aug 9, 2011
Aug 9, 2011
805
}
Aug 1, 2014
Aug 1, 2014
806
807
808
809
i++;
} else {
/* copy var from env_block */
len = wcslen(*ptr_copy) + 1;
Aug 10, 2014
Aug 10, 2014
810
wmemcpy(ptr, *ptr_copy, len);
Aug 1, 2014
Aug 1, 2014
811
812
813
ptr_copy++;
if (cmp == 0)
i++;
Aug 9, 2011
Aug 9, 2011
814
815
816
}
}
Aug 28, 2012
Aug 28, 2012
817
/* Terminate with an extra NULL. */
Aug 1, 2014
Aug 1, 2014
818
assert(env_len == (ptr - dst));
Jul 30, 2011
Jul 30, 2011
819
*ptr = L'\0';
Aug 28, 2012
Aug 28, 2012
820
Jun 2, 2015
Jun 2, 2015
821
uv__free(dst_copy);
Aug 28, 2012
Aug 28, 2012
822
*dst_ptr = dst;
Jul 7, 2013
Jul 7, 2013
823
return 0;
Jul 30, 2011
Jul 30, 2011
824
825
}
Aug 6, 2014
Aug 6, 2014
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
* Attempt to find the value of the PATH environment variable in the child's
* preprocessed environment.
*
* If found, a pointer into `env` is returned. If not found, NULL is returned.
*/
static WCHAR* find_path(WCHAR *env) {
for (; env != NULL && *env != 0; env += wcslen(env) + 1) {
if (wcsncmp(env, L"PATH=", 5) == 0)
return &env[5];
}
return NULL;
}
Jul 30, 2011
Jul 30, 2011
840
Aug 2, 2011
Aug 2, 2011
841
842
843
/*
* Called on Windows thread-pool thread to indicate that
* a child process has exited.
Jul 30, 2011
Jul 30, 2011
844
845
*/
static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
Aug 13, 2012
Aug 13, 2012
846
uv_process_t* process = (uv_process_t*) data;
Aug 31, 2011
Aug 31, 2011
847
uv_loop_t* loop = process->loop;
Aug 2, 2011
Aug 2, 2011
848
Jul 30, 2011
Jul 30, 2011
849
850
assert(didTimeout == FALSE);
assert(process);
Aug 13, 2012
Aug 13, 2012
851
assert(!process->exit_cb_pending);
Jul 30, 2011
Jul 30, 2011
852
Aug 13, 2012
Aug 13, 2012
853
process->exit_cb_pending = 1;
Aug 2, 2011
Aug 2, 2011
854
855
/* Post completed */
Aug 31, 2011
Aug 31, 2011
856
POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
Aug 2, 2011
Aug 2, 2011
857
858
859
}
Sep 6, 2011
Sep 6, 2011
860
861
/* 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
862
int64_t exit_code;
Jul 7, 2013
Jul 7, 2013
863
DWORD status;
Jul 30, 2011
Jul 30, 2011
864
Aug 13, 2012
Aug 13, 2012
865
866
867
868
869
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
870
if (handle->flags & UV__HANDLE_CLOSING) {
Aug 13, 2012
Aug 13, 2012
871
uv_want_endgame(loop, (uv_handle_t*) handle);
May 17, 2012
May 17, 2012
872
873
874
return;
}
Jul 30, 2011
Jul 30, 2011
875
876
877
878
879
880
/* 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
881
882
883
884
/* Set the handle to inactive: no callbacks will be made after the exit */
/* callback.*/
uv__handle_stop(handle);
Nov 7, 2013
Nov 7, 2013
885
if (GetExitCodeProcess(handle->process_handle, &status)) {
Aug 24, 2013
Aug 24, 2013
886
887
exit_code = status;
} else {
Aug 13, 2012
Aug 13, 2012
888
/* Unable to to obtain the exit code. This should never happen. */
Jul 7, 2013
Jul 7, 2013
889
exit_code = uv_translate_sys_error(GetLastError());
Aug 13, 2012
Aug 13, 2012
890
891
}
Jul 30, 2011
Jul 30, 2011
892
893
894
895
896
897
898
/* Fire the exit callback. */
if (handle->exit_cb) {
handle->exit_cb(handle, exit_code, handle->exit_signal);
}
}
Aug 31, 2011
Aug 31, 2011
899
void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
Aug 27, 2012
Aug 27, 2012
900
uv__handle_closing(handle);
May 17, 2012
May 17, 2012
901
Jul 30, 2011
Jul 30, 2011
902
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
Aug 13, 2012
Aug 13, 2012
903
904
905
906
907
908
909
/* 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
910
Aug 13, 2012
Aug 13, 2012
911
912
913
914
handle->wait_handle = INVALID_HANDLE_VALUE;
}
if (!handle->exit_cb_pending) {
Aug 31, 2011
Aug 31, 2011
915
uv_want_endgame(loop, (uv_handle_t*)handle);
Jul 30, 2011
Jul 30, 2011
916
917
918
919
}
}
May 17, 2012
May 17, 2012
920
void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
Aug 13, 2012
Aug 13, 2012
921
assert(!handle->exit_cb_pending);
Sep 13, 2012
Sep 13, 2012
922
assert(handle->flags & UV__HANDLE_CLOSING);
Aug 13, 2012
Aug 13, 2012
923
assert(!(handle->flags & UV_HANDLE_CLOSED));
May 17, 2012
May 17, 2012
924
Aug 13, 2012
Aug 13, 2012
925
926
/* Clean-up the process handle. */
CloseHandle(handle->process_handle);
Jun 1, 2012
Jun 1, 2012
927
Aug 13, 2012
Aug 13, 2012
928
uv__handle_close(handle);
May 17, 2012
May 17, 2012
929
930
931
}
Sep 1, 2013
Sep 1, 2013
932
933
934
int uv_spawn(uv_loop_t* loop,
uv_process_t* process,
const uv_process_options_t* options) {
Aug 13, 2012
Aug 13, 2012
935
int i;
Jul 7, 2013
Jul 7, 2013
936
int err = 0;
Aug 6, 2014
Aug 6, 2014
937
WCHAR* path = NULL, *alloc_path = NULL;
Aug 31, 2011
Aug 31, 2011
938
BOOL result;
Aug 13, 2012
Aug 13, 2012
939
WCHAR* application_path = NULL, *application = NULL, *arguments = NULL,
Aug 28, 2012
Aug 28, 2012
940
*env = NULL, *cwd = NULL;
Jul 30, 2011
Jul 30, 2011
941
942
STARTUPINFOW startup;
PROCESS_INFORMATION info;
Jun 1, 2012
Jun 1, 2012
943
DWORD process_flags;
Aug 2, 2011
Aug 2, 2011
944
Feb 16, 2014
Feb 16, 2014
945
946
947
uv_process_init(loop, process);
process->exit_cb = options->exit_cb;
Sep 1, 2013
Sep 1, 2013
948
if (options->flags & (UV_PROCESS_SETGID | UV_PROCESS_SETUID)) {
Jul 7, 2013
Jul 7, 2013
949
return UV_ENOTSUP;
Sep 19, 2011
Sep 19, 2011
950
951
}
Sep 1, 2013
Sep 1, 2013
952
953
if (options->file == NULL ||
options->args == NULL) {
Jul 7, 2013
Jul 7, 2013
954
return UV_EINVAL;
Aug 13, 2012
Aug 13, 2012
955
956
}
Sep 1, 2013
Sep 1, 2013
957
958
959
960
961
962
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
963
Sep 1, 2013
Sep 1, 2013
964
err = uv_utf8_to_utf16_alloc(options->file, &application);
Jul 7, 2013
Jul 7, 2013
965
if (err)
Dec 12, 2013
Dec 12, 2013
966
goto done;
Aug 13, 2012
Aug 13, 2012
967
Sep 1, 2013
Sep 1, 2013
968
969
970
971
err = make_program_args(
options->args,
options->flags & UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
&arguments);
Jul 7, 2013
Jul 7, 2013
972
if (err)
Dec 12, 2013
Dec 12, 2013
973
goto done;
Jul 30, 2011
Jul 30, 2011
974
Sep 1, 2013
Sep 1, 2013
975
976
if (options->env) {
err = make_program_env(options->env, &env);
Aug 23, 2013
Aug 23, 2013
977
if (err)
Dec 12, 2013
Dec 12, 2013
978
goto done;
Aug 28, 2012
Aug 28, 2012
979
980
}
Sep 1, 2013
Sep 1, 2013
981
if (options->cwd) {
Aug 13, 2012
Aug 13, 2012
982
/* Explicit cwd */
Sep 1, 2013
Sep 1, 2013
983
err = uv_utf8_to_utf16_alloc(options->cwd, &cwd);
Jul 7, 2013
Jul 7, 2013
984
if (err)
Dec 12, 2013
Dec 12, 2013
985
goto done;
Aug 13, 2012
Aug 13, 2012
986
Jul 30, 2011
Jul 30, 2011
987
} else {
Aug 13, 2012
Aug 13, 2012
988
989
/* Inherit cwd */
DWORD cwd_len, r;
Jun 1, 2012
Jun 1, 2012
990
Aug 13, 2012
Aug 13, 2012
991
992
cwd_len = GetCurrentDirectoryW(0, NULL);
if (!cwd_len) {
Jul 7, 2013
Jul 7, 2013
993
err = GetLastError();
Dec 12, 2013
Dec 12, 2013
994
goto done;
Aug 13, 2012
Aug 13, 2012
995
996
}
Jun 2, 2015
Jun 2, 2015
997
cwd = (WCHAR*) uv__malloc(cwd_len * sizeof(WCHAR));
Aug 13, 2012
Aug 13, 2012
998
if (cwd == NULL) {
Jul 7, 2013
Jul 7, 2013
999
err = ERROR_OUTOFMEMORY;
Dec 12, 2013
Dec 12, 2013
1000
goto done;