foolio 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +21 -0
- data/examples/timer.rb +20 -0
- data/ext/foolio/extconf.rb +34 -0
- data/ext/foolio/foolio_ext.c +921 -0
- data/ext/foolio/gen.rb +50 -0
- data/ext/foolio/make_table.rb +12 -0
- data/ext/foolio/templ +243 -0
- data/ext/libuv/.gitignore +33 -0
- data/ext/libuv/.mailmap +13 -0
- data/ext/libuv/.travis.yml +9 -0
- data/ext/libuv/AUTHORS +61 -0
- data/ext/libuv/LICENSE +44 -0
- data/ext/libuv/Makefile +71 -0
- data/ext/libuv/README.md +90 -0
- data/ext/libuv/common.gypi +178 -0
- data/ext/libuv/gyp_uv +73 -0
- data/ext/libuv/include/uv-private/eio.h +403 -0
- data/ext/libuv/include/uv-private/ev.h +838 -0
- data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
- data/ext/libuv/include/uv-private/tree.h +768 -0
- data/ext/libuv/include/uv-private/uv-unix.h +324 -0
- data/ext/libuv/include/uv-private/uv-win.h +517 -0
- data/ext/libuv/include/uv.h +1838 -0
- data/ext/libuv/src/fs-poll.c +235 -0
- data/ext/libuv/src/inet.c +293 -0
- data/ext/libuv/src/unix/async.c +148 -0
- data/ext/libuv/src/unix/core.c +696 -0
- data/ext/libuv/src/unix/cygwin.c +83 -0
- data/ext/libuv/src/unix/darwin.c +342 -0
- data/ext/libuv/src/unix/dl.c +83 -0
- data/ext/libuv/src/unix/eio/Changes +63 -0
- data/ext/libuv/src/unix/eio/LICENSE +36 -0
- data/ext/libuv/src/unix/eio/Makefile.am +15 -0
- data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/eio/autogen.sh +3 -0
- data/ext/libuv/src/unix/eio/config.h.in +86 -0
- data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
- data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
- data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_linux.h +94 -0
- data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
- data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
- data/ext/libuv/src/unix/eio/configure.ac +22 -0
- data/ext/libuv/src/unix/eio/demo.c +194 -0
- data/ext/libuv/src/unix/eio/ecb.h +370 -0
- data/ext/libuv/src/unix/eio/eio.3 +3428 -0
- data/ext/libuv/src/unix/eio/eio.c +2593 -0
- data/ext/libuv/src/unix/eio/eio.pod +969 -0
- data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
- data/ext/libuv/src/unix/eio/xthread.h +164 -0
- data/ext/libuv/src/unix/error.c +105 -0
- data/ext/libuv/src/unix/ev/Changes +388 -0
- data/ext/libuv/src/unix/ev/LICENSE +36 -0
- data/ext/libuv/src/unix/ev/Makefile.am +18 -0
- data/ext/libuv/src/unix/ev/Makefile.in +771 -0
- data/ext/libuv/src/unix/ev/README +58 -0
- data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/ev/autogen.sh +6 -0
- data/ext/libuv/src/unix/ev/config.guess +1526 -0
- data/ext/libuv/src/unix/ev/config.h.in +125 -0
- data/ext/libuv/src/unix/ev/config.sub +1658 -0
- data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
- data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
- data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_linux.h +141 -0
- data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
- data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
- data/ext/libuv/src/unix/ev/configure +13037 -0
- data/ext/libuv/src/unix/ev/configure.ac +18 -0
- data/ext/libuv/src/unix/ev/depcomp +630 -0
- data/ext/libuv/src/unix/ev/ev++.h +816 -0
- data/ext/libuv/src/unix/ev/ev.3 +5311 -0
- data/ext/libuv/src/unix/ev/ev.c +3925 -0
- data/ext/libuv/src/unix/ev/ev.pod +5243 -0
- data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
- data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
- data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
- data/ext/libuv/src/unix/ev/ev_port.c +179 -0
- data/ext/libuv/src/unix/ev/ev_select.c +310 -0
- data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
- data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
- data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
- data/ext/libuv/src/unix/ev/event.c +402 -0
- data/ext/libuv/src/unix/ev/event.h +170 -0
- data/ext/libuv/src/unix/ev/install-sh +294 -0
- data/ext/libuv/src/unix/ev/libev.m4 +39 -0
- data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
- data/ext/libuv/src/unix/ev/missing +336 -0
- data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
- data/ext/libuv/src/unix/freebsd.c +326 -0
- data/ext/libuv/src/unix/fs.c +739 -0
- data/ext/libuv/src/unix/internal.h +188 -0
- data/ext/libuv/src/unix/kqueue.c +120 -0
- data/ext/libuv/src/unix/linux/inotify.c +239 -0
- data/ext/libuv/src/unix/linux/linux-core.c +557 -0
- data/ext/libuv/src/unix/linux/syscalls.c +388 -0
- data/ext/libuv/src/unix/linux/syscalls.h +124 -0
- data/ext/libuv/src/unix/loop-watcher.c +62 -0
- data/ext/libuv/src/unix/loop.c +94 -0
- data/ext/libuv/src/unix/netbsd.c +108 -0
- data/ext/libuv/src/unix/openbsd.c +295 -0
- data/ext/libuv/src/unix/pipe.c +259 -0
- data/ext/libuv/src/unix/poll.c +114 -0
- data/ext/libuv/src/unix/process.c +495 -0
- data/ext/libuv/src/unix/signal.c +269 -0
- data/ext/libuv/src/unix/stream.c +990 -0
- data/ext/libuv/src/unix/sunos.c +481 -0
- data/ext/libuv/src/unix/tcp.c +393 -0
- data/ext/libuv/src/unix/thread.c +251 -0
- data/ext/libuv/src/unix/timer.c +136 -0
- data/ext/libuv/src/unix/tty.c +145 -0
- data/ext/libuv/src/unix/udp.c +659 -0
- data/ext/libuv/src/unix/uv-eio.c +107 -0
- data/ext/libuv/src/unix/uv-eio.h +13 -0
- data/ext/libuv/src/uv-common.c +380 -0
- data/ext/libuv/src/uv-common.h +170 -0
- data/ext/libuv/src/win/async.c +100 -0
- data/ext/libuv/src/win/atomicops-inl.h +56 -0
- data/ext/libuv/src/win/core.c +278 -0
- data/ext/libuv/src/win/dl.c +86 -0
- data/ext/libuv/src/win/error.c +155 -0
- data/ext/libuv/src/win/fs-event.c +510 -0
- data/ext/libuv/src/win/fs.c +1948 -0
- data/ext/libuv/src/win/getaddrinfo.c +365 -0
- data/ext/libuv/src/win/handle-inl.h +149 -0
- data/ext/libuv/src/win/handle.c +154 -0
- data/ext/libuv/src/win/internal.h +343 -0
- data/ext/libuv/src/win/loop-watcher.c +122 -0
- data/ext/libuv/src/win/pipe.c +1672 -0
- data/ext/libuv/src/win/poll.c +616 -0
- data/ext/libuv/src/win/process-stdio.c +500 -0
- data/ext/libuv/src/win/process.c +1013 -0
- data/ext/libuv/src/win/req-inl.h +220 -0
- data/ext/libuv/src/win/req.c +25 -0
- data/ext/libuv/src/win/signal.c +57 -0
- data/ext/libuv/src/win/stream-inl.h +67 -0
- data/ext/libuv/src/win/stream.c +167 -0
- data/ext/libuv/src/win/tcp.c +1394 -0
- data/ext/libuv/src/win/thread.c +372 -0
- data/ext/libuv/src/win/threadpool.c +74 -0
- data/ext/libuv/src/win/timer.c +224 -0
- data/ext/libuv/src/win/tty.c +1799 -0
- data/ext/libuv/src/win/udp.c +716 -0
- data/ext/libuv/src/win/util.c +864 -0
- data/ext/libuv/src/win/winapi.c +132 -0
- data/ext/libuv/src/win/winapi.h +4452 -0
- data/ext/libuv/src/win/winsock.c +557 -0
- data/ext/libuv/src/win/winsock.h +171 -0
- data/ext/libuv/test/benchmark-async-pummel.c +97 -0
- data/ext/libuv/test/benchmark-async.c +137 -0
- data/ext/libuv/test/benchmark-fs-stat.c +135 -0
- data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
- data/ext/libuv/test/benchmark-list.h +127 -0
- data/ext/libuv/test/benchmark-loop-count.c +88 -0
- data/ext/libuv/test/benchmark-million-timers.c +65 -0
- data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
- data/ext/libuv/test/benchmark-pound.c +324 -0
- data/ext/libuv/test/benchmark-pump.c +462 -0
- data/ext/libuv/test/benchmark-sizes.c +44 -0
- data/ext/libuv/test/benchmark-spawn.c +162 -0
- data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
- data/ext/libuv/test/benchmark-thread.c +64 -0
- data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
- data/ext/libuv/test/blackhole-server.c +118 -0
- data/ext/libuv/test/dns-server.c +321 -0
- data/ext/libuv/test/echo-server.c +378 -0
- data/ext/libuv/test/fixtures/empty_file +0 -0
- data/ext/libuv/test/fixtures/load_error.node +1 -0
- data/ext/libuv/test/run-benchmarks.c +64 -0
- data/ext/libuv/test/run-tests.c +138 -0
- data/ext/libuv/test/runner-unix.c +295 -0
- data/ext/libuv/test/runner-unix.h +36 -0
- data/ext/libuv/test/runner-win.c +285 -0
- data/ext/libuv/test/runner-win.h +42 -0
- data/ext/libuv/test/runner.c +355 -0
- data/ext/libuv/test/runner.h +159 -0
- data/ext/libuv/test/task.h +112 -0
- data/ext/libuv/test/test-async.c +118 -0
- data/ext/libuv/test/test-callback-order.c +76 -0
- data/ext/libuv/test/test-callback-stack.c +203 -0
- data/ext/libuv/test/test-connection-fail.c +148 -0
- data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
- data/ext/libuv/test/test-delayed-accept.c +188 -0
- data/ext/libuv/test/test-dlerror.c +58 -0
- data/ext/libuv/test/test-error.c +59 -0
- data/ext/libuv/test/test-fail-always.c +29 -0
- data/ext/libuv/test/test-fs-event.c +474 -0
- data/ext/libuv/test/test-fs-poll.c +146 -0
- data/ext/libuv/test/test-fs.c +1843 -0
- data/ext/libuv/test/test-get-currentexe.c +63 -0
- data/ext/libuv/test/test-get-loadavg.c +36 -0
- data/ext/libuv/test/test-get-memory.c +38 -0
- data/ext/libuv/test/test-getaddrinfo.c +122 -0
- data/ext/libuv/test/test-getsockname.c +342 -0
- data/ext/libuv/test/test-hrtime.c +54 -0
- data/ext/libuv/test/test-idle.c +81 -0
- data/ext/libuv/test/test-ipc-send-recv.c +209 -0
- data/ext/libuv/test/test-ipc.c +620 -0
- data/ext/libuv/test/test-list.h +427 -0
- data/ext/libuv/test/test-loop-handles.c +336 -0
- data/ext/libuv/test/test-multiple-listen.c +102 -0
- data/ext/libuv/test/test-mutexes.c +63 -0
- data/ext/libuv/test/test-pass-always.c +28 -0
- data/ext/libuv/test/test-ping-pong.c +253 -0
- data/ext/libuv/test/test-pipe-bind-error.c +140 -0
- data/ext/libuv/test/test-pipe-connect-error.c +96 -0
- data/ext/libuv/test/test-platform-output.c +87 -0
- data/ext/libuv/test/test-poll-close.c +72 -0
- data/ext/libuv/test/test-poll.c +573 -0
- data/ext/libuv/test/test-process-title.c +49 -0
- data/ext/libuv/test/test-ref.c +338 -0
- data/ext/libuv/test/test-run-once.c +48 -0
- data/ext/libuv/test/test-semaphore.c +111 -0
- data/ext/libuv/test/test-shutdown-close.c +103 -0
- data/ext/libuv/test/test-shutdown-eof.c +183 -0
- data/ext/libuv/test/test-signal.c +162 -0
- data/ext/libuv/test/test-spawn.c +863 -0
- data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
- data/ext/libuv/test/test-tcp-bind-error.c +191 -0
- data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
- data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
- data/ext/libuv/test/test-tcp-close.c +129 -0
- data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
- data/ext/libuv/test/test-tcp-connect-error.c +70 -0
- data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
- data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
- data/ext/libuv/test/test-tcp-flags.c +51 -0
- data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
- data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
- data/ext/libuv/test/test-tcp-write-error.c +168 -0
- data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
- data/ext/libuv/test/test-tcp-writealot.c +170 -0
- data/ext/libuv/test/test-thread.c +183 -0
- data/ext/libuv/test/test-threadpool.c +57 -0
- data/ext/libuv/test/test-timer-again.c +141 -0
- data/ext/libuv/test/test-timer.c +152 -0
- data/ext/libuv/test/test-tty.c +110 -0
- data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
- data/ext/libuv/test/test-udp-ipv6.c +156 -0
- data/ext/libuv/test/test-udp-multicast-join.c +139 -0
- data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
- data/ext/libuv/test/test-udp-options.c +86 -0
- data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
- data/ext/libuv/test/test-util.c +97 -0
- data/ext/libuv/test/test-walk-handles.c +77 -0
- data/ext/libuv/uv.gyp +375 -0
- data/ext/libuv/vcbuild.bat +105 -0
- data/foolio.gemspec +18 -0
- data/lib/foolio.rb +9 -0
- data/lib/foolio/handle.rb +27 -0
- data/lib/foolio/listener.rb +26 -0
- data/lib/foolio/loop.rb +79 -0
- data/lib/foolio/stream.rb +109 -0
- data/lib/foolio/version.rb +3 -0
- metadata +309 -0
@@ -0,0 +1,1013 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
*
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
+
* IN THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include <assert.h>
|
23
|
+
#include <io.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
#include <signal.h>
|
27
|
+
|
28
|
+
#include "uv.h"
|
29
|
+
#include "internal.h"
|
30
|
+
#include "handle-inl.h"
|
31
|
+
#include "req-inl.h"
|
32
|
+
|
33
|
+
|
34
|
+
#define SIGKILL 9
|
35
|
+
|
36
|
+
|
37
|
+
typedef struct env_var {
|
38
|
+
const char* narrow;
|
39
|
+
const WCHAR* wide;
|
40
|
+
int len; /* including null or '=' */
|
41
|
+
int supplied;
|
42
|
+
int value_len;
|
43
|
+
} env_var_t;
|
44
|
+
|
45
|
+
#define E_V(str) { str "=", L##str, sizeof(str), 0, 0 }
|
46
|
+
|
47
|
+
|
48
|
+
static uv_err_t uv_utf8_to_utf16_alloc(const char* s, WCHAR** ws_ptr) {
|
49
|
+
int ws_len, r;
|
50
|
+
WCHAR* ws;
|
51
|
+
|
52
|
+
ws_len = MultiByteToWideChar(CP_UTF8,
|
53
|
+
0,
|
54
|
+
s,
|
55
|
+
-1,
|
56
|
+
NULL,
|
57
|
+
0);
|
58
|
+
if (ws_len <= 0) {
|
59
|
+
return uv__new_sys_error(GetLastError());
|
60
|
+
}
|
61
|
+
|
62
|
+
ws = (WCHAR*) malloc(ws_len * sizeof(WCHAR));
|
63
|
+
if (ws == NULL) {
|
64
|
+
return uv__new_artificial_error(UV_ENOMEM);
|
65
|
+
}
|
66
|
+
|
67
|
+
r = MultiByteToWideChar(CP_UTF8,
|
68
|
+
0,
|
69
|
+
s,
|
70
|
+
-1,
|
71
|
+
ws,
|
72
|
+
ws_len);
|
73
|
+
assert(r == ws_len);
|
74
|
+
|
75
|
+
*ws_ptr = ws;
|
76
|
+
return uv_ok_;
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
static void uv_process_init(uv_loop_t* loop, uv_process_t* handle) {
|
81
|
+
uv__handle_init(loop, (uv_handle_t*) handle, UV_PROCESS);
|
82
|
+
handle->exit_cb = NULL;
|
83
|
+
handle->pid = 0;
|
84
|
+
handle->spawn_error = uv_ok_;
|
85
|
+
handle->exit_signal = 0;
|
86
|
+
handle->wait_handle = INVALID_HANDLE_VALUE;
|
87
|
+
handle->process_handle = INVALID_HANDLE_VALUE;
|
88
|
+
handle->child_stdio_buffer = NULL;
|
89
|
+
handle->exit_cb_pending = 0;
|
90
|
+
|
91
|
+
uv_req_init(loop, (uv_req_t*)&handle->exit_req);
|
92
|
+
handle->exit_req.type = UV_PROCESS_EXIT;
|
93
|
+
handle->exit_req.data = handle;
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
/*
|
98
|
+
* Path search functions
|
99
|
+
*/
|
100
|
+
|
101
|
+
/*
|
102
|
+
* Helper function for search_path
|
103
|
+
*/
|
104
|
+
static WCHAR* search_path_join_test(const WCHAR* dir,
|
105
|
+
int dir_len,
|
106
|
+
const WCHAR* name,
|
107
|
+
int name_len,
|
108
|
+
const WCHAR* ext,
|
109
|
+
int ext_len,
|
110
|
+
const WCHAR* cwd,
|
111
|
+
int cwd_len) {
|
112
|
+
WCHAR *result, *result_pos;
|
113
|
+
DWORD attrs;
|
114
|
+
|
115
|
+
if (dir_len >= 1 && (dir[0] == L'/' || dir[0] == L'\\')) {
|
116
|
+
/* It's a full path without drive letter, use cwd's drive letter only */
|
117
|
+
cwd_len = 2;
|
118
|
+
} else if (dir_len >= 2 && dir[1] == L':' &&
|
119
|
+
(dir_len < 3 || (dir[2] != L'/' && dir[2] != L'\\'))) {
|
120
|
+
/* It's a relative path with drive letter (ext.g. D:../some/file)
|
121
|
+
* Replace drive letter in dir by full cwd if it points to the same drive,
|
122
|
+
* otherwise use the dir only.
|
123
|
+
*/
|
124
|
+
if (cwd_len < 2 || _wcsnicmp(cwd, dir, 2) != 0) {
|
125
|
+
cwd_len = 0;
|
126
|
+
} else {
|
127
|
+
dir += 2;
|
128
|
+
dir_len -= 2;
|
129
|
+
}
|
130
|
+
} else if (dir_len > 2 && dir[1] == L':') {
|
131
|
+
/* It's an absolute path with drive letter
|
132
|
+
* Don't use the cwd at all
|
133
|
+
*/
|
134
|
+
cwd_len = 0;
|
135
|
+
}
|
136
|
+
|
137
|
+
/* Allocate buffer for output */
|
138
|
+
result = result_pos = (WCHAR*)malloc(sizeof(WCHAR) *
|
139
|
+
(cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
|
140
|
+
|
141
|
+
/* Copy cwd */
|
142
|
+
wcsncpy(result_pos, cwd, cwd_len);
|
143
|
+
result_pos += cwd_len;
|
144
|
+
|
145
|
+
/* Add a path separator if cwd didn't end with one */
|
146
|
+
if (cwd_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
|
147
|
+
result_pos[0] = L'\\';
|
148
|
+
result_pos++;
|
149
|
+
}
|
150
|
+
|
151
|
+
/* Copy dir */
|
152
|
+
wcsncpy(result_pos, dir, dir_len);
|
153
|
+
result_pos += dir_len;
|
154
|
+
|
155
|
+
/* Add a separator if the dir didn't end with one */
|
156
|
+
if (dir_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
|
157
|
+
result_pos[0] = L'\\';
|
158
|
+
result_pos++;
|
159
|
+
}
|
160
|
+
|
161
|
+
/* Copy filename */
|
162
|
+
wcsncpy(result_pos, name, name_len);
|
163
|
+
result_pos += name_len;
|
164
|
+
|
165
|
+
if (ext_len) {
|
166
|
+
/* Add a dot if the filename didn't end with one */
|
167
|
+
if (name_len && result_pos[-1] != '.') {
|
168
|
+
result_pos[0] = L'.';
|
169
|
+
result_pos++;
|
170
|
+
}
|
171
|
+
|
172
|
+
/* Copy extension */
|
173
|
+
wcsncpy(result_pos, ext, ext_len);
|
174
|
+
result_pos += ext_len;
|
175
|
+
}
|
176
|
+
|
177
|
+
/* Null terminator */
|
178
|
+
result_pos[0] = L'\0';
|
179
|
+
|
180
|
+
attrs = GetFileAttributesW(result);
|
181
|
+
|
182
|
+
if (attrs != INVALID_FILE_ATTRIBUTES &&
|
183
|
+
!(attrs & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) {
|
184
|
+
return result;
|
185
|
+
}
|
186
|
+
|
187
|
+
free(result);
|
188
|
+
return NULL;
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
/*
|
193
|
+
* Helper function for search_path
|
194
|
+
*/
|
195
|
+
static WCHAR* path_search_walk_ext(const WCHAR *dir,
|
196
|
+
int dir_len,
|
197
|
+
const WCHAR *name,
|
198
|
+
int name_len,
|
199
|
+
WCHAR *cwd,
|
200
|
+
int cwd_len,
|
201
|
+
int name_has_ext) {
|
202
|
+
WCHAR* result;
|
203
|
+
|
204
|
+
/* If the name itself has a nonempty extension, try this extension first */
|
205
|
+
if (name_has_ext) {
|
206
|
+
result = search_path_join_test(dir, dir_len,
|
207
|
+
name, name_len,
|
208
|
+
L"", 0,
|
209
|
+
cwd, cwd_len);
|
210
|
+
if (result != NULL) {
|
211
|
+
return result;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
/* Try .com extension */
|
216
|
+
result = search_path_join_test(dir, dir_len,
|
217
|
+
name, name_len,
|
218
|
+
L"com", 3,
|
219
|
+
cwd, cwd_len);
|
220
|
+
if (result != NULL) {
|
221
|
+
return result;
|
222
|
+
}
|
223
|
+
|
224
|
+
/* Try .exe extension */
|
225
|
+
result = search_path_join_test(dir, dir_len,
|
226
|
+
name, name_len,
|
227
|
+
L"exe", 3,
|
228
|
+
cwd, cwd_len);
|
229
|
+
if (result != NULL) {
|
230
|
+
return result;
|
231
|
+
}
|
232
|
+
|
233
|
+
return NULL;
|
234
|
+
}
|
235
|
+
|
236
|
+
|
237
|
+
/*
|
238
|
+
* search_path searches the system path for an executable filename -
|
239
|
+
* the windows API doesn't provide this as a standalone function nor as an
|
240
|
+
* option to CreateProcess.
|
241
|
+
*
|
242
|
+
* It tries to return an absolute filename.
|
243
|
+
*
|
244
|
+
* Furthermore, it tries to follow the semantics that cmd.exe, with this
|
245
|
+
* exception that PATHEXT environment variable isn't used. Since CreateProcess
|
246
|
+
* can start only .com and .exe files, only those extensions are tried. This
|
247
|
+
* behavior equals that of msvcrt's spawn functions.
|
248
|
+
*
|
249
|
+
* - Do not search the path if the filename already contains a path (either
|
250
|
+
* relative or absolute).
|
251
|
+
*
|
252
|
+
* - If there's really only a filename, check the current directory for file,
|
253
|
+
* then search all path directories.
|
254
|
+
*
|
255
|
+
* - If filename specified has *any* extension, search for the file with the
|
256
|
+
* specified extension first.
|
257
|
+
*
|
258
|
+
* - If the literal filename is not found in a directory, try *appending*
|
259
|
+
* (not replacing) .com first and then .exe.
|
260
|
+
*
|
261
|
+
* - The path variable may contain relative paths; relative paths are relative
|
262
|
+
* to the cwd.
|
263
|
+
*
|
264
|
+
* - Directories in path may or may not end with a trailing backslash.
|
265
|
+
*
|
266
|
+
* - CMD does not trim leading/trailing whitespace from path/pathex entries
|
267
|
+
* nor from the environment variables as a whole.
|
268
|
+
*
|
269
|
+
* - When cmd.exe cannot read a directory, it will just skip it and go on
|
270
|
+
* searching. However, unlike posix-y systems, it will happily try to run a
|
271
|
+
* file that is not readable/executable; if the spawn fails it will not
|
272
|
+
* continue searching.
|
273
|
+
*
|
274
|
+
* TODO: correctly interpret UNC paths
|
275
|
+
*/
|
276
|
+
static WCHAR* search_path(const WCHAR *file,
|
277
|
+
WCHAR *cwd,
|
278
|
+
const WCHAR *path) {
|
279
|
+
int file_has_dir;
|
280
|
+
WCHAR* result = NULL;
|
281
|
+
WCHAR *file_name_start;
|
282
|
+
WCHAR *dot;
|
283
|
+
const WCHAR *dir_start, *dir_end, *dir_path;
|
284
|
+
int dir_len;
|
285
|
+
int name_has_ext;
|
286
|
+
|
287
|
+
int file_len = wcslen(file);
|
288
|
+
int cwd_len = wcslen(cwd);
|
289
|
+
|
290
|
+
/* If the caller supplies an empty filename,
|
291
|
+
* we're not gonna return c:\windows\.exe -- GFY!
|
292
|
+
*/
|
293
|
+
if (file_len == 0
|
294
|
+
|| (file_len == 1 && file[0] == L'.')) {
|
295
|
+
return NULL;
|
296
|
+
}
|
297
|
+
|
298
|
+
/* Find the start of the filename so we can split the directory from the */
|
299
|
+
/* name. */
|
300
|
+
for (file_name_start = (WCHAR*)file + file_len;
|
301
|
+
file_name_start > file
|
302
|
+
&& file_name_start[-1] != L'\\'
|
303
|
+
&& file_name_start[-1] != L'/'
|
304
|
+
&& file_name_start[-1] != L':';
|
305
|
+
file_name_start--);
|
306
|
+
|
307
|
+
file_has_dir = file_name_start != file;
|
308
|
+
|
309
|
+
/* Check if the filename includes an extension */
|
310
|
+
dot = wcschr(file_name_start, L'.');
|
311
|
+
name_has_ext = (dot != NULL && dot[1] != L'\0');
|
312
|
+
|
313
|
+
if (file_has_dir) {
|
314
|
+
/* The file has a path inside, don't use path */
|
315
|
+
result = path_search_walk_ext(
|
316
|
+
file, file_name_start - file,
|
317
|
+
file_name_start, file_len - (file_name_start - file),
|
318
|
+
cwd, cwd_len,
|
319
|
+
name_has_ext);
|
320
|
+
|
321
|
+
} else {
|
322
|
+
dir_end = path;
|
323
|
+
|
324
|
+
/* The file is really only a name; look in cwd first, then scan path */
|
325
|
+
result = path_search_walk_ext(L"", 0,
|
326
|
+
file, file_len,
|
327
|
+
cwd, cwd_len,
|
328
|
+
name_has_ext);
|
329
|
+
|
330
|
+
while (result == NULL) {
|
331
|
+
if (*dir_end == L'\0') {
|
332
|
+
break;
|
333
|
+
}
|
334
|
+
|
335
|
+
/* Skip the separator that dir_end now points to */
|
336
|
+
if (dir_end != path) {
|
337
|
+
dir_end++;
|
338
|
+
}
|
339
|
+
|
340
|
+
/* Next slice starts just after where the previous one ended */
|
341
|
+
dir_start = dir_end;
|
342
|
+
|
343
|
+
/* Slice until the next ; or \0 is found */
|
344
|
+
dir_end = wcschr(dir_start, L';');
|
345
|
+
if (dir_end == NULL) {
|
346
|
+
dir_end = wcschr(dir_start, L'\0');
|
347
|
+
}
|
348
|
+
|
349
|
+
/* If the slice is zero-length, don't bother */
|
350
|
+
if (dir_end - dir_start == 0) {
|
351
|
+
continue;
|
352
|
+
}
|
353
|
+
|
354
|
+
dir_path = dir_start;
|
355
|
+
dir_len = dir_end - dir_start;
|
356
|
+
|
357
|
+
/* Adjust if the path is quoted. */
|
358
|
+
if (dir_path[0] == '"' || dir_path[0] == '\'') {
|
359
|
+
++dir_path;
|
360
|
+
--dir_len;
|
361
|
+
}
|
362
|
+
|
363
|
+
if (dir_path[dir_len - 1] == '"' || dir_path[dir_len - 1] == '\'') {
|
364
|
+
--dir_len;
|
365
|
+
}
|
366
|
+
|
367
|
+
result = path_search_walk_ext(dir_path, dir_len,
|
368
|
+
file, file_len,
|
369
|
+
cwd, cwd_len,
|
370
|
+
name_has_ext);
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
return result;
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
/*
|
379
|
+
* Quotes command line arguments
|
380
|
+
* Returns a pointer to the end (next char to be written) of the buffer
|
381
|
+
*/
|
382
|
+
WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target) {
|
383
|
+
int len = wcslen(source),
|
384
|
+
i, quote_hit;
|
385
|
+
WCHAR* start;
|
386
|
+
|
387
|
+
/*
|
388
|
+
* Check if the string must be quoted;
|
389
|
+
* if unnecessary, don't do it, it may only confuse older programs.
|
390
|
+
*/
|
391
|
+
if (len == 0) {
|
392
|
+
return target;
|
393
|
+
}
|
394
|
+
|
395
|
+
if (NULL == wcspbrk(source, L" \t\"")) {
|
396
|
+
/* No quotation needed */
|
397
|
+
wcsncpy(target, source, len);
|
398
|
+
target += len;
|
399
|
+
return target;
|
400
|
+
}
|
401
|
+
|
402
|
+
if (NULL == wcspbrk(source, L"\"\\")) {
|
403
|
+
/*
|
404
|
+
* No embedded double quotes or backlashes, so I can just wrap
|
405
|
+
* quote marks around the whole thing.
|
406
|
+
*/
|
407
|
+
*(target++) = L'"';
|
408
|
+
wcsncpy(target, source, len);
|
409
|
+
target += len;
|
410
|
+
*(target++) = L'"';
|
411
|
+
return target;
|
412
|
+
}
|
413
|
+
|
414
|
+
/*
|
415
|
+
* Expected input/output:
|
416
|
+
* input : hello"world
|
417
|
+
* output: "hello\"world"
|
418
|
+
* input : hello""world
|
419
|
+
* output: "hello\"\"world"
|
420
|
+
* input : hello\world
|
421
|
+
* output: hello\world
|
422
|
+
* input : hello\\world
|
423
|
+
* output: hello\\world
|
424
|
+
* input : hello\"world
|
425
|
+
* output: "hello\\\"world"
|
426
|
+
* input : hello\\"world
|
427
|
+
* output: "hello\\\\\"world"
|
428
|
+
* input : hello world\
|
429
|
+
* output: "hello world\"
|
430
|
+
*/
|
431
|
+
|
432
|
+
*(target++) = L'"';
|
433
|
+
start = target;
|
434
|
+
quote_hit = 1;
|
435
|
+
|
436
|
+
for (i = len; i > 0; --i) {
|
437
|
+
*(target++) = source[i - 1];
|
438
|
+
|
439
|
+
if (quote_hit && source[i - 1] == L'\\') {
|
440
|
+
*(target++) = L'\\';
|
441
|
+
} else if(source[i - 1] == L'"') {
|
442
|
+
quote_hit = 1;
|
443
|
+
*(target++) = L'\\';
|
444
|
+
} else {
|
445
|
+
quote_hit = 0;
|
446
|
+
}
|
447
|
+
}
|
448
|
+
target[0] = L'\0';
|
449
|
+
wcsrev(start);
|
450
|
+
*(target++) = L'"';
|
451
|
+
return target;
|
452
|
+
}
|
453
|
+
|
454
|
+
|
455
|
+
uv_err_t make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr) {
|
456
|
+
char** arg;
|
457
|
+
WCHAR* dst = NULL;
|
458
|
+
WCHAR* temp_buffer = NULL;
|
459
|
+
size_t dst_len = 0;
|
460
|
+
size_t temp_buffer_len = 0;
|
461
|
+
WCHAR* pos;
|
462
|
+
int arg_count = 0;
|
463
|
+
uv_err_t err = uv_ok_;
|
464
|
+
|
465
|
+
/* Count the required size. */
|
466
|
+
for (arg = args; *arg; arg++) {
|
467
|
+
DWORD arg_len;
|
468
|
+
|
469
|
+
arg_len = MultiByteToWideChar(CP_UTF8,
|
470
|
+
0,
|
471
|
+
*arg,
|
472
|
+
-1,
|
473
|
+
NULL,
|
474
|
+
0);
|
475
|
+
if (arg_len == 0) {
|
476
|
+
return uv__new_sys_error(GetLastError());
|
477
|
+
}
|
478
|
+
|
479
|
+
dst_len += arg_len;
|
480
|
+
|
481
|
+
if (arg_len > temp_buffer_len)
|
482
|
+
temp_buffer_len = arg_len;
|
483
|
+
|
484
|
+
arg_count++;
|
485
|
+
}
|
486
|
+
|
487
|
+
/* Adjust for potential quotes. Also assume the worst-case scenario */
|
488
|
+
/* that every character needs escaping, so we need twice as much space. */
|
489
|
+
dst_len = dst_len * 2 + arg_count * 2;
|
490
|
+
|
491
|
+
/* Allocate buffer for the final command line. */
|
492
|
+
dst = (WCHAR*) malloc(dst_len * sizeof(WCHAR));
|
493
|
+
if (dst == NULL) {
|
494
|
+
err = uv__new_artificial_error(UV_ENOMEM);
|
495
|
+
goto error;
|
496
|
+
}
|
497
|
+
|
498
|
+
/* Allocate temporary working buffer. */
|
499
|
+
temp_buffer = (WCHAR*) malloc(temp_buffer_len * sizeof(WCHAR));
|
500
|
+
if (temp_buffer == NULL) {
|
501
|
+
err = uv__new_artificial_error(UV_ENOMEM);
|
502
|
+
goto error;
|
503
|
+
}
|
504
|
+
|
505
|
+
pos = dst;
|
506
|
+
for (arg = args; *arg; arg++) {
|
507
|
+
DWORD arg_len;
|
508
|
+
|
509
|
+
/* Convert argument to wide char. */
|
510
|
+
arg_len = MultiByteToWideChar(CP_UTF8,
|
511
|
+
0,
|
512
|
+
*arg,
|
513
|
+
-1,
|
514
|
+
temp_buffer,
|
515
|
+
dst + dst_len - pos);
|
516
|
+
if (arg_len == 0) {
|
517
|
+
goto error;
|
518
|
+
}
|
519
|
+
|
520
|
+
if (verbatim_arguments) {
|
521
|
+
/* Copy verbatim. */
|
522
|
+
wcscpy(pos, temp_buffer);
|
523
|
+
pos += arg_len - 1;
|
524
|
+
} else {
|
525
|
+
/* Quote/escape, if needed. */
|
526
|
+
pos = quote_cmd_arg(temp_buffer, pos);
|
527
|
+
}
|
528
|
+
|
529
|
+
*pos++ = *(arg + 1) ? L' ' : L'\0';
|
530
|
+
}
|
531
|
+
|
532
|
+
free(temp_buffer);
|
533
|
+
|
534
|
+
*dst_ptr = dst;
|
535
|
+
return uv_ok_;
|
536
|
+
|
537
|
+
error:
|
538
|
+
free(dst);
|
539
|
+
free(temp_buffer);
|
540
|
+
return err;
|
541
|
+
}
|
542
|
+
|
543
|
+
|
544
|
+
/*
|
545
|
+
* If we learn that people are passing in huge environment blocks
|
546
|
+
* then we should probably qsort() the array and then bsearch()
|
547
|
+
* to see if it contains this variable. But there are ownership
|
548
|
+
* issues associated with that solution; this is the caller's
|
549
|
+
* char**, and modifying it is rude.
|
550
|
+
*/
|
551
|
+
static void check_required_vars_contains_var(env_var_t* required, int size,
|
552
|
+
const char* var) {
|
553
|
+
int i;
|
554
|
+
for (i = 0; i < size; ++i) {
|
555
|
+
if (_strnicmp(required[i].narrow, var, required[i].len) == 0) {
|
556
|
+
required[i].supplied = 1;
|
557
|
+
return;
|
558
|
+
}
|
559
|
+
}
|
560
|
+
}
|
561
|
+
|
562
|
+
|
563
|
+
/*
|
564
|
+
* The way windows takes environment variables is different than what C does;
|
565
|
+
* Windows wants a contiguous block of null-terminated strings, terminated
|
566
|
+
* with an additional null.
|
567
|
+
*
|
568
|
+
* Windows has a few "essential" environment variables. winsock will fail
|
569
|
+
* to initialize if SYSTEMROOT is not defined; some APIs make reference to
|
570
|
+
* TEMP. SYSTEMDRIVE is probably also important. We therefore ensure that
|
571
|
+
* these get defined if the input environment block does not contain any
|
572
|
+
* values for them.
|
573
|
+
*/
|
574
|
+
WCHAR* make_program_env(char** env_block) {
|
575
|
+
WCHAR* dst;
|
576
|
+
WCHAR* ptr;
|
577
|
+
char** env;
|
578
|
+
int env_len = 1 * sizeof(WCHAR); /* room for closing null */
|
579
|
+
int len;
|
580
|
+
int i;
|
581
|
+
DWORD var_size;
|
582
|
+
|
583
|
+
env_var_t required_vars[] = {
|
584
|
+
E_V("SYSTEMROOT"),
|
585
|
+
E_V("SYSTEMDRIVE"),
|
586
|
+
E_V("TEMP"),
|
587
|
+
};
|
588
|
+
|
589
|
+
for (env = env_block; *env; env++) {
|
590
|
+
check_required_vars_contains_var(required_vars,
|
591
|
+
ARRAY_SIZE(required_vars),
|
592
|
+
*env);
|
593
|
+
env_len += (uv_utf8_to_utf16(*env, NULL, 0) * sizeof(WCHAR));
|
594
|
+
}
|
595
|
+
|
596
|
+
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
|
597
|
+
if (!required_vars[i].supplied) {
|
598
|
+
env_len += required_vars[i].len * sizeof(WCHAR);
|
599
|
+
var_size = GetEnvironmentVariableW(required_vars[i].wide, NULL, 0);
|
600
|
+
if (var_size == 0) {
|
601
|
+
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
|
602
|
+
}
|
603
|
+
required_vars[i].value_len = (int)var_size;
|
604
|
+
env_len += (int)var_size * sizeof(WCHAR);
|
605
|
+
}
|
606
|
+
}
|
607
|
+
|
608
|
+
dst = malloc(env_len);
|
609
|
+
if (!dst) {
|
610
|
+
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
|
611
|
+
}
|
612
|
+
|
613
|
+
ptr = dst;
|
614
|
+
|
615
|
+
for (env = env_block; *env; env++, ptr += len) {
|
616
|
+
len = uv_utf8_to_utf16(*env, ptr, (size_t)(env_len - (ptr - dst)));
|
617
|
+
if (!len) {
|
618
|
+
free(dst);
|
619
|
+
return NULL;
|
620
|
+
}
|
621
|
+
}
|
622
|
+
|
623
|
+
for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
|
624
|
+
if (!required_vars[i].supplied) {
|
625
|
+
wcscpy(ptr, required_vars[i].wide);
|
626
|
+
ptr += required_vars[i].len - 1;
|
627
|
+
*ptr++ = L'=';
|
628
|
+
var_size = GetEnvironmentVariableW(required_vars[i].wide,
|
629
|
+
ptr,
|
630
|
+
required_vars[i].value_len);
|
631
|
+
if (var_size == 0) {
|
632
|
+
uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
|
633
|
+
}
|
634
|
+
ptr += required_vars[i].value_len;
|
635
|
+
}
|
636
|
+
}
|
637
|
+
|
638
|
+
*ptr = L'\0';
|
639
|
+
return dst;
|
640
|
+
}
|
641
|
+
|
642
|
+
|
643
|
+
/*
|
644
|
+
* Called on Windows thread-pool thread to indicate that
|
645
|
+
* a child process has exited.
|
646
|
+
*/
|
647
|
+
static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
|
648
|
+
uv_process_t* process = (uv_process_t*) data;
|
649
|
+
uv_loop_t* loop = process->loop;
|
650
|
+
|
651
|
+
assert(didTimeout == FALSE);
|
652
|
+
assert(process);
|
653
|
+
assert(!process->exit_cb_pending);
|
654
|
+
|
655
|
+
process->exit_cb_pending = 1;
|
656
|
+
|
657
|
+
/* Post completed */
|
658
|
+
POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
|
659
|
+
}
|
660
|
+
|
661
|
+
|
662
|
+
/* Called on main thread after a child process has exited. */
|
663
|
+
void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle) {
|
664
|
+
DWORD exit_code;
|
665
|
+
|
666
|
+
assert(handle->exit_cb_pending);
|
667
|
+
handle->exit_cb_pending = 0;
|
668
|
+
|
669
|
+
/* If we're closing, don't call the exit callback. Just schedule a close */
|
670
|
+
/* callback now. */
|
671
|
+
if (handle->flags & UV_HANDLE_CLOSING) {
|
672
|
+
uv_want_endgame(loop, (uv_handle_t*) handle);
|
673
|
+
return;
|
674
|
+
}
|
675
|
+
|
676
|
+
/* Unregister from process notification. */
|
677
|
+
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
|
678
|
+
UnregisterWait(handle->wait_handle);
|
679
|
+
handle->wait_handle = INVALID_HANDLE_VALUE;
|
680
|
+
}
|
681
|
+
|
682
|
+
/* Set the handle to inactive: no callbacks will be made after the exit */
|
683
|
+
/* callback.*/
|
684
|
+
uv__handle_stop(handle);
|
685
|
+
|
686
|
+
if (handle->spawn_error.code != UV_OK) {
|
687
|
+
/* Spawning failed. */
|
688
|
+
exit_code = (DWORD) -1;
|
689
|
+
} else if (!GetExitCodeProcess(handle->process_handle, &exit_code)) {
|
690
|
+
/* Unable to to obtain the exit code. This should never happen. */
|
691
|
+
exit_code = (DWORD) -1;
|
692
|
+
}
|
693
|
+
|
694
|
+
/* Fire the exit callback. */
|
695
|
+
if (handle->exit_cb) {
|
696
|
+
loop->last_err = handle->spawn_error;
|
697
|
+
handle->exit_cb(handle, exit_code, handle->exit_signal);
|
698
|
+
}
|
699
|
+
}
|
700
|
+
|
701
|
+
|
702
|
+
void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
|
703
|
+
uv__handle_start(handle);
|
704
|
+
|
705
|
+
if (handle->wait_handle != INVALID_HANDLE_VALUE) {
|
706
|
+
/* This blocks until either the wait was cancelled, or the callback has */
|
707
|
+
/* completed. */
|
708
|
+
BOOL r = UnregisterWaitEx(handle->wait_handle, INVALID_HANDLE_VALUE);
|
709
|
+
if (!r) {
|
710
|
+
/* This should never happen, and if it happens, we can't recover... */
|
711
|
+
uv_fatal_error(GetLastError(), "UnregisterWaitEx");
|
712
|
+
}
|
713
|
+
|
714
|
+
handle->wait_handle = INVALID_HANDLE_VALUE;
|
715
|
+
}
|
716
|
+
|
717
|
+
if (!handle->exit_cb_pending) {
|
718
|
+
uv_want_endgame(loop, (uv_handle_t*)handle);
|
719
|
+
}
|
720
|
+
}
|
721
|
+
|
722
|
+
|
723
|
+
void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
|
724
|
+
assert(!handle->exit_cb_pending);
|
725
|
+
assert(handle->flags & UV_HANDLE_CLOSING);
|
726
|
+
assert(!(handle->flags & UV_HANDLE_CLOSED));
|
727
|
+
|
728
|
+
uv__handle_stop(handle);
|
729
|
+
|
730
|
+
/* Clean-up the process handle. */
|
731
|
+
CloseHandle(handle->process_handle);
|
732
|
+
|
733
|
+
uv__handle_close(handle);
|
734
|
+
}
|
735
|
+
|
736
|
+
|
737
|
+
int uv_spawn(uv_loop_t* loop, uv_process_t* process,
|
738
|
+
uv_process_options_t options) {
|
739
|
+
int i;
|
740
|
+
uv_err_t err = uv_ok_;
|
741
|
+
WCHAR* path = NULL;
|
742
|
+
BOOL result;
|
743
|
+
WCHAR* application_path = NULL, *application = NULL, *arguments = NULL,
|
744
|
+
*env = NULL, *cwd = NULL;
|
745
|
+
STARTUPINFOW startup;
|
746
|
+
PROCESS_INFORMATION info;
|
747
|
+
DWORD process_flags;
|
748
|
+
|
749
|
+
if (options.flags & (UV_PROCESS_SETGID | UV_PROCESS_SETUID)) {
|
750
|
+
uv__set_artificial_error(loop, UV_ENOTSUP);
|
751
|
+
return -1;
|
752
|
+
}
|
753
|
+
|
754
|
+
if (options.file == NULL ||
|
755
|
+
options.args == NULL) {
|
756
|
+
uv__set_artificial_error(loop, UV_EINVAL);
|
757
|
+
return -1;
|
758
|
+
}
|
759
|
+
|
760
|
+
assert(options.file != NULL);
|
761
|
+
assert(!(options.flags & ~(UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS |
|
762
|
+
UV_PROCESS_DETACHED |
|
763
|
+
UV_PROCESS_SETGID |
|
764
|
+
UV_PROCESS_SETUID)));
|
765
|
+
|
766
|
+
uv_process_init(loop, process);
|
767
|
+
process->exit_cb = options.exit_cb;
|
768
|
+
|
769
|
+
err = uv_utf8_to_utf16_alloc(options.file, &application);
|
770
|
+
if (err.code != UV_OK)
|
771
|
+
goto done;
|
772
|
+
|
773
|
+
err = make_program_args(options.args,
|
774
|
+
options.flags & UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
|
775
|
+
&arguments);
|
776
|
+
if (err.code != UV_OK)
|
777
|
+
goto done;
|
778
|
+
|
779
|
+
if (options.cwd) {
|
780
|
+
/* Explicit cwd */
|
781
|
+
err = uv_utf8_to_utf16_alloc(options.cwd, &cwd);
|
782
|
+
if (err.code != UV_OK)
|
783
|
+
goto done;
|
784
|
+
|
785
|
+
} else {
|
786
|
+
/* Inherit cwd */
|
787
|
+
DWORD cwd_len, r;
|
788
|
+
|
789
|
+
cwd_len = GetCurrentDirectoryW(0, NULL);
|
790
|
+
if (!cwd_len) {
|
791
|
+
err = uv__new_sys_error(GetLastError());
|
792
|
+
goto done;
|
793
|
+
}
|
794
|
+
|
795
|
+
cwd = (WCHAR*) malloc(cwd_len * sizeof(WCHAR));
|
796
|
+
if (cwd == NULL) {
|
797
|
+
err = uv__new_artificial_error(UV_ENOMEM);
|
798
|
+
goto done;
|
799
|
+
}
|
800
|
+
|
801
|
+
r = GetCurrentDirectoryW(cwd_len, cwd);
|
802
|
+
if (r == 0 || r >= cwd_len) {
|
803
|
+
err = uv__new_sys_error(GetLastError());
|
804
|
+
goto done;
|
805
|
+
}
|
806
|
+
}
|
807
|
+
|
808
|
+
/* Get PATH environment variable. */
|
809
|
+
{
|
810
|
+
DWORD path_len, r;
|
811
|
+
|
812
|
+
path_len = GetEnvironmentVariableW(L"PATH", NULL, 0);
|
813
|
+
if (path_len == 0) {
|
814
|
+
err = uv__new_sys_error(GetLastError());
|
815
|
+
goto done;
|
816
|
+
}
|
817
|
+
|
818
|
+
|
819
|
+
path = (WCHAR*) malloc(path_len * sizeof(WCHAR));
|
820
|
+
if (path == NULL) {
|
821
|
+
err = uv__new_artificial_error(UV_ENOMEM);
|
822
|
+
goto done;
|
823
|
+
}
|
824
|
+
|
825
|
+
r = GetEnvironmentVariableW(L"PATH", path, path_len);
|
826
|
+
if (r == 0 || r >= path_len) {
|
827
|
+
err = uv__new_sys_error(GetLastError());
|
828
|
+
goto done;
|
829
|
+
}
|
830
|
+
}
|
831
|
+
|
832
|
+
application_path = search_path(application,
|
833
|
+
cwd,
|
834
|
+
path);
|
835
|
+
if (application_path == NULL) {
|
836
|
+
/* Not found. */
|
837
|
+
err = uv__new_artificial_error(UV_ENOENT);
|
838
|
+
goto done;
|
839
|
+
}
|
840
|
+
|
841
|
+
|
842
|
+
err = uv__stdio_create(loop, &options, &process->child_stdio_buffer);
|
843
|
+
if (err.code != UV_OK)
|
844
|
+
goto done;
|
845
|
+
|
846
|
+
startup.cb = sizeof(startup);
|
847
|
+
startup.lpReserved = NULL;
|
848
|
+
startup.lpDesktop = NULL;
|
849
|
+
startup.lpTitle = NULL;
|
850
|
+
startup.dwFlags = STARTF_USESTDHANDLES;
|
851
|
+
startup.cbReserved2 = uv__stdio_size(process->child_stdio_buffer);
|
852
|
+
startup.lpReserved2 = (BYTE*) process->child_stdio_buffer;
|
853
|
+
startup.hStdInput = uv__stdio_handle(process->child_stdio_buffer, 0);
|
854
|
+
startup.hStdOutput = uv__stdio_handle(process->child_stdio_buffer, 1);
|
855
|
+
startup.hStdError = uv__stdio_handle(process->child_stdio_buffer, 2);
|
856
|
+
|
857
|
+
process_flags = CREATE_UNICODE_ENVIRONMENT;
|
858
|
+
if (options.flags & UV_PROCESS_DETACHED) {
|
859
|
+
process_flags |= DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP;
|
860
|
+
}
|
861
|
+
|
862
|
+
if (CreateProcessW(application_path,
|
863
|
+
arguments,
|
864
|
+
NULL,
|
865
|
+
NULL,
|
866
|
+
1,
|
867
|
+
process_flags,
|
868
|
+
env,
|
869
|
+
cwd,
|
870
|
+
&startup,
|
871
|
+
&info)) {
|
872
|
+
/* Spawn succeeded */
|
873
|
+
process->process_handle = info.hProcess;
|
874
|
+
process->pid = info.dwProcessId;
|
875
|
+
|
876
|
+
/* Set IPC pid to all IPC pipes. */
|
877
|
+
for (i = 0; i < options.stdio_count; i++) {
|
878
|
+
const uv_stdio_container_t* fdopt = &options.stdio[i];
|
879
|
+
if (fdopt->flags & UV_CREATE_PIPE &&
|
880
|
+
fdopt->data.stream->type == UV_NAMED_PIPE &&
|
881
|
+
((uv_pipe_t*) fdopt->data.stream)->ipc) {
|
882
|
+
((uv_pipe_t*) fdopt->data.stream)->ipc_pid = info.dwProcessId;
|
883
|
+
}
|
884
|
+
}
|
885
|
+
|
886
|
+
/* Setup notifications for when the child process exits. */
|
887
|
+
result = RegisterWaitForSingleObject(&process->wait_handle,
|
888
|
+
process->process_handle, exit_wait_callback, (void*)process, INFINITE,
|
889
|
+
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE);
|
890
|
+
if (!result) {
|
891
|
+
uv_fatal_error(GetLastError(), "RegisterWaitForSingleObject");
|
892
|
+
}
|
893
|
+
|
894
|
+
CloseHandle(info.hThread);
|
895
|
+
|
896
|
+
} else {
|
897
|
+
/* CreateProcessW failed. */
|
898
|
+
err = uv__new_sys_error(GetLastError());
|
899
|
+
}
|
900
|
+
|
901
|
+
done:
|
902
|
+
free(application);
|
903
|
+
free(application_path);
|
904
|
+
free(arguments);
|
905
|
+
free(cwd);
|
906
|
+
free(env);
|
907
|
+
free(path);
|
908
|
+
|
909
|
+
process->spawn_error = err;
|
910
|
+
|
911
|
+
if (process->child_stdio_buffer != NULL) {
|
912
|
+
/* Clean up child stdio handles. */
|
913
|
+
uv__stdio_destroy(process->child_stdio_buffer);
|
914
|
+
process->child_stdio_buffer = NULL;
|
915
|
+
}
|
916
|
+
|
917
|
+
/* Make the handle active. It will remain active until the exit callback */
|
918
|
+
/* is made or the handle is closed, whichever happens first. */
|
919
|
+
uv__handle_start(process);
|
920
|
+
|
921
|
+
/* If an error happened, queue the exit req. */
|
922
|
+
if (err.code != UV_OK) {
|
923
|
+
process->exit_cb_pending = 1;
|
924
|
+
uv_insert_pending_req(loop, (uv_req_t*) &process->exit_req);
|
925
|
+
}
|
926
|
+
|
927
|
+
return 0;
|
928
|
+
}
|
929
|
+
|
930
|
+
|
931
|
+
static uv_err_t uv__kill(HANDLE process_handle, int signum) {
|
932
|
+
switch (signum) {
|
933
|
+
case SIGTERM:
|
934
|
+
case SIGKILL:
|
935
|
+
case SIGINT: {
|
936
|
+
/* Unconditionally terminate the process. On Windows, killed processes */
|
937
|
+
/* normally return 1. */
|
938
|
+
DWORD error, status;
|
939
|
+
|
940
|
+
if (TerminateProcess(process_handle, 1))
|
941
|
+
return uv_ok_;
|
942
|
+
|
943
|
+
/* If the process already exited before TerminateProcess was called, */
|
944
|
+
/* TerminateProcess will fail with ERROR_ACESS_DENIED. */
|
945
|
+
error = GetLastError();
|
946
|
+
if (error == ERROR_ACCESS_DENIED &&
|
947
|
+
GetExitCodeProcess(process_handle, &status) &&
|
948
|
+
status != STILL_ACTIVE) {
|
949
|
+
return uv__new_artificial_error(UV_ESRCH);
|
950
|
+
}
|
951
|
+
|
952
|
+
return uv__new_sys_error(error);
|
953
|
+
}
|
954
|
+
|
955
|
+
case 0: {
|
956
|
+
/* Health check: is the process still alive? */
|
957
|
+
DWORD status;
|
958
|
+
|
959
|
+
if (!GetExitCodeProcess(process_handle, &status))
|
960
|
+
return uv__new_sys_error(GetLastError());
|
961
|
+
|
962
|
+
if (status != STILL_ACTIVE)
|
963
|
+
return uv__new_artificial_error(UV_ESRCH);
|
964
|
+
|
965
|
+
return uv_ok_;
|
966
|
+
}
|
967
|
+
|
968
|
+
default:
|
969
|
+
/* Unsupported signal. */
|
970
|
+
return uv__new_artificial_error(UV_ENOSYS);
|
971
|
+
}
|
972
|
+
}
|
973
|
+
|
974
|
+
|
975
|
+
int uv_process_kill(uv_process_t* process, int signum) {
|
976
|
+
uv_err_t err;
|
977
|
+
|
978
|
+
if (process->process_handle == INVALID_HANDLE_VALUE) {
|
979
|
+
uv__set_artificial_error(process->loop, UV_EINVAL);
|
980
|
+
return -1;
|
981
|
+
}
|
982
|
+
|
983
|
+
err = uv__kill(process->process_handle, signum);
|
984
|
+
|
985
|
+
if (err.code != UV_OK) {
|
986
|
+
uv__set_error(process->loop, err.code, err.sys_errno_);
|
987
|
+
return -1;
|
988
|
+
}
|
989
|
+
|
990
|
+
process->exit_signal = signum;
|
991
|
+
|
992
|
+
return 0;
|
993
|
+
}
|
994
|
+
|
995
|
+
|
996
|
+
uv_err_t uv_kill(int pid, int signum) {
|
997
|
+
uv_err_t err;
|
998
|
+
HANDLE process_handle = OpenProcess(PROCESS_TERMINATE |
|
999
|
+
PROCESS_QUERY_INFORMATION, FALSE, pid);
|
1000
|
+
|
1001
|
+
if (process_handle == NULL) {
|
1002
|
+
if (GetLastError() == ERROR_INVALID_PARAMETER) {
|
1003
|
+
return uv__new_artificial_error(UV_ESRCH);
|
1004
|
+
} else {
|
1005
|
+
return uv__new_sys_error(GetLastError());
|
1006
|
+
}
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
err = uv__kill(process_handle, signum);
|
1010
|
+
CloseHandle(process_handle);
|
1011
|
+
|
1012
|
+
return err;
|
1013
|
+
}
|