TODO: Add the project list from the hackathon.
[sliver-openvswitch.git] / lib / daemon.c
index 1c9ebe2..c1c6550 100644 (file)
@@ -69,10 +69,13 @@ static bool save_fds[3];
 
 static void check_already_running(void);
 static int lock_pidfile(FILE *, int command);
+static char *make_pidfile_name(const char *name);
+static pid_t fork_and_clean_up(void);
+static void daemonize_post_detach(void);
 
 /* Returns the file name that would be used for a pidfile if 'name' were
  * provided to set_pidfile().  The caller must free the returned string. */
-char *
+static char *
 make_pidfile_name(const char *name)
 {
     return (!name
@@ -94,15 +97,6 @@ set_pidfile(const char *name)
     pidfile = make_pidfile_name(name);
 }
 
-/* Returns an absolute path to the configured pidfile, or a null pointer if no
- * pidfile is configured.  The caller must not modify or free the returned
- * string. */
-const char *
-get_pidfile(void)
-{
-    return pidfile;
-}
-
 /* Sets that we do not chdir to "/". */
 void
 set_no_chdir(void)
@@ -110,13 +104,6 @@ set_no_chdir(void)
     chdir_ = false;
 }
 
-/* Will we chdir to "/" as part of daemonizing? */
-bool
-is_chdir_enabled(void)
-{
-    return chdir_;
-}
-
 /* Normally, daemonize() or damonize_start() will terminate the program with a
  * message if a locked pidfile already exists.  If this function is called, an
  * existing pidfile will be replaced, with a warning. */
@@ -165,26 +152,6 @@ daemon_save_fd(int fd)
     save_fds[fd] = true;
 }
 
-/* Unregisters pidfile from being unlinked when the program terminates via
-* exit() or a fatal signal. */
-void
-remove_pidfile_from_unlink(void)
-{
-    if (pidfile) {
-        fatal_signal_remove_file_to_unlink(pidfile);
-    }
-}
-
-/* Registers pidfile to be unlinked when the program terminates via exit() or a
- * fatal signal. */
-void
-add_pidfile_to_unlink(void)
-{
-    if (pidfile) {
-        fatal_signal_add_file_to_unlink(pidfile);
-    }
-}
-
 /* If a pidfile has been configured, creates it and stores the running
  * process's pid in it.  Ensures that the pidfile will be deleted when the
  * process exits. */
@@ -280,7 +247,7 @@ daemonize(void)
  * 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
+static pid_t
 fork_and_clean_up(void)
 {
     pid_t pid = xfork();
@@ -341,7 +308,7 @@ fork_and_wait_for_startup(int *fdp)
             } else if (retval < 0) {
                 VLOG_FATAL("waitpid failed (%s)", ovs_strerror(errno));
             } else {
-                NOT_REACHED();
+                OVS_NOT_REACHED();
             }
         }
         close(fds[0]);
@@ -376,6 +343,8 @@ should_restart(int status)
 {
     if (WIFSIGNALED(status)) {
         static const int error_signals[] = {
+            /* This list of signals is documented in daemon.man.  If you
+             * change the list, update the documentation too. */
             SIGABRT, SIGALRM, SIGBUS, SIGFPE, SIGILL, SIGPIPE, SIGSEGV,
             SIGXCPU, SIGXFSZ
         };
@@ -447,7 +416,7 @@ monitor_daemon(pid_t daemon_pid)
                         if (now >= wakeup) {
                             break;
                         }
-                        sleep(wakeup - now);
+                        xsleep(wakeup - now);
                     }
                 }
                 last_restart = time(NULL);
@@ -472,6 +441,26 @@ monitor_daemon(pid_t daemon_pid)
     set_subprogram_name("");
 }
 
+/* Returns a readable and writable fd for /dev/null, if successful, otherwise
+ * a negative errno value.  The caller must not close the returned fd (because
+ * the same fd will be handed out to subsequent callers). */
+static int
+get_null_fd(void)
+{
+    static int null_fd;
+
+    if (!null_fd) {
+        null_fd = open("/dev/null", O_RDWR);
+        if (null_fd < 0) {
+            int error = errno;
+            VLOG_ERR("could not open /dev/null: %s", ovs_strerror(error));
+            null_fd = -error;
+        }
+    }
+
+    return null_fd;
+}
+
 /* 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. */
@@ -568,7 +557,7 @@ daemonize_complete(void)
  * It only makes sense to call this function as part of an implementation of a
  * special daemon subprocess.  A normal daemon should just call
  * daemonize_complete(). */
-void
+static void
 daemonize_post_detach(void)
 {
     if (detach) {
@@ -748,3 +737,22 @@ check_already_running(void)
                    pidfile, ovs_strerror(-pid));
     }
 }
+
+\f
+/* stub functions for non-windows platform. */
+
+void
+service_start(int *argc OVS_UNUSED, char **argv[] OVS_UNUSED)
+{
+}
+
+void
+service_stop(void)
+{
+}
+
+bool
+should_service_stop(void)
+{
+    return false;
+}