blob: 0892cb06e277bb19aab6c96722db159a57d26cac [file] [log] [blame]
Andy Greenb2de95d2013-01-17 15:49:27 +08001/*
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 Pentchev9a4fef72013-03-30 09:52:21 +080024#include "private-libwebsockets.h"
25
Andy Green24cba922013-01-19 13:56:10 +080026int pid_daemon;
Andy Greenf8624632013-01-21 10:36:12 +080027static char *lock_path;
Andy Greenb2de95d2013-01-17 15:49:27 +080028
Joakim SoĢˆderberg68e8d732013-02-06 15:27:39 +090029int get_daemonize_pid()
30{
31 return pid_daemon;
32}
33
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080034static void
35child_handler(int signum)
Andy Greenb2de95d2013-01-17 15:49:27 +080036{
37 int fd;
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080038 int len;
39 int sent;
Andy Greenb2de95d2013-01-17 15:49:27 +080040 char sz[20];
41
42 switch (signum) {
43
Peter Pentcheve46f4122015-10-01 12:25:05 +030044 case SIGALRM: /* timed out daemonizing */
Andy Greenb2de95d2013-01-17 15:49:27 +080045 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 Greenb5b23192013-02-11 17:13:32 +080053 fprintf(stderr,
54 "unable to create lock file %s, code=%d (%s)\n",
Andy Greenb2de95d2013-01-17 15:49:27 +080055 lock_path, errno, strerror(errno));
56 exit(1);
57 }
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080058 len = sprintf(sz, "%u", pid_daemon);
59 sent = write(fd, sz, len);
60 if (sent != len)
Andy Greenb5b23192013-02-11 17:13:32 +080061 fprintf(stderr,
Peter Pentcheve46f4122015-10-01 12:25:05 +030062 "unable to write pid to lock file %s, code=%d (%s)\n",
Andy Greenb5b23192013-02-11 17:13:32 +080063 lock_path, errno, strerror(errno));
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080064
Andy Greenb5b23192013-02-11 17:13:32 +080065 close(fd);
66 exit(!!(sent == len));
Andy Greenb2de95d2013-01-17 15:49:27 +080067
68 case SIGCHLD: /* daemonization failed */
69 exit(1);
70 break;
71 }
72}
73
Andy Green24cba922013-01-19 13:56:10 +080074static void lws_daemon_closing(int sigact)
75{
76 if (getpid() == pid_daemon)
Andy Greenf8624632013-01-21 10:36:12 +080077 if (lock_path) {
78 unlink(lock_path);
Alejandro Meryac3ec392014-12-05 00:09:20 +010079 lws_free2(lock_path);
Andy Greenf8624632013-01-21 10:36:12 +080080 }
Andy Green24cba922013-01-19 13:56:10 +080081
82 kill(getpid(), SIGKILL);
83}
84
Andy Greenb2de95d2013-01-17 15:49:27 +080085/*
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 Pentchev9a4fef72013-03-30 09:52:21 +080093LWS_VISIBLE int
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080094lws_daemonize(const char *_lock_path)
Andy Greenb2de95d2013-01-17 15:49:27 +080095{
96 pid_t sid, parent;
Andy Green24cba922013-01-19 13:56:10 +080097 int fd;
98 char buf[10];
99 int n, ret;
100 struct sigaction act;
Andy Greenb2de95d2013-01-17 15:49:27 +0800101
102 /* already a daemon */
103 if (getppid() == 1)
Andy Greenf8624632013-01-21 10:36:12 +0800104 return 1;
Andy Greenb2de95d2013-01-17 15:49:27 +0800105
Andy Green24cba922013-01-19 13:56:10 +0800106 fd = open(_lock_path, O_RDONLY);
Andy Green0c5f6702014-11-30 13:47:36 +0800107 if (fd >= 0) {
Andy Greenb5b23192013-02-11 17:13:32 +0800108 n = read(fd, buf, sizeof(buf));
Andy Green24cba922013-01-19 13:56:10 +0800109 close(fd);
110 if (n) {
111 n = atoi(buf);
112 ret = kill(n, 0);
113 if (ret >= 0) {
Andy Greenb5b23192013-02-11 17:13:32 +0800114 fprintf(stderr,
115 "Daemon already running from pid %d\n", n);
Andy Green24cba922013-01-19 13:56:10 +0800116 exit(1);
117 }
Andy Greenb5b23192013-02-11 17:13:32 +0800118 fprintf(stderr,
119 "Removing stale lock file %s from dead pid %d\n",
120 _lock_path, n);
Andy Green24cba922013-01-19 13:56:10 +0800121 unlink(lock_path);
122 }
Andy Greendfa0f942014-11-30 13:32:27 +0800123 }
Andy Green24cba922013-01-19 13:56:10 +0800124
Andy Greenf8624632013-01-21 10:36:12 +0800125 n = strlen(_lock_path) + 1;
Alejandro Mery6ff28242014-12-04 23:59:35 +0100126 lock_path = lws_malloc(n);
Andy Greenf8624632013-01-21 10:36:12 +0800127 if (!lock_path) {
128 fprintf(stderr, "Out of mem in lws_daemonize\n");
129 return 1;
130 }
131 strcpy(lock_path, _lock_path);
Andy Greenb2de95d2013-01-17 15:49:27 +0800132
Peter Pentcheve46f4122015-10-01 12:25:05 +0300133 /* Trap signals that we expect to receive */
Andy Greenb2de95d2013-01-17 15:49:27 +0800134 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 Green24cba922013-01-19 13:56:10 +0800163 pid_daemon = getpid();
Andy Greenb2de95d2013-01-17 15:49:27 +0800164
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 Oetelaar759c9ac2013-01-18 09:20:54 +0800188 if (chdir("/") < 0) {
Andy Greenb2de95d2013-01-17 15:49:27 +0800189 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 Oetelaar759c9ac2013-01-18 09:20:54 +0800196 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 Greenb2de95d2013-01-17 15:49:27 +0800207
208 /* Tell the parent process that we are A-okay */
209 kill(parent, SIGUSR1);
210
Andy Green24cba922013-01-19 13:56:10 +0800211 act.sa_handler = lws_daemon_closing;
212 sigemptyset(&act.sa_mask);
213 act.sa_flags = 0;
Andy Greenb5b23192013-02-11 17:13:32 +0800214
Andy Green24cba922013-01-19 13:56:10 +0800215 sigaction(SIGTERM, &act, NULL);
216
Andy Greenb2de95d2013-01-17 15:49:27 +0800217 /* return to continue what is now "the daemon" */
218
Andy Greenb5b23192013-02-11 17:13:32 +0800219 return 0;
Andy Greenb2de95d2013-01-17 15:49:27 +0800220}
221