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>
24 static int pid_daemon;
25 static char lock_path[PATH_MAX];
28 child_handler(int signum)
37 case SIGALRM: /* timedout daemonizing */
41 case SIGUSR1: /* positive confirmation we daemonized well */
42 /* Create the lock file as the current user */
44 fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
46 fprintf(stderr, "unable to create lock"
47 " file %s, code=%d (%s)",
48 lock_path, errno, strerror(errno));
51 len = sprintf(sz, "%u", pid_daemon);
52 sent = write(fd, sz, len);
54 fprintf(stderr, "unable write pid to lock"
55 " file %s, code=%d (%s)",
56 lock_path, errno, strerror(errno));
59 exit(!!(sent == len));
61 case SIGCHLD: /* daemonization failed */
68 * You just need to call this from your main(), when it
69 * returns you are all set "in the background" decoupled
70 * from the console you were started from.
72 * The process context you called from has been terminated then.
76 lws_daemonize(const char *_lock_path)
80 /* already a daemon */
84 strncpy(lock_path, _lock_path, sizeof lock_path);
85 lock_path[sizeof(lock_path) - 1] = '\0';
87 /* Trap signals that we expect to recieve */
88 signal(SIGCHLD, child_handler); /* died */
89 signal(SIGUSR1, child_handler); /* was happy */
90 signal(SIGALRM, child_handler); /* timeout daemonizing */
92 /* Fork off the parent process */
95 fprintf(stderr, "unable to fork daemon, code=%d (%s)",
96 errno, strerror(errno));
100 /* If we got a good PID, then we can exit the parent process. */
101 if (pid_daemon > 0) {
104 * Wait for confirmation signal from the child via
105 * SIGCHILD / USR1, or for two seconds to elapse
106 * (SIGALRM). pause() should not return.
111 /* should not be reachable */
115 /* At this point we are executing as the child process */
118 /* Cancel certain signals */
119 signal(SIGCHLD, SIG_DFL); /* A child process dies */
120 signal(SIGTSTP, SIG_IGN); /* Various TTY signals */
121 signal(SIGTTOU, SIG_IGN);
122 signal(SIGTTIN, SIG_IGN);
123 signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
125 /* Change the file mode mask */
128 /* Create a new SID for the child process */
132 "unable to create a new session, code %d (%s)",
133 errno, strerror(errno));
138 * Change the current working directory. This prevents the current
139 * directory from being locked; hence not being able to remove it.
141 if (chdir("/") < 0) {
143 "unable to change directory to %s, code %d (%s)",
144 "/", errno, strerror(errno));
148 /* Redirect standard files to /dev/null */
149 if (!freopen("/dev/null", "r", stdin))
150 fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
151 errno, strerror(errno));
153 if (!freopen("/dev/null", "w", stdout))
154 fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
155 errno, strerror(errno));
157 if (!freopen("/dev/null", "w", stderr))
158 fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
159 errno, strerror(errno));
161 /* Tell the parent process that we are A-okay */
162 kill(parent, SIGUSR1);
164 /* return to continue what is now "the daemon" */