Debian Codegraph @

lib/usual/daemon.c

raw symbols SCIP
1 /*
2 * Daemonization & pidfile handling.
3 *
4 * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20 #include <usual/daemon.h>
21
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include <usual/logging.h>
28 #include <usual/signal.h>
29
30 /*
31 * pidfile management.
32 */
33
34 static char *g_pidfile;
35
36 static void remove_pidfile(void)
37 {
38 if (!g_pidfile)
39 return;
40 unlink(g_pidfile);
41 free(g_pidfile);
42 g_pidfile = NULL;
43 }
44
45 /*
46 * Reads pid from pidfile and sends a signal to it.
47 *
48 * true - signaling was successful.
49 * false - ENOENT / ESRCH
50 *
51 * fatal() otherwise.
52 */
53 bool signal_pidfile(const char *pidfile, int sig)
54 {
55 char buf[128 + 1];
56 struct stat st;
57 pid_t pid = 0;
58 int fd, res;
59
60 if (!pidfile || !pidfile[0])
61 return false;
62
63 intr_loop:
64 /* check if pidfile exists */
65 if (stat(pidfile, &st) < 0)
66 goto fail;
67
68 /* read old pid */
69 fd = open(pidfile, O_RDONLY);
70 if (fd < 0)
71 goto fail;
72 res = read(fd, buf, sizeof(buf) - 1);
73 close(fd);
74 if (res <= 0)
75 goto fail;
76
77 /* parse pid */
78 buf[res] = 0;
79 errno = 0;
80 pid = strtoul(buf, NULL, 10);
81 if (errno) {
82 /* should we panic, or say no such process exists? */
83 if (0)
84 errno = ESRCH;
85 goto fail;
86 }
87
88 /* send the signal */
89 res = kill(pid, sig);
90 if (res == 0)
91 return true;
92 fail:
93 /* decide error seriousness */
94 if (errno == EINTR)
95 goto intr_loop;
96 if (errno == ENOENT || errno == ESRCH)
97 return false;
98 fatal_perror("signal_pidfile: unexpected error");
99 }
100
101 static void check_pidfile(const char *pidfile)
102 {
103 if (signal_pidfile(pidfile, 0))
104 die("pidfile exists, another instance running?");
105 if (errno == ESRCH) {
106 log_info("stale pidfile, removing");
107 unlink(pidfile);
108 }
109 }
110
111 static void write_pidfile(const char *pidfile, bool first_write)
112 {
113 char buf[64];
114 pid_t pid;
115 int res, fd, len;
116 static int atexit_hook = 0;
117 int flags = O_WRONLY | O_CREAT;
118
119 if (!pidfile || !pidfile[0])
120 return;
121
122 free(g_pidfile);
123 g_pidfile = strdup(pidfile);
124 if (!g_pidfile)
125 die("out of memory");
126
127 pid = getpid();
128 snprintf(buf, sizeof(buf), "%u\n", (unsigned)pid);
129
130 /* don't allow overwrite on first write */
131 if (first_write)
132 flags |= O_EXCL;
133
134 fd = open(pidfile, flags, 0644);
135 if (fd < 0)
136 die("could not open pidfile '%s': %s", pidfile, strerror(errno));
137 len = strlen(buf);
138 loop:
139 res = write(fd, buf, len);
140 if (res < 0) {
141 if (errno == EINTR)
142 goto loop;
143 die("write to pidfile '%s' failed: %s", pidfile, strerror(errno));
144 } else if (res < len) {
145 len -= res;
146 goto loop;
147 }
148 close(fd);
149
150 if (!atexit_hook) {
151 /* only remove when we have it actually written */
152 atexit(remove_pidfile);
153 atexit_hook = 1;
154 }
155 }
156
157 /*
158 * Function: daemonize
159 *
160 * Handle pidfile and daemonization.
161 *
162 * If pidfile is given, check if old process is running.
163 *
164 * If going background is required, require non-empty pidfile
165 * and logfile. Then fork to background and write pidfile.
166 */
167 void daemonize(const char *pidfile, bool go_background)
168 {
169 int pid, fd;
170
171 if (pidfile && pidfile[0]) {
172 check_pidfile(pidfile);
173 /* write pidfile twice, to be able to show problems to user */
174 write_pidfile(pidfile, true);
175 } else if (go_background)
176 fatal("daemon needs pidfile configured");
177
178 if (!go_background)
179 return;
180
181 if ((!cf_logfile || !cf_logfile[0]) && !cf_syslog)
182 fatal("daemon needs logging configured");
183
184 /* send stdin, stdout, stderr to /dev/null */
185 fd = open("/dev/null", O_RDWR);
186 if (fd < 0)
187 die("could not open /dev/null: %s", strerror(errno));
188 dup2(fd, 0);
189 dup2(fd, 1);
190 dup2(fd, 2);
191 if (fd > 2)
192 close(fd);
193
194 /* fork new process */
195 pid = fork();
196 if (pid < 0)
197 die("fork failed: %s", strerror(errno));
198 if (pid > 0)
199 _exit(0);
200
201 /* create new session */
202 pid = setsid();
203 if (pid < 0)
204 die("setsid: %s", strerror(errno));
205
206 /* fork again to avoid being session leader */
207 pid = fork();
208 if (pid < 0)
209 die("fork failed; %s", strerror(errno));
210 if (pid > 0)
211 _exit(0);
212
213 write_pidfile(pidfile, false);
214 }