]> git.proxmox.com Git - mirror_ovs.git/blob - lib/daemon.c
ovsdb-server: Maintain the database lock with --detach.
[mirror_ovs.git] / lib / daemon.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "daemon.h"
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "fatal-signal.h"
25 #include "dirs.h"
26 #include "lockfile.h"
27 #include "timeval.h"
28 #include "util.h"
29
30 #define THIS_MODULE VLM_daemon
31 #include "vlog.h"
32
33 /* Should we run in the background? */
34 static bool detach;
35
36 /* Name of pidfile (null if none). */
37 static char *pidfile;
38
39 /* Create pidfile even if one already exists and is locked? */
40 static bool overwrite_pidfile;
41
42 /* Should we chdir to "/". */
43 static bool chdir_ = true;
44
45 /* Returns the file name that would be used for a pidfile if 'name' were
46 * provided to set_pidfile(). The caller must free the returned string. */
47 char *
48 make_pidfile_name(const char *name)
49 {
50 return (!name ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
51 : *name == '/' ? xstrdup(name)
52 : xasprintf("%s/%s", ovs_rundir, name));
53 }
54
55 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
56 * If 'name' begins with '/', then it is treated as an absolute path.
57 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
58 * default.
59 *
60 * If 'name' is null, then program_name followed by ".pid" is used. */
61 void
62 set_pidfile(const char *name)
63 {
64 free(pidfile);
65 pidfile = make_pidfile_name(name);
66 }
67
68 /* Returns an absolute path to the configured pidfile, or a null pointer if no
69 * pidfile is configured. The caller must not modify or free the returned
70 * string. */
71 const char *
72 get_pidfile(void)
73 {
74 return pidfile;
75 }
76
77 /* Sets that we do not chdir to "/". */
78 void
79 set_no_chdir(void)
80 {
81 chdir_ = false;
82 }
83
84 /* Will we chdir to "/" as part of daemonizing? */
85 bool
86 is_chdir_enabled(void)
87 {
88 return chdir_;
89 }
90
91 /* Normally, die_if_already_running() will terminate the program with a message
92 * if a locked pidfile already exists. If this function is called,
93 * die_if_already_running() will merely log a warning. */
94 void
95 ignore_existing_pidfile(void)
96 {
97 overwrite_pidfile = true;
98 }
99
100 /* Sets up a following call to daemonize() to detach from the foreground
101 * session, running this process in the background. */
102 void
103 set_detach(void)
104 {
105 detach = true;
106 }
107
108 /* Will daemonize() really detach? */
109 bool
110 get_detach(void)
111 {
112 return detach;
113 }
114
115 /* If a pidfile has been configured and that pidfile already exists and is
116 * locked by a running process, returns the pid of the running process.
117 * Otherwise, returns 0. */
118 static pid_t
119 already_running(void)
120 {
121 pid_t pid = 0;
122 if (pidfile) {
123 int fd = open(pidfile, O_RDWR);
124 if (fd >= 0) {
125 struct flock lck;
126 lck.l_type = F_WRLCK;
127 lck.l_whence = SEEK_SET;
128 lck.l_start = 0;
129 lck.l_len = 0;
130 if (fcntl(fd, F_GETLK, &lck) != -1 && lck.l_type != F_UNLCK) {
131 pid = lck.l_pid;
132 }
133 close(fd);
134 }
135 }
136 return pid;
137 }
138
139 /* If a locked pidfile exists, issue a warning message and, unless
140 * ignore_existing_pidfile() has been called, terminate the program. */
141 void
142 die_if_already_running(void)
143 {
144 pid_t pid = already_running();
145 if (pid) {
146 if (!overwrite_pidfile) {
147 ovs_fatal(0, "%s: already running as pid %ld",
148 get_pidfile(), (long int) pid);
149 } else {
150 VLOG_WARN("%s: %s already running as pid %ld",
151 get_pidfile(), program_name, (long int) pid);
152 }
153 }
154 }
155
156 /* If a pidfile has been configured, creates it and stores the running process'
157 * pid init. Ensures that the pidfile will be deleted when the process
158 * exits. */
159 static void
160 make_pidfile(void)
161 {
162 if (pidfile) {
163 /* Create pidfile via temporary file, so that observers never see an
164 * empty pidfile or an unlocked pidfile. */
165 long int pid = getpid();
166 char *tmpfile;
167 int fd;
168
169 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
170 fatal_signal_add_file_to_unlink(tmpfile);
171 fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
172 if (fd >= 0) {
173 struct flock lck;
174 lck.l_type = F_WRLCK;
175 lck.l_whence = SEEK_SET;
176 lck.l_start = 0;
177 lck.l_len = 0;
178 if (fcntl(fd, F_SETLK, &lck) != -1) {
179 char *text = xasprintf("%ld\n", pid);
180 if (write(fd, text, strlen(text)) == strlen(text)) {
181 fatal_signal_add_file_to_unlink(pidfile);
182 if (rename(tmpfile, pidfile) < 0) {
183 VLOG_ERR("failed to rename \"%s\" to \"%s\": %s",
184 tmpfile, pidfile, strerror(errno));
185 fatal_signal_remove_file_to_unlink(pidfile);
186 close(fd);
187 } else {
188 /* Keep 'fd' open to retain the lock. */
189 }
190 free(text);
191 } else {
192 VLOG_ERR("%s: write failed: %s", tmpfile, strerror(errno));
193 close(fd);
194 }
195 } else {
196 VLOG_ERR("%s: fcntl failed: %s", tmpfile, strerror(errno));
197 close(fd);
198 }
199 } else {
200 VLOG_ERR("%s: create failed: %s", tmpfile, strerror(errno));
201 }
202 fatal_signal_remove_file_to_unlink(tmpfile);
203 free(tmpfile);
204 }
205 free(pidfile);
206 pidfile = NULL;
207 }
208
209 /* If configured with set_pidfile() or set_detach(), creates the pid file and
210 * detaches from the foreground session. */
211 void
212 daemonize(void)
213 {
214 if (detach) {
215 char c = 0;
216 int fds[2];
217 if (pipe(fds) < 0) {
218 ovs_fatal(errno, "pipe failed");
219 }
220
221 switch (fork()) {
222 default:
223 /* Parent process: wait for child to create pidfile, then exit. */
224 close(fds[1]);
225 fatal_signal_fork();
226 if (read(fds[0], &c, 1) != 1) {
227 ovs_fatal(errno, "daemon child failed to signal startup");
228 }
229 exit(0);
230
231 case 0:
232 /* Child process. */
233 close(fds[0]);
234 make_pidfile();
235 write(fds[1], &c, 1);
236 close(fds[1]);
237 setsid();
238 if (chdir_) {
239 chdir("/");
240 }
241 time_postfork();
242 lockfile_postfork();
243 break;
244
245 case -1:
246 /* Error. */
247 ovs_fatal(errno, "could not fork");
248 break;
249 }
250 } else {
251 make_pidfile();
252 }
253 }
254
255 void
256 daemon_usage(void)
257 {
258 printf(
259 "\nDaemon options:\n"
260 " --detach run in background as daemon\n"
261 " --no-chdir do not chdir to '/'\n"
262 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
263 " --overwrite-pidfile with --pidfile, start even if already "
264 "running\n",
265 ovs_rundir, program_name);
266 }
267
268 /* Opens and reads a PID from 'pidfile'. Returns the nonnegative PID if
269 * successful, otherwise a negative errno value. */
270 pid_t
271 read_pidfile(const char *pidfile)
272 {
273 char line[128];
274 struct flock lck;
275 FILE *file;
276 int error;
277
278 file = fopen(pidfile, "r");
279 if (!file) {
280 error = errno;
281 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
282 goto error;
283 }
284
285 lck.l_type = F_WRLCK;
286 lck.l_whence = SEEK_SET;
287 lck.l_start = 0;
288 lck.l_len = 0;
289 if (fcntl(fileno(file), F_GETLK, &lck)) {
290 error = errno;
291 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
292 goto error;
293 }
294 if (lck.l_type == F_UNLCK) {
295 error = ESRCH;
296 VLOG_WARN("%s: pid file is not locked", pidfile);
297 goto error;
298 }
299
300 if (!fgets(line, sizeof line, file)) {
301 if (ferror(file)) {
302 error = errno;
303 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
304 } else {
305 error = ESRCH;
306 VLOG_WARN("%s: read: unexpected end of file", pidfile);
307 }
308 goto error;
309 }
310
311 if (lck.l_pid != strtoul(line, NULL, 10)) {
312 error = ESRCH;
313 VLOG_WARN("l_pid (%ld) != %s pid (%s)",
314 (long int) lck.l_pid, pidfile, line);
315 goto error;
316 }
317
318 fclose(file);
319 return lck.l_pid;
320
321 error:
322 if (file) {
323 fclose(file);
324 }
325 return -error;
326 }