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