Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 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 and is (c)2006 - 2013 Andy Green <andy@warmcat.com> |
| 11 | */ |
| 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 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 24 | #include "private-libwebsockets.h" |
| 25 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 26 | int pid_daemon; |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 27 | static char *lock_path; |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 28 | |
Joakim SoĢderberg | 68e8d73 | 2013-02-06 15:27:39 +0900 | [diff] [blame] | 29 | int get_daemonize_pid() |
| 30 | { |
| 31 | return pid_daemon; |
| 32 | } |
| 33 | |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 34 | static void |
| 35 | child_handler(int signum) |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 36 | { |
| 37 | int fd; |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 38 | int len; |
| 39 | int sent; |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 40 | char sz[20]; |
| 41 | |
| 42 | switch (signum) { |
| 43 | |
Peter Pentchev | e46f412 | 2015-10-01 12:25:05 +0300 | [diff] [blame] | 44 | case SIGALRM: /* timed out daemonizing */ |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 45 | exit(1); |
| 46 | break; |
| 47 | |
| 48 | case SIGUSR1: /* positive confirmation we daemonized well */ |
| 49 | /* Create the lock file as the current user */ |
| 50 | |
| 51 | fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640); |
| 52 | if (fd < 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 53 | fprintf(stderr, |
| 54 | "unable to create lock file %s, code=%d (%s)\n", |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 55 | lock_path, errno, strerror(errno)); |
| 56 | exit(1); |
| 57 | } |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 58 | len = sprintf(sz, "%u", pid_daemon); |
| 59 | sent = write(fd, sz, len); |
| 60 | if (sent != len) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 61 | fprintf(stderr, |
Peter Pentchev | e46f412 | 2015-10-01 12:25:05 +0300 | [diff] [blame] | 62 | "unable to write pid to lock file %s, code=%d (%s)\n", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 63 | lock_path, errno, strerror(errno)); |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 64 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 65 | close(fd); |
| 66 | exit(!!(sent == len)); |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 67 | |
| 68 | case SIGCHLD: /* daemonization failed */ |
| 69 | exit(1); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 74 | static void lws_daemon_closing(int sigact) |
| 75 | { |
| 76 | if (getpid() == pid_daemon) |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 77 | if (lock_path) { |
| 78 | unlink(lock_path); |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 79 | lws_free2(lock_path); |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 80 | } |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 81 | |
| 82 | kill(getpid(), SIGKILL); |
| 83 | } |
| 84 | |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 85 | /* |
| 86 | * You just need to call this from your main(), when it |
| 87 | * returns you are all set "in the background" decoupled |
| 88 | * from the console you were started from. |
| 89 | * |
| 90 | * The process context you called from has been terminated then. |
| 91 | */ |
| 92 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 93 | LWS_VISIBLE int |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 94 | lws_daemonize(const char *_lock_path) |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 95 | { |
| 96 | pid_t sid, parent; |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 97 | int fd; |
| 98 | char buf[10]; |
| 99 | int n, ret; |
| 100 | struct sigaction act; |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 101 | |
| 102 | /* already a daemon */ |
| 103 | if (getppid() == 1) |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 104 | return 1; |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 105 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 106 | fd = open(_lock_path, O_RDONLY); |
Andy Green | 0c5f670 | 2014-11-30 13:47:36 +0800 | [diff] [blame] | 107 | if (fd >= 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 108 | n = read(fd, buf, sizeof(buf)); |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 109 | close(fd); |
| 110 | if (n) { |
| 111 | n = atoi(buf); |
| 112 | ret = kill(n, 0); |
| 113 | if (ret >= 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 114 | fprintf(stderr, |
| 115 | "Daemon already running from pid %d\n", n); |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 116 | exit(1); |
| 117 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 118 | fprintf(stderr, |
| 119 | "Removing stale lock file %s from dead pid %d\n", |
| 120 | _lock_path, n); |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 121 | unlink(lock_path); |
| 122 | } |
Andy Green | dfa0f94 | 2014-11-30 13:32:27 +0800 | [diff] [blame] | 123 | } |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 124 | |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 125 | n = strlen(_lock_path) + 1; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 126 | lock_path = lws_malloc(n); |
Andy Green | f862463 | 2013-01-21 10:36:12 +0800 | [diff] [blame] | 127 | if (!lock_path) { |
| 128 | fprintf(stderr, "Out of mem in lws_daemonize\n"); |
| 129 | return 1; |
| 130 | } |
| 131 | strcpy(lock_path, _lock_path); |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 132 | |
Peter Pentchev | e46f412 | 2015-10-01 12:25:05 +0300 | [diff] [blame] | 133 | /* Trap signals that we expect to receive */ |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 134 | signal(SIGCHLD, child_handler); /* died */ |
| 135 | signal(SIGUSR1, child_handler); /* was happy */ |
| 136 | signal(SIGALRM, child_handler); /* timeout daemonizing */ |
| 137 | |
| 138 | /* Fork off the parent process */ |
| 139 | pid_daemon = fork(); |
| 140 | if (pid_daemon < 0) { |
| 141 | fprintf(stderr, "unable to fork daemon, code=%d (%s)", |
| 142 | errno, strerror(errno)); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
| 146 | /* If we got a good PID, then we can exit the parent process. */ |
| 147 | if (pid_daemon > 0) { |
| 148 | |
| 149 | /* |
| 150 | * Wait for confirmation signal from the child via |
| 151 | * SIGCHILD / USR1, or for two seconds to elapse |
| 152 | * (SIGALRM). pause() should not return. |
| 153 | */ |
| 154 | alarm(2); |
| 155 | |
| 156 | pause(); |
| 157 | /* should not be reachable */ |
| 158 | exit(1); |
| 159 | } |
| 160 | |
| 161 | /* At this point we are executing as the child process */ |
| 162 | parent = getppid(); |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 163 | pid_daemon = getpid(); |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 164 | |
| 165 | /* Cancel certain signals */ |
| 166 | signal(SIGCHLD, SIG_DFL); /* A child process dies */ |
| 167 | signal(SIGTSTP, SIG_IGN); /* Various TTY signals */ |
| 168 | signal(SIGTTOU, SIG_IGN); |
| 169 | signal(SIGTTIN, SIG_IGN); |
| 170 | signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */ |
| 171 | |
| 172 | /* Change the file mode mask */ |
| 173 | umask(0); |
| 174 | |
| 175 | /* Create a new SID for the child process */ |
| 176 | sid = setsid(); |
| 177 | if (sid < 0) { |
| 178 | fprintf(stderr, |
| 179 | "unable to create a new session, code %d (%s)", |
| 180 | errno, strerror(errno)); |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Change the current working directory. This prevents the current |
| 186 | * directory from being locked; hence not being able to remove it. |
| 187 | */ |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 188 | if (chdir("/") < 0) { |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 189 | fprintf(stderr, |
| 190 | "unable to change directory to %s, code %d (%s)", |
| 191 | "/", errno, strerror(errno)); |
| 192 | exit(1); |
| 193 | } |
| 194 | |
| 195 | /* Redirect standard files to /dev/null */ |
Edwin van den Oetelaar | 759c9ac | 2013-01-18 09:20:54 +0800 | [diff] [blame] | 196 | if (!freopen("/dev/null", "r", stdin)) |
| 197 | fprintf(stderr, "unable to freopen() stdin, code %d (%s)", |
| 198 | errno, strerror(errno)); |
| 199 | |
| 200 | if (!freopen("/dev/null", "w", stdout)) |
| 201 | fprintf(stderr, "unable to freopen() stdout, code %d (%s)", |
| 202 | errno, strerror(errno)); |
| 203 | |
| 204 | if (!freopen("/dev/null", "w", stderr)) |
| 205 | fprintf(stderr, "unable to freopen() stderr, code %d (%s)", |
| 206 | errno, strerror(errno)); |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 207 | |
| 208 | /* Tell the parent process that we are A-okay */ |
| 209 | kill(parent, SIGUSR1); |
| 210 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 211 | act.sa_handler = lws_daemon_closing; |
| 212 | sigemptyset(&act.sa_mask); |
| 213 | act.sa_flags = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 214 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 215 | sigaction(SIGTERM, &act, NULL); |
| 216 | |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 217 | /* return to continue what is now "the daemon" */ |
| 218 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 219 | return 0; |
Andy Green | b2de95d | 2013-01-17 15:49:27 +0800 | [diff] [blame] | 220 | } |
| 221 | |