dbus/bus driver.c,1.47,1.48 main.c,1.22,1.23

Kristian Hogsberg krh at pdx.freedesktop.org
Thu May 20 11:45:18 PDT 2004


Update of /cvs/dbus/dbus/bus
In directory pdx:/tmp/cvs-serv18328/bus

Modified Files:
	driver.c main.c 
Log Message:
	Patch from Jon Trowbridge <trow at ximian.com>:
 
 	* bus/main.c (setup_reload_pipe): Added.  Creates a pipe and sets
 	up a watch that triggers a config reload when one end of the pipe
 	becomes readable.
 	(signal_handler): Instead of doing the config reload in our SIGHUP
 	handler, just write to the reload pipe and let the associated
 	watch handle the reload when control returns to the main loop.
 
 	* bus/driver.c (bus_driver_handle_reload_config): Added.
 	Implements a ReloadConfig method for requesting a configuration
 	file reload via the bus driver.


Index: driver.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/driver.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -d -r1.47 -r1.48
--- a/driver.c	15 Apr 2004 22:08:04 -0000	1.47
+++ b/driver.c	20 May 2004 18:45:16 -0000	1.48
@@ -829,6 +829,32 @@
   return FALSE;
 }
 
+static dbus_bool_t
+bus_driver_handle_reload_config (DBusConnection *connection,
+				 BusTransaction *transaction,
+				 DBusMessage    *message,
+				 DBusError      *error)
+{
+  BusContext *context;
+  dbus_bool_t retval;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  retval = FALSE;
+
+  context = bus_connection_get_context (connection);
+  if (!bus_context_reload_config (context, error))
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      goto out;
+    }
+
+  retval = TRUE;
+  
+ out:
+  return retval;
+}
+
 /* For speed it might be useful to sort this in order of
  * frequency of use (but doesn't matter with only a few items
  * anyhow)
@@ -848,7 +874,8 @@
   { "ListServices", bus_driver_handle_list_services },
   { "AddMatch", bus_driver_handle_add_match },
   { "RemoveMatch", bus_driver_handle_remove_match },
-  { "GetServiceOwner", bus_driver_handle_get_service_owner }
+  { "GetServiceOwner", bus_driver_handle_get_service_owner },
+  { "ReloadConfig", bus_driver_handle_reload_config }
 };
 
 dbus_bool_t

Index: main.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/main.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- a/main.c	16 Apr 2004 15:01:25 -0000	1.22
+++ b/main.c	20 May 2004 18:45:16 -0000	1.23
@@ -22,6 +22,7 @@
  */
 #include "bus.h"
 #include <dbus/dbus-internals.h>
+#include <dbus/dbus-watch.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -30,23 +31,23 @@
 
 static BusContext *context;
 
+static int reload_pipe[2];
+#define RELOAD_READ_END 0
+#define RELOAD_WRITE_END 1
+
+
 static void
 signal_handler (int sig)
 {
-  DBusError error;
+  DBusString str;
 
   switch (sig)
     {
     case SIGHUP:
-      /* FIXME: We shouldn't be reloading the config in the
-	 signal handler.  We should use a pipe or something to
-	 make the reload happen in the main loop. */
-      dbus_error_init (&error);
-      if (!bus_context_reload_config (context, &error))
+      _dbus_string_init_const (&str, "foo");
+      if (!_dbus_write (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
 	{
-	  _dbus_warn ("Unable to reload configuration: %s\n",
-		      error.message);
-	  dbus_error_free (&error);
+	  _dbus_warn ("Unable to write to reload pipe.\n");
 	  exit (1);
 	}
       break;
@@ -111,6 +112,80 @@
     }
 }
 
+static dbus_bool_t
+handle_reload_watch (DBusWatch    *watch,
+		     unsigned int  flags,
+		     void         *data)
+{
+  DBusError error;
+  DBusString str;
+  _dbus_string_init (&str);
+  if (_dbus_read (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
+    {
+      _dbus_warn ("Couldn't read from reload pipe.\n");
+      exit (1);
+    }
+  _dbus_string_free (&str);
+
+  dbus_error_init (&error);
+  if (! bus_context_reload_config (context, &error))
+    {
+      _dbus_warn ("Unable to reload configuration: %s\n",
+		  error.message);
+      dbus_error_free (&error);
+      exit (1);
+    }
+  return TRUE;
+}
+
+static dbus_bool_t
+reload_watch_callback (DBusWatch    *watch,
+		       unsigned int  condition,
+		       void         *data)
+{
+  return dbus_watch_handle (watch, condition);
+}
+
+static void
+setup_reload_pipe (DBusLoop *loop)
+{
+  DBusError error;
+  DBusWatch *watch;
+
+  dbus_error_init (&error);
+
+  if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
+			       TRUE, &error))
+    {
+      _dbus_warn ("Unable to create reload pipe: %s\n",
+		  error.message);
+      dbus_error_free (&error);
+      exit (1);
+    }
+
+  watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
+			   DBUS_WATCH_READABLE, TRUE,
+			   handle_reload_watch, NULL, NULL);
+
+  if (watch == NULL)
+    {
+      _dbus_warn ("Unable to create reload watch: %s\n",
+		  error.message);
+      dbus_error_free (&error);
+      exit (1);
+    }
+
+  if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
+			     NULL, NULL))
+    {
+      _dbus_warn ("Unable to add reload watch to main loop: %s\n",
+		  error.message);
+      dbus_error_free (&error);
+      exit (1);
+    }
+
+}
+
 int
 main (int argc, char **argv)
 {
@@ -309,7 +384,9 @@
       dbus_error_free (&error);
       exit (1);
     }
-  
+
+  setup_reload_pipe (bus_context_get_loop (context));
+ 
   _dbus_set_signal_handler (SIGHUP, signal_handler);
   _dbus_set_signal_handler (SIGTERM, signal_handler);
   




More information about the dbus-commit mailing list