]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
728a8b14 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "daemon.h" | |
19 | #include <errno.h> | |
20 | #include <fcntl.h> | |
3762274e | 21 | #include <signal.h> |
064af421 BP |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
3762274e | 24 | #include <sys/resource.h> |
95440284 | 25 | #include <sys/wait.h> |
309eaa2b | 26 | #include <sys/stat.h> |
064af421 | 27 | #include <unistd.h> |
40f0707c | 28 | #include "command-line.h" |
064af421 BP |
29 | #include "fatal-signal.h" |
30 | #include "dirs.h" | |
ac718c9d | 31 | #include "lockfile.h" |
728a8b14 | 32 | #include "ovs-thread.h" |
ff8decf1 | 33 | #include "process.h" |
b8781ff0 | 34 | #include "socket-util.h" |
03fbffbd | 35 | #include "timeval.h" |
064af421 | 36 | #include "util.h" |
064af421 BP |
37 | #include "vlog.h" |
38 | ||
d98e6007 | 39 | VLOG_DEFINE_THIS_MODULE(daemon); |
5136ce49 | 40 | |
d4db8309 | 41 | /* --detach: Should we run in the background? */ |
e8087a87 BP |
42 | static bool detach; /* Was --detach specified? */ |
43 | static bool detached; /* Have we already detached? */ | |
064af421 | 44 | |
d4db8309 | 45 | /* --pidfile: Name of pidfile (null if none). */ |
064af421 BP |
46 | static char *pidfile; |
47 | ||
e4bd5e2a BP |
48 | /* Device and inode of pidfile, so we can avoid reopening it. */ |
49 | static dev_t pidfile_dev; | |
50 | static ino_t pidfile_ino; | |
51 | ||
d4db8309 BP |
52 | /* --overwrite-pidfile: Create pidfile even if one already exists and is |
53 | locked? */ | |
e7bd7d78 | 54 | static bool overwrite_pidfile; |
064af421 | 55 | |
d4db8309 | 56 | /* --no-chdir: Should we chdir to "/"? */ |
91a1e24d JP |
57 | static bool chdir_ = true; |
58 | ||
7943cd51 BP |
59 | /* File descriptor used by daemonize_start() and daemonize_complete(). */ |
60 | static int daemonize_fd = -1; | |
95440284 | 61 | |
ff8decf1 BP |
62 | /* --monitor: Should a supervisory process monitor the daemon and restart it if |
63 | * it dies due to an error signal? */ | |
64 | static bool monitor; | |
65 | ||
7d0c5973 BP |
66 | /* For each of the standard file descriptors, whether to replace it by |
67 | * /dev/null (if false) or keep it for the daemon to use (if true). */ | |
68 | static bool save_fds[3]; | |
69 | ||
aacea8ba BP |
70 | static void check_already_running(void); |
71 | static int lock_pidfile(FILE *, int command); | |
72 | ||
064af421 BP |
73 | /* Returns the file name that would be used for a pidfile if 'name' were |
74 | * provided to set_pidfile(). The caller must free the returned string. */ | |
75 | char * | |
d295e8e9 | 76 | make_pidfile_name(const char *name) |
064af421 | 77 | { |
daf03c53 | 78 | return (!name |
b43c6fe2 BP |
79 | ? xasprintf("%s/%s.pid", ovs_rundir(), program_name) |
80 | : abs_file_name(ovs_rundir(), name)); | |
064af421 BP |
81 | } |
82 | ||
83 | /* Sets up a following call to daemonize() to create a pidfile named 'name'. | |
84 | * If 'name' begins with '/', then it is treated as an absolute path. | |
85 | * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by | |
86 | * default. | |
87 | * | |
88 | * If 'name' is null, then program_name followed by ".pid" is used. */ | |
89 | void | |
90 | set_pidfile(const char *name) | |
91 | { | |
728a8b14 | 92 | assert_single_threaded(); |
064af421 BP |
93 | free(pidfile); |
94 | pidfile = make_pidfile_name(name); | |
95 | } | |
96 | ||
97 | /* Returns an absolute path to the configured pidfile, or a null pointer if no | |
98 | * pidfile is configured. The caller must not modify or free the returned | |
99 | * string. */ | |
100 | const char * | |
101 | get_pidfile(void) | |
102 | { | |
103 | return pidfile; | |
104 | } | |
105 | ||
91a1e24d JP |
106 | /* Sets that we do not chdir to "/". */ |
107 | void | |
108 | set_no_chdir(void) | |
109 | { | |
110 | chdir_ = false; | |
111 | } | |
112 | ||
eb077b26 BP |
113 | /* Will we chdir to "/" as part of daemonizing? */ |
114 | bool | |
115 | is_chdir_enabled(void) | |
116 | { | |
117 | return chdir_; | |
118 | } | |
119 | ||
00c08589 BP |
120 | /* Normally, daemonize() or damonize_start() will terminate the program with a |
121 | * message if a locked pidfile already exists. If this function is called, an | |
122 | * existing pidfile will be replaced, with a warning. */ | |
064af421 BP |
123 | void |
124 | ignore_existing_pidfile(void) | |
125 | { | |
e7bd7d78 | 126 | overwrite_pidfile = true; |
064af421 BP |
127 | } |
128 | ||
129 | /* Sets up a following call to daemonize() to detach from the foreground | |
130 | * session, running this process in the background. */ | |
131 | void | |
132 | set_detach(void) | |
133 | { | |
134 | detach = true; | |
135 | } | |
136 | ||
eb077b26 BP |
137 | /* Will daemonize() really detach? */ |
138 | bool | |
139 | get_detach(void) | |
140 | { | |
141 | return detach; | |
142 | } | |
143 | ||
ff8decf1 BP |
144 | /* Sets up a following call to daemonize() to fork a supervisory process to |
145 | * monitor the daemon and restart it if it dies due to an error signal. */ | |
146 | void | |
147 | daemon_set_monitor(void) | |
148 | { | |
149 | monitor = true; | |
150 | } | |
151 | ||
7d0c5973 BP |
152 | /* A daemon doesn't normally have any use for the file descriptors for stdin, |
153 | * stdout, and stderr after it detaches. To keep these file descriptors from | |
154 | * e.g. holding an SSH session open, by default detaching replaces each of | |
155 | * these file descriptors by /dev/null. But a few daemons expect the user to | |
156 | * redirect stdout or stderr to a file, in which case it is desirable to keep | |
157 | * these file descriptors. This function, therefore, disables replacing 'fd' | |
158 | * by /dev/null when the daemon detaches. */ | |
159 | void | |
160 | daemon_save_fd(int fd) | |
161 | { | |
cb22974d BP |
162 | ovs_assert(fd == STDIN_FILENO || |
163 | fd == STDOUT_FILENO || | |
164 | fd == STDERR_FILENO); | |
7d0c5973 BP |
165 | save_fds[fd] = true; |
166 | } | |
167 | ||
7ffd3f69 GS |
168 | /* Unregisters pidfile from being unlinked when the program terminates via |
169 | * exit() or a fatal signal. */ | |
170 | void | |
171 | remove_pidfile_from_unlink(void) | |
172 | { | |
173 | if (pidfile) { | |
174 | fatal_signal_remove_file_to_unlink(pidfile); | |
175 | } | |
176 | } | |
177 | ||
178 | /* Registers pidfile to be unlinked when the program terminates via exit() or a | |
179 | * fatal signal. */ | |
180 | void | |
181 | add_pidfile_to_unlink(void) | |
182 | { | |
183 | if (pidfile) { | |
184 | fatal_signal_add_file_to_unlink(pidfile); | |
185 | } | |
186 | } | |
187 | ||
d4db8309 BP |
188 | /* If a pidfile has been configured, creates it and stores the running |
189 | * process's pid in it. Ensures that the pidfile will be deleted when the | |
190 | * process exits. */ | |
064af421 BP |
191 | static void |
192 | make_pidfile(void) | |
193 | { | |
aacea8ba BP |
194 | long int pid = getpid(); |
195 | struct stat s; | |
196 | char *tmpfile; | |
197 | FILE *file; | |
198 | int error; | |
199 | ||
200 | /* Create a temporary pidfile. */ | |
2388a783 EJ |
201 | if (overwrite_pidfile) { |
202 | tmpfile = xasprintf("%s.tmp%ld", pidfile, pid); | |
203 | fatal_signal_add_file_to_unlink(tmpfile); | |
204 | } else { | |
205 | /* Everyone shares the same file which will be treated as a lock. To | |
206 | * avoid some uncomfortable race conditions, we can't set up the fatal | |
207 | * signal unlink until we've acquired it. */ | |
208 | tmpfile = xasprintf("%s.tmp", pidfile); | |
209 | } | |
210 | ||
211 | file = fopen(tmpfile, "a+"); | |
aacea8ba BP |
212 | if (!file) { |
213 | VLOG_FATAL("%s: create failed (%s)", tmpfile, strerror(errno)); | |
214 | } | |
215 | ||
2388a783 EJ |
216 | error = lock_pidfile(file, F_SETLK); |
217 | if (error) { | |
218 | /* Looks like we failed to acquire the lock. Note that, if we failed | |
219 | * for some other reason (and '!overwrite_pidfile'), we will have | |
220 | * left 'tmpfile' as garbage in the file system. */ | |
221 | VLOG_FATAL("%s: fcntl(F_SETLK) failed (%s)", tmpfile, strerror(error)); | |
222 | } | |
223 | ||
224 | if (!overwrite_pidfile) { | |
225 | /* We acquired the lock. Make sure to clean up on exit, and verify | |
226 | * that we're allowed to create the actual pidfile. */ | |
227 | fatal_signal_add_file_to_unlink(tmpfile); | |
228 | check_already_running(); | |
229 | } | |
230 | ||
aacea8ba BP |
231 | if (fstat(fileno(file), &s) == -1) { |
232 | VLOG_FATAL("%s: fstat failed (%s)", tmpfile, strerror(errno)); | |
233 | } | |
234 | ||
2388a783 EJ |
235 | if (ftruncate(fileno(file), 0) == -1) { |
236 | VLOG_FATAL("%s: truncate failed (%s)", tmpfile, strerror(errno)); | |
237 | } | |
238 | ||
aacea8ba BP |
239 | fprintf(file, "%ld\n", pid); |
240 | if (fflush(file) == EOF) { | |
241 | VLOG_FATAL("%s: write failed (%s)", tmpfile, strerror(errno)); | |
242 | } | |
243 | ||
2388a783 | 244 | error = rename(tmpfile, pidfile); |
aacea8ba | 245 | |
2388a783 EJ |
246 | /* Due to a race, 'tmpfile' may be owned by a different process, so we |
247 | * shouldn't delete it on exit. */ | |
248 | fatal_signal_remove_file_to_unlink(tmpfile); | |
249 | ||
250 | if (error < 0) { | |
251 | VLOG_FATAL("failed to rename \"%s\" to \"%s\" (%s)", | |
252 | tmpfile, pidfile, strerror(errno)); | |
aacea8ba BP |
253 | } |
254 | ||
255 | /* Ensure that the pidfile will get deleted on exit. */ | |
256 | fatal_signal_add_file_to_unlink(pidfile); | |
257 | ||
aacea8ba BP |
258 | /* Clean up. |
259 | * | |
260 | * We don't close 'file' because its file descriptor must remain open to | |
261 | * hold the lock. */ | |
262 | pidfile_dev = s.st_dev; | |
263 | pidfile_ino = s.st_ino; | |
264 | free(tmpfile); | |
064af421 BP |
265 | } |
266 | ||
267 | /* If configured with set_pidfile() or set_detach(), creates the pid file and | |
268 | * detaches from the foreground session. */ | |
269 | void | |
270 | daemonize(void) | |
95440284 BP |
271 | { |
272 | daemonize_start(); | |
273 | daemonize_complete(); | |
274 | } | |
275 | ||
8aee05cc BP |
276 | /* Calls fork() and on success returns its return value. On failure, logs an |
277 | * error and exits unsuccessfully. | |
278 | * | |
279 | * Post-fork, but before returning, this function calls a few other functions | |
280 | * that are generally useful if the child isn't planning to exec a new | |
281 | * process. */ | |
282 | pid_t | |
283 | fork_and_clean_up(void) | |
284 | { | |
728a8b14 | 285 | pid_t pid = xfork(); |
8aee05cc BP |
286 | if (pid > 0) { |
287 | /* Running in parent process. */ | |
288 | fatal_signal_fork(); | |
289 | } else if (!pid) { | |
290 | /* Running in child process. */ | |
291 | time_postfork(); | |
292 | lockfile_postfork(); | |
8aee05cc | 293 | } |
8aee05cc BP |
294 | return pid; |
295 | } | |
296 | ||
e6c5e539 BP |
297 | /* Forks, then: |
298 | * | |
299 | * - In the parent, waits for the child to signal that it has completed its | |
300 | * startup sequence. Then stores -1 in '*fdp' and returns the child's pid. | |
301 | * | |
302 | * - In the child, stores a fd in '*fdp' and returns 0. The caller should | |
303 | * pass the fd to fork_notify_startup() after it finishes its startup | |
304 | * sequence. | |
305 | * | |
306 | * If something goes wrong with the fork, logs a critical error and aborts the | |
307 | * process. */ | |
7943cd51 BP |
308 | static pid_t |
309 | fork_and_wait_for_startup(int *fdp) | |
310 | { | |
311 | int fds[2]; | |
312 | pid_t pid; | |
313 | ||
279c9e03 | 314 | xpipe(fds); |
7943cd51 | 315 | |
8aee05cc | 316 | pid = fork_and_clean_up(); |
7943cd51 BP |
317 | if (pid > 0) { |
318 | /* Running in parent process. */ | |
af9a1442 | 319 | size_t bytes_read; |
7943cd51 BP |
320 | char c; |
321 | ||
322 | close(fds[1]); | |
af9a1442 | 323 | if (read_fully(fds[0], &c, 1, &bytes_read) != 0) { |
7943cd51 BP |
324 | int retval; |
325 | int status; | |
326 | ||
327 | do { | |
328 | retval = waitpid(pid, &status, 0); | |
329 | } while (retval == -1 && errno == EINTR); | |
330 | ||
2c8fcc9c BP |
331 | if (retval == pid) { |
332 | if (WIFEXITED(status) && WEXITSTATUS(status)) { | |
333 | /* Child exited with an error. Convey the same error | |
334 | * to our parent process as a courtesy. */ | |
335 | exit(WEXITSTATUS(status)); | |
336 | } else { | |
337 | char *status_msg = process_status_msg(status); | |
338 | VLOG_FATAL("fork child died before signaling startup (%s)", | |
339 | status_msg); | |
340 | } | |
341 | } else if (retval < 0) { | |
342 | VLOG_FATAL("waitpid failed (%s)", strerror(errno)); | |
343 | } else { | |
344 | NOT_REACHED(); | |
7943cd51 | 345 | } |
7943cd51 BP |
346 | } |
347 | close(fds[0]); | |
348 | *fdp = -1; | |
349 | } else if (!pid) { | |
350 | /* Running in child process. */ | |
351 | close(fds[0]); | |
7943cd51 | 352 | *fdp = fds[1]; |
7943cd51 BP |
353 | } |
354 | ||
355 | return pid; | |
356 | } | |
357 | ||
358 | static void | |
359 | fork_notify_startup(int fd) | |
360 | { | |
361 | if (fd != -1) { | |
362 | size_t bytes_written; | |
363 | int error; | |
364 | ||
365 | error = write_fully(fd, "", 1, &bytes_written); | |
366 | if (error) { | |
279c9e03 | 367 | VLOG_FATAL("pipe write failed (%s)", strerror(error)); |
7943cd51 BP |
368 | } |
369 | ||
370 | close(fd); | |
371 | } | |
372 | } | |
373 | ||
ff8decf1 BP |
374 | static bool |
375 | should_restart(int status) | |
376 | { | |
377 | if (WIFSIGNALED(status)) { | |
378 | static const int error_signals[] = { | |
379 | SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGILL, SIGPIPE, SIGSEGV, | |
380 | SIGXCPU, SIGXFSZ | |
381 | }; | |
382 | ||
383 | size_t i; | |
384 | ||
385 | for (i = 0; i < ARRAY_SIZE(error_signals); i++) { | |
386 | if (error_signals[i] == WTERMSIG(status)) { | |
387 | return true; | |
388 | } | |
389 | } | |
390 | } | |
391 | return false; | |
392 | } | |
393 | ||
394 | static void | |
395 | monitor_daemon(pid_t daemon_pid) | |
396 | { | |
ff8decf1 | 397 | /* XXX Should log daemon's stderr output at startup time. */ |
a9633ada | 398 | time_t last_restart; |
40f0707c | 399 | char *status_msg; |
cbbdf81c | 400 | int crashes; |
ff8decf1 | 401 | |
781dee08 | 402 | subprogram_name = "monitor"; |
40f0707c | 403 | status_msg = xstrdup("healthy"); |
a9633ada | 404 | last_restart = TIME_MIN; |
cbbdf81c | 405 | crashes = 0; |
ff8decf1 BP |
406 | for (;;) { |
407 | int retval; | |
408 | int status; | |
409 | ||
d86a6c09 EM |
410 | proctitle_set("monitoring pid %lu (%s)", |
411 | (unsigned long int) daemon_pid, status_msg); | |
40f0707c | 412 | |
ff8decf1 BP |
413 | do { |
414 | retval = waitpid(daemon_pid, &status, 0); | |
415 | } while (retval == -1 && errno == EINTR); | |
416 | ||
417 | if (retval == -1) { | |
279c9e03 | 418 | VLOG_FATAL("waitpid failed (%s)", strerror(errno)); |
ff8decf1 | 419 | } else if (retval == daemon_pid) { |
40f0707c | 420 | char *s = process_status_msg(status); |
40f0707c | 421 | if (should_restart(status)) { |
2bf9d87a BP |
422 | free(status_msg); |
423 | status_msg = xasprintf("%d crashes: pid %lu died, %s", | |
424 | ++crashes, | |
425 | (unsigned long int) daemon_pid, s); | |
426 | free(s); | |
427 | ||
7c2dd4c6 BP |
428 | if (WCOREDUMP(status)) { |
429 | /* Disable further core dumps to save disk space. */ | |
430 | struct rlimit r; | |
431 | ||
432 | r.rlim_cur = 0; | |
433 | r.rlim_max = 0; | |
434 | if (setrlimit(RLIMIT_CORE, &r) == -1) { | |
435 | VLOG_WARN("failed to disable core dumps: %s", | |
436 | strerror(errno)); | |
437 | } | |
438 | } | |
439 | ||
a9633ada BP |
440 | /* Throttle restarts to no more than once every 10 seconds. */ |
441 | if (time(NULL) < last_restart + 10) { | |
442 | VLOG_WARN("%s, waiting until 10 seconds since last " | |
443 | "restart", status_msg); | |
444 | for (;;) { | |
445 | time_t now = time(NULL); | |
446 | time_t wakeup = last_restart + 10; | |
447 | if (now >= wakeup) { | |
448 | break; | |
449 | } | |
450 | sleep(wakeup - now); | |
451 | } | |
452 | } | |
453 | last_restart = time(NULL); | |
454 | ||
40f0707c | 455 | VLOG_ERR("%s, restarting", status_msg); |
ff8decf1 BP |
456 | daemon_pid = fork_and_wait_for_startup(&daemonize_fd); |
457 | if (!daemon_pid) { | |
458 | break; | |
459 | } | |
460 | } else { | |
2bf9d87a BP |
461 | VLOG_INFO("pid %lu died, %s, exiting", |
462 | (unsigned long int) daemon_pid, s); | |
463 | free(s); | |
ff8decf1 BP |
464 | exit(0); |
465 | } | |
466 | } | |
467 | } | |
b2d06cb8 | 468 | free(status_msg); |
ff8decf1 BP |
469 | |
470 | /* Running in new daemon process. */ | |
40f0707c | 471 | proctitle_restore(); |
781dee08 | 472 | subprogram_name = ""; |
ff8decf1 BP |
473 | } |
474 | ||
7d0c5973 BP |
475 | /* Close standard file descriptors (except any that the client has requested we |
476 | * leave open by calling daemon_save_fd()). If we're started from e.g. an SSH | |
477 | * session, then this keeps us from holding that session open artificially. */ | |
7943cd51 BP |
478 | static void |
479 | close_standard_fds(void) | |
480 | { | |
481 | int null_fd = get_null_fd(); | |
482 | if (null_fd >= 0) { | |
7d0c5973 BP |
483 | int fd; |
484 | ||
485 | for (fd = 0; fd < 3; fd++) { | |
486 | if (!save_fds[fd]) { | |
487 | dup2(null_fd, fd); | |
488 | } | |
489 | } | |
7943cd51 | 490 | } |
d3824212 BP |
491 | |
492 | /* Disable logging to stderr to avoid wasting CPU time. */ | |
c1a543a8 | 493 | vlog_set_levels(NULL, VLF_CONSOLE, VLL_OFF); |
7943cd51 BP |
494 | } |
495 | ||
95440284 BP |
496 | /* If daemonization is configured, then starts daemonization, by forking and |
497 | * returning in the child process. The parent process hangs around until the | |
498 | * child lets it know either that it completed startup successfully (by calling | |
499 | * daemon_complete()) or that it failed to start up (by exiting with a nonzero | |
500 | * exit code). */ | |
501 | void | |
502 | daemonize_start(void) | |
064af421 | 503 | { |
728a8b14 | 504 | assert_single_threaded(); |
7943cd51 | 505 | daemonize_fd = -1; |
95440284 | 506 | |
7943cd51 BP |
507 | if (detach) { |
508 | if (fork_and_wait_for_startup(&daemonize_fd) > 0) { | |
95440284 | 509 | /* Running in parent process. */ |
064af421 | 510 | exit(0); |
064af421 | 511 | } |
066f329e | 512 | |
ff8decf1 | 513 | /* Running in daemon or monitor process. */ |
066f329e | 514 | setsid(); |
ff8decf1 BP |
515 | } |
516 | ||
517 | if (monitor) { | |
518 | int saved_daemonize_fd = daemonize_fd; | |
519 | pid_t daemon_pid; | |
520 | ||
521 | daemon_pid = fork_and_wait_for_startup(&daemonize_fd); | |
522 | if (daemon_pid > 0) { | |
523 | /* Running in monitor process. */ | |
524 | fork_notify_startup(saved_daemonize_fd); | |
525 | close_standard_fds(); | |
526 | monitor_daemon(daemon_pid); | |
527 | } | |
7943cd51 | 528 | /* Running in daemon process. */ |
064af421 | 529 | } |
7943cd51 | 530 | |
aacea8ba BP |
531 | if (pidfile) { |
532 | make_pidfile(); | |
533 | } | |
df5d2ed9 BP |
534 | |
535 | /* Make sure that the unixctl commands for vlog get registered in a | |
536 | * daemon, even before the first log message. */ | |
537 | vlog_init(); | |
064af421 BP |
538 | } |
539 | ||
95440284 | 540 | /* If daemonization is configured, then this function notifies the parent |
e8087a87 BP |
541 | * process that the child process has completed startup successfully. It also |
542 | * call daemonize_post_detach(). | |
a7ff9bd7 BP |
543 | * |
544 | * Calling this function more than once has no additional effect. */ | |
95440284 BP |
545 | void |
546 | daemonize_complete(void) | |
547 | { | |
7ffd3f69 GS |
548 | if (pidfile) { |
549 | free(pidfile); | |
550 | pidfile = NULL; | |
551 | } | |
552 | ||
e8087a87 BP |
553 | if (!detached) { |
554 | detached = true; | |
555 | ||
556 | fork_notify_startup(daemonize_fd); | |
557 | daemonize_fd = -1; | |
558 | daemonize_post_detach(); | |
559 | } | |
560 | } | |
95440284 | 561 | |
e8087a87 BP |
562 | /* If daemonization is configured, then this function does traditional Unix |
563 | * daemonization behavior: join a new session, chdir to the root (if not | |
564 | * disabled), and close the standard file descriptors. | |
565 | * | |
566 | * It only makes sense to call this function as part of an implementation of a | |
567 | * special daemon subprocess. A normal daemon should just call | |
568 | * daemonize_complete(). */ | |
569 | void | |
570 | daemonize_post_detach(void) | |
571 | { | |
7943cd51 | 572 | if (detach) { |
95440284 BP |
573 | if (chdir_) { |
574 | ignore(chdir("/")); | |
575 | } | |
7943cd51 | 576 | close_standard_fds(); |
95440284 BP |
577 | } |
578 | } | |
579 | ||
064af421 BP |
580 | void |
581 | daemon_usage(void) | |
582 | { | |
583 | printf( | |
584 | "\nDaemon options:\n" | |
e7bd7d78 | 585 | " --detach run in background as daemon\n" |
91a1e24d | 586 | " --no-chdir do not chdir to '/'\n" |
e7bd7d78 JP |
587 | " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n" |
588 | " --overwrite-pidfile with --pidfile, start even if already " | |
589 | "running\n", | |
b43c6fe2 | 590 | ovs_rundir(), program_name); |
064af421 BP |
591 | } |
592 | ||
aacea8ba BP |
593 | static int |
594 | lock_pidfile__(FILE *file, int command, struct flock *lck) | |
595 | { | |
596 | int error; | |
597 | ||
598 | lck->l_type = F_WRLCK; | |
599 | lck->l_whence = SEEK_SET; | |
600 | lck->l_start = 0; | |
601 | lck->l_len = 0; | |
602 | lck->l_pid = 0; | |
603 | ||
604 | do { | |
605 | error = fcntl(fileno(file), command, lck) == -1 ? errno : 0; | |
606 | } while (error == EINTR); | |
607 | return error; | |
608 | } | |
609 | ||
610 | static int | |
611 | lock_pidfile(FILE *file, int command) | |
612 | { | |
613 | struct flock lck; | |
614 | ||
615 | return lock_pidfile__(file, command, &lck); | |
616 | } | |
617 | ||
18e124a2 | 618 | static pid_t |
aacea8ba | 619 | read_pidfile__(const char *pidfile, bool delete_if_stale) |
064af421 | 620 | { |
aacea8ba | 621 | struct stat s, s2; |
064af421 | 622 | struct flock lck; |
aacea8ba | 623 | char line[128]; |
064af421 BP |
624 | FILE *file; |
625 | int error; | |
626 | ||
e4bd5e2a BP |
627 | if ((pidfile_ino || pidfile_dev) |
628 | && !stat(pidfile, &s) | |
629 | && s.st_ino == pidfile_ino && s.st_dev == pidfile_dev) { | |
630 | /* It's our own pidfile. We can't afford to open it, because closing | |
631 | * *any* fd for a file that a process has locked also releases all the | |
632 | * locks on that file. | |
633 | * | |
634 | * Fortunately, we know the associated pid anyhow: */ | |
635 | return getpid(); | |
636 | } | |
637 | ||
aacea8ba | 638 | file = fopen(pidfile, "r+"); |
064af421 | 639 | if (!file) { |
aacea8ba | 640 | if (errno == ENOENT && delete_if_stale) { |
18e124a2 BP |
641 | return 0; |
642 | } | |
064af421 BP |
643 | error = errno; |
644 | VLOG_WARN("%s: open: %s", pidfile, strerror(error)); | |
645 | goto error; | |
646 | } | |
647 | ||
aacea8ba BP |
648 | error = lock_pidfile__(file, F_GETLK, &lck); |
649 | if (error) { | |
064af421 BP |
650 | VLOG_WARN("%s: fcntl: %s", pidfile, strerror(error)); |
651 | goto error; | |
652 | } | |
653 | if (lck.l_type == F_UNLCK) { | |
aacea8ba BP |
654 | /* pidfile exists but it isn't locked by anyone. We need to delete it |
655 | * so that a new pidfile can go in its place. But just calling | |
656 | * unlink(pidfile) makes a nasty race: what if someone else unlinks it | |
657 | * before we do and then replaces it by a valid pidfile? We'd unlink | |
658 | * their valid pidfile. We do a little dance to avoid the race, by | |
659 | * locking the invalid pidfile. Only one process can have the invalid | |
660 | * pidfile locked, and only that process has the right to unlink it. */ | |
661 | if (!delete_if_stale) { | |
662 | error = ESRCH; | |
0b376942 | 663 | VLOG_DBG("%s: pid file is stale", pidfile); |
aacea8ba BP |
664 | goto error; |
665 | } | |
666 | ||
667 | /* Get the lock. */ | |
668 | error = lock_pidfile(file, F_SETLK); | |
669 | if (error) { | |
670 | /* We lost a race with someone else doing the same thing. */ | |
671 | VLOG_WARN("%s: lost race to lock pidfile", pidfile); | |
672 | goto error; | |
673 | } | |
674 | ||
675 | /* Is the file we have locked still named 'pidfile'? */ | |
676 | if (stat(pidfile, &s) || fstat(fileno(file), &s2) | |
677 | || s.st_ino != s2.st_ino || s.st_dev != s2.st_dev) { | |
678 | /* No. We lost a race with someone else who got the lock before | |
679 | * us, deleted the pidfile, and closed it (releasing the lock). */ | |
680 | error = EALREADY; | |
681 | VLOG_WARN("%s: lost race to delete pidfile", pidfile); | |
682 | goto error; | |
683 | } | |
684 | ||
685 | /* We won the right to delete the stale pidfile. */ | |
686 | if (unlink(pidfile)) { | |
687 | error = errno; | |
688 | VLOG_WARN("%s: failed to delete stale pidfile (%s)", | |
689 | pidfile, strerror(error)); | |
690 | goto error; | |
691 | } | |
692 | VLOG_DBG("%s: deleted stale pidfile", pidfile); | |
693 | fclose(file); | |
694 | return 0; | |
064af421 BP |
695 | } |
696 | ||
697 | if (!fgets(line, sizeof line, file)) { | |
698 | if (ferror(file)) { | |
699 | error = errno; | |
700 | VLOG_WARN("%s: read: %s", pidfile, strerror(error)); | |
701 | } else { | |
702 | error = ESRCH; | |
703 | VLOG_WARN("%s: read: unexpected end of file", pidfile); | |
704 | } | |
705 | goto error; | |
706 | } | |
707 | ||
708 | if (lck.l_pid != strtoul(line, NULL, 10)) { | |
aacea8ba BP |
709 | /* The process that has the pidfile locked is not the process that |
710 | * created it. It must be stale, with the process that has it locked | |
711 | * preparing to delete it. */ | |
064af421 | 712 | error = ESRCH; |
aacea8ba BP |
713 | VLOG_WARN("%s: stale pidfile for pid %s being deleted by pid %ld", |
714 | pidfile, line, (long int) lck.l_pid); | |
064af421 BP |
715 | goto error; |
716 | } | |
717 | ||
718 | fclose(file); | |
719 | return lck.l_pid; | |
720 | ||
721 | error: | |
722 | if (file) { | |
723 | fclose(file); | |
724 | } | |
725 | return -error; | |
726 | } | |
18e124a2 BP |
727 | |
728 | /* Opens and reads a PID from 'pidfile'. Returns the positive PID if | |
729 | * successful, otherwise a negative errno value. */ | |
730 | pid_t | |
731 | read_pidfile(const char *pidfile) | |
732 | { | |
aacea8ba | 733 | return read_pidfile__(pidfile, false); |
18e124a2 BP |
734 | } |
735 | ||
aacea8ba BP |
736 | /* Checks whether a process with the given 'pidfile' is already running and, |
737 | * if so, aborts. If 'pidfile' is stale, deletes it. */ | |
738 | static void | |
739 | check_already_running(void) | |
18e124a2 | 740 | { |
aacea8ba BP |
741 | long int pid = read_pidfile__(pidfile, true); |
742 | if (pid > 0) { | |
743 | VLOG_FATAL("%s: already running as pid %ld, aborting", pidfile, pid); | |
744 | } else if (pid < 0) { | |
745 | VLOG_FATAL("%s: pidfile check failed (%s), aborting", | |
746 | pidfile, strerror(-pid)); | |
747 | } | |
18e124a2 | 748 | } |