From 8aee05ccf4818ea3a070163f7d3dc43126d8443e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 21 May 2012 11:08:13 -0700 Subject: [PATCH] daemon: Factor out code into new function fork_and_wait_for_startup(). This function will be useful in an upcoming commit. Signed-off-by: Ben Pfaff --- lib/daemon.c | 33 +++++++++++++++++++++++++++------ lib/daemon.h | 2 ++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/daemon.c b/lib/daemon.c index ecca6061..a2eff07c 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -245,6 +245,32 @@ daemonize(void) daemonize_complete(); } +/* Calls fork() and on success returns its return value. On failure, logs an + * error and exits unsuccessfully. + * + * Post-fork, but before returning, this function calls a few other functions + * that are generally useful if the child isn't planning to exec a new + * process. */ +pid_t +fork_and_clean_up(void) +{ + pid_t pid; + + pid = fork(); + if (pid > 0) { + /* Running in parent process. */ + fatal_signal_fork(); + } else if (!pid) { + /* Running in child process. */ + time_postfork(); + lockfile_postfork(); + } else { + VLOG_FATAL("fork failed (%s)", strerror(errno)); + } + + return pid; +} + /* Forks, then: * * - In the parent, waits for the child to signal that it has completed its @@ -264,14 +290,13 @@ fork_and_wait_for_startup(int *fdp) xpipe(fds); - pid = fork(); + pid = fork_and_clean_up(); if (pid > 0) { /* Running in parent process. */ size_t bytes_read; char c; close(fds[1]); - fatal_signal_fork(); if (read_fully(fds[0], &c, 1, &bytes_read) != 0) { int retval; int status; @@ -301,11 +326,7 @@ fork_and_wait_for_startup(int *fdp) } else if (!pid) { /* Running in child process. */ close(fds[0]); - time_postfork(); - lockfile_postfork(); *fdp = fds[1]; - } else { - VLOG_FATAL("fork failed (%s)", strerror(errno)); } return pid; diff --git a/lib/daemon.h b/lib/daemon.h index 8d525943..63b64b99 100644 --- a/lib/daemon.h +++ b/lib/daemon.h @@ -73,4 +73,6 @@ void daemon_usage(void); pid_t read_pidfile(const char *name); pid_t read_pidfile_if_exists(const char *name); +pid_t fork_and_clean_up(void); + #endif /* daemon.h */ -- 2.30.2