diff mbox series

[v6,4/8] util: add dbus helper unit

Message ID 20191211134506.1803403-5-marcandre.lureau@redhat.com (mailing list archive)
State New, archived
Headers show
Series Add dbus-vmstate | expand

Commit Message

Marc-André Lureau Dec. 11, 2019, 1:45 p.m. UTC
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 MAINTAINERS         |  2 ++
 include/qemu/dbus.h | 18 +++++++++++++++
 util/Makefile.objs  |  3 +++
 util/dbus.c         | 55 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+)
 create mode 100644 include/qemu/dbus.h
 create mode 100644 util/dbus.c

Comments

Daniel P. Berrangé Dec. 12, 2019, 11:38 a.m. UTC | #1
On Wed, Dec 11, 2019 at 05:45:02PM +0400, Marc-André Lureau wrote:
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  MAINTAINERS         |  2 ++
>  include/qemu/dbus.h | 18 +++++++++++++++
>  util/Makefile.objs  |  3 +++
>  util/dbus.c         | 55 +++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 78 insertions(+)
>  create mode 100644 include/qemu/dbus.h
>  create mode 100644 util/dbus.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 19faa0e868..f08fb4f24e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -2202,6 +2202,8 @@ F: qapi/migration.json
>  D-Bus
>  M: Marc-André Lureau <marcandre.lureau@redhat.com>
>  S: Maintained
> +F: util/dbus.c
> +F: include/qemu/dbus.h
>  F: docs/interop/dbus.rst
>  
>  Seccomp
> diff --git a/include/qemu/dbus.h b/include/qemu/dbus.h
> new file mode 100644
> index 0000000000..efd74bef96
> --- /dev/null
> +++ b/include/qemu/dbus.h
> @@ -0,0 +1,18 @@
> +/*
> + * Helpers for using D-Bus
> + *
> + * Copyright (C) 2019 Red Hat, Inc.
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2.  See
> + * the COPYING file in the top-level directory.
> + */
> +
> +#ifndef DBUS_H
> +#define DBUS_H
> +
> +#include <gio/gio.h>
> +
> +GStrv qemu_dbus_get_queued_owners(GDBusConnection *connection,
> +                                  const char *name);
> +
> +#endif /* DBUS_H */
> diff --git a/util/Makefile.objs b/util/Makefile.objs
> index df124af1c5..80b76352cd 100644
> --- a/util/Makefile.objs
> +++ b/util/Makefile.objs
> @@ -55,5 +55,8 @@ util-obj-$(CONFIG_INOTIFY1) += filemonitor-inotify.o
>  util-obj-$(CONFIG_LINUX) += vfio-helpers.o
>  util-obj-$(CONFIG_POSIX) += drm.o
>  util-obj-y += guest-random.o
> +util-obj-$(CONFIG_GIO) += dbus.o
> +dbus.o-cflags = $(GIO_CFLAGS)
> +dbus.o-libs = $(GIO_LIBS)
>  
>  stub-obj-y += filemonitor-stub.o
> diff --git a/util/dbus.c b/util/dbus.c
> new file mode 100644
> index 0000000000..bb51870e54
> --- /dev/null
> +++ b/util/dbus.c
> @@ -0,0 +1,55 @@
> +/*
> + * Helpers for using D-Bus
> + *
> + * Copyright (C) 2019 Red Hat, Inc.
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2.  See
> + * the COPYING file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qemu/dbus.h"
> +#include "qemu/error-report.h"
> +
> +/*
> + * qemu_dbus_get_queued_owners() - return the list of queued unique names
> + * @connection: A GDBusConnection
> + * @name: a service name
> + *
> + * Return: a GStrv of unique names, or NULL on failure.
> + */
> +GStrv
> +qemu_dbus_get_queued_owners(GDBusConnection *connection, const char *name)
> +{
> +    g_autoptr(GDBusProxy) proxy = NULL;
> +    g_autoptr(GVariant) result = NULL;
> +    g_autoptr(GVariant) child = NULL;
> +    g_autoptr(GError) err = NULL;
> +
> +    proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE, NULL,
> +                                  "org.freedesktop.DBus",
> +                                  "/org/freedesktop/DBus",
> +                                  "org.freedesktop.DBus",
> +                                  NULL, &err);
> +    if (!proxy) {
> +        error_report("Failed to create DBus proxy: %s", err->message);
> +        return NULL;
> +    }
> +
> +    result = g_dbus_proxy_call_sync(proxy, "ListQueuedOwners",
> +                                    g_variant_new("(s)", name),
> +                                    G_DBUS_CALL_FLAGS_NO_AUTO_START,
> +                                    -1, NULL, &err);
> +    if (!result) {
> +        if (g_error_matches(err,
> +                            G_DBUS_ERROR,
> +                            G_DBUS_ERROR_NAME_HAS_NO_OWNER)) {
> +            return g_new0(char *, 1);
> +        }
> +        error_report("Failed to call ListQueuedOwners: %s", err->message);

IMHO, helper code shouldn't be callling error_report, there should be
an Error **errp output parameter.

> +        return NULL;
> +    }
> +
> +    child = g_variant_get_child_value(result, 0);
> +    return g_variant_dup_strv(child, NULL);
> +}

