This removes all the systemd sd-bus code. Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx> --- README | 1 - configure.ac | 3 - data/Makefile.am | 4 +- data/org.libvirt.Connect.xml | 56 +++++ data/org.libvirt.Domain.xml | 51 +++++ libvirt-dbus.spec.in | 4 +- src/Makefile.am | 3 - src/connect.c | 221 ++++++++----------- src/connect.h | 16 +- src/domain.c | 513 +++++++++++++++++++------------------------ src/domain.h | 4 +- src/events.c | 133 +++++------ src/main.c | 100 +++------ src/util.c | 87 ++++---- src/util.h | 15 +- test/travis-run | 2 +- 16 files changed, 586 insertions(+), 627 deletions(-) create mode 100644 data/org.libvirt.Connect.xml create mode 100644 data/org.libvirt.Domain.xml diff --git a/README b/README index 242c9ba..518217f 100644 --- a/README +++ b/README @@ -56,7 +56,6 @@ raised in future releases based on this distro build target policy. The packages required to build libvirt-dbus are - - systemd-211 - dbus - libvirt diff --git a/configure.ac b/configure.ac index c8dcf04..8836c6f 100644 --- a/configure.ac +++ b/configure.ac @@ -12,10 +12,8 @@ AC_USE_SYSTEM_EXTENSIONS AM_SILENT_RULES([yes]) LIBVIRT_REQUIRED=1.2.8 -SYSTEMD_REQUIRED=211 DBUS_REQUIRED=1.10.24 AC_SUBST([LIBVIRT_REQUIRED]) dnl used in the .spec file -AC_SUBST([SYSTEMD_REQUIRED]) dnl used in the .spec file AC_SUBST([DBUS_REQUIRED]) dnl used in the .spec file LIBVIRT_DBUS_MAJOR_VERSION=`echo $VERSION | awk -F. '{print $1}'` @@ -36,7 +34,6 @@ AC_PROG_MKDIR_P AM_PROG_CC_C_O PKG_CHECK_MODULES(LIBVIRT, libvirt >= $LIBVIRT_REQUIRED) -PKG_CHECK_MODULES(SYSTEMD, libsystemd >= $SYSTEMD_REQUIRED) PKG_CHECK_MODULES(DBUS, dbus-1 >= $DBUS_REQUIRED) LIBVIRT_COMPILE_WARNINGS diff --git a/data/Makefile.am b/data/Makefile.am index a886687..dd60713 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -18,7 +18,9 @@ polkit_files = \ polkitdir = $(sysconfdir)/polkit-1/rules.d polkit_DATA = $(polkit_files:.rules.in=.rules) -interfaces_files = +interfaces_files = \ + org.libvirt.Connect.xml \ + org.libvirt.Domain.xml interfacesdir = $(DBUS_INTERFACES_DIR) interfaces_DATA = $(interfaces_files) diff --git a/data/org.libvirt.Connect.xml b/data/org.libvirt.Connect.xml new file mode 100644 index 0000000..a40ce1d --- /dev/null +++ b/data/org.libvirt.Connect.xml @@ -0,0 +1,56 @@ +<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> + +<node name="/org/libvirt/connect"> + <interface name="org.libvirt.Connect"> + <method name="CreateXML"> + <arg name="xml" type="s" direction="in"/> + <arg name="flags" type="u" direction="in"/> + <arg name="domain" type="o" direction="out"/> + </method> + <method name="DefineXML"> + <arg name="xml" type="s" direction="in"/> + <arg name="domain" type="o" direction="out"/> + </method> + <method name="ListDomains"> + <arg name="flags" type="u" direction="in"/> + <arg name="domains" type="ao" direction="out"/> + </method> + <signal name="DomainCrashed"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainDefined"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainPMSuspended"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainResumed"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainShutdown"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainStarted"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainStopped"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainSuspended"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + <signal name="DomainUndefined"> + <arg name="reason" type="s"/> + <arg name="domain" type="o"/> + </signal> + </interface> +</node> diff --git a/data/org.libvirt.Domain.xml b/data/org.libvirt.Domain.xml new file mode 100644 index 0000000..48bf40f --- /dev/null +++ b/data/org.libvirt.Domain.xml @@ -0,0 +1,51 @@ +<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> + +<node name="/org/libvirt/domain"> + <interface name="org.libvirt.Domain"> + <property name="Name" type="s" access="read"/> + <property name="UUID" type="s" access="read"/> + <property name="Id" type="u" access="read"/> + <property name="Vcpus" type="u" access="read"/> + <property name="OSType" type="s" access="read"/> + <property name="Active" type="b" access="read"/> + <property name="Persistent" type="b" access="read"/> + <property name="State" type="s" access="read"/> + <property name="Autostart" type="b" access="read"/> + <method name="GetXMLDesc"> + <arg name="flags" type="u" direction="in"/> + <arg name="xml" type="s" direction="out"/> + </method> + <method name="GetStats"> + <arg name="stats" type="u" direction="in"/> + <arg name="flags" type="u" direction="in"/> + <arg name="records" type="a{sv}" direction="out"/> + </method> + <method name="Shutdown"/> + <method name="Destroy"/> + <method name="Reboot"> + <arg name="flags" type="u" direction="in"/> + </method> + <method name="Reset"> + <arg name="flags" type="u" direction="in"/> + </method> + <method name="Create"/> + <method name="Undefine"/> + <signal name="DeviceAdded"> + <arg name="device" type="s"/> + </signal> + <signal name="DeviceRemoved"> + <arg name="device" type="s"/> + </signal> + <signal name="DiskChange"> + <arg name="oldSrcPath" type="s"/> + <arg name="newSrcPath" type="s"/> + <arg name="device" type="s"/> + <arg name="reason" type="s"/> + </signal> + <signal name="TrayChange"> + <arg name="device" type="s"/> + <arg name="reason" type="s"/> + </signal> + </interface> +</node> diff --git a/libvirt-dbus.spec.in b/libvirt-dbus.spec.in index 512f4fc..d458d71 100644 --- a/libvirt-dbus.spec.in +++ b/libvirt-dbus.spec.in @@ -1,7 +1,6 @@ # -*- rpm-spec -*- %define libvirt_version @LIBVIRT_REQUIRED@ -%define systemd_version @SYSTEMD_REQUIRED@ %define dbus_version @DBUS_REQUIRED@ %define system_user @SYSTEM_USER@ @@ -16,11 +15,9 @@ Source0: ftp://libvirt.org/libvirt/dbus/%{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRequires: libvirt-devel >= %{libvirt_version} -BuildRequires: systemd-devel >= %{systemd_version} BuildRequires: dbus-devel >= %{dbus_version} Requires: libvirt-libs >= %{libvirt_version} -Requires: systemd-libs >= %{systemd_version} Requires: dbus-libs >= %{dbus_version} Requires(pre): shadow-utils @@ -57,5 +54,6 @@ exit 0 %{_datadir}/dbus-1/services/org.libvirt.service %{_datadir}/dbus-1/system-services/org.libvirt.service %{_datadir}/dbus-1/system.d/org.libvirt.conf +%{_datadir}/dbus-1/interfaces/org.libvirt.*.xml %changelog diff --git a/src/Makefile.am b/src/Makefile.am index 1f0d990..9e23f1b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,6 @@ bin_PROGRAMS = libvirt-dbus libvirt_dbus_SOURCES = $(DAEMON_SOURCES) libvirt_dbus_CFLAGS = \ - $(SYSTEMD_CFLAGS) \ $(LIBVIRT_CFLAGS) \ $(DBUS_CFLAGS) \ $(WARN_CFLAGS) \ @@ -29,7 +28,6 @@ libvirt_dbus_CFLAGS = \ $(NULL) libvirt_dbus_LDFLAGS = \ - $(SYSTEMD_LDFLAGS) \ $(LIBVIRT_LDFLAGS) \ $(DBUS_LDFLAGS) \ $(RELRO_LDFLAGS) \ @@ -37,6 +35,5 @@ libvirt_dbus_LDFLAGS = \ $(NULL) libvirt_dbus_LDADD = \ - $(SYSTEMD_LIBS) \ $(LIBVIRT_LIBS) \ $(DBUS_LIBS) diff --git a/src/connect.c b/src/connect.c index 695fc0d..2fe305f 100644 --- a/src/connect.c +++ b/src/connect.c @@ -1,9 +1,11 @@ +#include "dbus.h" #include "domain.h" #include "events.h" #include "connect.h" #include "util.h" #include <stdbool.h> +#include <stdint.h> #include <stdlib.h> static int virtDBusConnectCredType[] = { @@ -15,16 +17,18 @@ static int virtDBusConnectCredType[] = { VIR_CRED_EXTERNAL, }; +static const char *introspectXML = NULL; + static int virtDBusConnectAuthCallback(virConnectCredentialPtr cred VIRT_ATTR_UNUSED, unsigned int ncred VIRT_ATTR_UNUSED, void *cbdata) { - sd_bus_error *error = cbdata; + virtDBusMessage *msg = cbdata; - return virtDBusUtilSetError(error, - "Interactive authentication is not supported. " - "Use client configuration file for libvirt."); + return virtDBusMessageSetError(msg, VIRT_DBUS_ERROR_INTERFACE, + "Interactive authentication is not supported. " + "Use client configuration file for libvirt."); } static virConnectAuth virtDBusConnectAuth = { @@ -38,7 +42,6 @@ static void virtDBusConnectClose(virtDBusConnect *connect, bool deregisterEvents) { - for (int i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i += 1) { if (connect->callback_ids[i] >= 0) { if (deregisterEvents) { @@ -55,7 +58,7 @@ virtDBusConnectClose(virtDBusConnect *connect, int virtDBusConnectOpen(virtDBusConnect *connect, - sd_bus_error *error) + virtDBusMessage *msg) { if (connect->connection) { if (virConnectIsAlive(connect->connection)) @@ -64,12 +67,12 @@ virtDBusConnectOpen(virtDBusConnect *connect, virtDBusConnectClose(connect, false); } - virtDBusConnectAuth.cbdata = error; + virtDBusConnectAuth.cbdata = msg; connect->connection = virConnectOpenAuth(connect->uri, &virtDBusConnectAuth, 0); if (!connect->connection) - return virtDBusUtilSetLastVirtError(error); + return virtDBusUtilSetLastVirtError(msg); virtDBusEventsRegister(connect); @@ -77,68 +80,29 @@ virtDBusConnectOpen(virtDBusConnect *connect, } static int -virtDBusConnectEnumarateDomains(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path VIRT_ATTR_UNUSED, - void *userdata, - char ***nodes, - sd_bus_error *error) -{ - virtDBusConnect *connect = userdata; - _cleanup_(virtDBusUtilVirDomainListFreep) virDomainPtr *domains = NULL; - _cleanup_(virtDBusUtilStrvFreep) char **paths = NULL; - int n_domains; - int r; - - r = virtDBusConnectOpen(connect, error); - if (r < 0) - return r; - - n_domains = virConnectListAllDomains(connect->connection, &domains, 0); - if (n_domains < 0) - return virtDBusUtilSetLastVirtError(error); - - paths = calloc(n_domains + 1, sizeof(char *)); - - for (int i = 0; i < n_domains; i += 1) - paths[i] = virtDBusUtilBusPathForVirDomain(domains[i], - connect->domainPath); - - *nodes = paths; - paths = NULL; - - return 0; -} - -static int -virtDBusConnectListDomains(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusConnectListDomains(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + virtDBusConnect *connect = data; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *reply = NULL; _cleanup_(virtDBusUtilVirDomainListFreep) virDomainPtr *domains = NULL; uint32_t flags; - int r; - r = sd_bus_message_read(message, "u", &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; - r = virtDBusConnectOpen(connect, error); - if (r < 0) - return r; + if (virtDBusConnectOpen(connect, msg) < 0) + return -1; - r = virConnectListAllDomains(connect->connection, &domains, flags); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virConnectListAllDomains(connect->connection, &domains, flags) < 0) + return virtDBusUtilSetLastVirtError(msg); - r = sd_bus_message_new_method_return(message, &reply); - if (r < 0) - return r; + reply = virtDBusMessageMethodReturn(msg); + if (!reply) + return -1; - r = sd_bus_message_open_container(reply, 'a', "o"); - if (r < 0) - return r; + if (virtDBusMessageOpenContainer(reply, DBUS_TYPE_ARRAY, "o") < 0) + return -1; for (int i = 0; domains[i] != NULL; i += 1) { _cleanup_(virtDBusUtilFreep) char *path = NULL; @@ -146,125 +110,120 @@ virtDBusConnectListDomains(sd_bus_message *message, path = virtDBusUtilBusPathForVirDomain(domains[i], connect->domainPath); - r = sd_bus_message_append(reply, "o", path); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(reply, DBUS_TYPE_OBJECT_PATH, &path) < 0) + return -1; } - r = sd_bus_message_close_container(reply); - if (r < 0) - return r; + if (virtDBusMessageCloseContainer(reply) < 0) + return -1; - return sd_bus_send(NULL, reply, NULL); + return virtDBusSendMessage(reply); } static int -virtDBusConnectCreateXML(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusConnectCreateXML(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; - const char *xml; - uint32_t flags; + virtDBusConnect *connect = data; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *reply = NULL; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; - int r; + const char *xml; + uint32_t flags; - r = sd_bus_message_read(message, "su", &xml, &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_STRING, &xml) < 0) + return -1; - r = virtDBusConnectOpen(connect, error); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; + + if (virtDBusConnectOpen(connect, msg) < 0) + return -1; domain = virDomainCreateXML(connect->connection, xml, flags); if (!domain) - return virtDBusUtilSetLastVirtError(error); + return virtDBusUtilSetLastVirtError(msg); path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - return sd_bus_reply_method_return(message, "o", path); + reply = virtDBusMessageMethodReturn(msg); + if (!reply) + return -1; + + if (virtDBusMessageAppendBasic(reply, DBUS_TYPE_OBJECT_PATH, &path) < 0) + return -1; + + return virtDBusSendMessage(reply); } static int -virtDBusConnectDefineXML(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusConnectDefineXML(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; - const char *xml; + virtDBusConnect *connect = data; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *reply = NULL; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; - int r; + const char *xml; - r = sd_bus_message_read(message, "s", &xml); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_STRING, &xml) < 0) + return -1; - r = virtDBusConnectOpen(connect, error); - if (r < 0) - return r; + if (virtDBusConnectOpen(connect, msg) < 0) + return -1; domain = virDomainDefineXML(connect->connection, xml); if (!domain) - return virtDBusUtilSetLastVirtError(error); + return virtDBusUtilSetLastVirtError(msg); path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - return sd_bus_reply_method_return(message, "o", path); -} - -static const sd_bus_vtable virt_connect_vtable[] = { - SD_BUS_VTABLE_START(0), + reply = virtDBusMessageMethodReturn(msg); + if (!reply) + return -1; - SD_BUS_METHOD("ListDomains", "u", "ao", virtDBusConnectListDomains, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("CreateXML", "su", "o", virtDBusConnectCreateXML, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("DefineXML", "s", "o", virtDBusConnectDefineXML, SD_BUS_VTABLE_UNPRIVILEGED), + if (virtDBusMessageAppendBasic(reply, DBUS_TYPE_OBJECT_PATH, &path) < 0) + return -1; - SD_BUS_SIGNAL("DomainDefined", "so", 0), - SD_BUS_SIGNAL("DomainUndefined", "so", 0), - SD_BUS_SIGNAL("DomainStarted", "so", 0), - SD_BUS_SIGNAL("DomainSuspended", "so", 0), - SD_BUS_SIGNAL("DomainResumed", "so", 0), - SD_BUS_SIGNAL("DomainStopped", "so", 0), - SD_BUS_SIGNAL("DomainShutdown", "so", 0), - SD_BUS_SIGNAL("DomainPMSuspended", "so", 0), - SD_BUS_SIGNAL("DomainCrashed", "so", 0), + return virtDBusSendMessage(reply); +} - SD_BUS_VTABLE_END +static virtDBusMethodHandlers virtDBusConnectMethods[] = { + { "CreateXML", "su", virtDBusConnectCreateXML }, + { "DefineXML", "s", virtDBusConnectDefineXML }, + { "ListDomains", "u", virtDBusConnectListDomains }, + { NULL, NULL, NULL }, }; int virtDBusConnectNew(virtDBusConnect **connectp, - sd_bus *bus, + virtDBusObjectList *objectList, + DBusConnection *bus, const char *uri, const char *connectPath) { _cleanup_(virtDBusConnectFreep) virtDBusConnect *connect = NULL; - int r; connect = calloc(1, sizeof(virtDBusConnect)); for (int i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i += 1) connect->callback_ids[i] = -1; - connect->bus = sd_bus_ref(bus); + connect->bus = dbus_connection_ref(bus); connect->uri = uri; connect->connectPath = connectPath; - connect->enumerateDomains = virtDBusConnectEnumarateDomains; - - r = sd_bus_add_object_vtable(connect->bus, - NULL, - connect->connectPath, - VIRT_DBUS_CONNECT_INTERFACE, - virt_connect_vtable, - connect); - if (r < 0) - return r; + if (virtDBusObjectListRegister(objectList, + connect->connectPath, + &introspectXML, + VIRT_DBUS_CONNECT_INTERFACE, + VIRT_DBUS_OBJECT_TYPE_MATCH, + NULL, + virtDBusConnectMethods, + connect) < 0) + return -1; - if ((r = virtDBusDomainRegister(connect, bus) < 0)) - return r; + if (virtDBusDomainRegister(connect, objectList) < 0) + return -1; *connectp = connect; connect = NULL; @@ -276,7 +235,7 @@ virtDBusConnect * virtDBusConnectFree(virtDBusConnect *connect) { if (connect->bus) - sd_bus_unref(connect->bus); + dbus_connection_unref(connect->bus); if (connect->connection) virtDBusConnectClose(connect, true); diff --git a/src/connect.h b/src/connect.h index 9e5f533..e685b41 100644 --- a/src/connect.h +++ b/src/connect.h @@ -2,37 +2,37 @@ #define VIR_ENUM_SENTINELS +#include <dbus/dbus.h> #include <libvirt/libvirt.h> -#include <systemd/sd-bus.h> #define VIRT_DBUS_CONNECT_INTERFACE "org.libvirt.Connect" -struct virtDBusConnect { - sd_bus *bus; +struct _virtDBusConnect { + DBusConnection *bus; const char *uri; const char *connectPath; char *domainPath; virConnectPtr connection; - sd_bus_node_enumerator_t enumerateDomains; - int callback_ids[VIR_DOMAIN_EVENT_ID_LAST]; }; -typedef struct virtDBusConnect virtDBusConnect; +typedef struct _virtDBusConnect virtDBusConnect; int virtDBusConnectNew(virtDBusConnect **connectp, - sd_bus *bus, + virtDBusObjectList *objectList, + DBusConnection *bus, const char *uri, const char *connectPath); int virtDBusConnectOpen(virtDBusConnect *connect, - sd_bus_error *error); + virtDBusMessage *msg); virtDBusConnect * virtDBusConnectFree(virtDBusConnect *connect); + void virtDBusConnectFreep(virtDBusConnect **connectp); diff --git a/src/domain.c b/src/domain.c index daff32d..394be33 100644 --- a/src/domain.c +++ b/src/domain.c @@ -1,27 +1,31 @@ #define _GNU_SOURCE +#include "dbus.h" #include "domain.h" #include "util.h" #include <libvirt/libvirt.h> #include <stdio.h> +#include <stdint.h> +#include <string.h> + +static const char *introspectXML = NULL; static virDomainPtr virtDBusDomainGetVirDomain(virtDBusConnect *connect, - const char *path, - sd_bus_error *error) + virtDBusMessage *msg) { virDomainPtr domain; + const char *path = virtDBusMessageGetPath(msg); - if (virtDBusConnectOpen(connect, error) < 0) + if (virtDBusConnectOpen(connect, msg) < 0) return NULL; domain = virtDBusUtilVirDomainFromBusPath(connect->connection, path, connect->domainPath); - if (domain == NULL) { - sd_bus_error_setf(error, - SD_BUS_ERROR_UNKNOWN_OBJECT, - "Unknown object '%s'.", path); + if (!domain) { + virtDBusMessageSetError(msg, DBUS_ERROR_UNKNOWN_OBJECT, + "invalid domain object '%s'", path); return NULL; } @@ -29,177 +33,177 @@ virtDBusDomainGetVirDomain(virtDBusConnect *connect, } static int -virtDBusDomainGetName(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetName(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - const char *name = ""; + const char *name = NULL; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; name = virDomainGetName(domain); - if (name == NULL) - return sd_bus_message_append(reply, "s", ""); + if (!name) + name = ""; - return sd_bus_message_append(reply, "s", name); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_STRING, + "s", &name); } static int -virtDBusDomainGetUUID(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetUUID(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; char uuid[VIR_UUID_STRING_BUFLEN] = ""; + char *tmp; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; virDomainGetUUIDString(domain, uuid); + tmp = uuid; - return sd_bus_message_append(reply, "s", uuid); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_STRING, + "s", &tmp); } static int -virtDBusDomainGetId(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetId(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; + unsigned int id; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - return sd_bus_message_append(reply, "u", virDomainGetID(domain)); + id = virDomainGetID(domain); + + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_UINT32, + "u", &id); } static int -virtDBusDomainGetVcpus(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetVcpus(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; + int vcpus; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - return sd_bus_message_append(reply, "u", virDomainGetVcpusFlags(domain, VIR_DOMAIN_VCPU_CURRENT)); + vcpus = virDomainGetVcpusFlags(domain, VIR_DOMAIN_VCPU_CURRENT); + + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_UINT32, + "u", &vcpus); } static int -virtDBusDomainGetOsType(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetOsType(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; _cleanup_(virtDBusUtilFreep) char *os_type = NULL; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; os_type = virDomainGetOSType(domain); - if (os_type == NULL) - return sd_bus_message_append(reply, "s", ""); + if (!os_type) + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_STRING, + "s", &""); - return sd_bus_message_append(reply, "s", os_type); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_STRING, + "s", &os_type); } static int -virtDBusDomainGetActive(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetActive(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; int active; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; active = virDomainIsActive(domain); if (active < 0) - return sd_bus_message_append(reply, "b", 0); + active = 0; - return sd_bus_message_append(reply, "b", active); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_BOOLEAN, + "b", &active); } static int -virtDBusDomainGetPersistent(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetPersistent(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; int persistent; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; persistent = virDomainIsPersistent(domain); if (persistent < 0) - return sd_bus_message_append(reply, "b", 0); + persistent = 0; - return sd_bus_message_append(reply, "b", persistent); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_BOOLEAN, + "b", &persistent); } static int -virtDBusDomainGetState(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetState(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; int state = 0; const char *string; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; virDomainGetState(domain, &state, NULL, 0); @@ -232,57 +236,60 @@ virtDBusDomainGetState(sd_bus *bus VIRT_ATTR_UNUSED, break; } - return sd_bus_message_append(reply, "s", string); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_STRING, + "s", &string); } static int -virtDBusDomainGetAutostart(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, +virtDBusDomainGetAutostart(virtDBusMessage *reply, + virtDBusMessage *msg, const char *interface VIRT_ATTR_UNUSED, const char *property VIRT_ATTR_UNUSED, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error VIRT_ATTR_UNUSED) + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; int autostart = 0; - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; virDomainGetAutostart(domain, &autostart); - return sd_bus_message_append(reply, "b", autostart); + return virtDBusMessageAppendVariant(reply, DBUS_TYPE_BOOLEAN, + "b", &autostart); } static int -virtDBusDomainGetXMLDesc(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainGetXMLDesc(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *reply = NULL; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; _cleanup_(virtDBusUtilFreep) char *description = NULL; uint32_t flags; - int r; - r = sd_bus_message_read(message, "u", &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; description = virDomainGetXMLDesc(domain, flags); if (!description) - return virtDBusUtilSetLastVirtError(error); + return virtDBusUtilSetLastVirtError(msg); + + reply = virtDBusMessageMethodReturn(msg); + if (!reply) + return -1; + + if (virtDBusMessageAppendBasic(reply, DBUS_TYPE_STRING, &description) < 0) + return -1; - return sd_bus_reply_method_return(message, "s", description); + return virtDBusSendMessage(reply); } static void @@ -293,263 +300,195 @@ virtDBusDomainStatsRecordListFreep(virDomainStatsRecordPtr **statsp) } static int -virtDBusDomainGetStats(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainGetStats(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *reply = NULL; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - virDomainPtr domains[2]; _cleanup_(virtDBusDomainStatsRecordListFreep) virDomainStatsRecordPtr *records = NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + virDomainPtr domains[2]; uint32_t flags, stats; - int r; - r = sd_bus_message_read(message, "uu", &stats, &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &stats) < 0) + return -1; + + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; domains[0] = domain; domains[1] = NULL; if (virDomainListGetStats(domains, stats, &records, flags) != 1) - return virtDBusUtilSetLastVirtError(error); + return virtDBusUtilSetLastVirtError(msg); - r = sd_bus_message_new_method_return(message, &reply); - if (r < 0) - return r; + reply = virtDBusMessageMethodReturn(msg); + if (!reply) + return -1; - r = virtDBusUtilMessageAppendTypedParameters(reply, records[0]->params, records[0]->nparams); - if (r < 0) - return r; + if (virtDBusUtilMessageAppendTypedParameters(reply, + records[0]->params, + records[0]->nparams) < 0) + return -1; - return sd_bus_send(NULL, reply, NULL); + return virtDBusSendMessage(reply); } static int -virtDBusDomainShutdown(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainShutdown(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - int r; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainShutdown(domain); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainShutdown(domain) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } static int -virtDBusDomainDestroy(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainDestroy(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - int r; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainDestroy(domain); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainDestroy(domain) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } static int -virtDBusDomainReboot(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainReboot(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; uint32_t flags; - int r; - r = sd_bus_message_read(message, "u", &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainReboot(domain, flags); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainReboot(domain, flags) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } static int -virtDBusDomainReset(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainReset(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; uint32_t flags; - int r; - r = sd_bus_message_read(message, "u", &flags); - if (r < 0) - return r; + if (virtDBusMessageReadBasic(msg, DBUS_TYPE_UINT32, &flags) < 0) + return -1; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainReset(domain, flags); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainReset(domain, flags) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } static int -virtDBusDomainCreate(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainCreate(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - int r; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainCreate(domain); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainCreate(domain) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } static int -virtDBusDomainUndefine(sd_bus_message *message, - void *userdata, - sd_bus_error *error) +virtDBusDomainUndefine(virtDBusMessage *msg, + void *data) { - virtDBusConnect *connect = userdata; + virtDBusConnect *connect = data; _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - int r; - domain = virtDBusDomainGetVirDomain(connect, - sd_bus_message_get_path(message), - error); - if (domain == NULL) + domain = virtDBusDomainGetVirDomain(connect, msg); + if (!domain) return -1; - r = virDomainUndefine(domain); - if (r < 0) - return virtDBusUtilSetLastVirtError(error); + if (virDomainUndefine(domain) < 0) + return virtDBusUtilSetLastVirtError(msg); - return sd_bus_reply_method_return(message, ""); + return virtDBusSendEmptyMessage(msg); } -static const sd_bus_vtable virt_domain_vtable[] = { - SD_BUS_VTABLE_START(0), - - SD_BUS_PROPERTY("Name", "s", virtDBusDomainGetName, 0, 0), - SD_BUS_PROPERTY("UUID", "s", virtDBusDomainGetUUID, 0, 0), - SD_BUS_PROPERTY("Id", "u", virtDBusDomainGetId, 0, 0), - SD_BUS_PROPERTY("Vcpus", "u", virtDBusDomainGetVcpus, 0, 0), - SD_BUS_PROPERTY("OSType", "s", virtDBusDomainGetOsType, 0, 0), - SD_BUS_PROPERTY("Active", "b", virtDBusDomainGetActive, 0, 0), - SD_BUS_PROPERTY("Persistent", "b", virtDBusDomainGetPersistent, 0, 0), - SD_BUS_PROPERTY("State", "s", virtDBusDomainGetState, 0, 0), - SD_BUS_PROPERTY("Autostart", "b", virtDBusDomainGetAutostart, 0, 0), - - SD_BUS_METHOD("GetXMLDesc", "u", "s", virtDBusDomainGetXMLDesc, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("GetStats", "uu", "a{sv}", virtDBusDomainGetStats, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Shutdown", "", "", virtDBusDomainShutdown, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Destroy", "", "", virtDBusDomainDestroy, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Reboot", "u", "", virtDBusDomainReboot, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Reset", "u", "", virtDBusDomainReset, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Create", "", "", virtDBusDomainCreate, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("Undefine", "", "", virtDBusDomainUndefine, SD_BUS_VTABLE_UNPRIVILEGED), - - SD_BUS_SIGNAL("DeviceAdded", "s", 0), - SD_BUS_SIGNAL("DeviceRemoved", "s", 0), - SD_BUS_SIGNAL("DiskChange", "ssss", 0), - SD_BUS_SIGNAL("TrayChange", "ss", 0), - - SD_BUS_VTABLE_END +static virtDBusPropertyHandlers virtDBusDomainProperties[] = { + { "Name", virtDBusDomainGetName, NULL }, + { "UUID", virtDBusDomainGetUUID, NULL }, + { "Id", virtDBusDomainGetId, NULL }, + { "Vcpus", virtDBusDomainGetVcpus, NULL }, + { "OSType", virtDBusDomainGetOsType, NULL }, + { "Active", virtDBusDomainGetActive, NULL }, + { "Persistent", virtDBusDomainGetPersistent, NULL }, + { "State", virtDBusDomainGetState, NULL }, + { "Autostart", virtDBusDomainGetAutostart, NULL }, + { NULL, NULL, NULL }, }; -static int -virtDBusDomainLookup(sd_bus *bus VIRT_ATTR_UNUSED, - const char *path, - const char *interface VIRT_ATTR_UNUSED, - void *userdata, - void **found, - sd_bus_error *error VIRT_ATTR_UNUSED) -{ - virtDBusConnect *connect = userdata; - _cleanup_(virtDBusUtilFreep) char *name = NULL; - _cleanup_(virtDBusUtilVirDomainFreep) virDomainPtr domain = NULL; - - domain = virtDBusDomainGetVirDomain(connect, path, error); - if (!domain) - return 0; - - /* - * There's no way to unref the pointer we're returning here. So, - * return the connect object and look up the domain again in the - * domain_* callbacks. - */ - *found = connect; - - return 1; -} +static virtDBusMethodHandlers virtDBusDomainMethods[] = { + { "GetXMLDesc", "u", virtDBusDomainGetXMLDesc }, + { "GetStats", "u", virtDBusDomainGetStats }, + { "Shutdown", "", virtDBusDomainShutdown }, + { "Destroy", "", virtDBusDomainDestroy }, + { "Reboot", "u", virtDBusDomainReboot }, + { "Reset", "u", virtDBusDomainReset }, + { "Create", "", virtDBusDomainCreate }, + { "Undefine", "", virtDBusDomainUndefine }, + { NULL, NULL, NULL }, +}; int virtDBusDomainRegister(virtDBusConnect *connect, - sd_bus *bus) + virtDBusObjectList *objectList) { - int r; - - r = asprintf(&connect->domainPath, "%s/domain", connect->connectPath); - if (r < 0) - return r; - - r = sd_bus_add_node_enumerator(bus, NULL, connect->domainPath, - connect->enumerateDomains, connect); - if (r < 0) - return r; - - return sd_bus_add_fallback_vtable(bus, - NULL, - connect->domainPath, - VIRT_DBUS_DOMAIN_INTERFACE, - virt_domain_vtable, - virtDBusDomainLookup, - connect); + if (asprintf(&connect->domainPath, "%s/domain", connect->connectPath) < 0) + return -1; + + if (virtDBusObjectListRegister(objectList, + connect->domainPath, + &introspectXML, + VIRT_DBUS_DOMAIN_INTERFACE, + VIRT_DBUS_OBJECT_TYPE_PREFIX, + virtDBusDomainProperties, + virtDBusDomainMethods, + connect) < 0) + return -1; + + return 0; } diff --git a/src/domain.h b/src/domain.h index 03a29ed..45db0f5 100644 --- a/src/domain.h +++ b/src/domain.h @@ -2,10 +2,8 @@ #include "connect.h" -#include <systemd/sd-bus.h> - #define VIRT_DBUS_DOMAIN_INTERFACE "org.libvirt.Domain" int virtDBusDomainRegister(virtDBusConnect *connect, - sd_bus *bus); + virtDBusObjectList *objectList); diff --git a/src/events.c b/src/events.c index c45acd7..8368a30 100644 --- a/src/events.c +++ b/src/events.c @@ -1,10 +1,10 @@ +#include "dbus.h" #include "domain.h" #include "events.h" #include "util.h" #include <assert.h> #include <libvirt/libvirt.h> -#include <systemd/sd-bus.h> static int virtDBusEventsDomainLifecycle(virConnectPtr connection VIRT_ATTR_UNUSED, @@ -14,11 +14,10 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection VIRT_ATTR_UNUSED, void *opaque) { virtDBusConnect *connect = opaque; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *msg = NULL; const char *signal = NULL; const char *name; _cleanup_(virtDBusUtilFreep) char *path = NULL; - int r; switch (event) { case VIR_DOMAIN_EVENT_DEFINED: @@ -52,22 +51,23 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection VIRT_ATTR_UNUSED, return 0; } - r = sd_bus_message_new_signal(connect->bus, - &message, - connect->connectPath, - VIRT_DBUS_CONNECT_INTERFACE, - signal); - if (r < 0) - return r; + msg = virtDBusMessageNewSignal(connect->bus, + connect->connectPath, + VIRT_DBUS_CONNECT_INTERFACE, + signal); + if (!msg) + return -1; name = virDomainGetName(domain); path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - r = sd_bus_message_append(message, "so", name ? : "", path); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &name) < 0) + return -1; - return sd_bus_send(connect->bus, message, NULL); + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_OBJECT_PATH, &path) < 0) + return -1; + + return virtDBusSendMessage(msg); } static int @@ -77,25 +77,22 @@ virtDBusEventsDomainDeviceAdded(virConnectPtr connection VIRT_ATTR_UNUSED, void *opaque) { virtDBusConnect *connect = opaque; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *msg = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; - int r; path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - r = sd_bus_message_new_signal(connect->bus, - &message, - path, - VIRT_DBUS_DOMAIN_INTERFACE, - "DeviceAdded"); - if (r < 0) - return r; + msg = virtDBusMessageNewSignal(connect->bus, + path, + VIRT_DBUS_DOMAIN_INTERFACE, + "DeviceAdded"); + if (!msg) + return -1; - r = sd_bus_message_append(message, "s", device); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &device) < 0) + return -1; - return sd_bus_send(connect->bus, message, NULL); + return virtDBusSendMessage(msg); } static int @@ -105,25 +102,23 @@ virtDBusEventsDomainDeviceRemoved(virConnectPtr connection VIRT_ATTR_UNUSED, void *opaque) { virtDBusConnect *connect = opaque; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *msg = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; - int r; path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - r = sd_bus_message_new_signal(connect->bus, - &message, - path, - VIRT_DBUS_DOMAIN_INTERFACE, - "DeviceRemoved"); - if (r < 0) - return r; + msg = virtDBusMessageNewSignal(connect->bus, + path, + VIRT_DBUS_DOMAIN_INTERFACE, + "DeviceRemoved"); + + if (!msg) + return -1; - r = sd_bus_message_append(message, "s", device); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &device) < 0) + return -1; - return sd_bus_send(connect->bus, message, NULL); + return virtDBusSendMessage(msg); } static int @@ -134,20 +129,18 @@ virtDBusEventsDomainTrayChange(virConnectPtr connection VIRT_ATTR_UNUSED, void *opaque) { virtDBusConnect *connect = opaque; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *msg = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; const char *reasonstr; - int r; path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - r = sd_bus_message_new_signal(connect->bus, - &message, - path, - VIRT_DBUS_DOMAIN_INTERFACE, - "TrayChange"); - if (r < 0) - return r; + msg = virtDBusMessageNewSignal(connect->bus, + path, + VIRT_DBUS_DOMAIN_INTERFACE, + "TrayChange"); + if (!msg) + return -1; switch (reason) { case VIR_DOMAIN_EVENT_TRAY_CHANGE_OPEN: @@ -161,11 +154,13 @@ virtDBusEventsDomainTrayChange(virConnectPtr connection VIRT_ATTR_UNUSED, break; } - r = sd_bus_message_append(message, "ss", device, reasonstr); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &device) < 0) + return -1; + + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &reasonstr) < 0) + return -1; - return sd_bus_send(connect->bus, message, NULL); + return virtDBusSendMessage(msg); } static int @@ -178,20 +173,18 @@ virtDBusEventsDomainDiskChange(virConnectPtr connection VIRT_ATTR_UNUSED, void *opaque) { virtDBusConnect *connect = opaque; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; + _cleanup_(virtDBusMessageFreep) virtDBusMessage *msg = NULL; _cleanup_(virtDBusUtilFreep) char *path = NULL; const char *reasonstr; - int r; path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath); - r = sd_bus_message_new_signal(connect->bus, - &message, - path, - VIRT_DBUS_DOMAIN_INTERFACE, - "DiskChange"); - if (r < 0) - return r; + msg = virtDBusMessageNewSignal(connect->bus, + path, + VIRT_DBUS_DOMAIN_INTERFACE, + "DiskChange"); + if (!msg) + return -1; switch (reason) { case VIR_DOMAIN_EVENT_DISK_CHANGE_MISSING_ON_START: @@ -205,11 +198,19 @@ virtDBusEventsDomainDiskChange(virConnectPtr connection VIRT_ATTR_UNUSED, break; } - r = sd_bus_message_append(message, "ssss", old_src_path, new_src_path, device, reasonstr); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &old_src_path) < 0) + return -1; + + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &new_src_path) < 0) + return -1; + + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &device) < 0) + return -1; + + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, &reasonstr) < 0) + return -1; - return sd_bus_send(connect->bus, message, NULL); + return virtDBusSendMessage(msg); } static void diff --git a/src/main.c b/src/main.c index 6f11aab..bef5dcc 100644 --- a/src/main.c +++ b/src/main.c @@ -1,44 +1,31 @@ #include "config.h" +#include "dbus.h" #include "connect.h" +#include "domain.h" #include "util.h" #include <errno.h> #include <getopt.h> #include <poll.h> +#include <signal.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <sys/signalfd.h> -#include <systemd/sd-bus.h> #include <unistd.h> -static int loop_status; +static int loop_status = 0; static int -virtDBusGetLibvirtEvents(sd_bus *bus) -{ - int events; - int virt_events = 0; - - events = sd_bus_get_events(bus); - - if (events & POLLIN) - virt_events |= VIR_EVENT_HANDLE_READABLE; - - if (events & POLLOUT) - virt_events |= VIR_EVENT_HANDLE_WRITABLE; - - return virt_events; -} - -static int -virtDBusProcessEvents(sd_bus *bus) +virtDBusProcessEvents(DBusConnection *bus, + virtDBusObjectList *objectList) { for (;;) { int r; - r = sd_bus_process(bus, NULL); + r = virtDBusDispatchMessage(bus, objectList); if (r < 0) return r; @@ -65,22 +52,6 @@ virtDBusHandleSignal(int watch VIRT_ATTR_UNUSED, loop_status = -ECANCELED; } -static void -virtDBusHandleBusEvent(int watch, - int fd VIRT_ATTR_UNUSED, - int events VIRT_ATTR_UNUSED, - void *opaque) -{ - sd_bus *bus = opaque; - - loop_status = virtDBusProcessEvents(bus); - - if (loop_status < 0) - return; - - virEventUpdateHandle(watch, virtDBusGetLibvirtEvents(bus)); -} - struct virtDBusDriver { const char *uri; const char *object; @@ -123,23 +94,23 @@ main(int argc, char *argv[]) {} }; - bool system_bus; + DBusBusType busType; const struct virtDBusDriver *drivers = NULL; int ndrivers = 0; - _cleanup_(virtDBusConnectListFree) virtDBusConnect **connect = NULL; - _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; + _cleanup_(virtDBusConnectListFree) virtDBusConnect **connectList = NULL; + _cleanup_(virtDBusObjectListFree) virtDBusObjectList objectList = { 0 }; + _cleanup_(virtDBusConnectionClose) DBusConnection *bus = NULL; _cleanup_(virtDBusUtilClosep) int signal_fd = -1; - _cleanup_(virtDBusVirEventRemoveHandlep) int bus_watch = -1; _cleanup_(virtDBusVirEventRemoveHandlep) int signal_watch = -1; sigset_t mask; int c; int r; if (geteuid() == 0) { - system_bus = true; + busType = DBUS_BUS_SYSTEM; } else { - system_bus = false; + busType = DBUS_BUS_SESSION; } while ((c = getopt_long(argc, argv, "hc:", options, NULL)) >= 0) { @@ -155,11 +126,11 @@ main(int argc, char *argv[]) return 0; case ARG_SYSTEM: - system_bus = true; + busType = DBUS_BUS_SYSTEM; break; case ARG_SESSION: - system_bus = false; + busType = DBUS_BUS_SESSION; break; default: @@ -174,19 +145,11 @@ main(int argc, char *argv[]) virEventRegisterDefaultImpl(); - r = system_bus ? sd_bus_open_system(&bus) : sd_bus_open_user(&bus); - if (r < 0) { - fprintf(stderr, "Failed to connect to session bus: %s\n", strerror(-r)); + bus = virtDBusConnectionOpen(busType, "org.libvirt"); + if (!bus) return EXIT_FAILURE; - } - - r = sd_bus_request_name(bus, "org.libvirt", 0); - if (r < 0) { - fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); - return EXIT_FAILURE; - } - if (system_bus) { + if (busType == DBUS_BUS_SYSTEM) { drivers = systemDrivers; ndrivers = VIRT_N_ELEMENTS(systemDrivers); } else { @@ -194,10 +157,10 @@ main(int argc, char *argv[]) ndrivers = VIRT_N_ELEMENTS(sessionDrivers); } - connect = calloc(ndrivers + 1, sizeof(virtDBusConnect *)); + connectList = calloc(ndrivers + 1, sizeof(virtDBusConnect *)); for (int i = 0; i < ndrivers; i += 1) { - r = virtDBusConnectNew(&connect[i], bus, + r = virtDBusConnectNew(&connectList[i], &objectList, bus, drivers[i].uri, drivers[i].object); if (r < 0) { fprintf(stderr, "Failed to register libvirt connection.\n"); @@ -205,16 +168,6 @@ main(int argc, char *argv[]) } } - r = virtDBusProcessEvents(bus); - if (r < 0) - return EXIT_FAILURE; - - bus_watch = virEventAddHandle(sd_bus_get_fd(bus), - virtDBusGetLibvirtEvents(bus), - virtDBusHandleBusEvent, - bus, - NULL); - signal_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC); signal_watch = virEventAddHandle(signal_fd, VIR_EVENT_HANDLE_READABLE, @@ -226,9 +179,18 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } - while (loop_status >= 0) + r = virtDBusProcessEvents(bus, &objectList); + if (r < 0) + return EXIT_FAILURE; + + while (loop_status >= 0) { virEventRunDefaultImpl(); + r = virtDBusProcessEvents(bus, &objectList); + if (r < 0) + return EXIT_FAILURE; + } + if (loop_status < 0 && loop_status != -ECANCELED) { fprintf(stderr, "Error: %s\n", strerror(-loop_status)); return EXIT_FAILURE; diff --git a/src/util.c b/src/util.c index b6919fb..6f8b7be 100644 --- a/src/util.c +++ b/src/util.c @@ -6,6 +6,7 @@ #include <libvirt/virterror.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> @@ -14,77 +15,79 @@ #define VIRT_UTIL_FILE_LIMIT 1024 * 1024 int -virtDBusUtilMessageAppendTypedParameters(sd_bus_message *message, - virTypedParameterPtr parameters, - int n_parameters) +virtDBusUtilMessageAppendTypedParameters(virtDBusMessage *msg, + virTypedParameterPtr params, + int nparams) { - int r; + if (virtDBusMessageOpenContainer(msg, DBUS_TYPE_ARRAY, "{sv}") < 0) + return -1; - r = sd_bus_message_open_container(message, 'a', "{sv}"); - if (r < 0) - return r; + for (int i = 0; i < nparams; i += 1) { + if (virtDBusMessageOpenContainer(msg, DBUS_TYPE_DICT_ENTRY, NULL) < 0) + return -1; - for (int i = 0; i < n_parameters; i += 1) { - r = sd_bus_message_open_container(message, SD_BUS_TYPE_DICT_ENTRY, "sv"); - if (r < 0) - return r; + if (virtDBusMessageAppendBasic(msg, DBUS_TYPE_STRING, ¶ms[i].field) < 0) + return -1; - r = sd_bus_message_append(message, "s", parameters[i].field); - if (r < 0) - return r; - - switch (parameters[i].type) { + switch (params[i].type) { case VIR_TYPED_PARAM_INT: - r = sd_bus_message_append(message, "v", "i", parameters[i].value.i); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_INT32, + "i", ¶ms[i].value.i) < 0) + return -1; break; case VIR_TYPED_PARAM_UINT: - r = sd_bus_message_append(message, "v", "u", parameters[i].value.ui); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_UINT32, + "u", ¶ms[i].value.ui) < 0) + return -1; break; case VIR_TYPED_PARAM_LLONG: - r = sd_bus_message_append(message, "v", "x", parameters[i].value.l); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_INT64, + "x", ¶ms[i].value.l) < 0) + return -1; break; case VIR_TYPED_PARAM_ULLONG: - r = sd_bus_message_append(message, "v", "t", parameters[i].value.ul); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_UINT64, + "t", ¶ms[i].value.ul) < 0) + return -1; break; case VIR_TYPED_PARAM_DOUBLE: - r = sd_bus_message_append(message, "v", "d", parameters[i].value.d); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_DOUBLE, + "d", ¶ms[i].value.d) < 0) + return -1; break; case VIR_TYPED_PARAM_BOOLEAN: - r = sd_bus_message_append(message, "v", "b", parameters[i].value.b); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_BOOLEAN, + "b", ¶ms[i].value.b) < 0) + return -1; break; case VIR_TYPED_PARAM_STRING: - r = sd_bus_message_append(message, "v", "s", parameters[i].value.s); + if (virtDBusMessageAppendVariant(msg, DBUS_TYPE_STRING, + "s", ¶ms[i].value.s) < 0) + return -1; break; } - if (r < 0) - return r; - - r = sd_bus_message_close_container(message); - if (r < 0) - return r; + if (virtDBusMessageCloseContainer(msg) < 0) + return -1; } - return sd_bus_message_close_container(message); + return virtDBusMessageCloseContainer(msg); } int -virtDBusUtilSetLastVirtError(sd_bus_error *error) +virtDBusUtilSetLastVirtError(virtDBusMessage *msg) { - virErrorPtr vir_error; + virErrorPtr virError; - vir_error = virGetLastError(); - if (!vir_error) - return 0; + virError = virGetLastError(); - return sd_bus_error_set(error, VIRT_DBUS_ERROR_INTERFACE, vir_error->message); -} + if (!virError) { + return virtDBusMessageSetError(msg, VIRT_DBUS_ERROR_INTERFACE, + "unknown error"); + } -int -virtDBusUtilSetError(sd_bus_error *error, - const char *message) -{ - return sd_bus_error_set(error, VIRT_DBUS_ERROR_INTERFACE, message); + return virtDBusMessageSetError(msg, VIRT_DBUS_ERROR_INTERFACE, "%s", + virError->message); } char * diff --git a/src/util.h b/src/util.h index c42d2cc..16c54df 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,8 @@ #pragma once +#include "dbus.h" + #include <libvirt/libvirt.h> -#include <systemd/sd-bus.h> #define VIRT_DBUS_ERROR_INTERFACE "org.libvirt.Error" @@ -18,16 +19,12 @@ } while(0) int -virtDBusUtilMessageAppendTypedParameters(sd_bus_message *message, - virTypedParameterPtr parameters, - int n_parameters); - -int -virtDBusUtilSetLastVirtError(sd_bus_error *error); +virtDBusUtilMessageAppendTypedParameters(virtDBusMessage *msg, + virTypedParameterPtr params, + int nparams); int -virtDBusUtilSetError(sd_bus_error *error, - const char *message); +virtDBusUtilSetLastVirtError(virtDBusMessage *msg); char * virtDBusUtilReadFile(const char *filename); diff --git a/test/travis-run b/test/travis-run index 47fe70c..7c96609 100755 --- a/test/travis-run +++ b/test/travis-run @@ -22,7 +22,7 @@ sudo chroot "$CHROOT" << EOF set -ex # install build deps apt-get update -apt-get install -y dh-autoreconf pkg-config libvirt-dev libsystemd-dev libdbus-1-dev libtool python3-gi python3-dbus dbus +apt-get install -y dh-autoreconf pkg-config libvirt-dev libdbus-1-dev libtool python3-gi python3-dbus dbus # run build and tests as user chown -R buildd:buildd /build -- 2.14.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list