PageRenderTime 732ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/libuv/src/unix/process.c

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