Regards,
Daniel
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 19faa0e868..f08fb4f24e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2202,6 +2202,8 @@  F: qapi/migration.json
 D-Bus
 M: Marc-André Lureau <marcandre.lureau@redhat.com>
 S: Maintained
+F: util/dbus.c
+F: include/qemu/dbus.h
 F: docs/interop/dbus.rst
 
 Seccomp
diff --git a/include/qemu/dbus.h b/include/qemu/dbus.h
new file mode 100644
index 0000000000..efd74bef96
--- /dev/null
+++ b/include/qemu/dbus.h
@@ -0,0 +1,18 @@ 
+/*
+ * Helpers for using D-Bus
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef DBUS_H
+#define DBUS_H
+
+#include <gio/gio.h>
+
+GStrv qemu_dbus_get_queued_owners(GDBusConnection *connection,
+                                  const char *name);
+
+#endif /* DBUS_H */
diff --git a/util/Makefile.objs b/util/Makefile.objs
index df124af1c5..80b76352cd 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -55,5 +55,8 @@  util-obj-$(CONFIG_INOTIFY1) += filemonitor-inotify.o
 util-obj-$(CONFIG_LINUX) += vfio-helpers.o
 util-obj-$(CONFIG_POSIX) += drm.o
 util-obj-y += guest-random.o
+util-obj-$(CONFIG_GIO) += dbus.o
+dbus.o-cflags = $(GIO_CFLAGS)
+dbus.o-libs = $(GIO_LIBS)
 
 stub-obj-y += filemonitor-stub.o
diff --git a/util/dbus.c b/util/dbus.c
new file mode 100644
index 0000000000..bb51870e54
--- /dev/null
+++ b/util/dbus.c
@@ -0,0 +1,55 @@ 
+/*
+ * Helpers for using D-Bus
+ *
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/dbus.h"
+#include "qemu/error-report.h"
+
+/*
+ * qemu_dbus_get_queued_owners() - return the list of queued unique names
+ * @connection: A GDBusConnection
+ * @name: a service name
+ *
+ * Return: a GStrv of unique names, or NULL on failure.
+ */
+GStrv
+qemu_dbus_get_queued_owners(GDBusConnection *connection, const char *name)
+{
+    g_autoptr(GDBusProxy) proxy = NULL;
+    g_autoptr(GVariant) result = NULL;
+    g_autoptr(GVariant) child = NULL;
+    g_autoptr(GError) err = NULL;
+
+    proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE, NULL,
+                                  "org.freedesktop.DBus",
+                                  "/org/freedesktop/DBus",
+                                  "org.freedesktop.DBus",
+                                  NULL, &err);
+    if (!proxy) {
+        error_report("Failed to create DBus proxy: %s", err->message);
+        return NULL;
+    }
+
+    result = g_dbus_proxy_call_sync(proxy, "ListQueuedOwners",
+                                    g_variant_new("(s)", name),
+                                    G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                    -1, NULL, &err);
+    if (!result) {
+        if (g_error_matches(err,
+                            G_DBUS_ERROR,
+                            G_DBUS_ERROR_NAME_HAS_NO_OWNER)) {
+            return g_new0(char *, 1);
+        }
+        error_report("Failed to call ListQueuedOwners: %s", err->message);
+        return NULL;
+    }
+
+    child = g_variant_get_child_value(result, 0);
+    return g_variant_dup_strv(child, NULL);
+}