daemon.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file daemon.c
  7. * \brief Run the tor process in the background (unix only)
  8. **/
  9. #include "orconfig.h"
  10. #include "lib/process/daemon.h"
  11. #ifndef _WIN32
  12. #include "lib/fs/files.h"
  13. #include "lib/log/log.h"
  14. #include "lib/thread/threads.h"
  15. #ifdef HAVE_SYS_TYPES_H
  16. #include <sys/types.h>
  17. #endif
  18. #ifdef HAVE_UNISTD_H
  19. #include <unistd.h>
  20. #endif
  21. #ifdef HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. /* Based on code contributed by christian grothoff */
  28. /** True iff we've called start_daemon(). */
  29. static int start_daemon_called = 0;
  30. /** True iff we've called finish_daemon(). */
  31. static int finish_daemon_called = 0;
  32. /** Socketpair used to communicate between parent and child process while
  33. * daemonizing. */
  34. static int daemon_filedes[2];
  35. /**
  36. * Return true iff we've called start_daemon() at least once.
  37. */
  38. bool
  39. start_daemon_has_been_called(void)
  40. {
  41. return start_daemon_called != 0;
  42. }
  43. /** Start putting the process into daemon mode: fork and drop all resources
  44. * except standard fds. The parent process never returns, but stays around
  45. * until finish_daemon is called. (Note: it's safe to call this more
  46. * than once: calls after the first are ignored.) Return true if we actually
  47. * forked and this is the child; false otherwise.
  48. */
  49. int
  50. start_daemon(void)
  51. {
  52. pid_t pid;
  53. if (start_daemon_called)
  54. return 0;
  55. start_daemon_called = 1;
  56. if (pipe(daemon_filedes)) {
  57. /* LCOV_EXCL_START */
  58. log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
  59. exit(1); // exit ok: during daemonize, pipe failed.
  60. /* LCOV_EXCL_STOP */
  61. }
  62. pid = fork();
  63. if (pid < 0) {
  64. /* LCOV_EXCL_START */
  65. log_err(LD_GENERAL,"fork failed. Exiting.");
  66. exit(1); // exit ok: during daemonize, fork failed
  67. /* LCOV_EXCL_STOP */
  68. }
  69. if (pid) { /* Parent */
  70. int ok;
  71. char c;
  72. close(daemon_filedes[1]); /* we only read */
  73. ok = -1;
  74. while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
  75. if (c == '.')
  76. ok = 1;
  77. }
  78. fflush(stdout);
  79. if (ok == 1)
  80. exit(0); // exit ok: during daemonize, daemonizing.
  81. else
  82. exit(1); /* child reported error. exit ok: daemonize failed. */
  83. return 0; // LCOV_EXCL_LINE unreachable
  84. } else { /* Child */
  85. close(daemon_filedes[0]); /* we only write */
  86. (void) setsid(); /* Detach from controlling terminal */
  87. /*
  88. * Fork one more time, so the parent (the session group leader) can exit.
  89. * This means that we, as a non-session group leader, can never regain a
  90. * controlling terminal. This part is recommended by Stevens's
  91. * _Advanced Programming in the Unix Environment_.
  92. */
  93. if (fork() != 0) {
  94. exit(0); // exit ok: during daemonize, fork failed (2)
  95. }
  96. set_main_thread(); /* We are now the main thread. */
  97. return 1;
  98. }
  99. }
  100. /** Finish putting the process into daemon mode: drop standard fds, and tell
  101. * the parent process to exit. (Note: it's safe to call this more than once:
  102. * calls after the first are ignored. Calls start_daemon first if it hasn't
  103. * been called already.) Return true if we actually did a fork; false if we
  104. * didn't.
  105. */
  106. int
  107. finish_daemon(const char *desired_cwd)
  108. {
  109. int nullfd;
  110. char c = '.';
  111. if (finish_daemon_called)
  112. return 0;
  113. if (!start_daemon_called)
  114. start_daemon();
  115. finish_daemon_called = 1;
  116. if (!desired_cwd)
  117. desired_cwd = "/";
  118. /* Don't hold the wrong FS mounted */
  119. if (chdir(desired_cwd) < 0) {
  120. log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
  121. exit(1); // exit ok: during daemonize, chdir failed.
  122. }
  123. nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
  124. if (nullfd < 0) {
  125. /* LCOV_EXCL_START */
  126. log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
  127. exit(1); // exit ok: during daemonize, couldn't open /dev/null
  128. /* LCOV_EXCL_STOP */
  129. }
  130. /* close fds linking to invoking terminal, but
  131. * close usual incoming fds, but redirect them somewhere
  132. * useful so the fds don't get reallocated elsewhere.
  133. */
  134. if (dup2(nullfd,0) < 0 ||
  135. dup2(nullfd,1) < 0 ||
  136. dup2(nullfd,2) < 0) {
  137. /* LCOV_EXCL_START */
  138. log_err(LD_GENERAL,"dup2 failed. Exiting.");
  139. exit(1); // exit ok: during daemonize, dup2 failed.
  140. /* LCOV_EXCL_STOP */
  141. }
  142. if (nullfd > 2)
  143. close(nullfd);
  144. /* signal success */
  145. if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
  146. log_err(LD_GENERAL,"write failed. Exiting.");
  147. }
  148. close(daemon_filedes[1]);
  149. return 0;
  150. }
  151. #else /* !(!defined(_WIN32)) */
  152. /* defined(_WIN32) */
  153. int
  154. start_daemon(void)
  155. {
  156. return 0;
  157. }
  158. int
  159. finish_daemon(const char *cp)
  160. {
  161. (void)cp;
  162. return 0;
  163. }
  164. bool
  165. start_daemon_has_been_called(void)
  166. {
  167. return false;
  168. }
  169. #endif /* !defined(_WIN32) */