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 and is (c)2006 - 2013 Andy Green <andy@warmcat.com>
17 #include <sys/types.h>
25 static char *lock_path;
27 int get_daemonize_pid()
33 child_handler(int signum)
42 case SIGALRM: /* timedout daemonizing */
46 case SIGUSR1: /* positive confirmation we daemonized well */
47 /* Create the lock file as the current user */
49 fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
52 "unable to create lock file %s, code=%d (%s)\n",
53 lock_path, errno, strerror(errno));
56 len = sprintf(sz, "%u", pid_daemon);
57 sent = write(fd, sz, len);
60 "unable write pid to lock file %s, code=%d (%s)\n",
61 lock_path, errno, strerror(errno));
64 exit(!!(sent == len));
66 case SIGCHLD: /* daemonization failed */
72 static void lws_daemon_closing(int sigact)
74 if (getpid() == pid_daemon)
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)
101 /* already a daemon */
105 fd = open(_lock_path, O_RDONLY);
107 n = read(fd, buf, sizeof(buf));
114 "Daemon already running from pid %d\n", n);
118 "Removing stale lock file %s from dead pid %d\n",
124 n = strlen(_lock_path) + 1;
125 lock_path = malloc(n);
127 fprintf(stderr, "Out of mem in lws_daemonize\n");
130 strcpy(lock_path, _lock_path);
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 */
137 /* Fork off the parent process */
139 if (pid_daemon < 0) {
140 fprintf(stderr, "unable to fork daemon, code=%d (%s)",
141 errno, strerror(errno));
145 /* If we got a good PID, then we can exit the parent process. */
146 if (pid_daemon > 0) {
149 * Wait for confirmation signal from the child via
150 * SIGCHILD / USR1, or for two seconds to elapse
151 * (SIGALRM). pause() should not return.
156 /* should not be reachable */
160 /* At this point we are executing as the child process */
162 pid_daemon = getpid();
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 */
171 /* Change the file mode mask */
174 /* Create a new SID for the child process */
178 "unable to create a new session, code %d (%s)",
179 errno, strerror(errno));
184 * Change the current working directory. This prevents the current
185 * directory from being locked; hence not being able to remove it.
187 if (chdir("/") < 0) {
189 "unable to change directory to %s, code %d (%s)",
190 "/", errno, strerror(errno));
194 /* Redirect standard files to /dev/null */
195 if (!freopen("/dev/null", "r", stdin))
196 fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
197 errno, strerror(errno));
199 if (!freopen("/dev/null", "w", stdout))
200 fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
201 errno, strerror(errno));
203 if (!freopen("/dev/null", "w", stderr))
204 fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
205 errno, strerror(errno));
207 /* Tell the parent process that we are A-okay */
208 kill(parent, SIGUSR1);
210 act.sa_handler = lws_daemon_closing;
211 sigemptyset(&act.sa_mask);
214 sigaction(SIGTERM, &act, NULL);
216 /* return to continue what is now "the daemon" */