[ovs-dev] [PATCH 2/3] daemon: Provide a callback option for a monitor process.

Gurucharan Shetty shettyg at nicira.com
Mon Jun 10 08:10:29 UTC 2013


The callback option makes the monitor library more customizable.
Each client of the library can now provide callbacks to run any
tasks in the monitor. It is the responsibility of the client to
return from the function in case the monitored process dies.

An upcoming commit becomes the first client.

Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/daemon.c               |   19 +++++++++++++++----
 lib/daemon.h               |    4 +++-
 ovsdb/ovsdb-server.c       |    2 +-
 tests/test-netflow.c       |    2 +-
 tests/test-sflow.c         |    2 +-
 utilities/ovs-controller.c |    2 +-
 utilities/ovs-ofctl.c      |    2 +-
 vswitchd/ovs-vswitchd.c    |    2 +-
 8 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/lib/daemon.c b/lib/daemon.c
index e12bc14..d563152 100644
--- a/lib/daemon.c
+++ b/lib/daemon.c
@@ -147,6 +147,12 @@ daemon_set_monitor(void)
     monitor = true;
 }
 
+bool
+daemon_get_monitor(void)
+{
+    return monitor;
+}
+
 /* 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
@@ -267,7 +273,7 @@ make_pidfile(void)
 void
 daemonize(void)
 {
-    daemonize_start();
+    daemonize_start(NULL);
     daemonize_complete();
 }
 
@@ -395,7 +401,7 @@ should_restart(int status)
 }
 
 static void
-monitor_daemon(pid_t daemon_pid)
+monitor_daemon(pid_t daemon_pid, monitor_callback *cb)
 {
     /* XXX Should log daemon's stderr output at startup time. */
     time_t last_restart;
@@ -414,6 +420,11 @@ monitor_daemon(pid_t daemon_pid)
                       (unsigned long int) daemon_pid, status_msg);
 
         do {
+            /* It is the responsibility of the callback function to return
+             * if the monitored daemon dies. */
+            if (cb) {
+                cb();
+            }
             retval = waitpid(daemon_pid, &status, 0);
         } while (retval == -1 && errno == EINTR);
 
@@ -502,7 +513,7 @@ close_standard_fds(void)
  * daemon_complete()) or that it failed to start up (by exiting with a nonzero
  * exit code). */
 void
-daemonize_start(void)
+daemonize_start(monitor_callback *cb)
 {
     daemonize_fd = -1;
 
@@ -525,7 +536,7 @@ daemonize_start(void)
             /* Running in monitor process. */
             fork_notify_startup(saved_daemonize_fd);
             close_standard_fds();
-            monitor_daemon(daemon_pid);
+            monitor_daemon(daemon_pid, cb);
         }
         /* Running in daemon process. */
     }
diff --git a/lib/daemon.h b/lib/daemon.h
index 14436f3..1068b48 100644
--- a/lib/daemon.h
+++ b/lib/daemon.h
@@ -56,6 +56,7 @@
             daemon_set_monitor();               \
             break;
 
+typedef void monitor_callback(void);
 char *make_pidfile_name(const char *name);
 void set_pidfile(const char *name);
 const char *get_pidfile(void);
@@ -64,11 +65,12 @@ bool is_chdir_enabled(void);
 void set_detach(void);
 bool get_detach(void);
 void daemon_set_monitor(void);
+bool daemon_get_monitor(void);
 void daemon_save_fd(int fd);
 void remove_pidfile_from_unlink(void);
 void add_pidfile_to_unlink(void);
 void daemonize(void);
-void daemonize_start(void);
+void daemonize_start(monitor_callback *cb);
 void daemonize_complete(void);
 void ignore_existing_pidfile(void);
 void daemon_usage(void);
diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c
index 1ba7c3c..6311c6b 100644
--- a/ovsdb/ovsdb-server.c
+++ b/ovsdb/ovsdb-server.c
@@ -125,7 +125,7 @@ main(int argc, char *argv[])
 
     parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
 
-    daemonize_start();
+    daemonize_start(NULL);
 
     n_dbs = MAX(1, argc);
     dbs = xcalloc(n_dbs + 1, sizeof *dbs);
diff --git a/tests/test-netflow.c b/tests/test-netflow.c
index 921f0fd..b51cbc1 100644
--- a/tests/test-netflow.c
+++ b/tests/test-netflow.c
@@ -188,7 +188,7 @@ main(int argc, char *argv[])
     }
 
     daemon_save_fd(STDOUT_FILENO);
-    daemonize_start();
+    daemonize_start(NULL);
 
     error = unixctl_server_create(NULL, &server);
     if (error) {
diff --git a/tests/test-sflow.c b/tests/test-sflow.c
index edb996c..9b96482 100644
--- a/tests/test-sflow.c
+++ b/tests/test-sflow.c
@@ -514,7 +514,7 @@ main(int argc, char *argv[])
     }
 
     daemon_save_fd(STDOUT_FILENO);
-    daemonize_start();
+    daemonize_start(NULL);
 
     error = unixctl_server_create(NULL, &server);
     if (error) {
diff --git a/utilities/ovs-controller.c b/utilities/ovs-controller.c
index 70bc2e6..817223e 100644
--- a/utilities/ovs-controller.c
+++ b/utilities/ovs-controller.c
@@ -143,7 +143,7 @@ main(int argc, char *argv[])
         ovs_fatal(0, "no active or passive switch connections");
     }
 
-    daemonize_start();
+    daemonize_start(NULL);
 
     retval = unixctl_server_create(unixctl_path, &unixctl);
     if (retval) {
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index 48f0fbf..90c4d84 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -1326,7 +1326,7 @@ monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
     int error;
 
     daemon_save_fd(STDERR_FILENO);
-    daemonize_start();
+    daemonize_start(NULL);
     error = unixctl_server_create(NULL, &server);
     if (error) {
         ovs_fatal(error, "failed to create unixctl server");
diff --git a/vswitchd/ovs-vswitchd.c b/vswitchd/ovs-vswitchd.c
index e11febd..c978559 100644
--- a/vswitchd/ovs-vswitchd.c
+++ b/vswitchd/ovs-vswitchd.c
@@ -83,7 +83,7 @@ main(int argc, char *argv[])
     process_init();
     ovsrec_init();
 
-    daemonize_start();
+    daemonize_start(NULL);
 
     if (want_mlockall) {
 #ifdef HAVE_MLOCKALL
-- 
1.7.9.5




More information about the dev mailing list