virdbus: Add virDBusGetSessionBus helper

This splits out some common code from virDBusGetSystemBus and
uses it to implement a new virDBusGetSessionBus helper.
This commit is contained in:
Alexander Larsson 2012-10-31 19:03:49 +00:00 committed by Daniel P. Berrange
parent 7492276317
commit d74b03e51c
3 changed files with 66 additions and 20 deletions

View File

@ -1335,6 +1335,7 @@ virConsoleOpen;
# virdbus.h # virdbus.h
virDBusGetSessionBus;
virDBusGetSystemBus; virDBusGetSystemBus;

View File

@ -32,40 +32,49 @@
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
static DBusConnection *systembus = NULL; static DBusConnection *systembus = NULL;
static virOnceControl once = VIR_ONCE_CONTROL_INITIALIZER; static DBusConnection *sessionbus = NULL;
static DBusError dbuserr; static virOnceControl systemonce = VIR_ONCE_CONTROL_INITIALIZER;
static virOnceControl sessiononce = VIR_ONCE_CONTROL_INITIALIZER;
static DBusError systemdbuserr;
static DBusError sessiondbuserr;
static dbus_bool_t virDBusAddWatch(DBusWatch *watch, void *data); static dbus_bool_t virDBusAddWatch(DBusWatch *watch, void *data);
static void virDBusRemoveWatch(DBusWatch *watch, void *data); static void virDBusRemoveWatch(DBusWatch *watch, void *data);
static void virDBusToggleWatch(DBusWatch *watch, void *data); static void virDBusToggleWatch(DBusWatch *watch, void *data);
static void virDBusSystemBusInit(void) static DBusConnection *virDBusBusInit(DBusBusType type, DBusError *dbuserr)
{ {
DBusConnection *bus;
/* Allocate and initialize a new HAL context */ /* Allocate and initialize a new HAL context */
dbus_connection_set_change_sigpipe(FALSE); dbus_connection_set_change_sigpipe(FALSE);
dbus_threads_init_default(); dbus_threads_init_default();
dbus_error_init(&dbuserr); dbus_error_init(dbuserr);
if (!(systembus = dbus_bus_get(DBUS_BUS_SYSTEM, &dbuserr))) if (!(bus = dbus_bus_get(type, dbuserr)))
return; return NULL;
dbus_connection_set_exit_on_disconnect(systembus, FALSE); dbus_connection_set_exit_on_disconnect(bus, FALSE);
/* Register dbus watch callbacks */ /* Register dbus watch callbacks */
if (!dbus_connection_set_watch_functions(systembus, if (!dbus_connection_set_watch_functions(bus,
virDBusAddWatch, virDBusAddWatch,
virDBusRemoveWatch, virDBusRemoveWatch,
virDBusToggleWatch, virDBusToggleWatch,
NULL, NULL)) { bus, NULL)) {
systembus = NULL; return NULL;
return;
} }
return bus;
} }
static void virDBusSystemBusInit(void)
{
systembus = virDBusBusInit(DBUS_BUS_SYSTEM, &systemdbuserr);
}
DBusConnection *virDBusGetSystemBus(void) DBusConnection *virDBusGetSystemBus(void)
{ {
if (virOnce(&once, virDBusSystemBusInit) < 0) { if (virOnce(&systemonce, virDBusSystemBusInit) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to run one time DBus initializer")); _("Unable to run one time DBus initializer"));
return NULL; return NULL;
@ -74,7 +83,7 @@ DBusConnection *virDBusGetSystemBus(void)
if (!systembus) { if (!systembus) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to get DBus system bus connection: %s"), _("Unable to get DBus system bus connection: %s"),
dbuserr.message ? dbuserr.message : "watch setup failed"); systemdbuserr.message ? systemdbuserr.message : "watch setup failed");
return NULL; return NULL;
} }
@ -82,13 +91,45 @@ DBusConnection *virDBusGetSystemBus(void)
} }
static void virDBusSessionBusInit(void)
{
sessionbus = virDBusBusInit(DBUS_BUS_SESSION, &sessiondbuserr);
}
DBusConnection *virDBusGetSessionBus(void)
{
if (virOnce(&sessiononce, virDBusSessionBusInit) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to run one time DBus initializer"));
return NULL;
}
if (!sessionbus) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to get DBus session bus connection: %s"),
sessiondbuserr.message ? sessiondbuserr.message : "watch setup failed");
return NULL;
}
return sessionbus;
}
struct virDBusWatch
{
int watch;
DBusConnection *bus;
};
static void virDBusWatchCallback(int fdatch ATTRIBUTE_UNUSED, static void virDBusWatchCallback(int fdatch ATTRIBUTE_UNUSED,
int fd ATTRIBUTE_UNUSED, int fd ATTRIBUTE_UNUSED,
int events, void *opaque) int events, void *opaque)
{ {
DBusWatch *watch = opaque; DBusWatch *watch = opaque;
struct virDBusWatch *info;
int dbus_flags = 0; int dbus_flags = 0;
info = dbus_watch_get_data(watch);
if (events & VIR_EVENT_HANDLE_READABLE) if (events & VIR_EVENT_HANDLE_READABLE)
dbus_flags |= DBUS_WATCH_READABLE; dbus_flags |= DBUS_WATCH_READABLE;
if (events & VIR_EVENT_HANDLE_WRITABLE) if (events & VIR_EVENT_HANDLE_WRITABLE)
@ -100,7 +141,7 @@ static void virDBusWatchCallback(int fdatch ATTRIBUTE_UNUSED,
(void)dbus_watch_handle(watch, dbus_flags); (void)dbus_watch_handle(watch, dbus_flags);
while (dbus_connection_dispatch(systembus) == DBUS_DISPATCH_DATA_REMAINS) while (dbus_connection_dispatch(info->bus) == DBUS_DISPATCH_DATA_REMAINS)
/* keep dispatching while data remains */; /* keep dispatching while data remains */;
} }
@ -120,18 +161,13 @@ static int virDBusTranslateWatchFlags(int dbus_flags)
} }
struct virDBusWatch
{
int watch;
};
static void virDBusWatchFree(void *data) { static void virDBusWatchFree(void *data) {
struct virDBusWatch *info = data; struct virDBusWatch *info = data;
VIR_FREE(info); VIR_FREE(info);
} }
static dbus_bool_t virDBusAddWatch(DBusWatch *watch, static dbus_bool_t virDBusAddWatch(DBusWatch *watch,
void *data ATTRIBUTE_UNUSED) void *data)
{ {
int flags = 0; int flags = 0;
int fd; int fd;
@ -148,6 +184,7 @@ static dbus_bool_t virDBusAddWatch(DBusWatch *watch,
# else # else
fd = dbus_watch_get_fd(watch); fd = dbus_watch_get_fd(watch);
# endif # endif
info->bus = (DBusConnection *)data;
info->watch = virEventAddHandle(fd, flags, info->watch = virEventAddHandle(fd, flags,
virDBusWatchCallback, virDBusWatchCallback,
watch, NULL); watch, NULL);
@ -194,4 +231,11 @@ DBusConnection *virDBusGetSystemBus(void)
return NULL; return NULL;
} }
DBusConnection *virDBusGetSessionBus(void)
{
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("DBus support not compiled into this binary"));
return NULL;
}
#endif /* ! HAVE_DBUS */ #endif /* ! HAVE_DBUS */

View File

@ -30,5 +30,6 @@
# include "internal.h" # include "internal.h"
DBusConnection *virDBusGetSystemBus(void); DBusConnection *virDBusGetSystemBus(void);
DBusConnection *virDBusGetSessionBus(void);
#endif /* __VIR_DBUS_H__ */ #endif /* __VIR_DBUS_H__ */