Skip to content

Latest commit

 

History

History
783 lines (651 loc) · 22.4 KB

process.c

File metadata and controls

783 lines (651 loc) · 22.4 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <windows.h>
#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))) { \
uv_set_sys_error(GetLastError()); \
err = -1; \
goto done; \
}
static const wchar_t DEFAULT_PATH[1] = L"";
static const wchar_t DEFAULT_PATH_EXT[20] = L".COM;.EXE;.BAT;.CMD";
static void uv_process_init(uv_process_t* handle) {
handle->type = UV_PROCESS;
handle->flags = 0;
handle->error = uv_ok_;
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;
handle->stdio_pipes[0].server_pipe = NULL;
handle->stdio_pipes[0].child_pipe = INVALID_HANDLE_VALUE;
handle->stdio_pipes[1].server_pipe = NULL;
handle->stdio_pipes[1].child_pipe = INVALID_HANDLE_VALUE;
handle->stdio_pipes[2].server_pipe = NULL;
handle->stdio_pipes[2].child_pipe = INVALID_HANDLE_VALUE;
uv_req_init((uv_req_t*)&handle->exit_req);
handle->exit_req.type = UV_PROCESS_EXIT;
handle->exit_req.data = handle;
uv_req_init((uv_req_t*)&handle->close_req);
handle->close_req.type = UV_PROCESS_CLOSE;
handle->close_req.data = handle;
uv_counters()->handle_init++;
uv_counters()->process_init++;
uv_ref();
}
/*
* 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 */
result = result_pos =
(wchar_t*)malloc(sizeof(wchar_t) * (cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
/* 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;
/* Copy extension */
if (ext_len) {
result_pos[0] = L'.';
result_pos++;
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,
const wchar_t *path_ext,
int name_has_ext) {
wchar_t* result = NULL;
const wchar_t *ext_start,
*ext_end = path_ext;
/* If the name itself has a nonemtpy extension, try this extension first */
if (name_has_ext) {
result = search_path_join_test(dir, dir_len,
name, name_len,
L"", 0,
cwd, cwd_len);
}
/* Add path_ext extensions and try to find a name that matches */
while (result == NULL) {
if (*ext_end == L'\0') {
break;
}
/* Skip the separator that ext_end now points to */
if (ext_end != path_ext) {
ext_end++;
}
/* Find the next dot in path_ext */
ext_start = wcschr(ext_end, L'.');
if (ext_start == NULL) {
break;
}
/* Skip the dot */
ext_start++;
/* Slice until we found a ; or alternatively a \0 */
ext_end = wcschr(ext_start, L';');
if (ext_end == NULL) {
ext_end = wcschr(ext_start, '\0');
}
result = search_path_join_test(dir, dir_len,
name, name_len,
ext_start, (ext_end - ext_start),
cwd, cwd_len);
}
return result;
}
/*
* 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.
*
* Furthermore, it tries to follow the semantics that cmd.exe uses as closely
* as possible:
*
* - Do not search the path if the filename already contains a path (either
* relative or absolute).
* (but do use path_ext)
*
* - If there's really only a filename, check the current directory for file,
* then search all path directories.
*
* - If filename specifies has *any* extension, search for the file with the
* specified extension first.
* (not necessary an executable one or one that appears in path_ext;
* *but* no extension or just a dot is *not* allowed)
*
* - If the literal filename is not found in a directory, try *appending*
* (not replacing) extensions from path_ext in the specified order.
* (an extension consisting of just a dot *may* appear in path_ext;
* unlike what happens if the specified filename ends with a dot,
* if path_ext specifies a single dot cmd.exe *does* look for an
* extension-less file)
*
* - 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.
*
* - Extensions path_ext portions must always start with a dot.
*
* - CMD does not trim leading/trailing whitespace from path/pathex entries
* nor from the environment variables as a whole.
*
* - When cmd.exe cannot read a directory, it wil just skip it and go on
* 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
* TODO: check with cmd what should happen when a pathext entry does not start
* with a dot
*/
static wchar_t* search_path(const wchar_t *file,
wchar_t *cwd,
const wchar_t *path,
const wchar_t *path_ext) {
int file_has_dir;
wchar_t* result = NULL;
wchar_t *file_name_start;
wchar_t *dot;
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;
}
/* Find the start of the filename so we can split the directory from the name */
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) {
/* The file has a path inside, don't use path (but do use path_ex) */
result = path_search_walk_ext(
file, file_name_start - file,
file_name_start, file_len - (file_name_start - file),
cwd, cwd_len,
path_ext, name_has_ext);
} else {
const wchar_t *dir_start,
*dir_end = path;
/* 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,
path_ext, name_has_ext);
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;
}
result = path_search_walk_ext(dir_start, dir_end - dir_start,
file, file_len,
cwd, cwd_len,
path_ext, name_has_ext);
}
}
return result;
}
static wchar_t* make_program_args(char** args) {
wchar_t* dst;
wchar_t* ptr;
char** arg;
size_t size = 0;
size_t len;
int arg_count = 0;
/* Count the required size. */
for (arg = args; *arg; arg++) {
size += (uv_utf8_to_utf16(*arg, NULL, 0) * sizeof(wchar_t));
arg_count++;
}
/* Arguments are separated with a space. */
if (arg_count > 0) {
size += arg_count - 1;
}
dst = (wchar_t*)malloc(size);
if (!dst) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
ptr = dst;
for (arg = args; *arg; arg++, ptr += len) {
len = uv_utf8_to_utf16(*arg, ptr, (size_t)(size - (ptr - dst)));
if (!len) {
free(dst);
return NULL;
}
if (*(arg + 1)) {
/* Replace with a space if there are more args. */
*((ptr + len) - 1) = L' ';
}
}
return dst;
}
/*
* 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.
* Get a pointer to the pathext and path environment variables as well,
* because search_path needs it. These are just pointers into env_win.
*/
wchar_t* make_program_env(char** env_block, const wchar_t **path, const wchar_t **path_ext) {
wchar_t* dst;
wchar_t* ptr;
char** env;
int env_len = 1 * sizeof(wchar_t); /* room for closing null */
int len;
for (env = env_block; *env; env++) {
env_len += (uv_utf8_to_utf16(*env, NULL, 0) * sizeof(wchar_t));
}
dst = (wchar_t*)malloc(env_len);
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;
}
/* Try to get a pointer to PATH and PATHEXT */
if (_wcsnicmp(L"PATH=", ptr, 5) == 0) {
*path = ptr + 5;
}
if (_wcsnicmp(L"PATHEXT=", ptr, 8) == 0) {
*path_ext = ptr + 8;
}
}
*ptr = L'\0';
return dst;
}
/*
* Called on Windows thread-pool thread to indicate that
* a child process has exited.
*/
static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
uv_process_t* process = (uv_process_t*)data;
assert(didTimeout == FALSE);
assert(process);
memset(&process->exit_req.overlapped, 0, sizeof(process->exit_req.overlapped));
/* Post completed */
if (!PostQueuedCompletionStatus(LOOP->iocp,
0,
0,
&process->exit_req.overlapped)) {
uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus");
}
}
/*
* Called on Windows thread-pool thread to indicate that
* UnregisterWaitEx has completed.
*/
static void CALLBACK close_wait_callback(void* data, BOOLEAN didTimeout) {
uv_process_t* process = (uv_process_t*)data;
assert(didTimeout == FALSE);
assert(process);
memset(&process->close_req.overlapped, 0, sizeof(process->close_req.overlapped));
/* Post completed */
if (!PostQueuedCompletionStatus(LOOP->iocp,
0,
0,
&process->close_req.overlapped)) {
uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus");
}
}
/* Called on main thread after a child process has exited. */
void uv_process_proc_exit(uv_process_t* handle) {
int i;
DWORD exit_code;
/* Close stdio handles. */
for (i = 0; i < COUNTOF(handle->stdio_pipes); i++) {
if (handle->stdio_pipes[i].child_pipe != INVALID_HANDLE_VALUE) {
CloseHandle(handle->stdio_pipes[i].child_pipe);
handle->stdio_pipes[i].child_pipe = INVALID_HANDLE_VALUE;
}
}
/* Unregister from process notification. */
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
UnregisterWait(handle->wait_handle);
handle->wait_handle = INVALID_HANDLE_VALUE;
}
/* Clean-up the process handle. */
if (handle->process_handle != INVALID_HANDLE_VALUE) {
/* Get the exit code. */
if (!GetExitCodeProcess(handle->process_handle, &exit_code)) {
exit_code = 1;
}
CloseHandle(handle->process_handle);
handle->process_handle = INVALID_HANDLE_VALUE;
}
/* Fire the exit callback. */
if (handle->exit_cb) {
handle->exit_cb(handle, exit_code, handle->exit_signal);
}
}
/* Called on main thread after UnregisterWaitEx finishes. */
void uv_process_proc_close(uv_process_t* handle) {
uv_want_endgame((uv_handle_t*)handle);
}
void uv_process_endgame(uv_process_t* handle) {
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);
}
uv_unref();
}
}
void uv_process_close(uv_process_t* handle) {
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 {
uv_want_endgame((uv_handle_t*)handle);
}
}
static int uv_create_stdio_pipe_pair(uv_pipe_t* server_pipe, HANDLE* child_pipe, DWORD server_access, DWORD child_access) {
int err;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
char pipe_name[64];
if (server_pipe->type != UV_NAMED_PIPE) {
uv_set_error(UV_EINVAL, 0);
err = -1;
goto done;
}
/* Create server pipe handle. */
err = uv_stdio_pipe_server(server_pipe, server_access, pipe_name, sizeof(pipe_name));
if (err) {
goto done;
}
/* Create child pipe handle. */
*child_pipe = CreateFileA(pipe_name,
child_access,
0,
&sa,
OPEN_EXISTING,
0,
NULL);
if (*child_pipe == INVALID_HANDLE_VALUE) {
uv_set_sys_error(GetLastError());
err = -1;
goto done;
}
/* 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) {
uv_set_sys_error(GetLastError());
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;
}
629
630
int uv_spawn(uv_process_t* process, uv_process_options_t options) {
Jul 30, 2011
Jul 30, 2011
631
632
633
634
635
636
637
638
639
640
641
642
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
int err, i;
const wchar_t* path = NULL;
const wchar_t* path_ext = NULL;
int size;
wchar_t* application_path, *application, *arguments, *env, *cwd;
STARTUPINFOW startup;
PROCESS_INFORMATION info;
uv_process_init(process);
process->exit_cb = options.exit_cb;
UTF8_TO_UTF16(options.file, application);
arguments = options.args ? make_program_args(options.args) : NULL;
env = options.env ? make_program_env(options.env, &path, &path_ext) : NULL;
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 {
uv_set_sys_error(GetLastError());
err = -1;
goto done;
}
}
application_path = search_path(application,
cwd,
path ? path : DEFAULT_PATH,
path_ext ? path_ext : DEFAULT_PATH_EXT);
if (!application_path) {
uv_set_error(UV_EINVAL, 0);
err = -1;
goto done;
}
/* Create stdio pipes. */
if (options.stdin_stream) {
err = uv_create_stdio_pipe_pair(options.stdin_stream, &process->stdio_pipes[0].child_pipe, PIPE_ACCESS_OUTBOUND, GENERIC_READ);
if (err) {
goto done;
}
process->stdio_pipes[0].server_pipe = options.stdin_stream;
}
if (options.stdout_stream) {
err = uv_create_stdio_pipe_pair(options.stdout_stream, &process->stdio_pipes[1].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE);
if (err) {
goto done;
}
process->stdio_pipes[1].server_pipe = options.stdout_stream;
}
if (options.stderr_stream) {
err = uv_create_stdio_pipe_pair(options.stderr_stream, &process->stdio_pipes[2].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE);
if (err) {
goto done;
}
process->stdio_pipes[2].server_pipe = options.stderr_stream;
}
startup.cb = sizeof(startup);
startup.lpReserved = NULL;
startup.lpDesktop = NULL;
startup.lpTitle = NULL;
startup.dwFlags = STARTF_USESTDHANDLES;
startup.cbReserved2 = 0;
startup.lpReserved2 = NULL;
startup.hStdInput = process->stdio_pipes[0].child_pipe;
startup.hStdOutput = process->stdio_pipes[1].child_pipe;
startup.hStdError = process->stdio_pipes[2].child_pipe;
if (!CreateProcessW(application_path,
arguments,
NULL,
NULL,
1,
CREATE_UNICODE_ENVIRONMENT,
env,
cwd,
&startup,
&info)) {
uv_set_sys_error(GetLastError());
err = -1;
goto done;
}
process->process_handle = info.hProcess;
process->pid = info.dwProcessId;
/* Setup notifications for when the child process exits. */
if (!RegisterWaitForSingleObject(&process->wait_handle, process->process_handle,
exit_wait_callback, (void*)process, INFINITE,
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) {
uv_set_sys_error(GetLastError());
err = -1;
goto done;
}
CloseHandle(info.hThread);
err = 0;
done:
free(application_path);
free(application);
free(arguments);
free(cwd);
free(env);
if (err) {
for (i = 0; i < COUNTOF(process->stdio_pipes); i++) {
if (process->stdio_pipes[i].child_pipe != INVALID_HANDLE_VALUE) {
CloseHandle(process->stdio_pipes[i].child_pipe);
process->stdio_pipes[i].child_pipe = INVALID_HANDLE_VALUE;
}
}
if (process->wait_handle != INVALID_HANDLE_VALUE) {
UnregisterWait(process->wait_handle);
process->wait_handle = INVALID_HANDLE_VALUE;
}
if (process->process_handle != INVALID_HANDLE_VALUE) {
CloseHandle(process->process_handle);
process->process_handle = INVALID_HANDLE_VALUE;
}
}
return err;
770
771
772
773
}
int uv_process_kill(uv_process_t* process, int signum) {
Jul 30, 2011
Jul 30, 2011
774
775
776
777
778
779
780
781
782
process->exit_signal = signum;
/* On windows killed processes normally return 1 */
if (process->process_handle != INVALID_HANDLE_VALUE &&
TerminateProcess(process->process_handle, 1)) {
return 0;
}
return -1;