+/* 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 = xfork();
+ if (pid > 0) {
+ /* Running in parent process. */
+ fatal_signal_fork();
+ } else if (!pid) {
+ /* Running in child process. */
+ time_postfork();
+ lockfile_postfork();
+ }
+ 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)
+{
+ int fds[2];
+ pid_t pid;
+
+ xpipe(fds);
+
+ pid = fork_and_clean_up();
+ if (pid > 0) {
+ /* Running in parent process. */
+ size_t bytes_read;
+ char c;
+
+ close(fds[1]);
+ if (read_fully(fds[0], &c, 1, &bytes_read) != 0) {
+ int retval;
+ int status;
+
+ do {
+ retval = waitpid(pid, &status, 0);
+ } while (retval == -1 && errno == EINTR);
+
+ if (retval == pid) {
+ if (WIFEXITED(status) && WEXITSTATUS(status)) {
+ /* Child exited with an error. Convey the same error
+ * to our parent process as a courtesy. */
+ exit(WEXITSTATUS(status));
+ } else {
+ char *status_msg = process_status_msg(status);
+ VLOG_FATAL("fork child died before signaling startup (%s)",
+ status_msg);
+ }
+ } else if (retval < 0) {
+ VLOG_FATAL("waitpid failed (%s)", ovs_strerror(errno));
+ } else {
+ NOT_REACHED();
+ }
+ }
+ close(fds[0]);
+ *fdp = -1;
+ } else if (!pid) {
+ /* Running in child process. */
+ close(fds[0]);
+ *fdp = fds[1];
+ }
+
+ return pid;
+}
+
+static void
+fork_notify_startup(int fd)
+{
+ if (fd != -1) {
+ size_t bytes_written;
+ int error;
+
+ error = write_fully(fd, "", 1, &bytes_written);
+ if (error) {
+ VLOG_FATAL("pipe write failed (%s)", ovs_strerror(error));
+ }
+
+ close(fd);
+ }
+}
+
+static bool
+should_restart(int status)
+{
+ if (WIFSIGNALED(status)) {
+ static const int error_signals[] = {
+ SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGILL, SIGPIPE, SIGSEGV,
+ SIGXCPU, SIGXFSZ
+ };
+
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(error_signals); i++) {
+ if (error_signals[i] == WTERMSIG(status)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+static void
+monitor_daemon(pid_t daemon_pid)
+{
+ /* XXX Should log daemon's stderr output at startup time. */
+ time_t last_restart;
+ char *status_msg;
+ int crashes;
+
+ subprogram_name = "monitor";
+ status_msg = xstrdup("healthy");
+ last_restart = TIME_MIN;
+ crashes = 0;
+ for (;;) {
+ int retval;
+ int status;
+
+ proctitle_set("monitoring pid %lu (%s)",
+ (unsigned long int) daemon_pid, status_msg);
+
+ do {
+ retval = waitpid(daemon_pid, &status, 0);
+ } while (retval == -1 && errno == EINTR);
+
+ if (retval == -1) {
+ VLOG_FATAL("waitpid failed (%s)", ovs_strerror(errno));
+ } else if (retval == daemon_pid) {
+ char *s = process_status_msg(status);
+ if (should_restart(status)) {
+ free(status_msg);
+ status_msg = xasprintf("%d crashes: pid %lu died, %s",
+ ++crashes,
+ (unsigned long int) daemon_pid, s);
+ free(s);
+
+ if (WCOREDUMP(status)) {
+ /* Disable further core dumps to save disk space. */
+ struct rlimit r;
+
+ r.rlim_cur = 0;
+ r.rlim_max = 0;
+ if (setrlimit(RLIMIT_CORE, &r) == -1) {
+ VLOG_WARN("failed to disable core dumps: %s",
+ ovs_strerror(errno));
+ }
+ }
+
+ /* Throttle restarts to no more than once every 10 seconds. */
+ if (time(NULL) < last_restart + 10) {
+ VLOG_WARN("%s, waiting until 10 seconds since last "
+ "restart", status_msg);
+ for (;;) {
+ time_t now = time(NULL);
+ time_t wakeup = last_restart + 10;
+ if (now >= wakeup) {
+ break;
+ }
+ sleep(wakeup - now);
+ }
+ }
+ last_restart = time(NULL);
+
+ VLOG_ERR("%s, restarting", status_msg);
+ daemon_pid = fork_and_wait_for_startup(&daemonize_fd);
+ if (!daemon_pid) {
+ break;
+ }
+ } else {
+ VLOG_INFO("pid %lu died, %s, exiting",
+ (unsigned long int) daemon_pid, s);
+ free(s);
+ exit(0);
+ }
+ }
+ }
+ free(status_msg);
+
+ /* Running in new daemon process. */
+ proctitle_restore();
+ subprogram_name = "";
+}
+
+/* Close standard file descriptors (except any that the client has requested we
+ * leave open by calling daemon_save_fd()). If we're started from e.g. an SSH
+ * session, then this keeps us from holding that session open artificially. */
+static void
+close_standard_fds(void)
+{
+ int null_fd = get_null_fd();
+ if (null_fd >= 0) {
+ int fd;
+
+ for (fd = 0; fd < 3; fd++) {
+ if (!save_fds[fd]) {
+ dup2(null_fd, fd);
+ }
+ }
+ }
+
+ /* Disable logging to stderr to avoid wasting CPU time. */
+ vlog_set_levels(NULL, VLF_CONSOLE, VLL_OFF);
+}
+