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