X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fdaemon.c;h=a2eff07c038c0c3c53ccbbce57de9d2086ae2a85;hb=8aee05ccf4818ea3a070163f7d3dc43126d8443e;hp=c4a82647f8ace909101e36d89409a024cce9b70d;hpb=e0edde6fee279cdbbf3c179f5f50adaf0c7c7f1e;p=openvswitch diff --git a/lib/daemon.c b/lib/daemon.c index c4a82647..a2eff07c 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -245,6 +245,43 @@ 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 + * startup sequence. Then stores -1 in '*fdp' and returns the child's pid. + * + * - In the child, stores a fd in '*fdp' and returns 0. The caller should + * pass the fd to fork_notify_startup() after it finishes its startup + * sequence. + * + * If something goes wrong with the fork, logs a critical error and aborts the + * process. */ static pid_t fork_and_wait_for_startup(int *fdp) { @@ -253,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; @@ -290,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; @@ -340,13 +372,11 @@ static void monitor_daemon(pid_t daemon_pid) { /* XXX Should log daemon's stderr output at startup time. */ - const char *saved_program_name; time_t last_restart; char *status_msg; int crashes; - saved_program_name = program_name; - program_name = xasprintf("monitor(%s)", program_name); + subprogram_name = "monitor"; status_msg = xstrdup("healthy"); last_restart = TIME_MIN; crashes = 0; @@ -355,7 +385,7 @@ monitor_daemon(pid_t daemon_pid) int status; proctitle_set("%s: monitoring pid %lu (%s)", - saved_program_name, (unsigned long int) daemon_pid, + program_name, (unsigned long int) daemon_pid, status_msg); do { @@ -417,8 +447,7 @@ monitor_daemon(pid_t daemon_pid) /* Running in new daemon process. */ proctitle_restore(); - free((char *) program_name); - program_name = saved_program_name; + subprogram_name = ""; } /* Close standard file descriptors (except any that the client has requested we