daemon: Move some common code to daemon.c
[sliver-openvswitch.git] / lib / daemon-unix.c
index 74914a4..676a7f3 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <config.h>
 #include "daemon.h"
+#include "daemon-private.h"
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
 VLOG_DEFINE_THIS_MODULE(daemon_unix);
 
 /* --detach: Should we run in the background? */
-static bool detach;             /* Was --detach specified? */
+bool detach;                    /* Was --detach specified? */
 static bool detached;           /* Have we already detached? */
 
 /* --pidfile: Name of pidfile (null if none). */
-static char *pidfile;
+char *pidfile;
 
 /* Device and inode of pidfile, so we can avoid reopening it. */
 static dev_t pidfile_dev;
@@ -63,19 +64,14 @@ static int daemonize_fd = -1;
  * it dies due to an error signal? */
 static bool monitor;
 
-/* For each of the standard file descriptors, whether to replace it by
- * /dev/null (if false) or keep it for the daemon to use (if true). */
-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. */
-static char *
+char *
 make_pidfile_name(const char *name)
 {
     return (!name
@@ -83,20 +79,6 @@ make_pidfile_name(const char *name)
             : abs_file_name(ovs_rundir(), name));
 }
 
-/* Sets up a following call to daemonize() to create a pidfile named 'name'.
- * If 'name' begins with '/', then it is treated as an absolute path.
- * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
- * default.
- *
- * If 'name' is null, then program_name followed by ".pid" is used. */
-void
-set_pidfile(const char *name)
-{
-    assert_single_threaded();
-    free(pidfile);
-    pidfile = make_pidfile_name(name);
-}
-
 /* Sets that we do not chdir to "/". */
 void
 set_no_chdir(void)
@@ -121,13 +103,6 @@ set_detach(void)
     detach = true;
 }
 
-/* Will daemonize() really detach? */
-bool
-get_detach(void)
-{
-    return detach;
-}
-
 /* Sets up a following call to daemonize() to fork a supervisory process to
  * monitor the daemon and restart it if it dies due to an error signal.  */
 void
@@ -136,22 +111,6 @@ daemon_set_monitor(void)
     monitor = true;
 }
 
-/* A daemon doesn't normally have any use for the file descriptors for stdin,
- * stdout, and stderr after it detaches.  To keep these file descriptors from
- * e.g. holding an SSH session open, by default detaching replaces each of
- * these file descriptors by /dev/null.  But a few daemons expect the user to
- * redirect stdout or stderr to a file, in which case it is desirable to keep
- * these file descriptors.  This function, therefore, disables replacing 'fd'
- * by /dev/null when the daemon detaches. */
-void
-daemon_save_fd(int fd)
-{
-    ovs_assert(fd == STDIN_FILENO ||
-               fd == STDOUT_FILENO ||
-               fd == STDERR_FILENO);
-    save_fds[fd] = true;
-}
-
 /* 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. */
@@ -232,15 +191,6 @@ make_pidfile(void)
     free(tmpfile);
 }
 
-/* If configured with set_pidfile() or set_detach(), creates the pid file and
- * detaches from the foreground session.  */
-void
-daemonize(void)
-{
-    daemonize_start();
-    daemonize_complete();
-}
-
 /* Calls fork() and on success returns its return value.  On failure, logs an
  * error and exits unsuccessfully.
  *
@@ -441,47 +391,6 @@ 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. */
-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);
-}
-
 /* If daemonization is configured, then starts daemonization, by forking and
  * returning in the child process.  The parent process hangs around until the
  * child lets it know either that it completed startup successfully (by calling