process_unix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file process_unix.c
  7. * \brief Module for working with Unix processes.
  8. **/
  9. #define PROCESS_UNIX_PRIVATE
  10. #include "lib/intmath/cmp.h"
  11. #include "lib/buf/buffers.h"
  12. #include "lib/net/buffers_net.h"
  13. #include "lib/container/smartlist.h"
  14. #include "lib/evloop/compat_libevent.h"
  15. #include "lib/log/log.h"
  16. #include "lib/log/util_bug.h"
  17. #include "lib/process/process.h"
  18. #include "lib/process/process_unix.h"
  19. #include "lib/process/waitpid.h"
  20. #include "lib/process/env.h"
  21. #include <stdio.h>
  22. #ifdef HAVE_STRING_H
  23. #include <string.h>
  24. #endif
  25. #ifdef HAVE_ERRNO_H
  26. #include <errno.h>
  27. #endif
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #ifdef HAVE_FCNTL_H
  32. #include <fcntl.h>
  33. #endif
  34. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  35. #include <sys/prctl.h>
  36. #endif
  37. #if HAVE_SIGNAL_H
  38. #include <signal.h>
  39. #endif
  40. #ifndef _WIN32
  41. /** Maximum number of file descriptors, if we cannot get it via sysconf() */
  42. #define DEFAULT_MAX_FD 256
  43. /** Internal state for Unix handles. */
  44. struct process_unix_handle_t {
  45. /** Unix File Descriptor. */
  46. int fd;
  47. /** Have we reached end of file? */
  48. bool reached_eof;
  49. /** Event structure for libevent. */
  50. struct event *event;
  51. /** Are we writing? */
  52. bool is_writing;
  53. };
  54. /** Internal state for our Unix process. */
  55. struct process_unix_t {
  56. /** Standard in handle. */
  57. process_unix_handle_t stdin_handle;
  58. /** Standard out handle. */
  59. process_unix_handle_t stdout_handle;
  60. /** Standard error handle. */
  61. process_unix_handle_t stderr_handle;
  62. /** The process identifier of our process. */
  63. pid_t pid;
  64. /** Waitpid Callback structure. */
  65. waitpid_callback_t *waitpid;
  66. };
  67. /** Returns a newly allocated <b>process_unix_t</b>. */
  68. process_unix_t *
  69. process_unix_new(void)
  70. {
  71. process_unix_t *unix_process;
  72. unix_process = tor_malloc_zero(sizeof(process_unix_t));
  73. unix_process->stdin_handle.fd = -1;
  74. unix_process->stderr_handle.fd = -1;
  75. unix_process->stdout_handle.fd = -1;
  76. return unix_process;
  77. }
  78. /** Deallocates the given <b>unix_process</b>. */
  79. void
  80. process_unix_free_(process_unix_t *unix_process)
  81. {
  82. if (! unix_process)
  83. return;
  84. /* Clean up our waitpid callback. */
  85. clear_waitpid_callback(unix_process->waitpid);
  86. /* FIXME(ahf): Refactor waitpid code? */
  87. unix_process->waitpid = NULL;
  88. /* Close all our file descriptors. */
  89. process_unix_close_file_descriptors(unix_process);
  90. tor_event_free(unix_process->stdout_handle.event);
  91. tor_event_free(unix_process->stderr_handle.event);
  92. tor_event_free(unix_process->stdin_handle.event);
  93. tor_free(unix_process);
  94. }
  95. /** Executes the given process as a child process of Tor. This function is
  96. * responsible for setting up the child process and run it. This includes
  97. * setting up pipes for interprocess communication, initialize the waitpid
  98. * callbacks, and finally run fork() followed by execve(). Returns
  99. * <b>PROCESS_STATUS_RUNNING</b> upon success. */
  100. process_status_t
  101. process_unix_exec(process_t *process)
  102. {
  103. static int max_fd = -1;
  104. process_unix_t *unix_process;
  105. pid_t pid;
  106. int stdin_pipe[2];
  107. int stdout_pipe[2];
  108. int stderr_pipe[2];
  109. int retval, fd;
  110. unix_process = process_get_unix_process(process);
  111. /* Create standard in pipe. */
  112. retval = pipe(stdin_pipe);
  113. if (-1 == retval) {
  114. log_warn(LD_PROCESS,
  115. "Unable to create pipe for stdin "
  116. "communication with process: %s",
  117. strerror(errno));
  118. return PROCESS_STATUS_ERROR;
  119. }
  120. /* Create standard out pipe. */
  121. retval = pipe(stdout_pipe);
  122. if (-1 == retval) {
  123. log_warn(LD_PROCESS,
  124. "Unable to create pipe for stdout "
  125. "communication with process: %s",
  126. strerror(errno));
  127. /** Cleanup standard in pipe. */
  128. close(stdin_pipe[0]);
  129. close(stdin_pipe[1]);
  130. return PROCESS_STATUS_ERROR;
  131. }
  132. /* Create standard error pipe. */
  133. retval = pipe(stderr_pipe);
  134. if (-1 == retval) {
  135. log_warn(LD_PROCESS,
  136. "Unable to create pipe for stderr "
  137. "communication with process: %s",
  138. strerror(errno));
  139. /** Cleanup standard in pipe. */
  140. close(stdin_pipe[0]);
  141. close(stdin_pipe[1]);
  142. /** Cleanup standard out pipe. */
  143. close(stdout_pipe[0]);
  144. close(stdout_pipe[1]);
  145. return PROCESS_STATUS_ERROR;
  146. }
  147. #ifdef _SC_OPEN_MAX
  148. if (-1 == max_fd) {
  149. max_fd = (int)sysconf(_SC_OPEN_MAX);
  150. if (max_fd == -1) {
  151. max_fd = DEFAULT_MAX_FD;
  152. log_warn(LD_PROCESS,
  153. "Cannot find maximum file descriptor, assuming: %d", max_fd);
  154. }
  155. }
  156. #else /* !defined(_SC_OPEN_MAX) */
  157. max_fd = DEFAULT_MAX_FD;
  158. #endif /* defined(_SC_OPEN_MAX) */
  159. pid = fork();
  160. if (0 == pid) {
  161. /* This code is running in the child process context. */
  162. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  163. /* Attempt to have the kernel issue a SIGTERM if the parent
  164. * goes away. Certain attributes of the binary being execve()ed
  165. * will clear this during the execve() call, but it's better
  166. * than nothing.
  167. */
  168. prctl(PR_SET_PDEATHSIG, SIGTERM);
  169. #endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) */
  170. /* Link process stdout to the write end of the pipe. */
  171. retval = dup2(stdout_pipe[1], STDOUT_FILENO);
  172. if (-1 == retval)
  173. goto error;
  174. /* Link process stderr to the write end of the pipe. */
  175. retval = dup2(stderr_pipe[1], STDERR_FILENO);
  176. if (-1 == retval)
  177. goto error;
  178. /* Link process stdin to the read end of the pipe */
  179. retval = dup2(stdin_pipe[0], STDIN_FILENO);
  180. if (-1 == retval)
  181. goto error;
  182. /* Close our pipes now after they have been dup2()'ed. */
  183. close(stderr_pipe[0]);
  184. close(stderr_pipe[1]);
  185. close(stdout_pipe[0]);
  186. close(stdout_pipe[1]);
  187. close(stdin_pipe[0]);
  188. close(stdin_pipe[1]);
  189. /* Close all other fds, including the read end of the pipe. XXX: We should
  190. * now be doing enough FD_CLOEXEC setting to make this needless.
  191. */
  192. for (fd = STDERR_FILENO + 1; fd < max_fd; fd++)
  193. close(fd);
  194. /* Create the argv value for our new process. */
  195. char **argv = process_get_argv(process);
  196. /* Create the env value for our new process. */
  197. process_environment_t *env = process_get_environment(process);
  198. /* Call the requested program. */
  199. execve(argv[0], argv, env->unixoid_environment_block);
  200. /* If we made it here it is because execve failed :-( */
  201. tor_free(argv);
  202. process_environment_free(env);
  203. error:
  204. fprintf(stderr, "Error from child process: %s", strerror(errno));
  205. _exit(1);
  206. }
  207. /* We are in the parent process. */
  208. if (-1 == pid) {
  209. log_warn(LD_PROCESS,
  210. "Failed to create child process: %s", strerror(errno));
  211. /** Cleanup standard in pipe. */
  212. close(stdin_pipe[0]);
  213. close(stdin_pipe[1]);
  214. /** Cleanup standard out pipe. */
  215. close(stdout_pipe[0]);
  216. close(stdout_pipe[1]);
  217. /** Cleanup standard error pipe. */
  218. close(stderr_pipe[0]);
  219. close(stderr_pipe[1]);
  220. return PROCESS_STATUS_ERROR;
  221. }
  222. /* Register our PID. */
  223. unix_process->pid = pid;
  224. /* Setup waitpid callbacks. */
  225. unix_process->waitpid = set_waitpid_callback(pid,
  226. process_unix_waitpid_callback,
  227. process);
  228. /* Handle standard out. */
  229. unix_process->stdout_handle.fd = stdout_pipe[0];
  230. retval = close(stdout_pipe[1]);
  231. if (-1 == retval) {
  232. log_warn(LD_PROCESS, "Failed to close write end of standard out pipe: %s",
  233. strerror(errno));
  234. }
  235. /* Handle standard error. */
  236. unix_process->stderr_handle.fd = stderr_pipe[0];
  237. retval = close(stderr_pipe[1]);
  238. if (-1 == retval) {
  239. log_warn(LD_PROCESS,
  240. "Failed to close write end of standard error pipe: %s",
  241. strerror(errno));
  242. }
  243. /* Handle standard in. */
  244. unix_process->stdin_handle.fd = stdin_pipe[1];
  245. retval = close(stdin_pipe[0]);
  246. if (-1 == retval) {
  247. log_warn(LD_PROCESS, "Failed to close read end of standard in pipe: %s",
  248. strerror(errno));
  249. }
  250. /* Setup our handles. */
  251. process_unix_setup_handle(process,
  252. &unix_process->stdout_handle,
  253. EV_READ|EV_PERSIST,
  254. stdout_read_callback);
  255. process_unix_setup_handle(process,
  256. &unix_process->stderr_handle,
  257. EV_READ|EV_PERSIST,
  258. stderr_read_callback);
  259. process_unix_setup_handle(process,
  260. &unix_process->stdin_handle,
  261. EV_WRITE|EV_PERSIST,
  262. stdin_write_callback);
  263. /* Start reading from standard out and standard error. */
  264. process_unix_start_reading(&unix_process->stdout_handle);
  265. process_unix_start_reading(&unix_process->stderr_handle);
  266. return PROCESS_STATUS_RUNNING;
  267. }
  268. /** Terminate the given process. Returns true on success, otherwise false. */
  269. bool
  270. process_unix_terminate(process_t *process)
  271. {
  272. tor_assert(process);
  273. process_unix_t *unix_process = process_get_unix_process(process);
  274. /* All running processes should have a waitpid. */
  275. if (BUG(unix_process->waitpid == NULL))
  276. return false;
  277. bool success = true;
  278. /* Send a SIGTERM to our child process. */
  279. int ret;
  280. ret = kill(unix_process->pid, SIGTERM);
  281. if (ret == -1) {
  282. log_warn(LD_PROCESS, "Unable to terminate process: %s",
  283. strerror(errno));
  284. success = false;
  285. }
  286. /* Close all our FD's. */
  287. if (! process_unix_close_file_descriptors(unix_process))
  288. success = false;
  289. return success;
  290. }
  291. /** Returns the unique process identifier for the given <b>process</b>. */
  292. process_pid_t
  293. process_unix_get_pid(process_t *process)
  294. {
  295. tor_assert(process);
  296. process_unix_t *unix_process = process_get_unix_process(process);
  297. return (process_pid_t)unix_process->pid;
  298. }
  299. /** Write the given <b>buffer</b> as input to the given <b>process</b>'s
  300. * standard input. Returns the number of bytes written. */
  301. int
  302. process_unix_write(process_t *process, buf_t *buffer)
  303. {
  304. tor_assert(process);
  305. tor_assert(buffer);
  306. process_unix_t *unix_process = process_get_unix_process(process);
  307. size_t buffer_flush_len = buf_datalen(buffer);
  308. const size_t max_to_write = MIN(PROCESS_MAX_WRITE, buffer_flush_len);
  309. /* If we have data to write (when buffer_flush_len > 0) and we are not
  310. * currently getting file descriptor events from the kernel, we tell the
  311. * kernel to start notifying us about when we can write to our file
  312. * descriptor and return. */
  313. if (buffer_flush_len > 0 && ! unix_process->stdin_handle.is_writing) {
  314. process_unix_start_writing(&unix_process->stdin_handle);
  315. return 0;
  316. }
  317. /* We don't have any data to write, but the kernel is currently notifying us
  318. * about whether we are able to write or not. Tell the kernel to stop
  319. * notifying us until we have data to write. */
  320. if (buffer_flush_len == 0 && unix_process->stdin_handle.is_writing) {
  321. process_unix_stop_writing(&unix_process->stdin_handle);
  322. return 0;
  323. }
  324. /* We have data to write and the kernel have told us to write it. */
  325. return buf_flush_to_pipe(buffer,
  326. process_get_unix_process(process)->stdin_handle.fd,
  327. max_to_write, &buffer_flush_len);
  328. }
  329. /** Read data from the given process's standard output and put it into
  330. * <b>buffer</b>. Returns the number of bytes read. */
  331. int
  332. process_unix_read_stdout(process_t *process, buf_t *buffer)
  333. {
  334. tor_assert(process);
  335. tor_assert(buffer);
  336. process_unix_t *unix_process = process_get_unix_process(process);
  337. return process_unix_read_handle(process,
  338. &unix_process->stdout_handle,
  339. buffer);
  340. }
  341. /** Read data from the given process's standard error and put it into
  342. * <b>buffer</b>. Returns the number of bytes read. */
  343. int
  344. process_unix_read_stderr(process_t *process, buf_t *buffer)
  345. {
  346. tor_assert(process);
  347. tor_assert(buffer);
  348. process_unix_t *unix_process = process_get_unix_process(process);
  349. return process_unix_read_handle(process,
  350. &unix_process->stderr_handle,
  351. buffer);
  352. }
  353. /** This function is called whenever libevent thinks we have data that could be
  354. * read from the child process's standard output. We notify the Process
  355. * subsystem, which is then responsible for calling back to us for doing the
  356. * actual reading of the data. */
  357. STATIC void
  358. stdout_read_callback(evutil_socket_t fd, short event, void *data)
  359. {
  360. (void)fd;
  361. (void)event;
  362. process_t *process = data;
  363. tor_assert(process);
  364. process_notify_event_stdout(process);
  365. }
  366. /** This function is called whenever libevent thinks we have data that could be
  367. * read from the child process's standard error. We notify the Process
  368. * subsystem, which is then responsible for calling back to us for doing the
  369. * actual reading of the data. */
  370. STATIC void
  371. stderr_read_callback(evutil_socket_t fd, short event, void *data)
  372. {
  373. (void)fd;
  374. (void)event;
  375. process_t *process = data;
  376. tor_assert(process);
  377. process_notify_event_stderr(process);
  378. }
  379. /** This function is called whenever libevent thinks we have data that could be
  380. * written the child process's standard input. We notify the Process subsystem,
  381. * which is then responsible for calling back to us for doing the actual write
  382. * of the data. */
  383. STATIC void
  384. stdin_write_callback(evutil_socket_t fd, short event, void *data)
  385. {
  386. (void)fd;
  387. (void)event;
  388. process_t *process = data;
  389. tor_assert(process);
  390. process_notify_event_stdin(process);
  391. }
  392. /** This function tells libevent that we are interested in receiving read
  393. * events from the given <b>handle</b>. */
  394. STATIC void
  395. process_unix_start_reading(process_unix_handle_t *handle)
  396. {
  397. tor_assert(handle);
  398. if (event_add(handle->event, NULL))
  399. log_warn(LD_PROCESS,
  400. "Unable to add libevent event for handle.");
  401. }
  402. /** This function tells libevent that we are no longer interested in receiving
  403. * read events from the given <b>handle</b>. */
  404. STATIC void
  405. process_unix_stop_reading(process_unix_handle_t *handle)
  406. {
  407. tor_assert(handle);
  408. if (handle->event == NULL)
  409. return;
  410. if (event_del(handle->event))
  411. log_warn(LD_PROCESS,
  412. "Unable to delete libevent event for handle.");
  413. }
  414. /** This function tells libevent that we are interested in receiving write
  415. * events from the given <b>handle</b>. */
  416. STATIC void
  417. process_unix_start_writing(process_unix_handle_t *handle)
  418. {
  419. tor_assert(handle);
  420. if (event_add(handle->event, NULL))
  421. log_warn(LD_PROCESS,
  422. "Unable to add libevent event for handle.");
  423. handle->is_writing = true;
  424. }
  425. /** This function tells libevent that we are no longer interested in receiving
  426. * write events from the given <b>handle</b>. */
  427. STATIC void
  428. process_unix_stop_writing(process_unix_handle_t *handle)
  429. {
  430. tor_assert(handle);
  431. if (handle->event == NULL)
  432. return;
  433. if (event_del(handle->event))
  434. log_warn(LD_PROCESS,
  435. "Unable to delete libevent event for handle.");
  436. handle->is_writing = false;
  437. }
  438. /** This function is called when the waitpid system have detected that our
  439. * process have terminated. We disable the waitpid system and notify the
  440. * Process subsystem that we have terminated. */
  441. STATIC void
  442. process_unix_waitpid_callback(int status, void *data)
  443. {
  444. tor_assert(data);
  445. process_t *process = data;
  446. process_unix_t *unix_process = process_get_unix_process(process);
  447. /* Remove our waitpid callback. */
  448. clear_waitpid_callback(unix_process->waitpid);
  449. unix_process->waitpid = NULL;
  450. /* Notify our process. */
  451. process_notify_event_exit(process, status);
  452. /* Make sure you don't modify the process after we have called
  453. * process_notify_event_exit() on it, to allow users to process_free() it in
  454. * the exit callback. */
  455. }
  456. /** This function sets the file descriptor in the <b>handle</b> as non-blocking
  457. * and configures the libevent event structure based on the given <b>flags</b>
  458. * to ensure that <b>callback</b> is called whenever we have events on the
  459. * given <b>handle</b>. */
  460. STATIC void
  461. process_unix_setup_handle(process_t *process,
  462. process_unix_handle_t *handle,
  463. short flags,
  464. event_callback_fn callback)
  465. {
  466. tor_assert(process);
  467. tor_assert(handle);
  468. tor_assert(callback);
  469. /* Put our file descriptor into non-blocking mode. */
  470. if (fcntl(handle->fd, F_SETFL, O_NONBLOCK) < 0) {
  471. log_warn(LD_PROCESS, "Unable mark Unix handle as non-blocking: %s",
  472. strerror(errno));
  473. }
  474. /* Setup libevent event. */
  475. handle->event = tor_event_new(tor_libevent_get_base(),
  476. handle->fd,
  477. flags,
  478. callback,
  479. process);
  480. }
  481. /** This function reads data from the given <b>handle</b> and puts it into
  482. * <b>buffer</b>. Returns the number of bytes read this way. */
  483. STATIC int
  484. process_unix_read_handle(process_t *process,
  485. process_unix_handle_t *handle,
  486. buf_t *buffer)
  487. {
  488. tor_assert(process);
  489. tor_assert(handle);
  490. tor_assert(buffer);
  491. int ret = 0;
  492. int eof = 0;
  493. int error = 0;
  494. ret = buf_read_from_pipe(buffer,
  495. handle->fd,
  496. PROCESS_MAX_READ,
  497. &eof,
  498. &error);
  499. if (error)
  500. log_warn(LD_PROCESS,
  501. "Unable to read data: %s", strerror(error));
  502. if (eof) {
  503. handle->reached_eof = true;
  504. process_unix_stop_reading(handle);
  505. }
  506. return ret;
  507. }
  508. /** Close the standard in, out, and error handles of the given
  509. * <b>unix_process</b>. */
  510. STATIC bool
  511. process_unix_close_file_descriptors(process_unix_t *unix_process)
  512. {
  513. tor_assert(unix_process);
  514. int ret;
  515. bool success = true;
  516. /* Stop reading and writing before we close() our
  517. * file descriptors. */
  518. if (! unix_process->stdout_handle.reached_eof)
  519. process_unix_stop_reading(&unix_process->stdout_handle);
  520. if (! unix_process->stderr_handle.reached_eof)
  521. process_unix_stop_reading(&unix_process->stderr_handle);
  522. if (unix_process->stdin_handle.is_writing)
  523. process_unix_stop_writing(&unix_process->stdin_handle);
  524. if (unix_process->stdin_handle.fd != -1) {
  525. ret = close(unix_process->stdin_handle.fd);
  526. if (ret == -1) {
  527. log_warn(LD_PROCESS, "Unable to close standard in");
  528. success = false;
  529. }
  530. unix_process->stdin_handle.fd = -1;
  531. }
  532. if (unix_process->stdout_handle.fd != -1) {
  533. ret = close(unix_process->stdout_handle.fd);
  534. if (ret == -1) {
  535. log_warn(LD_PROCESS, "Unable to close standard out");
  536. success = false;
  537. }
  538. unix_process->stdout_handle.fd = -1;
  539. }
  540. if (unix_process->stderr_handle.fd != -1) {
  541. ret = close(unix_process->stderr_handle.fd);
  542. if (ret == -1) {
  543. log_warn(LD_PROCESS, "Unable to close standard error");
  544. success = false;
  545. }
  546. unix_process->stderr_handle.fd = -1;
  547. }
  548. return success;
  549. }
  550. #endif /* !defined(_WIN32) */