2 * This code is mainly taken from Doug Potter's page
4 * http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize
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.
10 * This version is LGPL2.1+SLE like the rest of libwebsockets and is
11 * Copyright (c)2006 - 2013 Andy Green <andy@warmcat.com>
18 #include <sys/types.h>
25 #include "private-libwebsockets.h"
28 static char *lock_path;
30 int get_daemonize_pid()
36 child_handler(int signum)
43 case SIGALRM: /* timed out daemonizing */
47 case SIGUSR1: /* positive confirmation we daemonized well */
48 /* Create the lock file as the current user */
50 fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
53 "unable to create lock file %s, code=%d (%s)\n",
54 lock_path, errno, strerror(errno));
57 len = sprintf(sz, "%u", pid_daemon);
58 sent = write(fd, sz, len);
61 "unable to write pid to lock file %s, code=%d (%s)\n",
62 lock_path, errno, strerror(errno));
65 exit(!!(sent == len));
67 case SIGCHLD: /* daemonization failed */
73 static void lws_daemon_closing(int sigact)
75 if (getpid() == pid_daemon)
78 lws_free_set_NULL(lock_path);
81 kill(getpid(), SIGKILL);
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.
89 * The process context you called from has been terminated then.
93 lws_daemonize(const char *_lock_path)
100 /* already a daemon */
104 fd = open(_lock_path, O_RDONLY);
106 n = read(fd, buf, sizeof(buf));
113 "Daemon already running from pid %d\n", n);
117 "Removing stale lock file %s from dead pid %d\n",
123 n = strlen(_lock_path) + 1;
124 lock_path = lws_malloc(n);
126 fprintf(stderr, "Out of mem in lws_daemonize\n");
129 strcpy(lock_path, _lock_path);
131 /* Trap signals that we expect to receive */
132 signal(SIGCHLD, child_handler); /* died */
133 signal(SIGUSR1, child_handler); /* was happy */
134 signal(SIGALRM, child_handler); /* timeout daemonizing */
136 /* Fork off the parent process */
138 if (pid_daemon < 0) {
139 fprintf(stderr, "unable to fork daemon, code=%d (%s)",
140 errno, strerror(errno));
144 /* If we got a good PID, then we can exit the parent process. */
145 if (pid_daemon > 0) {
148 * Wait for confirmation signal from the child via
149 * SIGCHILD / USR1, or for two seconds to elapse
150 * (SIGALRM). pause() should not return.
155 /* should not be reachable */
159 /* At this point we are executing as the child process */
161 pid_daemon = getpid();
163 /* Cancel certain signals */
164 signal(SIGCHLD, SIG_DFL); /* A child process dies */
165 signal(SIGTSTP, SIG_IGN); /* Various TTY signals */
166 signal(SIGTTOU, SIG_IGN);
167 signal(SIGTTIN, SIG_IGN);
168 signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
170 /* Change the file mode mask */
173 /* Create a new SID for the child process */
177 "unable to create a new session, code %d (%s)",
178 errno, strerror(errno));
183 * Change the current working directory. This prevents the current
184 * directory from being locked; hence not being able to remove it.
186 if (chdir("/") < 0) {
188 "unable to change directory to %s, code %d (%s)",
189 "/", errno, strerror(errno));
193 /* Redirect standard files to /dev/null */
194 if (!freopen("/dev/null", "r", stdin))
195 fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
196 errno, strerror(errno));
198 if (!freopen("/dev/null", "w", stdout))
199 fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
200 errno, strerror(errno));
202 if (!freopen("/dev/null", "w", stderr))
203 fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
204 errno, strerror(errno));
206 /* Tell the parent process that we are A-okay */
207 kill(parent, SIGUSR1);
209 act.sa_handler = lws_daemon_closing;
210 sigemptyset(&act.sa_mask);
213 sigaction(SIGTERM, &act, NULL);
215 /* return to continue what is now "the daemon" */