daemonize.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * This code is mainly taken from Doug Potter's page
  3. *
  4. * http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize
  5. *
  6. * I contacted him 2007-04-16 about the license for the original code,
  7. * he replied it is Public Domain. Use the URL above to get the original
  8. * Public Domain version if you want it.
  9. *
  10. * This version is LGPL2.1+SLE like the rest of libwebsockets and is
  11. * Copyright (c)2006 - 2013 Andy Green <andy@warmcat.com>
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <limits.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include "private-libwebsockets.h"
  24. int pid_daemon;
  25. static char *lock_path;
  26. int get_daemonize_pid()
  27. {
  28. return pid_daemon;
  29. }
  30. static void
  31. child_handler(int signum)
  32. {
  33. int fd, len, sent;
  34. char sz[20];
  35. switch (signum) {
  36. case SIGALRM: /* timed out daemonizing */
  37. exit(0);
  38. break;
  39. case SIGUSR1: /* positive confirmation we daemonized well */
  40. if (lock_path) {
  41. /* Create the lock file as the current user */
  42. fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
  43. if (fd < 0) {
  44. fprintf(stderr,
  45. "unable to create lock file %s, code=%d (%s)\n",
  46. lock_path, errno, strerror(errno));
  47. exit(0);
  48. }
  49. len = sprintf(sz, "%u", pid_daemon);
  50. sent = write(fd, sz, len);
  51. if (sent != len)
  52. fprintf(stderr,
  53. "unable to write pid to lock file %s, code=%d (%s)\n",
  54. lock_path, errno, strerror(errno));
  55. close(fd);
  56. }
  57. exit(0);
  58. //!!(sent == len));
  59. case SIGCHLD: /* daemonization failed */
  60. exit(0);
  61. break;
  62. }
  63. }
  64. static void lws_daemon_closing(int sigact)
  65. {
  66. if (getpid() == pid_daemon)
  67. if (lock_path) {
  68. unlink(lock_path);
  69. lws_free_set_NULL(lock_path);
  70. }
  71. kill(getpid(), SIGKILL);
  72. }
  73. /*
  74. * You just need to call this from your main(), when it
  75. * returns you are all set "in the background" decoupled
  76. * from the console you were started from.
  77. *
  78. * The process context you called from has been terminated then.
  79. */
  80. LWS_VISIBLE int
  81. lws_daemonize(const char *_lock_path)
  82. {
  83. struct sigaction act;
  84. pid_t sid, parent;
  85. int n, fd, ret;
  86. char buf[10];
  87. /* already a daemon */
  88. // if (getppid() == 1)
  89. // return 1;
  90. if (_lock_path) {
  91. fd = open(_lock_path, O_RDONLY);
  92. if (fd >= 0) {
  93. n = read(fd, buf, sizeof(buf));
  94. close(fd);
  95. if (n) {
  96. n = atoi(buf);
  97. ret = kill(n, 0);
  98. if (ret >= 0) {
  99. fprintf(stderr,
  100. "Daemon already running from pid %d\n", n);
  101. exit(1);
  102. }
  103. fprintf(stderr,
  104. "Removing stale lock file %s from dead pid %d\n",
  105. _lock_path, n);
  106. unlink(lock_path);
  107. }
  108. }
  109. n = strlen(_lock_path) + 1;
  110. lock_path = lws_malloc(n);
  111. if (!lock_path) {
  112. fprintf(stderr, "Out of mem in lws_daemonize\n");
  113. return 1;
  114. }
  115. strcpy(lock_path, _lock_path);
  116. }
  117. /* Trap signals that we expect to receive */
  118. signal(SIGCHLD, child_handler); /* died */
  119. signal(SIGUSR1, child_handler); /* was happy */
  120. signal(SIGALRM, child_handler); /* timeout daemonizing */
  121. /* Fork off the parent process */
  122. pid_daemon = fork();
  123. if (pid_daemon < 0) {
  124. fprintf(stderr, "unable to fork daemon, code=%d (%s)",
  125. errno, strerror(errno));
  126. exit(9);
  127. }
  128. /* If we got a good PID, then we can exit the parent process. */
  129. if (pid_daemon > 0) {
  130. /*
  131. * Wait for confirmation signal from the child via
  132. * SIGCHILD / USR1, or for two seconds to elapse
  133. * (SIGALRM). pause() should not return.
  134. */
  135. alarm(2);
  136. pause();
  137. /* should not be reachable */
  138. exit(1);
  139. }
  140. /* At this point we are executing as the child process */
  141. parent = getppid();
  142. pid_daemon = getpid();
  143. /* Cancel certain signals */
  144. signal(SIGCHLD, SIG_DFL); /* A child process dies */
  145. signal(SIGTSTP, SIG_IGN); /* Various TTY signals */
  146. signal(SIGTTOU, SIG_IGN);
  147. signal(SIGTTIN, SIG_IGN);
  148. signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
  149. /* Change the file mode mask */
  150. umask(0);
  151. /* Create a new SID for the child process */
  152. sid = setsid();
  153. if (sid < 0) {
  154. fprintf(stderr,
  155. "unable to create a new session, code %d (%s)",
  156. errno, strerror(errno));
  157. exit(2);
  158. }
  159. /*
  160. * Change the current working directory. This prevents the current
  161. * directory from being locked; hence not being able to remove it.
  162. */
  163. if (chdir("/tmp") < 0) {
  164. fprintf(stderr,
  165. "unable to change directory to %s, code %d (%s)",
  166. "/", errno, strerror(errno));
  167. exit(3);
  168. }
  169. /* Redirect standard files to /dev/null */
  170. if (!freopen("/dev/null", "r", stdin))
  171. fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
  172. errno, strerror(errno));
  173. if (!freopen("/dev/null", "w", stdout))
  174. fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
  175. errno, strerror(errno));
  176. if (!freopen("/dev/null", "w", stderr))
  177. fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
  178. errno, strerror(errno));
  179. /* Tell the parent process that we are A-okay */
  180. kill(parent, SIGUSR1);
  181. act.sa_handler = lws_daemon_closing;
  182. sigemptyset(&act.sa_mask);
  183. act.sa_flags = 0;
  184. sigaction(SIGTERM, &act, NULL);
  185. /* return to continue what is now "the daemon" */
  186. return 0;
  187. }