Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 646 lines (563 sloc) 18.435 kb
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
1 /*
e766825 @blp daemon: Suppress valgrind warnings from read_pidfile().
blp authored
2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
3 *
a14bc59 @blp Update primary code license to Apache 2.0.
blp authored
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:
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
7 *
a14bc59 @blp Update primary code license to Apache 2.0.
blp authored
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.
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
15 */
16
17 #include <config.h>
18 #include "daemon.h"
19 #include <errno.h>
20 #include <fcntl.h>
3762274 @blp Add some missing "#include"s.
blp authored
21 #include <signal.h>
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
22 #include <stdlib.h>
23 #include <string.h>
3762274 @blp Add some missing "#include"s.
blp authored
24 #include <sys/resource.h>
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
25 #include <sys/wait.h>
309eaa2 @ejj lib: Remove warnings in daemon.c
ejj authored
26 #include <sys/stat.h>
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
27 #include <unistd.h>
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
28 #include "command-line.h"
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
29 #include "fatal-signal.h"
30 #include "dirs.h"
ac718c9 @blp Implement library for lockfiles and use it in cfg code.
blp authored
31 #include "lockfile.h"
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
32 #include "process.h"
b8781ff @blp daemon: Don't ignore failed write to pipe.
blp authored
33 #include "socket-util.h"
03fbffb @blp Make sure that time advances in a daemon between calls to time_refres…
blp authored
34 #include "timeval.h"
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
35 #include "util.h"
36 #include "vlog.h"
37
d98e600 @blp vlog: Make client supply semicolon for VLOG_DEFINE_THIS_MODULE.
blp authored
38 VLOG_DEFINE_THIS_MODULE(daemon);
5136ce4 @blp vlog: Introduce VLOG_DEFINE_THIS_MODULE for declaring vlog module in …
blp authored
39
d4db830 @blp daemon: Improve comments.
blp authored
40 /* --detach: Should we run in the background? */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
41 static bool detach;
42
d4db830 @blp daemon: Improve comments.
blp authored
43 /* --pidfile: Name of pidfile (null if none). */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
44 static char *pidfile;
45
e4bd5e2 @blp daemon: Fix behavior of read_pidfile() for our own pidfile.
blp authored
46 /* Device and inode of pidfile, so we can avoid reopening it. */
47 static dev_t pidfile_dev;
48 static ino_t pidfile_ino;
49
d4db830 @blp daemon: Improve comments.
blp authored
50 /* --overwrite-pidfile: Create pidfile even if one already exists and is
51 locked? */
e7bd7d7 @justinpettit daemon: Remove short options from daemon library
justinpettit authored
52 static bool overwrite_pidfile;
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
53
d4db830 @blp daemon: Improve comments.
blp authored
54 /* --no-chdir: Should we chdir to "/"? */
91a1e24 @justinpettit daemon: Provide option to not chdir to root
justinpettit authored
55 static bool chdir_ = true;
56
7943cd5 @blp daemon: Refactor code.
blp authored
57 /* File descriptor used by daemonize_start() and daemonize_complete(). */
58 static int daemonize_fd = -1;
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
59
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
60 /* --monitor: Should a supervisory process monitor the daemon and restart it if
61 * it dies due to an error signal? */
62 static bool monitor;
63
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
64 static void check_already_running(void);
65 static int lock_pidfile(FILE *, int command);
66
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
67 /* Returns the file name that would be used for a pidfile if 'name' were
68 * provided to set_pidfile(). The caller must free the returned string. */
69 char *
d295e8e treewide: Remove trailing whitespace
Joe Perches authored
70 make_pidfile_name(const char *name)
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
71 {
daf03c5 @blp util: New functions get_cwd(), abs_file_name().
blp authored
72 return (!name
b43c6fe @blp Make installation directories overridable at runtime.
blp authored
73 ? xasprintf("%s/%s.pid", ovs_rundir(), program_name)
74 : abs_file_name(ovs_rundir(), name));
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
75 }
76
77 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
78 * If 'name' begins with '/', then it is treated as an absolute path.
79 * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
80 * default.
81 *
82 * If 'name' is null, then program_name followed by ".pid" is used. */
83 void
84 set_pidfile(const char *name)
85 {
86 free(pidfile);
87 pidfile = make_pidfile_name(name);
88 }
89
90 /* Returns an absolute path to the configured pidfile, or a null pointer if no
91 * pidfile is configured. The caller must not modify or free the returned
92 * string. */
93 const char *
94 get_pidfile(void)
95 {
96 return pidfile;
97 }
98
91a1e24 @justinpettit daemon: Provide option to not chdir to root
justinpettit authored
99 /* Sets that we do not chdir to "/". */
100 void
101 set_no_chdir(void)
102 {
103 chdir_ = false;
104 }
105
eb077b2 @blp ovsdb-server: Maintain the database lock with --detach.
blp authored
106 /* Will we chdir to "/" as part of daemonizing? */
107 bool
108 is_chdir_enabled(void)
109 {
110 return chdir_;
111 }
112
00c0858 @blp daemon: Integrate checking for an existing pidfile into daemonize_sta…
blp authored
113 /* Normally, daemonize() or damonize_start() will terminate the program with a
114 * message if a locked pidfile already exists. If this function is called, an
115 * existing pidfile will be replaced, with a warning. */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
116 void
117 ignore_existing_pidfile(void)
118 {
e7bd7d7 @justinpettit daemon: Remove short options from daemon library
justinpettit authored
119 overwrite_pidfile = true;
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
120 }
121
122 /* Sets up a following call to daemonize() to detach from the foreground
123 * session, running this process in the background. */
124 void
125 set_detach(void)
126 {
127 detach = true;
128 }
129
eb077b2 @blp ovsdb-server: Maintain the database lock with --detach.
blp authored
130 /* Will daemonize() really detach? */
131 bool
132 get_detach(void)
133 {
134 return detach;
135 }
136
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
137 /* Sets up a following call to daemonize() to fork a supervisory process to
138 * monitor the daemon and restart it if it dies due to an error signal. */
139 void
140 daemon_set_monitor(void)
141 {
142 monitor = true;
143 }
144
d4db830 @blp daemon: Improve comments.
blp authored
145 /* If a pidfile has been configured, creates it and stores the running
146 * process's pid in it. Ensures that the pidfile will be deleted when the
147 * process exits. */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
148 static void
149 make_pidfile(void)
150 {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
151 long int pid = getpid();
152 struct stat s;
153 char *tmpfile;
154 FILE *file;
155 int error;
156
157 /* Create a temporary pidfile. */
158 tmpfile = xasprintf("%s.tmp%ld", pidfile, pid);
159 fatal_signal_add_file_to_unlink(tmpfile);
160 file = fopen(tmpfile, "w+");
161 if (!file) {
162 VLOG_FATAL("%s: create failed (%s)", tmpfile, strerror(errno));
163 }
164
165 if (fstat(fileno(file), &s) == -1) {
166 VLOG_FATAL("%s: fstat failed (%s)", tmpfile, strerror(errno));
167 }
168
169 fprintf(file, "%ld\n", pid);
170 if (fflush(file) == EOF) {
171 VLOG_FATAL("%s: write failed (%s)", tmpfile, strerror(errno));
172 }
173
174 error = lock_pidfile(file, F_SETLK);
175 if (error) {
176 VLOG_FATAL("%s: fcntl(F_SETLK) failed (%s)", tmpfile, strerror(error));
177 }
178
179 /* Rename or link it to the correct name. */
180 if (overwrite_pidfile) {
181 if (rename(tmpfile, pidfile) < 0) {
182 VLOG_FATAL("failed to rename \"%s\" to \"%s\" (%s)",
183 tmpfile, pidfile, strerror(errno));
184 }
185 } else {
186 do {
187 error = link(tmpfile, pidfile) == -1 ? errno : 0;
188 if (error == EEXIST) {
189 check_already_running();
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
190 }
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
191 } while (error == EINTR || error == EEXIST);
192 if (error) {
193 VLOG_FATAL("failed to link \"%s\" as \"%s\" (%s)",
194 tmpfile, pidfile, strerror(error));
195 }
196 }
197
198 /* Ensure that the pidfile will get deleted on exit. */
199 fatal_signal_add_file_to_unlink(pidfile);
200
201 /* Delete the temporary pidfile if it still exists. */
202 if (!overwrite_pidfile) {
203 error = fatal_signal_unlink_file_now(tmpfile);
204 if (error) {
205 VLOG_FATAL("%s: unlink failed (%s)", tmpfile, strerror(error));
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
206 }
207 }
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
208
209 /* Clean up.
210 *
211 * We don't close 'file' because its file descriptor must remain open to
212 * hold the lock. */
213 pidfile_dev = s.st_dev;
214 pidfile_ino = s.st_ino;
215 free(tmpfile);
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
216 free(pidfile);
217 pidfile = NULL;
218 }
219
220 /* If configured with set_pidfile() or set_detach(), creates the pid file and
221 * detaches from the foreground session. */
222 void
223 daemonize(void)
224 {
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
225 daemonize_start();
226 daemonize_complete();
227 }
228
7943cd5 @blp daemon: Refactor code.
blp authored
229 static pid_t
230 fork_and_wait_for_startup(int *fdp)
231 {
232 int fds[2];
233 pid_t pid;
234
279c9e0 @blp Log anything that could prevent a daemon from starting.
blp authored
235 xpipe(fds);
7943cd5 @blp daemon: Refactor code.
blp authored
236
237 pid = fork();
238 if (pid > 0) {
239 /* Running in parent process. */
af9a144 @blp daemon: Tolerate EINTR in fork_and_wait_for_startup().
blp authored
240 size_t bytes_read;
7943cd5 @blp daemon: Refactor code.
blp authored
241 char c;
242
243 close(fds[1]);
244 fatal_signal_fork();
af9a144 @blp daemon: Tolerate EINTR in fork_and_wait_for_startup().
blp authored
245 if (read_fully(fds[0], &c, 1, &bytes_read) != 0) {
7943cd5 @blp daemon: Refactor code.
blp authored
246 int retval;
247 int status;
248
249 do {
250 retval = waitpid(pid, &status, 0);
251 } while (retval == -1 && errno == EINTR);
252
253 if (retval == pid
254 && WIFEXITED(status)
255 && WEXITSTATUS(status)) {
256 /* Child exited with an error. Convey the same error to
257 * our parent process as a courtesy. */
258 exit(WEXITSTATUS(status));
259 }
260
279c9e0 @blp Log anything that could prevent a daemon from starting.
blp authored
261 VLOG_FATAL("fork child failed to signal startup (%s)",
262 strerror(errno));
7943cd5 @blp daemon: Refactor code.
blp authored
263 }
264 close(fds[0]);
265 *fdp = -1;
266 } else if (!pid) {
267 /* Running in child process. */
268 close(fds[0]);
269 time_postfork();
270 lockfile_postfork();
271 *fdp = fds[1];
272 } else {
279c9e0 @blp Log anything that could prevent a daemon from starting.
blp authored
273 VLOG_FATAL("fork failed (%s)", strerror(errno));
7943cd5 @blp daemon: Refactor code.
blp authored
274 }
275
276 return pid;
277 }
278
279 static void
280 fork_notify_startup(int fd)
281 {
282 if (fd != -1) {
283 size_t bytes_written;
284 int error;
285
286 error = write_fully(fd, "", 1, &bytes_written);
287 if (error) {
279c9e0 @blp Log anything that could prevent a daemon from starting.
blp authored
288 VLOG_FATAL("pipe write failed (%s)", strerror(error));
7943cd5 @blp daemon: Refactor code.
blp authored
289 }
290
291 close(fd);
292 }
293 }
294
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
295 static bool
296 should_restart(int status)
297 {
298 if (WIFSIGNALED(status)) {
299 static const int error_signals[] = {
300 SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGILL, SIGPIPE, SIGSEGV,
301 SIGXCPU, SIGXFSZ
302 };
303
304 size_t i;
305
306 for (i = 0; i < ARRAY_SIZE(error_signals); i++) {
307 if (error_signals[i] == WTERMSIG(status)) {
308 return true;
309 }
310 }
311 }
312 return false;
313 }
314
315 static void
316 monitor_daemon(pid_t daemon_pid)
317 {
318 /* XXX Should log daemon's stderr output at startup time. */
319 const char *saved_program_name;
a9633ad @blp daemon: Throttle max respawning rate.
blp authored
320 time_t last_restart;
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
321 char *status_msg;
cbbdf81 @blp daemon: Report number of crashes on monitor process command line.
blp authored
322 int crashes;
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
323
324 saved_program_name = program_name;
325 program_name = xasprintf("monitor(%s)", program_name);
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
326 status_msg = xstrdup("healthy");
a9633ad @blp daemon: Throttle max respawning rate.
blp authored
327 last_restart = TIME_MIN;
cbbdf81 @blp daemon: Report number of crashes on monitor process command line.
blp authored
328 crashes = 0;
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
329 for (;;) {
330 int retval;
331 int status;
332
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
333 proctitle_set("%s: monitoring pid %lu (%s)",
334 saved_program_name, (unsigned long int) daemon_pid,
335 status_msg);
336
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
337 do {
338 retval = waitpid(daemon_pid, &status, 0);
339 } while (retval == -1 && errno == EINTR);
340
341 if (retval == -1) {
279c9e0 @blp Log anything that could prevent a daemon from starting.
blp authored
342 VLOG_FATAL("waitpid failed (%s)", strerror(errno));
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
343 } else if (retval == daemon_pid) {
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
344 char *s = process_status_msg(status);
345 if (should_restart(status)) {
2bf9d87 @blp daemon: Don't call a normal exit from the monitor a "crash".
blp authored
346 free(status_msg);
347 status_msg = xasprintf("%d crashes: pid %lu died, %s",
348 ++crashes,
349 (unsigned long int) daemon_pid, s);
350 free(s);
351
7c2dd4c @blp daemon: Allow monitored daemon to dump core no more than once.
blp authored
352 if (WCOREDUMP(status)) {
353 /* Disable further core dumps to save disk space. */
354 struct rlimit r;
355
356 r.rlim_cur = 0;
357 r.rlim_max = 0;
358 if (setrlimit(RLIMIT_CORE, &r) == -1) {
359 VLOG_WARN("failed to disable core dumps: %s",
360 strerror(errno));
361 }
362 }
363
a9633ad @blp daemon: Throttle max respawning rate.
blp authored
364 /* Throttle restarts to no more than once every 10 seconds. */
365 if (time(NULL) < last_restart + 10) {
366 VLOG_WARN("%s, waiting until 10 seconds since last "
367 "restart", status_msg);
368 for (;;) {
369 time_t now = time(NULL);
370 time_t wakeup = last_restart + 10;
371 if (now >= wakeup) {
372 break;
373 }
374 sleep(wakeup - now);
375 }
376 }
377 last_restart = time(NULL);
378
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
379 VLOG_ERR("%s, restarting", status_msg);
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
380 daemon_pid = fork_and_wait_for_startup(&daemonize_fd);
381 if (!daemon_pid) {
382 break;
383 }
384 } else {
2bf9d87 @blp daemon: Don't call a normal exit from the monitor a "crash".
blp authored
385 VLOG_INFO("pid %lu died, %s, exiting",
386 (unsigned long int) daemon_pid, s);
387 free(s);
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
388 exit(0);
389 }
390 }
391 }
b2d06cb @blp daemon: Fix memory leak in --monitor implementation.
blp authored
392 free(status_msg);
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
393
394 /* Running in new daemon process. */
40f0707 @blp daemon: Make --monitor process change its process title.
blp authored
395 proctitle_restore();
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
396 free((char *) program_name);
397 program_name = saved_program_name;
398 }
399
7943cd5 @blp daemon: Refactor code.
blp authored
400 /* Close stdin, stdout, stderr. If we're started from e.g. an SSH session,
401 * then this keeps us from holding that session open artificially. */
402 static void
403 close_standard_fds(void)
404 {
405 int null_fd = get_null_fd();
406 if (null_fd >= 0) {
407 dup2(null_fd, STDIN_FILENO);
408 dup2(null_fd, STDOUT_FILENO);
409 dup2(null_fd, STDERR_FILENO);
410 }
d382421 @blp daemon: Disable logging to console after detaching.
blp authored
411
412 /* Disable logging to stderr to avoid wasting CPU time. */
413 vlog_set_levels(NULL, VLF_CONSOLE, VLL_EMER);
7943cd5 @blp daemon: Refactor code.
blp authored
414 }
415
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
416 /* If daemonization is configured, then starts daemonization, by forking and
417 * returning in the child process. The parent process hangs around until the
418 * child lets it know either that it completed startup successfully (by calling
419 * daemon_complete()) or that it failed to start up (by exiting with a nonzero
420 * exit code). */
421 void
422 daemonize_start(void)
423 {
7943cd5 @blp daemon: Refactor code.
blp authored
424 daemonize_fd = -1;
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
425
7943cd5 @blp daemon: Refactor code.
blp authored
426 if (detach) {
427 if (fork_and_wait_for_startup(&daemonize_fd) > 0) {
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
428 /* Running in parent process. */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
429 exit(0);
430 }
ff8decf @blp daemon: Add support for process monitoring and restart.
blp authored
431 /* Running in daemon or monitor process. */
432 }
433
434 if (monitor) {
435 int saved_daemonize_fd = daemonize_fd;
436 pid_t daemon_pid;
437
438 daemon_pid = fork_and_wait_for_startup(&daemonize_fd);
439 if (daemon_pid > 0) {
440 /* Running in monitor process. */
441 fork_notify_startup(saved_daemonize_fd);
442 close_standard_fds();
443 monitor_daemon(daemon_pid);
444 }
7943cd5 @blp daemon: Refactor code.
blp authored
445 /* Running in daemon process. */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
446 }
7943cd5 @blp daemon: Refactor code.
blp authored
447
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
448 if (pidfile) {
449 make_pidfile();
450 }
df5d2ed @blp daemon: Make sure that vlog is initialized when a process daemonizes.
blp authored
451
452 /* Make sure that the unixctl commands for vlog get registered in a
453 * daemon, even before the first log message. */
454 vlog_init();
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
455 }
456
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
457 /* If daemonization is configured, then this function notifies the parent
a7ff9bd @blp ovs-vswitchd: Complete daemonization only after initial configuration.
blp authored
458 * process that the child process has completed startup successfully.
459 *
460 * Calling this function more than once has no additional effect. */
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
461 void
462 daemonize_complete(void)
463 {
7943cd5 @blp daemon: Refactor code.
blp authored
464 fork_notify_startup(daemonize_fd);
a7ff9bd @blp ovs-vswitchd: Complete daemonization only after initial configuration.
blp authored
465 daemonize_fd = -1;
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
466
7943cd5 @blp daemon: Refactor code.
blp authored
467 if (detach) {
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
468 setsid();
469 if (chdir_) {
470 ignore(chdir("/"));
471 }
7943cd5 @blp daemon: Refactor code.
blp authored
472 close_standard_fds();
a7ff9bd @blp ovs-vswitchd: Complete daemonization only after initial configuration.
blp authored
473 detach = false;
9544028 @blp daemon: Allow daemon child process to report success or failure to pa…
blp authored
474 }
475 }
476
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
477 void
478 daemon_usage(void)
479 {
480 printf(
481 "\nDaemon options:\n"
e7bd7d7 @justinpettit daemon: Remove short options from daemon library
justinpettit authored
482 " --detach run in background as daemon\n"
91a1e24 @justinpettit daemon: Provide option to not chdir to root
justinpettit authored
483 " --no-chdir do not chdir to '/'\n"
e7bd7d7 @justinpettit daemon: Remove short options from daemon library
justinpettit authored
484 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
485 " --overwrite-pidfile with --pidfile, start even if already "
486 "running\n",
b43c6fe @blp Make installation directories overridable at runtime.
blp authored
487 ovs_rundir(), program_name);
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
488 }
489
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
490 static int
491 lock_pidfile__(FILE *file, int command, struct flock *lck)
492 {
493 int error;
494
495 lck->l_type = F_WRLCK;
496 lck->l_whence = SEEK_SET;
497 lck->l_start = 0;
498 lck->l_len = 0;
499 lck->l_pid = 0;
500
501 do {
502 error = fcntl(fileno(file), command, lck) == -1 ? errno : 0;
503 } while (error == EINTR);
504 return error;
505 }
506
507 static int
508 lock_pidfile(FILE *file, int command)
509 {
510 struct flock lck;
511
512 return lock_pidfile__(file, command, &lck);
513 }
514
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
515 static pid_t
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
516 read_pidfile__(const char *pidfile, bool delete_if_stale)
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
517 {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
518 struct stat s, s2;
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
519 struct flock lck;
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
520 char line[128];
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
521 FILE *file;
522 int error;
523
e4bd5e2 @blp daemon: Fix behavior of read_pidfile() for our own pidfile.
blp authored
524 if ((pidfile_ino || pidfile_dev)
525 && !stat(pidfile, &s)
526 && s.st_ino == pidfile_ino && s.st_dev == pidfile_dev) {
527 /* It's our own pidfile. We can't afford to open it, because closing
528 * *any* fd for a file that a process has locked also releases all the
529 * locks on that file.
530 *
531 * Fortunately, we know the associated pid anyhow: */
532 return getpid();
533 }
534
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
535 file = fopen(pidfile, "r+");
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
536 if (!file) {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
537 if (errno == ENOENT && delete_if_stale) {
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
538 return 0;
539 }
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
540 error = errno;
541 VLOG_WARN("%s: open: %s", pidfile, strerror(error));
542 goto error;
543 }
544
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
545 error = lock_pidfile__(file, F_GETLK, &lck);
546 if (error) {
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
547 VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error));
548 goto error;
549 }
550 if (lck.l_type == F_UNLCK) {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
551 /* pidfile exists but it isn't locked by anyone. We need to delete it
552 * so that a new pidfile can go in its place. But just calling
553 * unlink(pidfile) makes a nasty race: what if someone else unlinks it
554 * before we do and then replaces it by a valid pidfile? We'd unlink
555 * their valid pidfile. We do a little dance to avoid the race, by
556 * locking the invalid pidfile. Only one process can have the invalid
557 * pidfile locked, and only that process has the right to unlink it. */
558 if (!delete_if_stale) {
559 error = ESRCH;
0b37694 @blp daemon: Reduce log level of "pid file is stale" message.
blp authored
560 VLOG_DBG("%s: pid file is stale", pidfile);
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
561 goto error;
562 }
563
564 /* Get the lock. */
565 error = lock_pidfile(file, F_SETLK);
566 if (error) {
567 /* We lost a race with someone else doing the same thing. */
568 VLOG_WARN("%s: lost race to lock pidfile", pidfile);
569 goto error;
570 }
571
572 /* Is the file we have locked still named 'pidfile'? */
573 if (stat(pidfile, &s) || fstat(fileno(file), &s2)
574 || s.st_ino != s2.st_ino || s.st_dev != s2.st_dev) {
575 /* No. We lost a race with someone else who got the lock before
576 * us, deleted the pidfile, and closed it (releasing the lock). */
577 error = EALREADY;
578 VLOG_WARN("%s: lost race to delete pidfile", pidfile);
579 goto error;
580 }
581
582 /* We won the right to delete the stale pidfile. */
583 if (unlink(pidfile)) {
584 error = errno;
585 VLOG_WARN("%s: failed to delete stale pidfile (%s)",
586 pidfile, strerror(error));
587 goto error;
588 }
589 VLOG_DBG("%s: deleted stale pidfile", pidfile);
590 fclose(file);
591 return 0;
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
592 }
593
594 if (!fgets(line, sizeof line, file)) {
595 if (ferror(file)) {
596 error = errno;
597 VLOG_WARN("%s: read: %s", pidfile, strerror(error));
598 } else {
599 error = ESRCH;
600 VLOG_WARN("%s: read: unexpected end of file", pidfile);
601 }
602 goto error;
603 }
604
605 if (lck.l_pid != strtoul(line, NULL, 10)) {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
606 /* The process that has the pidfile locked is not the process that
607 * created it. It must be stale, with the process that has it locked
608 * preparing to delete it. */
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
609 error = ESRCH;
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
610 VLOG_WARN("%s: stale pidfile for pid %s being deleted by pid %ld",
611 pidfile, line, (long int) lck.l_pid);
064af42 @blp Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc…
blp authored
612 goto error;
613 }
614
615 fclose(file);
616 return lck.l_pid;
617
618 error:
619 if (file) {
620 fclose(file);
621 }
622 return -error;
623 }
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
624
625 /* Opens and reads a PID from 'pidfile'. Returns the positive PID if
626 * successful, otherwise a negative errno value. */
627 pid_t
628 read_pidfile(const char *pidfile)
629 {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
630 return read_pidfile__(pidfile, false);
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
631 }
632
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
633 /* Checks whether a process with the given 'pidfile' is already running and,
634 * if so, aborts. If 'pidfile' is stale, deletes it. */
635 static void
636 check_already_running(void)
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
637 {
aacea8b @blp daemon: Avoid races on pidfile creation.
blp authored
638 long int pid = read_pidfile__(pidfile, true);
639 if (pid > 0) {
640 VLOG_FATAL("%s: already running as pid %ld, aborting", pidfile, pid);
641 } else if (pid < 0) {
642 VLOG_FATAL("%s: pidfile check failed (%s), aborting",
643 pidfile, strerror(-pid));
644 }
18e124a @blp daemon: Avoid redundant code in already_running().
blp authored
645 }
Something went wrong with that request. Please try again.