From: Ben Pfaff Date: Mon, 21 May 2012 18:08:13 +0000 (-0700) Subject: daemon: Factor out code into new function fork_and_wait_for_startup(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8aee05ccf4818ea3a070163f7d3dc43126d8443e;p=openvswitch 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 --- 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 */