daemon: Move some common code to daemon.c
authorGurucharan Shetty <gshetty@nicira.com>
Wed, 23 Apr 2014 21:22:38 +0000 (14:22 -0700)
committerGurucharan Shetty <gshetty@nicira.com>
Thu, 24 Apr 2014 21:43:50 +0000 (14:43 -0700)
We have some common code between daemon-unix.c and
daemon-windows.c. Move them to daemon.c

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/automake.mk
lib/daemon-private.h [new file with mode: 0644]
lib/daemon-unix.c
lib/daemon-windows.c
lib/daemon.c
lib/daemon.h

index 519d243..de3f068 100644 (file)
@@ -47,6 +47,7 @@ lib_libopenvswitch_la_SOURCES = \
        lib/csum.h \
        lib/daemon.c \
        lib/daemon.h \
+       lib/daemon-private.h \
        lib/dhcp.h \
        lib/dummy.c \
        lib/dummy.h \
diff --git a/lib/daemon-private.h b/lib/daemon-private.h
new file mode 100644 (file)
index 0000000..8bc71e2
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2014 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef DAEMON_PRIVATE_H
+#define DAEMON_PRIVATE_H 1
+
+extern bool detach;
+extern char *pidfile;
+
+char *make_pidfile_name(const char *name);
+
+#endif /* daemon-private.h */
index 4dcb696..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;
@@ -65,13 +66,12 @@ static bool monitor;
 
 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
@@ -79,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)
@@ -117,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
@@ -212,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.
  *
index 4d33b8c..f94ad9b 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <config.h>
 #include "daemon.h"
+#include "daemon-private.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include "poll-loop.h"
@@ -30,12 +31,12 @@ static bool service_started;         /* Have we dispatched service to start? */
  * unexpectedly? */
 static bool monitor;
 
-static bool detach; /* Was --detach specified? */
-static bool detached; /* Running as the child process. */
-static HANDLE write_handle; /* End of pipe to write to parent. */
+bool detach;                 /* Was --detach specified? */
+static bool detached;        /* Running as the child process. */
+static HANDLE write_handle;  /* End of pipe to write to parent. */
 
-static char *pidfile;         /* --pidfile: Name of pidfile (null if none). */
-static FILE *filep_pidfile;   /* File pointer to access the pidfile. */
+char *pidfile;                 /* --pidfile: Name of pidfile (null if none). */
+static FILE *filep_pidfile;    /* File pointer to access the pidfile. */
 
 /* Handle to the Services Manager and the created service. */
 static SC_HANDLE manager, service;
@@ -395,20 +396,6 @@ detach_process(int argc, char *argv[])
     exit(0);
 }
 
-/* Will daemonize() really detach? */
-bool
-get_detach()
-{
-    return detach;
-}
-
-void
-daemonize(void)
-{
-    daemonize_start();
-    daemonize_complete();
-}
-
 static void
 unlink_pidfile(void)
 {
@@ -482,7 +469,7 @@ daemonize_complete(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)
 {
     if (name && strchr(name, ':')) {
@@ -491,17 +478,3 @@ make_pidfile_name(const char *name)
         return xasprintf("%s/%s.pid", ovs_rundir(), program_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);
-}
index 546612c..4c7c8e1 100644 (file)
@@ -15,6 +15,7 @@
  */
 #include <config.h>
 #include "daemon.h"
+#include "daemon-private.h"
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -26,6 +27,36 @@ VLOG_DEFINE_THIS_MODULE(daemon);
  * /dev/null (if false) or keep it for the daemon to use (if true). */
 static bool save_fds[3];
 
+/* Will daemonize() really detach? */
+bool
+get_detach(void)
+{
+    return detach;
+}
+
+/* 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();
+}
+
+/* Sets up a following call to daemonize() to create a pidfile named 'name'.
+ * If 'name' begins with '/' (or contains ':' in windows), 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);
+}
+
 /* 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
index 875569d..959341d 100644 (file)
@@ -27,7 +27,7 @@
  * POSIX platforms and some are applicable only on Windows. As such, the
  * function definitions unique to each platform are separated out with
  * ifdef macros. More descriptive comments on individual functions are provided
- * in daemon.c (for Linux) and daemon-windows.c (for Windows).
+ * in daemon-unix.c (for POSIX platforms) and daemon-windows.c (for Windows).
 
  * The DAEMON_OPTION_ENUMS, DAEMON_LONG_OPTIONS and DAEMON_OPTION_HANDLERS
  * macros are useful for parsing command-line options in individual utilities.