blob: 5e3af8ecb08a4f9254b9f80e93159a76fea4f790 [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
Andy Green24cba922013-01-19 13:56:10 +080024int pid_daemon;
Andy Greenf8624632013-01-21 10:36:12 +080025static char *lock_path;
Andy Greenb2de95d2013-01-17 15:49:27 +080026
Joakim SoĢˆderberg68e8d732013-02-06 15:27:39 +090027int get_daemonize_pid()
28{
29 return pid_daemon;
30}
31
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080032static void
33child_handler(int signum)
Andy Greenb2de95d2013-01-17 15:49:27 +080034{
35 int fd;
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080036 int len;
37 int sent;
Andy Greenb2de95d2013-01-17 15:49:27 +080038 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 Greenb5b23192013-02-11 17:13:32 +080051 fprintf(stderr,
52 "unable to create lock file %s, code=%d (%s)\n",
Andy Greenb2de95d2013-01-17 15:49:27 +080053 lock_path, errno, strerror(errno));
54 exit(1);
55 }
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080056 len = sprintf(sz, "%u", pid_daemon);
57 sent = write(fd, sz, len);
58 if (sent != len)
Andy Greenb5b23192013-02-11 17:13:32 +080059 fprintf(stderr,
60 "unable write pid to lock file %s, code=%d (%s)\n",
61 lock_path, errno, strerror(errno));
Edwin van den Oetelaar759c9ac2013-01-18 09:20:54 +080062
Andy Greenb5b23192013-02-11 17:13:32 +080063 close(fd);
64 exit(!!(sent == len));
Andy Greenb2de95d2013-01-17 15:49:27 +080065
66 case SIGCHLD: /* daemonization failed */
67 exit(1);
68 break;
69 }
70}
71
Andy Green24cba922013-01-19 13:56:10 +080072static void lws_daemon_closing(int sigact)
73{
74 if (getpid() == pid_daemon)
Andy Greenf8624632013-01-21 10:36:12 +080075 if (lock_path) {
76 unlink(lock_path);
77 free(lock_path);
78 lock_path = NULL;
79 }
Andy Green24cba922013-01-19 13:56:10 +080080
81 kill(getpid(), SIGKILL);
82}
83
Andy Greenb2de95d2013-01-17 15:49:27 +080084/*
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 Oetelaar759c9ac2013-01-18 09:20:54 +080092int
93lws_daemonize(const char *_lock_path)
Andy Greenb2de95d2013-01-17 15:49:27 +080094{
95 pid_t sid, parent;
Andy Green24cba922013-01-19 13:56:10 +080096 int fd;
97 char buf[10];
98 int n, ret;
99 struct sigaction act;
Andy Greenb2de95d2013-01-17 15:49:27 +0800100
101 /* already a daemon */
102 if (getppid() == 1)
Andy Greenf8624632013-01-21 10:36:12 +0800103 return 1;
Andy Greenb2de95d2013-01-17 15:49:27 +0800104
Andy Green24cba922013-01-19 13:56:10 +0800105 fd = open(_lock_path, O_RDONLY);
106 if (fd > 0) {
Andy Greenb5b23192013-02-11 17:13:32 +0800107 n = read(fd, buf, sizeof(buf));
Andy Green24cba922013-01-19 13:56:10 +0800108 close(fd);
109 if (n) {
110 n = atoi(buf);
111 ret = kill(n, 0);
112 if (ret >= 0) {
Andy Greenb5b23192013-02-11 17:13:32 +0800113 fprintf(stderr,
114 "Daemon already running from pid %d\n", n);
Andy Green24cba922013-01-19 13:56:10 +0800115 exit(1);
116 }
Andy Greenb5b23192013-02-11 17:13:32 +0800117 fprintf(stderr,
118 "Removing stale lock file %s from dead pid %d\n",
119 _lock_path, n);
Andy Green24cba922013-01-19 13:56:10 +0800120 unlink(lock_path);
121 }
122 }
123
Andy Greenf8624632013-01-21 10:36:12 +0800124 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 Greenb2de95d2013-01-17 15:49:27 +0800131
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 Green24cba922013-01-19 13:56:10 +0800162 pid_daemon = getpid();
Andy Greenb2de95d2013-01-17 15:49:27 +0800163
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 Oetelaar759c9ac2013-01-18 09:20:54 +0800187 if (chdir("/") < 0) {
Andy Greenb2de95d2013-01-17 15:49:27 +0800188 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 Oetelaar759c9ac2013-01-18 09:20:54 +0800195 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 Greenb2de95d2013-01-17 15:49:27 +0800206
207 /* Tell the parent process that we are A-okay */
208 kill(parent, SIGUSR1);
209
Andy Green24cba922013-01-19 13:56:10 +0800210 act.sa_handler = lws_daemon_closing;
211 sigemptyset(&act.sa_mask);
212 act.sa_flags = 0;
Andy Greenb5b23192013-02-11 17:13:32 +0800213
Andy Green24cba922013-01-19 13:56:10 +0800214 sigaction(SIGTERM, &act, NULL);
215
Andy Greenb2de95d2013-01-17 15:49:27 +0800216 /* return to continue what is now "the daemon" */
217
Andy Greenb5b23192013-02-11 17:13:32 +0800218 return 0;
Andy Greenb2de95d2013-01-17 15:49:27 +0800219}
220