[PATCH] Pull DBus event code out into common area

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

The policy kit and HAL node device drivers both require a
DBus connection. The HAL device code further requires that
the DBus connection is integrated with the event loop and
provides such glue logic itself.

The forthcoming FirewallD integration also requires a
dbus connection with event loop integration. Thus we need
to pull the current event loop glue out of the HAL driver.

Thus we create src/util/virdbus.{c,h} files. This contains
just one method virDBusGetSystemBus() which obtains a handle
to the single shared system bus instance, with event glue
automagically setup.

NB, I have not actually tested this on a system with HAL or
PolicyKit-0 installed, so it may well not compile. Hopefully
someone on list has a suitable system where they can test
those two. If not, I'll install a VM next week to test it in.

---
 .gitignore             |    6 ++--
 configure.ac           |    3 +-
 daemon/Makefile.am     |    3 +-
 daemon/libvirtd.c      |    4 ---
 src/Makefile.am        |    3 +-
 src/rpc/virnetserver.c |    6 ----
 src/rpc/virnetserver.h |    4 ---
 src/util/virdbus.c     |   68 ++++++++++++++++++++++-------------------------
 8 files changed, 40 insertions(+), 57 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5aa9c9b..14a21d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,12 +48,12 @@
 /daemon/*_dispatch.h
 /daemon/libvirt_qemud
 /daemon/libvirtd
-/daemon/libvirtd.init
-/daemon/libvirtd.service
 /daemon/libvirtd*.logrotate
 /daemon/libvirtd.8
 /daemon/libvirtd.8.in
+/daemon/libvirtd.init
 /daemon/libvirtd.pod
+/daemon/libvirtd.service
 /docs/devhelp/libvirt.devhelp
 /docs/hvsupport.html.in
 /docs/libvirt-api.xml
@@ -118,6 +118,7 @@
 /tests/eventtest
 /tests/hashtest
 /tests/jsontest
+/tests/libvirtdconftest
 /tests/networkxml2argvtest
 /tests/nodeinfotest
 /tests/nwfilterxml2xmltest
@@ -150,7 +151,6 @@
 /tests/vmx2xmltest
 /tests/xencapstest
 /tests/xmconfigtest
-/tests/libvirtdconftest
 /tools/*.[18]
 /tools/libvirt-guests.init
 /tools/virsh
diff --git a/configure.ac b/configure.ac
index 3863119..f49b620 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1116,9 +1116,8 @@ if test "$with_dbus" = "yes" || test "$with_dbus" = "check" ; then
        AC_MSG_ERROR([You must install DBus >= $DBUS_REQUIRED to compile libvirt])
      fi])
 fi
-echo ">>>>>>>>>> $with_dbus <<<<<<<<<<<<<<<<<"
+
 if test "$with_dbus" = "yes" ; then
-echo ">>>>>>>>>> $with_dbus <<<<<<<<<<<<<<<<<"
   AC_DEFINE_UNQUOTED([HAVE_DBUS], 1, [enable communication with DBus])
 
   save_LIBS="$LIBS"
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 5d9f5d7..24cce8f 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -94,7 +94,7 @@ libvirtd_SOURCES = $(DAEMON_SOURCES)
 #-D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_POSIX_C_SOURCE=199506L
 libvirtd_CFLAGS = \
 	$(LIBXML_CFLAGS) $(GNUTLS_CFLAGS) $(SASL_CFLAGS) \
-	$(XDR_CFLAGS) $(POLKIT_CFLAGS) \
+	$(XDR_CFLAGS) $(POLKIT_CFLAGS) $(DBUS_CFLAGS) \
 	$(WARN_CFLAGS) \
 	$(COVERAGE_CFLAGS) \
 	-DQEMUD_PID_FILE="\"$(QEMUD_PID_FILE)\"" \
@@ -108,6 +108,7 @@ libvirtd_LDADD =					\
 	$(LIBXML_LIBS)					\
 	$(GNUTLS_LIBS)					\
 	$(SASL_LIBS)					\
+	$(DBUS_LIBS)					\
 	$(POLKIT_LIBS)
 
 if WITH_DTRACE_PROBES
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index ce931d4..b098f6a 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -812,7 +812,6 @@ int main(int argc, char **argv) {
     struct daemonConfig *config;
     bool privileged = geteuid() == 0 ? true : false;
     bool implicit_conf = false;
-    bool use_polkit_dbus;
     char *run_dir = NULL;
     mode_t old_umask;
 
@@ -1008,8 +1007,6 @@ int main(int argc, char **argv) {
         goto cleanup;
     }
 
-    use_polkit_dbus = config->auth_unix_rw == REMOTE_AUTH_POLKIT ||
-            config->auth_unix_ro == REMOTE_AUTH_POLKIT;
     if (!(srv = virNetServerNew(config->min_workers,
                                 config->max_workers,
                                 config->prio_workers,
@@ -1018,7 +1015,6 @@ int main(int argc, char **argv) {
                                 config->keepalive_count,
                                 !!config->keepalive_required,
                                 config->mdns_adv ? config->mdns_name : NULL,
-                                use_polkit_dbus,
                                 remoteClientInitHook))) {
         ret = VIR_DAEMON_ERR_INIT;
         goto cleanup;
diff --git a/src/Makefile.am b/src/Makefile.am
index c6b7033..b8a19b4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1523,7 +1523,7 @@ libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(AM_LDFLAGS)
 libvirt_lxc_LDADD = $(CAPNG_LIBS) $(YAJL_LIBS) \
 		$(LIBXML_LIBS) $(NUMACTL_LIBS) $(THREAD_LIBS) \
 		$(LIBNL_LIBS) $(AUDIT_LIBS) $(DEVMAPPER_LIBS) \
-		$(RT_LIBS) \
+		$(RT_LIBS) $(DBUS_LIBS) \
 		../gnulib/lib/libgnu.la
 if WITH_DTRACE_PROBES
 libvirt_lxc_LDADD += probes.o
@@ -1540,6 +1540,7 @@ libvirt_lxc_CFLAGS =				\
 		$(CAPNG_CFLAGS)			\
 		$(YAJL_CFLAGS)			\
 		$(AUDIT_CFLAGS)			\
+		$(DBUS_CFLAGS)			\
 		-I$(top_srcdir)/src/conf	\
 		$(AM_CFLAGS)
 if HAVE_LIBBLKID
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index 0e0edd3..3965fc2 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -304,7 +304,6 @@ virNetServerPtr virNetServerNew(size_t min_workers,
                                 unsigned int keepaliveCount,
                                 bool keepaliveRequired,
                                 const char *mdnsGroupName,
-                                bool connectDBus ATTRIBUTE_UNUSED,
                                 virNetServerClientInitHook clientInitHook)
 {
     virNetServerPtr srv;
@@ -794,11 +793,6 @@ void virNetServerFree(virNetServerPtr srv)
     virNetServerMDNSFree(srv->mdns);
 #endif
 
-#if HAVE_DBUS
-    if (srv->sysbus)
-        dbus_connection_unref(srv->sysbus);
-#endif
-
     virNetServerUnlock(srv);
     virMutexDestroy(&srv->lock);
     VIR_FREE(srv);
diff --git a/src/rpc/virnetserver.h b/src/rpc/virnetserver.h
index a9dec18..0ebe00e 100644
--- a/src/rpc/virnetserver.h
+++ b/src/rpc/virnetserver.h
@@ -25,9 +25,6 @@
 # define __VIR_NET_SERVER_H__
 
 # include <signal.h>
-# if HAVE_DBUS
-#  include <dbus/dbus.h>
-# endif
 
 # include "virnettlscontext.h"
 # include "virnetserverprogram.h"
@@ -45,7 +42,6 @@ virNetServerPtr virNetServerNew(size_t min_workers,
                                 unsigned int keepaliveCount,
                                 bool keepaliveRequired,
                                 const char *mdnsGroupName,
-                                bool connectDBus,
                                 virNetServerClientInitHook clientInitHook);
 
 typedef int (*virNetServerAutoShutdownFunc)(virNetServerPtr srv, void *opaque);
diff --git a/src/util/virdbus.c b/src/util/virdbus.c
index 636cd45..badfe8c 100644
--- a/src/util/virdbus.c
+++ b/src/util/virdbus.c
@@ -22,6 +22,10 @@
 #include <config.h>
 
 #include "virdbus.h"
+#include "memory.h"
+#include "virterror_internal.h"
+#include "logging.h"
+#include "threads.h"
 
 #define VIR_FROM_THIS VIR_FROM_DBUS
 
@@ -33,7 +37,7 @@
 
 static DBusConnection *systembus = NULL;
 static virOnceControl once = VIR_ONCE_CONTROL_INITIALIZER;
-static DBusError err;
+static DBusError dbuserr;
 
 static dbus_bool_t virDBusAddWatch(DBusWatch *watch, void *data);
 static void virDBusRemoveWatch(DBusWatch *watch, void *data);
@@ -41,24 +45,21 @@ static void virDBusToggleWatch(DBusWatch *watch, void *data);
 
 static void virDBusSystemBusInit(void)
 {
-    DBusConnection *dbus_conn = NULL;
-    DBusError err;
-
     /* Allocate and initialize a new HAL context */
     dbus_connection_set_change_sigpipe(FALSE);
     dbus_threads_init_default();
 
