|
160
|
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 "uv.h"
|
|
|
23 #include "internal.h"
|
|
|
24
|
|
|
25 #include <stdio.h>
|
|
|
26 #include <stdlib.h>
|
|
|
27 #include <assert.h>
|
|
|
28 #include <errno.h>
|
|
|
29 #include <signal.h>
|
|
|
30 #include <string.h>
|
|
|
31
|
|
|
32 #include <sys/types.h>
|
|
|
33 #include <sys/wait.h>
|
|
|
34 #include <unistd.h>
|
|
|
35 #include <fcntl.h>
|
|
|
36 #include <poll.h>
|
|
|
37
|
|
|
38 #if defined(__APPLE__)
|
|
|
39 # include <spawn.h>
|
|
|
40 # include <paths.h>
|
|
|
41 # include <sys/kauth.h>
|
|
|
42 # include <sys/types.h>
|
|
|
43 # include <sys/sysctl.h>
|
|
|
44 # include <dlfcn.h>
|
|
|
45 # include <crt_externs.h>
|
|
|
46 # include <xlocale.h>
|
|
|
47 # define environ (*_NSGetEnviron())
|
|
|
48
|
|
|
49 /* macOS 10.14 back does not define this constant */
|
|
|
50 # ifndef POSIX_SPAWN_SETSID
|
|
|
51 # define POSIX_SPAWN_SETSID 1024
|
|
|
52 # endif
|
|
|
53
|
|
|
54 #else
|
|
|
55 extern char **environ;
|
|
|
56 #endif
|
|
|
57
|
|
|
58 #if defined(__linux__) || \
|
|
|
59 defined(__GNU__)
|
|
|
60 # include <grp.h>
|
|
|
61 #endif
|
|
|
62
|
|
|
63 #if defined(__MVS__)
|
|
|
64 # include "zos-base.h"
|
|
|
65 #endif
|
|
|
66
|
|
|
67 #ifdef UV_HAVE_KQUEUE
|
|
|
68 #include <sys/event.h>
|
|
|
69 #else
|
|
|
70 #define UV_USE_SIGCHLD
|
|
|
71 #endif
|
|
|
72
|
|
|
73
|
|
|
74 #ifdef UV_USE_SIGCHLD
|
|
|
75 static void uv__chld(uv_signal_t* handle, int signum) {
|
|
|
76 assert(signum == SIGCHLD);
|
|
|
77 uv__wait_children(handle->loop);
|
|
|
78 }
|
|
|
79
|
|
|
80
|
|
|
81 int uv__process_init(uv_loop_t* loop) {
|
|
|
82 int err;
|
|
|
83
|
|
|
84 err = uv_signal_init(loop, &loop->child_watcher);
|
|
|
85 if (err)
|
|
|
86 return err;
|
|
|
87 uv__handle_unref(&loop->child_watcher);
|
|
|
88 loop->child_watcher.flags |= UV_HANDLE_INTERNAL;
|
|
|
89 return 0;
|
|
|
90 }
|
|
|
91
|
|
|
92
|
|
|
93 #else
|
|
|
94 int uv__process_init(uv_loop_t* loop) {
|
|
|
95 memset(&loop->child_watcher, 0, sizeof(loop->child_watcher));
|
|
|
96 return 0;
|
|
|
97 }
|
|
|
98 #endif
|
|
|
99
|
|
|
100
|
|
|
101 void uv__wait_children(uv_loop_t* loop) {
|
|
|
102 uv_process_t* process;
|
|
|
103 int exit_status;
|
|
|
104 int term_signal;
|
|
|
105 int status;
|
|
|
106 int options;
|
|
|
107 pid_t pid;
|
|
|
108 struct uv__queue pending;
|
|
|
109 struct uv__queue* q;
|
|
|
110 struct uv__queue* h;
|
|
|
111
|
|
|
112 uv__queue_init(&pending);
|
|
|
113
|
|
|
114 h = &loop->process_handles;
|
|
|
115 q = uv__queue_head(h);
|
|
|
116 while (q != h) {
|
|
|
117 process = uv__queue_data(q, uv_process_t, queue);
|
|
|
118 q = uv__queue_next(q);
|
|
|
119
|
|
|
120 #ifndef UV_USE_SIGCHLD
|
|
|
121 if ((process->flags & UV_HANDLE_REAP) == 0)
|
|
|
122 continue;
|
|
|
123 options = 0;
|
|
|
124 process->flags &= ~UV_HANDLE_REAP;
|
|
|
125 loop->nfds--;
|
|
|
126 #else
|
|
|
127 options = WNOHANG;
|
|
|
128 #endif
|
|
|
129
|
|
|
130 do
|
|
|
131 pid = waitpid(process->pid, &status, options);
|
|
|
132 while (pid == -1 && errno == EINTR);
|
|
|
133
|
|
|
134 #ifdef UV_USE_SIGCHLD
|
|
|
135 if (pid == 0) /* Not yet exited */
|
|
|
136 continue;
|
|
|
137 #endif
|
|
|
138
|
|
|
139 if (pid == -1) {
|
|
|
140 if (errno != ECHILD)
|
|
|
141 abort();
|
|
|
142 /* The child died, and we missed it. This probably means someone else
|
|
|
143 * stole the waitpid from us. Handle this by not handling it at all. */
|
|
|
144 continue;
|
|
|
145 }
|
|
|
146
|
|
|
147 assert(pid == process->pid);
|
|
|
148 process->status = status;
|
|
|
149 uv__queue_remove(&process->queue);
|
|
|
150 uv__queue_insert_tail(&pending, &process->queue);
|
|
|
151 }
|
|
|
152
|
|
|
153 h = &pending;
|
|
|
154 q = uv__queue_head(h);
|
|
|
155 while (q != h) {
|
|
|
156 process = uv__queue_data(q, uv_process_t, queue);
|
|
|
157 q = uv__queue_next(q);
|
|
|
158
|
|
|
159 uv__queue_remove(&process->queue);
|
|
|
160 uv__queue_init(&process->queue);
|
|
|
161 uv__handle_stop(process);
|
|
|
162
|
|
|
163 if (process->exit_cb == NULL)
|
|
|
164 continue;
|
|
|
165
|
|
|
166 exit_status = 0;
|
|
|
167 if (WIFEXITED(process->status))
|
|
|
168 exit_status = WEXITSTATUS(process->status);
|
|
|
169
|
|
|
170 term_signal = 0;
|
|
|
171 if (WIFSIGNALED(process->status))
|
|
|
172 term_signal = WTERMSIG(process->status);
|
|
|
173
|
|
|
174 process->exit_cb(process, exit_status, term_signal);
|
|
|
175 }
|
|
|
176 assert(uv__queue_empty(&pending));
|
|
|
177 }
|
|
|
178
|
|
|
179 /*
|
|
|
180 * Used for initializing stdio streams like options.stdin_stream. Returns
|
|
|
181 * zero on success. See also the cleanup section in uv_spawn().
|
|
|
182 */
|
|
|
183 #if !(defined(__APPLE__) && (TARGET_OS_TV || TARGET_OS_WATCH))
|
|
|
184 /* execvp is marked __WATCHOS_PROHIBITED __TVOS_PROHIBITED, so must be
|
|
|
185 * avoided. Since this isn't called on those targets, the function
|
|
|
186 * doesn't even need to be defined for them.
|
|
|
187 */
|
|
|
188 static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) {
|
|
|
189 int mask;
|
|
|
190 int fd;
|
|
|
191 int ret;
|
|
|
192 int size;
|
|
|
193 int i;
|
|
|
194
|
|
|
195 mask = UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM;
|
|
|
196 size = 64 * 1024;
|
|
|
197
|
|
|
198 switch (container->flags & mask) {
|
|
|
199 case UV_IGNORE:
|
|
|
200 return 0;
|
|
|
201
|
|
|
202 case UV_CREATE_PIPE:
|
|
|
203 assert(container->data.stream != NULL);
|
|
|
204 if (container->data.stream->type != UV_NAMED_PIPE)
|
|
|
205 return UV_EINVAL;
|
|
|
206 else {
|
|
|
207 ret = uv_socketpair(SOCK_STREAM, 0, fds, 0, 0);
|
|
|
208
|
|
|
209 if (ret == 0)
|
|
|
210 for (i = 0; i < 2; i++) {
|
|
|
211 setsockopt(fds[i], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
|
|
|
212 setsockopt(fds[i], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
|
|
|
213 }
|
|
|
214 }
|
|
|
215
|
|
|
216 return ret;
|
|
|
217
|
|
|
218 case UV_INHERIT_FD:
|
|
|
219 case UV_INHERIT_STREAM:
|
|
|
220 if (container->flags & UV_INHERIT_FD)
|
|
|
221 fd = container->data.fd;
|
|
|
222 else
|
|
|
223 fd = uv__stream_fd(container->data.stream);
|
|
|
224
|
|
|
225 if (fd == -1)
|
|
|
226 return UV_EINVAL;
|
|
|
227
|
|
|
228 fds[1] = fd;
|
|
|
229 return 0;
|
|
|
230
|
|
|
231 default:
|
|
|
232 assert(0 && "Unexpected flags");
|
|
|
233 return UV_EINVAL;
|
|
|
234 }
|
|
|
235 }
|
|
|
236
|
|
|
237
|
|
|
238 static int uv__process_open_stream(uv_stdio_container_t* container,
|
|
|
239 int pipefds[2]) {
|
|
|
240 int flags;
|
|
|
241 int err;
|
|
|
242
|
|
|
243 if (!(container->flags & UV_CREATE_PIPE) || pipefds[0] < 0)
|
|
|
244 return 0;
|
|
|
245
|
|
|
246 err = uv__close(pipefds[1]);
|
|
|
247 if (err != 0)
|
|
|
248 abort();
|
|
|
249
|
|
|
250 pipefds[1] = -1;
|
|
|
251 uv__nonblock(pipefds[0], 1);
|
|
|
252
|
|
|
253 flags = 0;
|
|
|
254 if (container->flags & UV_WRITABLE_PIPE)
|
|
|
255 flags |= UV_HANDLE_READABLE;
|
|
|
256 if (container->flags & UV_READABLE_PIPE)
|
|
|
257 flags |= UV_HANDLE_WRITABLE;
|
|
|
258
|
|
|
259 return uv__stream_open(container->data.stream, pipefds[0], flags);
|
|
|
260 }
|
|
|
261
|
|
|
262
|
|
|
263 static void uv__process_close_stream(uv_stdio_container_t* container) {
|
|
|
264 if (!(container->flags & UV_CREATE_PIPE)) return;
|
|
|
265 uv__stream_close(container->data.stream);
|
|
|
266 }
|
|
|
267
|
|
|
268
|
|
|
269 static void uv__write_int(int fd, int val) {
|
|
|
270 ssize_t n;
|
|
|
271
|
|
|
272 do
|
|
|
273 n = write(fd, &val, sizeof(val));
|
|
|
274 while (n == -1 && errno == EINTR);
|
|
|
275
|
|
|
276 /* The write might have failed (e.g. if the parent process has died),
|
|
|
277 * but we have nothing left but to _exit ourself now too. */
|
|
|
278 _exit(127);
|
|
|
279 }
|
|
|
280
|
|
|
281
|
|
|
282 static void uv__write_errno(int error_fd) {
|
|
|
283 uv__write_int(error_fd, UV__ERR(errno));
|
|
|
284 }
|
|
|
285
|
|
|
286
|
|
|
287 static void uv__process_child_init(const uv_process_options_t* options,
|
|
|
288 int stdio_count,
|
|
|
289 int (*pipes)[2],
|
|
|
290 int error_fd) {
|
|
|
291 sigset_t signewset;
|
|
|
292 int close_fd;
|
|
|
293 int use_fd;
|
|
|
294 int fd;
|
|
|
295 int n;
|
|
|
296
|
|
|
297 /* Reset signal disposition first. Use a hard-coded limit because NSIG is not
|
|
|
298 * fixed on Linux: it's either 32, 34 or 64, depending on whether RT signals
|
|
|
299 * are enabled. We are not allowed to touch RT signal handlers, glibc uses
|
|
|
300 * them internally.
|
|
|
301 */
|
|
|
302 for (n = 1; n < 32; n += 1) {
|
|
|
303 if (n == SIGKILL || n == SIGSTOP)
|
|
|
304 continue; /* Can't be changed. */
|
|
|
305
|
|
|
306 #if defined(__HAIKU__)
|
|
|
307 if (n == SIGKILLTHR)
|
|
|
308 continue; /* Can't be changed. */
|
|
|
309 #endif
|
|
|
310
|
|
|
311 if (SIG_ERR != signal(n, SIG_DFL))
|
|
|
312 continue;
|
|
|
313
|
|
|
314 uv__write_errno(error_fd);
|
|
|
315 }
|
|
|
316
|
|
|
317 if (options->flags & UV_PROCESS_DETACHED)
|
|
|
318 setsid();
|
|
|
319
|
|
|
320 /* First duplicate low numbered fds, since it's not safe to duplicate them,
|
|
|
321 * they could get replaced. Example: swapping stdout and stderr; without
|
|
|
322 * this fd 2 (stderr) would be duplicated into fd 1, thus making both
|
|
|
323 * stdout and stderr go to the same fd, which was not the intention. */
|
|
|
324 for (fd = 0; fd < stdio_count; fd++) {
|
|
|
325 use_fd = pipes[fd][1];
|
|
|
326 if (use_fd < 0 || use_fd >= fd)
|
|
|
327 continue;
|
|
|
328 #ifdef F_DUPFD_CLOEXEC /* POSIX 2008 */
|
|
|
329 pipes[fd][1] = fcntl(use_fd, F_DUPFD_CLOEXEC, stdio_count);
|
|
|
330 #else
|
|
|
331 pipes[fd][1] = fcntl(use_fd, F_DUPFD, stdio_count);
|
|
|
332 #endif
|
|
|
333 if (pipes[fd][1] == -1)
|
|
|
334 uv__write_errno(error_fd);
|
|
|
335 #ifndef F_DUPFD_CLOEXEC /* POSIX 2008 */
|
|
|
336 n = uv__cloexec(pipes[fd][1], 1);
|
|
|
337 if (n)
|
|
|
338 uv__write_int(error_fd, n);
|
|
|
339 #endif
|
|
|
340 }
|
|
|
341
|
|
|
342 for (fd = 0; fd < stdio_count; fd++) {
|
|
|
343 close_fd = -1;
|
|
|
344 use_fd = pipes[fd][1];
|
|
|
345
|
|
|
346 if (use_fd < 0) {
|
|
|
347 if (fd >= 3)
|
|
|
348 continue;
|
|
|
349 else {
|
|
|
350 /* Redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
|
|
|
351 * set. */
|
|
|
352 uv__close_nocheckstdio(fd); /* Free up fd, if it happens to be open. */
|
|
|
353 use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
|
|
|
354 close_fd = use_fd;
|
|
|
355
|
|
|
356 if (use_fd < 0)
|
|
|
357 uv__write_errno(error_fd);
|
|
|
358 }
|
|
|
359 }
|
|
|
360
|
|
|
361 if (fd == use_fd) {
|
|
|
362 if (close_fd == -1) {
|
|
|
363 n = uv__cloexec(use_fd, 0);
|
|
|
364 if (n)
|
|
|
365 uv__write_int(error_fd, n);
|
|
|
366 }
|
|
|
367 }
|
|
|
368 else {
|
|
|
369 fd = dup2(use_fd, fd);
|
|
|
370 }
|
|
|
371
|
|
|
372 if (fd == -1)
|
|
|
373 uv__write_errno(error_fd);
|
|
|
374
|
|
|
375 if (fd <= 2 && close_fd == -1)
|
|
|
376 uv__nonblock_fcntl(fd, 0);
|
|
|
377
|
|
|
378 if (close_fd >= stdio_count)
|
|
|
379 uv__close(close_fd);
|
|
|
380 }
|
|
|
381
|
|
|
382 if (options->cwd != NULL && chdir(options->cwd))
|
|
|
383 uv__write_errno(error_fd);
|
|
|
384
|
|
|
385 if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
|
|
|
386 /* When dropping privileges from root, the `setgroups` call will
|
|
|
387 * remove any extraneous groups. If we don't call this, then
|
|
|
388 * even though our uid has dropped, we may still have groups
|
|
|
389 * that enable us to do super-user things. This will fail if we
|
|
|
390 * aren't root, so don't bother checking the return value, this
|
|
|
391 * is just done as an optimistic privilege dropping function.
|
|
|
392 */
|
|
|
393 SAVE_ERRNO(setgroups(0, NULL));
|
|
|
394 }
|
|
|
395
|
|
|
396 if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid))
|
|
|
397 uv__write_errno(error_fd);
|
|
|
398
|
|
|
399 if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid))
|
|
|
400 uv__write_errno(error_fd);
|
|
|
401
|
|
|
402 if (options->env != NULL)
|
|
|
403 environ = options->env;
|
|
|
404
|
|
|
405 /* Reset signal mask just before exec. */
|
|
|
406 sigemptyset(&signewset);
|
|
|
407 if (sigprocmask(SIG_SETMASK, &signewset, NULL) != 0)
|
|
|
408 abort();
|
|
|
409
|
|
|
410 #ifdef __MVS__
|
|
|
411 execvpe(options->file, options->args, environ);
|
|
|
412 #else
|
|
|
413 execvp(options->file, options->args);
|
|
|
414 #endif
|
|
|
415
|
|
|
416 uv__write_errno(error_fd);
|
|
|
417 }
|
|
|
418
|
|
|
419
|
|
|
420 #if defined(__APPLE__)
|
|
|
421 typedef struct uv__posix_spawn_fncs_tag {
|
|
|
422 struct {
|
|
|
423 int (*addchdir_np)(const posix_spawn_file_actions_t *, const char *);
|
|
|
424 } file_actions;
|
|
|
425 } uv__posix_spawn_fncs_t;
|
|
|
426
|
|
|
427
|
|
|
428 static uv_once_t posix_spawn_init_once = UV_ONCE_INIT;
|
|
|
429 static uv__posix_spawn_fncs_t posix_spawn_fncs;
|
|
|
430 static int posix_spawn_can_use_setsid;
|
|
|
431
|
|
|
432
|
|
|
433 static void uv__spawn_init_posix_spawn_fncs(void) {
|
|
|
434 /* Try to locate all non-portable functions at runtime */
|
|
|
435 posix_spawn_fncs.file_actions.addchdir_np =
|
|
|
436 dlsym(RTLD_DEFAULT, "posix_spawn_file_actions_addchdir_np");
|
|
|
437 }
|
|
|
438
|
|
|
439
|
|
|
440 static void uv__spawn_init_can_use_setsid(void) {
|
|
|
441 int which[] = {CTL_KERN, KERN_OSRELEASE};
|
|
|
442 unsigned major;
|
|
|
443 unsigned minor;
|
|
|
444 unsigned patch;
|
|
|
445 char buf[256];
|
|
|
446 size_t len;
|
|
|
447
|
|
|
448 len = sizeof(buf);
|
|
|
449 if (sysctl(which, ARRAY_SIZE(which), buf, &len, NULL, 0))
|
|
|
450 return;
|
|
|
451
|
|
|
452 /* NULL specifies to use LC_C_LOCALE */
|
|
|
453 if (3 != sscanf_l(buf, NULL, "%u.%u.%u", &major, &minor, &patch))
|
|
|
454 return;
|
|
|
455
|
|
|
456 posix_spawn_can_use_setsid = (major >= 19); /* macOS Catalina */
|
|
|
457 }
|
|
|
458
|
|
|
459
|
|
|
460 static void uv__spawn_init_posix_spawn(void) {
|
|
|
461 /* Init handles to all potentially non-defined functions */
|
|
|
462 uv__spawn_init_posix_spawn_fncs();
|
|
|
463
|
|
|
464 /* Init feature detection for POSIX_SPAWN_SETSID flag */
|
|
|
465 uv__spawn_init_can_use_setsid();
|
|
|
466 }
|
|
|
467
|
|
|
468
|
|
|
469 static int uv__spawn_set_posix_spawn_attrs(
|
|
|
470 posix_spawnattr_t* attrs,
|
|
|
471 const uv__posix_spawn_fncs_t* posix_spawn_fncs,
|
|
|
472 const uv_process_options_t* options) {
|
|
|
473 int err;
|
|
|
474 unsigned int flags;
|
|
|
475 sigset_t signal_set;
|
|
|
476
|
|
|
477 err = posix_spawnattr_init(attrs);
|
|
|
478 if (err != 0) {
|
|
|
479 /* If initialization fails, no need to de-init, just return */
|
|
|
480 return err;
|
|
|
481 }
|
|
|
482
|
|
|
483 if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
|
|
|
484 /* kauth_cred_issuser currently requires exactly uid == 0 for these
|
|
|
485 * posixspawn_attrs (set_groups_np, setuid_np, setgid_np), which deviates
|
|
|
486 * from the normal specification of setuid (which also uses euid), and they
|
|
|
487 * are also undocumented syscalls, so we do not use them. */
|
|
|
488 err = ENOSYS;
|
|
|
489 goto error;
|
|
|
490 }
|
|
|
491
|
|
|
492 /* Set flags for spawn behavior
|
|
|
493 * 1) POSIX_SPAWN_CLOEXEC_DEFAULT: (Apple Extension) All descriptors in the
|
|
|
494 * parent will be treated as if they had been created with O_CLOEXEC. The
|
|
|
495 * only fds that will be passed on to the child are those manipulated by
|
|
|
496 * the file actions
|
|
|
497 * 2) POSIX_SPAWN_SETSIGDEF: Signals mentioned in spawn-sigdefault in the
|
|
|
498 * spawn attributes will be reset to behave as their default
|
|
|
499 * 3) POSIX_SPAWN_SETSIGMASK: Signal mask will be set to the value of
|
|
|
500 * spawn-sigmask in attributes
|
|
|
501 * 4) POSIX_SPAWN_SETSID: Make the process a new session leader if a detached
|
|
|
502 * session was requested. */
|
|
|
503 flags = POSIX_SPAWN_CLOEXEC_DEFAULT |
|
|
|
504 POSIX_SPAWN_SETSIGDEF |
|
|
|
505 POSIX_SPAWN_SETSIGMASK;
|
|
|
506 if (options->flags & UV_PROCESS_DETACHED) {
|
|
|
507 /* If running on a version of macOS where this flag is not supported,
|
|
|
508 * revert back to the fork/exec flow. Otherwise posix_spawn will
|
|
|
509 * silently ignore the flag. */
|
|
|
510 if (!posix_spawn_can_use_setsid) {
|
|
|
511 err = ENOSYS;
|
|
|
512 goto error;
|
|
|
513 }
|
|
|
514
|
|
|
515 flags |= POSIX_SPAWN_SETSID;
|
|
|
516 }
|
|
|
517 err = posix_spawnattr_setflags(attrs, flags);
|
|
|
518 if (err != 0)
|
|
|
519 goto error;
|
|
|
520
|
|
|
521 /* Reset all signal the child to their default behavior */
|
|
|
522 sigfillset(&signal_set);
|
|
|
523 err = posix_spawnattr_setsigdefault(attrs, &signal_set);
|
|
|
524 if (err != 0)
|
|
|
525 goto error;
|
|
|
526
|
|
|
527 /* Reset the signal mask for all signals */
|
|
|
528 sigemptyset(&signal_set);
|
|
|
529 err = posix_spawnattr_setsigmask(attrs, &signal_set);
|
|
|
530 if (err != 0)
|
|
|
531 goto error;
|
|
|
532
|
|
|
533 return err;
|
|
|
534
|
|
|
535 error:
|
|
|
536 (void) posix_spawnattr_destroy(attrs);
|
|
|
537 return err;
|
|
|
538 }
|
|
|
539
|
|
|
540
|
|
|
541 static int uv__spawn_set_posix_spawn_file_actions(
|
|
|
542 posix_spawn_file_actions_t* actions,
|
|
|
543 const uv__posix_spawn_fncs_t* posix_spawn_fncs,
|
|
|
544 const uv_process_options_t* options,
|
|
|
545 int stdio_count,
|
|
|
546 int (*pipes)[2]) {
|
|
|
547 int fd;
|
|
|
548 int fd2;
|
|
|
549 int use_fd;
|
|
|
550 int err;
|
|
|
551
|
|
|
552 err = posix_spawn_file_actions_init(actions);
|
|
|
553 if (err != 0) {
|
|
|
554 /* If initialization fails, no need to de-init, just return */
|
|
|
555 return err;
|
|
|
556 }
|
|
|
557
|
|
|
558 /* Set the current working directory if requested */
|
|
|
559 if (options->cwd != NULL) {
|
|
|
560 if (posix_spawn_fncs->file_actions.addchdir_np == NULL) {
|
|
|
561 err = ENOSYS;
|
|
|
562 goto error;
|
|
|
563 }
|
|
|
564
|
|
|
565 err = posix_spawn_fncs->file_actions.addchdir_np(actions, options->cwd);
|
|
|
566 if (err != 0)
|
|
|
567 goto error;
|
|
|
568 }
|
|
|
569
|
|
|
570 /* Do not return ENOSYS after this point, as we may mutate pipes. */
|
|
|
571
|
|
|
572 /* First duplicate low numbered fds, since it's not safe to duplicate them,
|
|
|
573 * they could get replaced. Example: swapping stdout and stderr; without
|
|
|
574 * this fd 2 (stderr) would be duplicated into fd 1, thus making both
|
|
|
575 * stdout and stderr go to the same fd, which was not the intention. */
|
|
|
576 for (fd = 0; fd < stdio_count; fd++) {
|
|
|
577 use_fd = pipes[fd][1];
|
|
|
578 if (use_fd < 0 || use_fd >= fd)
|
|
|
579 continue;
|
|
|
580 use_fd = stdio_count;
|
|
|
581 for (fd2 = 0; fd2 < stdio_count; fd2++) {
|
|
|
582 /* If we were not setting POSIX_SPAWN_CLOEXEC_DEFAULT, we would need to
|
|
|
583 * also consider whether fcntl(fd, F_GETFD) returned without the
|
|
|
584 * FD_CLOEXEC flag set. */
|
|
|
585 if (pipes[fd2][1] == use_fd) {
|
|
|
586 use_fd++;
|
|
|
587 fd2 = 0;
|
|
|
588 }
|
|
|
589 }
|
|
|
590 err = posix_spawn_file_actions_adddup2(
|
|
|
591 actions,
|
|
|
592 pipes[fd][1],
|
|
|
593 use_fd);
|
|
|
594 assert(err != ENOSYS);
|
|
|
595 if (err != 0)
|
|
|
596 goto error;
|
|
|
597 pipes[fd][1] = use_fd;
|
|
|
598 }
|
|
|
599
|
|
|
600 /* Second, move the descriptors into their respective places */
|
|
|
601 for (fd = 0; fd < stdio_count; fd++) {
|
|
|
602 use_fd = pipes[fd][1];
|
|
|
603 if (use_fd < 0) {
|
|
|
604 if (fd >= 3)
|
|
|
605 continue;
|
|
|
606 else {
|
|
|
607 /* If ignored, redirect to (or from) /dev/null, */
|
|
|
608 err = posix_spawn_file_actions_addopen(
|
|
|
609 actions,
|
|
|
610 fd,
|
|
|
611 "/dev/null",
|
|
|
612 fd == 0 ? O_RDONLY : O_RDWR,
|
|
|
613 0);
|
|
|
614 assert(err != ENOSYS);
|
|
|
615 if (err != 0)
|
|
|
616 goto error;
|
|
|
617 continue;
|
|
|
618 }
|
|
|
619 }
|
|
|
620
|
|
|
621 if (fd == use_fd)
|
|
|
622 err = posix_spawn_file_actions_addinherit_np(actions, fd);
|
|
|
623 else
|
|
|
624 err = posix_spawn_file_actions_adddup2(actions, use_fd, fd);
|
|
|
625 assert(err != ENOSYS);
|
|
|
626 if (err != 0)
|
|
|
627 goto error;
|
|
|
628
|
|
|
629 /* Make sure the fd is marked as non-blocking (state shared between child
|
|
|
630 * and parent). */
|
|
|
631 uv__nonblock_fcntl(use_fd, 0);
|
|
|
632 }
|
|
|
633
|
|
|
634 /* Finally, close all the superfluous descriptors */
|
|
|
635 for (fd = 0; fd < stdio_count; fd++) {
|
|
|
636 use_fd = pipes[fd][1];
|
|
|
637 if (use_fd < stdio_count)
|
|
|
638 continue;
|
|
|
639
|
|
|
640 /* Check if we already closed this. */
|
|
|
641 for (fd2 = 0; fd2 < fd; fd2++) {
|
|
|
642 if (pipes[fd2][1] == use_fd)
|
|
|
643 break;
|
|
|
644 }
|
|
|
645 if (fd2 < fd)
|
|
|
646 continue;
|
|
|
647
|
|
|
648 err = posix_spawn_file_actions_addclose(actions, use_fd);
|
|
|
649 assert(err != ENOSYS);
|
|
|
650 if (err != 0)
|
|
|
651 goto error;
|
|
|
652 }
|
|
|
653
|
|
|
654 return 0;
|
|
|
655
|
|
|
656 error:
|
|
|
657 (void) posix_spawn_file_actions_destroy(actions);
|
|
|
658 return err;
|
|
|
659 }
|
|
|
660
|
|
|
661 char* uv__spawn_find_path_in_env(char** env) {
|
|
|
662 char** env_iterator;
|
|
|
663 const char path_var[] = "PATH=";
|
|
|
664
|
|
|
665 /* Look for an environment variable called PATH in the
|
|
|
666 * provided env array, and return its value if found */
|
|
|
667 for (env_iterator = env; *env_iterator != NULL; env_iterator++) {
|
|
|
668 if (strncmp(*env_iterator, path_var, sizeof(path_var) - 1) == 0) {
|
|
|
669 /* Found "PATH=" at the beginning of the string */
|
|
|
670 return *env_iterator + sizeof(path_var) - 1;
|
|
|
671 }
|
|
|
672 }
|
|
|
673
|
|
|
674 return NULL;
|
|
|
675 }
|
|
|
676
|
|
|
677
|
|
|
678 static int uv__spawn_resolve_and_spawn(const uv_process_options_t* options,
|
|
|
679 posix_spawnattr_t* attrs,
|
|
|
680 posix_spawn_file_actions_t* actions,
|
|
|
681 pid_t* pid) {
|
|
|
682 const char *p;
|
|
|
683 const char *z;
|
|
|
684 const char *path;
|
|
|
685 size_t l;
|
|
|
686 size_t k;
|
|
|
687 int err;
|
|
|
688 int seen_eacces;
|
|
|
689
|
|
|
690 path = NULL;
|
|
|
691 err = -1;
|
|
|
692 seen_eacces = 0;
|
|
|
693
|
|
|
694 /* Short circuit for erroneous case */
|
|
|
695 if (options->file == NULL)
|
|
|
696 return ENOENT;
|
|
|
697
|
|
|
698 /* The environment for the child process is that of the parent unless overridden
|
|
|
699 * by options->env */
|
|
|
700 char** env = environ;
|
|
|
701 if (options->env != NULL)
|
|
|
702 env = options->env;
|
|
|
703
|
|
|
704 /* If options->file contains a slash, posix_spawn/posix_spawnp should behave
|
|
|
705 * the same, and do not involve PATH resolution at all. The libc
|
|
|
706 * `posix_spawnp` provided by Apple is buggy (since 10.15), so we now emulate it
|
|
|
707 * here, per https://github.com/libuv/libuv/pull/3583. */
|
|
|
708 if (strchr(options->file, '/') != NULL) {
|
|
|
709 do
|
|
|
710 err = posix_spawn(pid, options->file, actions, attrs, options->args, env);
|
|
|
711 while (err == EINTR);
|
|
|
712 return err;
|
|
|
713 }
|
|
|
714
|
|
|
715 /* Look for the definition of PATH in the provided env */
|
|
|
716 path = uv__spawn_find_path_in_env(env);
|
|
|
717
|
|
|
718 /* The following resolution logic (execvpe emulation) is copied from
|
|
|
719 * https://git.musl-libc.org/cgit/musl/tree/src/process/execvp.c
|
|
|
720 * and adapted to work for our specific usage */
|
|
|
721
|
|
|
722 /* If no path was provided in env, use the default value
|
|
|
723 * to look for the executable */
|
|
|
724 if (path == NULL)
|
|
|
725 path = _PATH_DEFPATH;
|
|
|
726
|
|
|
727 k = strnlen(options->file, NAME_MAX + 1);
|
|
|
728 if (k > NAME_MAX)
|
|
|
729 return ENAMETOOLONG;
|
|
|
730
|
|
|
731 l = strnlen(path, PATH_MAX - 1) + 1;
|
|
|
732
|
|
|
733 for (p = path;; p = z) {
|
|
|
734 /* Compose the new process file from the entry in the PATH
|
|
|
735 * environment variable and the actual file name */
|
|
|
736 char b[PATH_MAX + NAME_MAX];
|
|
|
737 z = strchr(p, ':');
|
|
|
738 if (!z)
|
|
|
739 z = p + strlen(p);
|
|
|
740 if ((size_t)(z - p) >= l) {
|
|
|
741 if (!*z++)
|
|
|
742 break;
|
|
|
743
|
|
|
744 continue;
|
|
|
745 }
|
|
|
746 memcpy(b, p, z - p);
|
|
|
747 b[z - p] = '/';
|
|
|
748 memcpy(b + (z - p) + (z > p), options->file, k + 1);
|
|
|
749
|
|
|
750 /* Try to spawn the new process file. If it fails with ENOENT, the
|
|
|
751 * new process file is not in this PATH entry, continue with the next
|
|
|
752 * PATH entry. */
|
|
|
753 do
|
|
|
754 err = posix_spawn(pid, b, actions, attrs, options->args, env);
|
|
|
755 while (err == EINTR);
|
|
|
756
|
|
|
757 switch (err) {
|
|
|
758 case EACCES:
|
|
|
759 seen_eacces = 1;
|
|
|
760 break; /* continue search */
|
|
|
761 case ENOENT:
|
|
|
762 case ENOTDIR:
|
|
|
763 break; /* continue search */
|
|
|
764 default:
|
|
|
765 return err;
|
|
|
766 }
|
|
|
767
|
|
|
768 if (!*z++)
|
|
|
769 break;
|
|
|
770 }
|
|
|
771
|
|
|
772 if (seen_eacces)
|
|
|
773 return EACCES;
|
|
|
774 return err;
|
|
|
775 }
|
|
|
776
|
|
|
777
|
|
|
778 static int uv__spawn_and_init_child_posix_spawn(
|
|
|
779 const uv_process_options_t* options,
|
|
|
780 int stdio_count,
|
|
|
781 int (*pipes)[2],
|
|
|
782 pid_t* pid,
|
|
|
783 const uv__posix_spawn_fncs_t* posix_spawn_fncs) {
|
|
|
784 int err;
|
|
|
785 posix_spawnattr_t attrs;
|
|
|
786 posix_spawn_file_actions_t actions;
|
|
|
787
|
|
|
788 err = uv__spawn_set_posix_spawn_attrs(&attrs, posix_spawn_fncs, options);
|
|
|
789 if (err != 0)
|
|
|
790 goto error;
|
|
|
791
|
|
|
792 /* This may mutate pipes. */
|
|
|
793 err = uv__spawn_set_posix_spawn_file_actions(&actions,
|
|
|
794 posix_spawn_fncs,
|
|
|
795 options,
|
|
|
796 stdio_count,
|
|
|
797 pipes);
|
|
|
798 if (err != 0) {
|
|
|
799 (void) posix_spawnattr_destroy(&attrs);
|
|
|
800 goto error;
|
|
|
801 }
|
|
|
802
|
|
|
803 /* Try to spawn options->file resolving in the provided environment
|
|
|
804 * if any */
|
|
|
805 err = uv__spawn_resolve_and_spawn(options, &attrs, &actions, pid);
|
|
|
806 assert(err != ENOSYS);
|
|
|
807
|
|
|
808 /* Destroy the actions/attributes */
|
|
|
809 (void) posix_spawn_file_actions_destroy(&actions);
|
|
|
810 (void) posix_spawnattr_destroy(&attrs);
|
|
|
811
|
|
|
812 error:
|
|
|
813 /* In an error situation, the attributes and file actions are
|
|
|
814 * already destroyed, only the happy path requires cleanup */
|
|
|
815 return UV__ERR(err);
|
|
|
816 }
|
|
|
817 #endif
|
|
|
818
|
|
|
819 static int uv__spawn_and_init_child_fork(const uv_process_options_t* options,
|
|
|
820 int stdio_count,
|
|
|
821 int (*pipes)[2],
|
|
|
822 int error_fd,
|
|
|
823 pid_t* pid) {
|
|
|
824 sigset_t signewset;
|
|
|
825 sigset_t sigoldset;
|
|
|
826
|
|
|
827 /* Start the child with most signals blocked, to avoid any issues before we
|
|
|
828 * can reset them, but allow program failures to exit (and not hang). */
|
|
|
829 sigfillset(&signewset);
|
|
|
830 sigdelset(&signewset, SIGKILL);
|
|
|
831 sigdelset(&signewset, SIGSTOP);
|
|
|
832 sigdelset(&signewset, SIGTRAP);
|
|
|
833 sigdelset(&signewset, SIGSEGV);
|
|
|
834 sigdelset(&signewset, SIGBUS);
|
|
|
835 sigdelset(&signewset, SIGILL);
|
|
|
836 sigdelset(&signewset, SIGSYS);
|
|
|
837 sigdelset(&signewset, SIGABRT);
|
|
|
838 if (pthread_sigmask(SIG_BLOCK, &signewset, &sigoldset) != 0)
|
|
|
839 abort();
|
|
|
840
|
|
|
841 *pid = fork();
|
|
|
842
|
|
|
843 if (*pid == 0) {
|
|
|
844 /* Fork succeeded, in the child process */
|
|
|
845 uv__process_child_init(options, stdio_count, pipes, error_fd);
|
|
|
846 abort();
|
|
|
847 }
|
|
|
848
|
|
|
849 if (pthread_sigmask(SIG_SETMASK, &sigoldset, NULL) != 0)
|
|
|
850 abort();
|
|
|
851
|
|
|
852 if (*pid == -1)
|
|
|
853 /* Failed to fork */
|
|
|
854 return UV__ERR(errno);
|
|
|
855
|
|
|
856 /* Fork succeeded, in the parent process */
|
|
|
857 return 0;
|
|
|
858 }
|
|
|
859
|
|
|
860 static int uv__spawn_and_init_child(
|
|
|
861 uv_loop_t* loop,
|
|
|
862 const uv_process_options_t* options,
|
|
|
863 int stdio_count,
|
|
|
864 int (*pipes)[2],
|
|
|
865 pid_t* pid) {
|
|
|
866 int signal_pipe[2] = { -1, -1 };
|
|
|
867 int status;
|
|
|
868 int err;
|
|
|
869 int exec_errorno;
|
|
|
870 ssize_t r;
|
|
|
871
|
|
|
872 #if defined(__APPLE__)
|
|
|
873 uv_once(&posix_spawn_init_once, uv__spawn_init_posix_spawn);
|
|
|
874
|
|
|
875 /* Special child process spawn case for macOS Big Sur (11.0) onwards
|
|
|
876 *
|
|
|
877 * Big Sur introduced a significant performance degradation on a call to
|
|
|
878 * fork/exec when the process has many pages mmaped in with MAP_JIT, like, say
|
|
|
879 * a javascript interpreter. Electron-based applications, for example,
|
|
|
880 * are impacted; though the magnitude of the impact depends on how much the
|
|
|
881 * app relies on subprocesses.
|
|
|
882 *
|
|
|
883 * On macOS, though, posix_spawn is implemented in a way that does not
|
|
|
884 * exhibit the problem. This block implements the forking and preparation
|
|
|
885 * logic with posix_spawn and its related primitives. It also takes advantage of
|
|
|
886 * the macOS extension POSIX_SPAWN_CLOEXEC_DEFAULT that makes impossible to
|
|
|
887 * leak descriptors to the child process. */
|
|
|
888 err = uv__spawn_and_init_child_posix_spawn(options,
|
|
|
889 stdio_count,
|
|
|
890 pipes,
|
|
|
891 pid,
|
|
|
892 &posix_spawn_fncs);
|
|
|
893
|
|
|
894 /* The posix_spawn flow will return UV_ENOSYS if any of the posix_spawn_x_np
|
|
|
895 * non-standard functions is both _needed_ and _undefined_. In those cases,
|
|
|
896 * default back to the fork/execve strategy. For all other errors, just fail. */
|
|
|
897 if (err != UV_ENOSYS)
|
|
|
898 return err;
|
|
|
899
|
|
|
900 #endif
|
|
|
901
|
|
|
902 /* This pipe is used by the parent to wait until
|
|
|
903 * the child has called `execve()`. We need this
|
|
|
904 * to avoid the following race condition:
|
|
|
905 *
|
|
|
906 * if ((pid = fork()) > 0) {
|
|
|
907 * kill(pid, SIGTERM);
|
|
|
908 * }
|
|
|
909 * else if (pid == 0) {
|
|
|
910 * execve("/bin/cat", argp, envp);
|
|
|
911 * }
|
|
|
912 *
|
|
|
913 * The parent sends a signal immediately after forking.
|
|
|
914 * Since the child may not have called `execve()` yet,
|
|
|
915 * there is no telling what process receives the signal,
|
|
|
916 * our fork or /bin/cat.
|
|
|
917 *
|
|
|
918 * To avoid ambiguity, we create a pipe with both ends
|
|
|
919 * marked close-on-exec. Then, after the call to `fork()`,
|
|
|
920 * the parent polls the read end until it EOFs or errors with EPIPE.
|
|
|
921 */
|
|
|
922 err = uv__make_pipe(signal_pipe, 0);
|
|
|
923 if (err)
|
|
|
924 return err;
|
|
|
925
|
|
|
926 /* Acquire write lock to prevent opening new fds in worker threads */
|
|
|
927 uv_rwlock_wrlock(&loop->cloexec_lock);
|
|
|
928
|
|
|
929 err = uv__spawn_and_init_child_fork(options, stdio_count, pipes, signal_pipe[1], pid);
|
|
|
930
|
|
|
931 /* Release lock in parent process */
|
|
|
932 uv_rwlock_wrunlock(&loop->cloexec_lock);
|
|
|
933
|
|
|
934 uv__close(signal_pipe[1]);
|
|
|
935
|
|
|
936 if (err == 0) {
|
|
|
937 do
|
|
|
938 r = read(signal_pipe[0], &exec_errorno, sizeof(exec_errorno));
|
|
|
939 while (r == -1 && errno == EINTR);
|
|
|
940
|
|
|
941 if (r == 0)
|
|
|
942 ; /* okay, EOF */
|
|
|
943 else if (r == sizeof(exec_errorno)) {
|
|
|
944 do
|
|
|
945 err = waitpid(*pid, &status, 0); /* okay, read errorno */
|
|
|
946 while (err == -1 && errno == EINTR);
|
|
|
947 assert(err == *pid);
|
|
|
948 err = exec_errorno;
|
|
|
949 } else if (r == -1 && errno == EPIPE) {
|
|
|
950 /* Something unknown happened to our child before spawn */
|
|
|
951 do
|
|
|
952 err = waitpid(*pid, &status, 0); /* okay, got EPIPE */
|
|
|
953 while (err == -1 && errno == EINTR);
|
|
|
954 assert(err == *pid);
|
|
|
955 err = UV_EPIPE;
|
|
|
956 } else
|
|
|
957 abort();
|
|
|
958 }
|
|
|
959
|
|
|
960 uv__close_nocheckstdio(signal_pipe[0]);
|
|
|
961
|
|
|
962 return err;
|
|
|
963 }
|
|
|
964 #endif /* ISN'T TARGET_OS_TV || TARGET_OS_WATCH */
|
|
|
965
|
|
|
966 int uv_spawn(uv_loop_t* loop,
|
|
|
967 uv_process_t* process,
|
|
|
968 const uv_process_options_t* options) {
|
|
|
969 #if defined(__APPLE__) && (TARGET_OS_TV || TARGET_OS_WATCH)
|
|
|
970 /* fork is marked __WATCHOS_PROHIBITED __TVOS_PROHIBITED. */
|
|
|
971 return UV_ENOSYS;
|
|
|
972 #else
|
|
|
973 int pipes_storage[8][2];
|
|
|
974 int (*pipes)[2];
|
|
|
975 int stdio_count;
|
|
|
976 pid_t pid;
|
|
|
977 int err;
|
|
|
978 int exec_errorno;
|
|
|
979 int i;
|
|
|
980
|
|
|
981 assert(options->file != NULL);
|
|
|
982 assert(!(options->flags & ~(UV_PROCESS_DETACHED |
|
|
|
983 UV_PROCESS_SETGID |
|
|
|
984 UV_PROCESS_SETUID |
|
|
|
985 UV_PROCESS_WINDOWS_FILE_PATH_EXACT_NAME |
|
|
|
986 UV_PROCESS_WINDOWS_HIDE |
|
|
|
987 UV_PROCESS_WINDOWS_HIDE_CONSOLE |
|
|
|
988 UV_PROCESS_WINDOWS_HIDE_GUI |
|
|
|
989 UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS)));
|
|
|
990
|
|
|
991 uv__handle_init(loop, (uv_handle_t*)process, UV_PROCESS);
|
|
|
992 uv__queue_init(&process->queue);
|
|
|
993 process->status = 0;
|
|
|
994
|
|
|
995 stdio_count = options->stdio_count;
|
|
|
996 if (stdio_count < 3)
|
|
|
997 stdio_count = 3;
|
|
|
998
|
|
|
999 err = UV_ENOMEM;
|
|
|
1000 pipes = pipes_storage;
|
|
|
1001 if (stdio_count > (int) ARRAY_SIZE(pipes_storage))
|
|
|
1002 pipes = uv__malloc(stdio_count * sizeof(*pipes));
|
|
|
1003
|
|
|
1004 if (pipes == NULL)
|
|
|
1005 goto error;
|
|
|
1006
|
|
|
1007 for (i = 0; i < stdio_count; i++) {
|
|
|
1008 pipes[i][0] = -1;
|
|
|
1009 pipes[i][1] = -1;
|
|
|
1010 }
|
|
|
1011
|
|
|
1012 for (i = 0; i < options->stdio_count; i++) {
|
|
|
1013 err = uv__process_init_stdio(options->stdio + i, pipes[i]);
|
|
|
1014 if (err)
|
|
|
1015 goto error;
|
|
|
1016 }
|
|
|
1017
|
|
|
1018 #ifdef UV_USE_SIGCHLD
|
|
|
1019 uv_signal_start(&loop->child_watcher, uv__chld, SIGCHLD);
|
|
|
1020 #endif
|
|
|
1021
|
|
|
1022 /* Spawn the child */
|
|
|
1023 exec_errorno = uv__spawn_and_init_child(loop, options, stdio_count, pipes, &pid);
|
|
|
1024
|
|
|
1025 #if 0
|
|
|
1026 /* This runs into a nodejs issue (it expects initialized streams, even if the
|
|
|
1027 * exec failed).
|
|
|
1028 * See https://github.com/libuv/libuv/pull/3107#issuecomment-782482608 */
|
|
|
1029 if (exec_errorno != 0)
|
|
|
1030 goto error;
|
|
|
1031 #endif
|
|
|
1032
|
|
|
1033 /* Activate this handle if exec() happened successfully, even if we later
|
|
|
1034 * fail to open a stdio handle. This ensures we can eventually reap the child
|
|
|
1035 * with waitpid. */
|
|
|
1036 if (exec_errorno == 0) {
|
|
|
1037 #ifndef UV_USE_SIGCHLD
|
|
|
1038 struct kevent event;
|
|
|
1039 EV_SET(&event, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, 0);
|
|
|
1040 if (kevent(loop->backend_fd, &event, 1, NULL, 0, NULL)) {
|
|
|
1041 if (errno != ESRCH)
|
|
|
1042 abort();
|
|
|
1043 /* Process already exited. Call waitpid on the next loop iteration. */
|
|
|
1044 process->flags |= UV_HANDLE_REAP;
|
|
|
1045 loop->flags |= UV_LOOP_REAP_CHILDREN;
|
|
|
1046 }
|
|
|
1047 /* This prevents uv__io_poll() from bailing out prematurely, being unaware
|
|
|
1048 * that we added an event here for it to react to. We will decrement this
|
|
|
1049 * again after the waitpid call succeeds. */
|
|
|
1050 loop->nfds++;
|
|
|
1051 #endif
|
|
|
1052
|
|
|
1053 process->pid = pid;
|
|
|
1054 process->exit_cb = options->exit_cb;
|
|
|
1055 uv__queue_insert_tail(&loop->process_handles, &process->queue);
|
|
|
1056 uv__handle_start(process);
|
|
|
1057 }
|
|
|
1058
|
|
|
1059 for (i = 0; i < options->stdio_count; i++) {
|
|
|
1060 err = uv__process_open_stream(options->stdio + i, pipes[i]);
|
|
|
1061 if (err == 0)
|
|
|
1062 continue;
|
|
|
1063
|
|
|
1064 while (i--)
|
|
|
1065 uv__process_close_stream(options->stdio + i);
|
|
|
1066
|
|
|
1067 goto error;
|
|
|
1068 }
|
|
|
1069
|
|
|
1070 if (pipes != pipes_storage)
|
|
|
1071 uv__free(pipes);
|
|
|
1072
|
|
|
1073 return exec_errorno;
|
|
|
1074
|
|
|
1075 error:
|
|
|
1076 if (pipes != NULL) {
|
|
|
1077 for (i = 0; i < stdio_count; i++) {
|
|
|
1078 if (i < options->stdio_count)
|
|
|
1079 if (options->stdio[i].flags & (UV_INHERIT_FD | UV_INHERIT_STREAM))
|
|
|
1080 continue;
|
|
|
1081 if (pipes[i][0] != -1)
|
|
|
1082 uv__close_nocheckstdio(pipes[i][0]);
|
|
|
1083 if (pipes[i][1] != -1)
|
|
|
1084 uv__close_nocheckstdio(pipes[i][1]);
|
|
|
1085 }
|
|
|
1086
|
|
|
1087 if (pipes != pipes_storage)
|
|
|
1088 uv__free(pipes);
|
|
|
1089 }
|
|
|
1090
|
|
|
1091 return err;
|
|
|
1092 #endif
|
|
|
1093 }
|
|
|
1094
|
|
|
1095
|
|
|
1096 int uv_process_kill(uv_process_t* process, int signum) {
|
|
|
1097 return uv_kill(process->pid, signum);
|
|
|
1098 }
|
|
|
1099
|
|
|
1100
|
|
|
1101 int uv_kill(int pid, int signum) {
|
|
|
1102 if (kill(pid, signum)) {
|
|
|
1103 #if defined(__MVS__)
|
|
|
1104 /* EPERM is returned if the process is a zombie. */
|
|
|
1105 siginfo_t infop;
|
|
|
1106 if (errno == EPERM &&
|
|
|
1107 waitid(P_PID, pid, &infop, WNOHANG | WNOWAIT | WEXITED) == 0)
|
|
|
1108 return 0;
|
|
|
1109 #endif
|
|
|
1110 return UV__ERR(errno);
|
|
|
1111 } else
|
|
|
1112 return 0;
|
|
|
1113 }
|
|
|
1114
|
|
|
1115
|
|
|
1116 void uv__process_close(uv_process_t* handle) {
|
|
|
1117 uv__queue_remove(&handle->queue);
|
|
|
1118 uv__handle_stop(handle);
|
|
|
1119 #ifdef UV_USE_SIGCHLD
|
|
|
1120 if (uv__queue_empty(&handle->loop->process_handles))
|
|
|
1121 uv_signal_stop(&handle->loop->child_watcher);
|
|
|
1122 #endif
|
|
|
1123 }
|