PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/uv/src/unix/process.c

https://gitlab.com/chandanpasunoori/io.js
C | 519 lines | 347 code | 112 blank | 60 comment | 131 complexity | c7bf7da04b35615e634aaa9de6595197 MD5 | raw file
  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. #include "uv.h"
  22. #include "internal.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <poll.h>
  32. #if defined(__APPLE__) && !TARGET_OS_IPHONE
  33. # include <crt_externs.h>
  34. # define environ (*_NSGetEnviron())
  35. #else
  36. extern char **environ;
  37. #endif
  38. #ifdef __linux__
  39. # include <grp.h>
  40. #endif
  41. static void uv__chld(uv_signal_t* handle, int signum) {
  42. uv_process_t* process;
  43. uv_loop_t* loop;
  44. int exit_status;
  45. int term_signal;
  46. int status;
  47. pid_t pid;
  48. QUEUE pending;
  49. QUEUE* q;
  50. QUEUE* h;
  51. assert(signum == SIGCHLD);
  52. QUEUE_INIT(&pending);
  53. loop = handle->loop;
  54. h = &loop->process_handles;
  55. q = QUEUE_HEAD(h);
  56. while (q != h) {
  57. process = QUEUE_DATA(q, uv_process_t, queue);
  58. q = QUEUE_NEXT(q);
  59. do
  60. pid = waitpid(process->pid, &status, WNOHANG);
  61. while (pid == -1 && errno == EINTR);
  62. if (pid == 0)
  63. continue;
  64. if (pid == -1) {
  65. if (errno != ECHILD)
  66. abort();
  67. continue;
  68. }
  69. process->status = status;
  70. QUEUE_REMOVE(&process->queue);
  71. QUEUE_INSERT_TAIL(&pending, &process->queue);
  72. }
  73. QUEUE_FOREACH(q, &pending) {
  74. process = QUEUE_DATA(q, uv_process_t, queue);
  75. QUEUE_REMOVE(q);
  76. uv__handle_stop(process);
  77. if (process->exit_cb == NULL)
  78. continue;
  79. exit_status = 0;
  80. if (WIFEXITED(process->status))
  81. exit_status = WEXITSTATUS(process->status);
  82. term_signal = 0;
  83. if (WIFSIGNALED(process->status))
  84. term_signal = WTERMSIG(process->status);
  85. process->exit_cb(process, exit_status, term_signal);
  86. }
  87. assert(QUEUE_EMPTY(&pending));
  88. }
  89. int uv__make_socketpair(int fds[2], int flags) {
  90. #if defined(__linux__)
  91. static int no_cloexec;
  92. if (no_cloexec)
  93. goto skip;
  94. if (socketpair(AF_UNIX, SOCK_STREAM | UV__SOCK_CLOEXEC | flags, 0, fds) == 0)
  95. return 0;
  96. /* Retry on EINVAL, it means SOCK_CLOEXEC is not supported.
  97. * Anything else is a genuine error.
  98. */
  99. if (errno != EINVAL)
  100. return -errno;
  101. no_cloexec = 1;
  102. skip:
  103. #endif
  104. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
  105. return -errno;
  106. uv__cloexec(fds[0], 1);
  107. uv__cloexec(fds[1], 1);
  108. if (flags & UV__F_NONBLOCK) {
  109. uv__nonblock(fds[0], 1);
  110. uv__nonblock(fds[1], 1);
  111. }
  112. return 0;
  113. }
  114. int uv__make_pipe(int fds[2], int flags) {
  115. #if defined(__linux__)
  116. static int no_pipe2;
  117. if (no_pipe2)
  118. goto skip;
  119. if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0)
  120. return 0;
  121. if (errno != ENOSYS)
  122. return -errno;
  123. no_pipe2 = 1;
  124. skip:
  125. #endif
  126. if (pipe(fds))
  127. return -errno;
  128. uv__cloexec(fds[0], 1);
  129. uv__cloexec(fds[1], 1);
  130. if (flags & UV__F_NONBLOCK) {
  131. uv__nonblock(fds[0], 1);
  132. uv__nonblock(fds[1], 1);
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Used for initializing stdio streams like options.stdin_stream. Returns
  138. * zero on success. See also the cleanup section in uv_spawn().
  139. */
  140. static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) {
  141. int mask;
  142. int fd;
  143. mask = UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM;
  144. switch (container->flags & mask) {
  145. case UV_IGNORE:
  146. return 0;
  147. case UV_CREATE_PIPE:
  148. assert(container->data.stream != NULL);
  149. if (container->data.stream->type != UV_NAMED_PIPE)
  150. return -EINVAL;
  151. else
  152. return uv__make_socketpair(fds, 0);
  153. case UV_INHERIT_FD:
  154. case UV_INHERIT_STREAM:
  155. if (container->flags & UV_INHERIT_FD)
  156. fd = container->data.fd;
  157. else
  158. fd = uv__stream_fd(container->data.stream);
  159. if (fd == -1)
  160. return -EINVAL;
  161. fds[1] = fd;
  162. return 0;
  163. default:
  164. assert(0 && "Unexpected flags");
  165. return -EINVAL;
  166. }
  167. }
  168. static int uv__process_open_stream(uv_stdio_container_t* container,
  169. int pipefds[2],
  170. int writable) {
  171. int flags;
  172. if (!(container->flags & UV_CREATE_PIPE) || pipefds[0] < 0)
  173. return 0;
  174. if (uv__close(pipefds[1]))
  175. if (errno != EINTR && errno != EINPROGRESS)
  176. abort();
  177. pipefds[1] = -1;
  178. uv__nonblock(pipefds[0], 1);
  179. if (container->data.stream->type == UV_NAMED_PIPE &&
  180. ((uv_pipe_t*)container->data.stream)->ipc)
  181. flags = UV_STREAM_READABLE | UV_STREAM_WRITABLE;
  182. else if (writable)
  183. flags = UV_STREAM_WRITABLE;
  184. else
  185. flags = UV_STREAM_READABLE;
  186. return uv__stream_open(container->data.stream, pipefds[0], flags);
  187. }
  188. static void uv__process_close_stream(uv_stdio_container_t* container) {
  189. if (!(container->flags & UV_CREATE_PIPE)) return;
  190. uv__stream_close((uv_stream_t*)container->data.stream);
  191. }
  192. static void uv__write_int(int fd, int val) {
  193. ssize_t n;
  194. do
  195. n = write(fd, &val, sizeof(val));
  196. while (n == -1 && errno == EINTR);
  197. if (n == -1 && errno == EPIPE)
  198. return; /* parent process has quit */
  199. assert(n == sizeof(val));
  200. }
  201. static void uv__process_child_init(const uv_process_options_t* options,
  202. int stdio_count,
  203. int (*pipes)[2],
  204. int error_fd) {
  205. int close_fd;
  206. int use_fd;
  207. int fd;
  208. if (options->flags & UV_PROCESS_DETACHED)
  209. setsid();
  210. for (fd = 0; fd < stdio_count; fd++) {
  211. close_fd = pipes[fd][0];
  212. use_fd = pipes[fd][1];
  213. if (use_fd < 0) {
  214. if (fd >= 3)
  215. continue;
  216. else {
  217. /* redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
  218. * set
  219. */
  220. use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
  221. close_fd = use_fd;
  222. if (use_fd == -1) {
  223. uv__write_int(error_fd, -errno);
  224. _exit(127);
  225. }
  226. }
  227. }
  228. if (fd == use_fd)
  229. uv__cloexec(use_fd, 0);
  230. else
  231. dup2(use_fd, fd);
  232. if (fd <= 2)
  233. uv__nonblock(fd, 0);
  234. if (close_fd >= stdio_count)
  235. uv__close(close_fd);
  236. }
  237. for (fd = 0; fd < stdio_count; fd++) {
  238. use_fd = pipes[fd][1];
  239. if (use_fd >= 0 && fd != use_fd)
  240. close(use_fd);
  241. }
  242. if (options->cwd != NULL && chdir(options->cwd)) {
  243. uv__write_int(error_fd, -errno);
  244. _exit(127);
  245. }
  246. if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
  247. /* When dropping privileges from root, the `setgroups` call will
  248. * remove any extraneous groups. If we don't call this, then
  249. * even though our uid has dropped, we may still have groups
  250. * that enable us to do super-user things. This will fail if we
  251. * aren't root, so don't bother checking the return value, this
  252. * is just done as an optimistic privilege dropping function.
  253. */
  254. SAVE_ERRNO(setgroups(0, NULL));
  255. }
  256. if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid)) {
  257. uv__write_int(error_fd, -errno);
  258. _exit(127);
  259. }
  260. if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid)) {
  261. uv__write_int(error_fd, -errno);
  262. _exit(127);
  263. }
  264. if (options->env != NULL) {
  265. environ = options->env;
  266. }
  267. execvp(options->file, options->args);
  268. uv__write_int(error_fd, -errno);
  269. _exit(127);
  270. }
  271. int uv_spawn(uv_loop_t* loop,
  272. uv_process_t* process,
  273. const uv_process_options_t* options) {
  274. int signal_pipe[2] = { -1, -1 };
  275. int (*pipes)[2];
  276. int stdio_count;
  277. ssize_t r;
  278. pid_t pid;
  279. int err;
  280. int exec_errorno;
  281. int i;
  282. assert(options->file != NULL);
  283. assert(!(options->flags & ~(UV_PROCESS_DETACHED |
  284. UV_PROCESS_SETGID |
  285. UV_PROCESS_SETUID |
  286. UV_PROCESS_WINDOWS_HIDE |
  287. UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS)));
  288. uv__handle_init(loop, (uv_handle_t*)process, UV_PROCESS);
  289. QUEUE_INIT(&process->queue);
  290. stdio_count = options->stdio_count;
  291. if (stdio_count < 3)
  292. stdio_count = 3;
  293. err = -ENOMEM;
  294. pipes = malloc(stdio_count * sizeof(*pipes));
  295. if (pipes == NULL)
  296. goto error;
  297. for (i = 0; i < stdio_count; i++) {
  298. pipes[i][0] = -1;
  299. pipes[i][1] = -1;
  300. }
  301. for (i = 0; i < options->stdio_count; i++) {
  302. err = uv__process_init_stdio(options->stdio + i, pipes[i]);
  303. if (err)
  304. goto error;
  305. }
  306. /* This pipe is used by the parent to wait until
  307. * the child has called `execve()`. We need this
  308. * to avoid the following race condition:
  309. *
  310. * if ((pid = fork()) > 0) {
  311. * kill(pid, SIGTERM);
  312. * }
  313. * else if (pid == 0) {
  314. * execve("/bin/cat", argp, envp);
  315. * }
  316. *
  317. * The parent sends a signal immediately after forking.
  318. * Since the child may not have called `execve()` yet,
  319. * there is no telling what process receives the signal,
  320. * our fork or /bin/cat.
  321. *
  322. * To avoid ambiguity, we create a pipe with both ends
  323. * marked close-on-exec. Then, after the call to `fork()`,
  324. * the parent polls the read end until it EOFs or errors with EPIPE.
  325. */
  326. err = uv__make_pipe(signal_pipe, 0);
  327. if (err)
  328. goto error;
  329. uv_signal_start(&loop->child_watcher, uv__chld, SIGCHLD);
  330. /* Acquire write lock to prevent opening new fds in worker threads */
  331. uv_rwlock_wrlock(&loop->cloexec_lock);
  332. pid = fork();
  333. if (pid == -1) {
  334. err = -errno;
  335. uv_rwlock_wrunlock(&loop->cloexec_lock);
  336. uv__close(signal_pipe[0]);
  337. uv__close(signal_pipe[1]);
  338. goto error;
  339. }
  340. if (pid == 0) {
  341. uv__process_child_init(options, stdio_count, pipes, signal_pipe[1]);
  342. abort();
  343. }
  344. /* Release lock in parent process */
  345. uv_rwlock_wrunlock(&loop->cloexec_lock);
  346. uv__close(signal_pipe[1]);
  347. process->status = 0;
  348. exec_errorno = 0;
  349. do
  350. r = read(signal_pipe[0], &exec_errorno, sizeof(exec_errorno));
  351. while (r == -1 && errno == EINTR);
  352. if (r == 0)
  353. ; /* okay, EOF */
  354. else if (r == sizeof(exec_errorno))
  355. ; /* okay, read errorno */
  356. else if (r == -1 && errno == EPIPE)
  357. ; /* okay, got EPIPE */
  358. else
  359. abort();
  360. uv__close(signal_pipe[0]);
  361. for (i = 0; i < options->stdio_count; i++) {
  362. err = uv__process_open_stream(options->stdio + i, pipes[i], i == 0);
  363. if (err == 0)
  364. continue;
  365. while (i--)
  366. uv__process_close_stream(options->stdio + i);
  367. goto error;
  368. }
  369. /* Only activate this handle if exec() happened successfully */
  370. if (exec_errorno == 0) {
  371. QUEUE_INSERT_TAIL(&loop->process_handles, &process->queue);
  372. uv__handle_start(process);
  373. }
  374. process->pid = pid;
  375. process->exit_cb = options->exit_cb;
  376. free(pipes);
  377. return exec_errorno;
  378. error:
  379. if (pipes != NULL) {
  380. for (i = 0; i < stdio_count; i++) {
  381. if (i < options->stdio_count)
  382. if (options->stdio[i].flags & (UV_INHERIT_FD | UV_INHERIT_STREAM))
  383. continue;
  384. if (pipes[i][0] != -1)
  385. close(pipes[i][0]);
  386. if (pipes[i][1] != -1)
  387. close(pipes[i][1]);
  388. }
  389. free(pipes);
  390. }
  391. return err;
  392. }
  393. int uv_process_kill(uv_process_t* process, int signum) {
  394. return uv_kill(process->pid, signum);
  395. }
  396. int uv_kill(int pid, int signum) {
  397. if (kill(pid, signum))
  398. return -errno;
  399. else
  400. return 0;
  401. }
  402. void uv__process_close(uv_process_t* handle) {
  403. QUEUE_REMOVE(&handle->queue);
  404. uv__handle_stop(handle);
  405. if (QUEUE_EMPTY(&handle->loop->process_handles))
  406. uv_signal_stop(&handle->loop->child_watcher);
  407. }