-    dbus_error_init(&err);
-    if (!(systembus = dbus_bus_get(DBUS_BUS_SYSTEM, &err)))
+    dbus_error_init(&dbuserr);
+    if (!(systembus = dbus_bus_get(DBUS_BUS_SYSTEM, &dbuserr)))
         return;
 
     dbus_connection_set_exit_on_disconnect(systembus, FALSE);
 
     /* Register dbus watch callbacks */
     if (!dbus_connection_set_watch_functions(systembus,
-                                             add_dbus_watch,
-                                             remove_dbus_watch,
-                                             toggle_dbus_watch,
+                                             virDBusAddWatch,
+                                             virDBusRemoveWatch,
+                                             virDBusToggleWatch,
                                              NULL, NULL)) {
         systembus = NULL;
         return;
@@ -77,7 +78,7 @@ DBusConnection *virDBusGetSystemBus(void)
     if (!systembus) {
         virDBusReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Unable to get DBus system bus connection: %s"),
-                           err->message ? err->message : "watch setup failed");
+                           dbuserr.message ? dbuserr.message : "watch setup failed");
         return NULL;
     }
 
@@ -85,13 +86,11 @@ DBusConnection *virDBusGetSystemBus(void)
 }
 
 
-static void dbus_watch_callback(int fdatch ATTRIBUTE_UNUSED,
-                                int fd ATTRIBUTE_UNUSED,
-                                int events, void *opaque)
+static void virDBusWatchCallback(int fdatch ATTRIBUTE_UNUSED,
+                                 int fd ATTRIBUTE_UNUSED,
+                                 int events, void *opaque)
 {
     DBusWatch *watch = opaque;
-    LibHalContext *hal_ctx;
-    DBusConnection *dbus_conn;
     int dbus_flags = 0;
 
     if (events & VIR_EVENT_HANDLE_READABLE)
@@ -105,16 +104,12 @@ static void dbus_watch_callback(int fdatch ATTRIBUTE_UNUSED,
 
     (void)dbus_watch_handle(watch, dbus_flags);
 
-    nodeDeviceLock(driverState);
-    hal_ctx = DRV_STATE_HAL_CTX(driverState);
-    dbus_conn = libhal_ctx_get_dbus_connection(hal_ctx);
-    nodeDeviceUnlock(driverState);
-    while (dbus_connection_dispatch(dbus_conn) == DBUS_DISPATCH_DATA_REMAINS)
+    while (dbus_connection_dispatch(systembus) == DBUS_DISPATCH_DATA_REMAINS)
         /* keep dispatching while data remains */;
 }
 
 
-static int xlate_dbus_watch_flags(int dbus_flags)
+static int virDBusTranslateWatchFlags(int dbus_flags)
 {
     unsigned int flags = 0;
     if (dbus_flags & DBUS_WATCH_READABLE)
@@ -129,50 +124,51 @@ static int xlate_dbus_watch_flags(int dbus_flags)
 }
 
 
-struct nodeDeviceWatchInfo
+struct virDBusWatch
 {
     int watch;
 };
 
-static void nodeDeviceWatchFree(void *data) {
-    struct nodeDeviceWatchInfo *info = data;
+static void virDBusWatchFree(void *data) {
+    struct virDBusWatch *info = data;
     VIR_FREE(info);
 }
 
-static dbus_bool_t add_dbus_watch(DBusWatch *watch,
+static dbus_bool_t virDBusAddWatch(DBusWatch *watch,
                                   void *data ATTRIBUTE_UNUSED)
 {
     int flags = 0;
     int fd;
-    struct nodeDeviceWatchInfo *info;
+    struct virDBusWatch *info;
 
     if (VIR_ALLOC(info) < 0)
         return 0;
 
     if (dbus_watch_get_enabled(watch))
-        flags = xlate_dbus_watch_flags(dbus_watch_get_flags(watch));
+        flags = virDBusTranslateWatchFlags(dbus_watch_get_flags(watch));
 
 # if HAVE_DBUS_WATCH_GET_UNIX_FD
     fd = dbus_watch_get_unix_fd(watch);
 # else
     fd = dbus_watch_get_fd(watch);
 # endif
-    info->watch = virEventAddHandle(fd, flags, dbus_watch_callback,
+    info->watch = virEventAddHandle(fd, flags,
+                                    virDBusWatchCallback,
                                     watch, NULL);
     if (info->watch < 0) {
         VIR_FREE(info);
         return 0;
     }
-    dbus_watch_set_data(watch, info, nodeDeviceWatchFree);
+    dbus_watch_set_data(watch, info, virDBusWatchFree);
 
     return 1;
 }
 
 
-static void remove_dbus_watch(DBusWatch *watch,
-                              void *data ATTRIBUTE_UNUSED)
+static void virDBusRemoveWatch(DBusWatch *watch,
+                               void *data ATTRIBUTE_UNUSED)
 {
-    struct nodeDeviceWatchInfo *info;
+    struct virDBusWatch *info;
 
     info = dbus_watch_get_data(watch);
 
@@ -180,14 +176,14 @@ static void remove_dbus_watch(DBusWatch *watch,
 }
 
 
-static void toggle_dbus_watch(DBusWatch *watch,
-                              void *data ATTRIBUTE_UNUSED)
+static void virDBusToggleWatch(DBusWatch *watch,
+                               void *data ATTRIBUTE_UNUSED)
 {
     int flags = 0;
-    struct nodeDeviceWatchInfo *info;
+    struct virDBusWatch *info;
 
     if (dbus_watch_get_enabled(watch))
-        flags = xlate_dbus_watch_flags(dbus_watch_get_flags(watch));
+        flags = virDBusTranslateWatchFlags(dbus_watch_get_flags(watch));
 
     info = dbus_watch_get_data(watch);
 
-- 
1.7.7.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]