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