dbus.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Helpers for using D-Bus
  3. *
  4. * Copyright (C) 2019 Red Hat, Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/dbus.h"
  11. #include "qemu/error-report.h"
  12. #include "qapi/error.h"
  13. /*
  14. * qemu_dbus_get_queued_owners() - return the list of queued unique names
  15. * @connection: A GDBusConnection
  16. * @name: a service name
  17. *
  18. * Return: a GStrv of unique names, or NULL on failure.
  19. */
  20. GStrv
  21. qemu_dbus_get_queued_owners(GDBusConnection *connection, const char *name,
  22. Error **errp)
  23. {
  24. g_autoptr(GDBusProxy) proxy = NULL;
  25. g_autoptr(GVariant) result = NULL;
  26. g_autoptr(GVariant) child = NULL;
  27. g_autoptr(GError) err = NULL;
  28. proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE, NULL,
  29. "org.freedesktop.DBus",
  30. "/org/freedesktop/DBus",
  31. "org.freedesktop.DBus",
  32. NULL, &err);
  33. if (!proxy) {
  34. error_setg(errp, "Failed to create DBus proxy: %s", err->message);
  35. return NULL;
  36. }
  37. result = g_dbus_proxy_call_sync(proxy, "ListQueuedOwners",
  38. g_variant_new("(s)", name),
  39. G_DBUS_CALL_FLAGS_NO_AUTO_START,
  40. -1, NULL, &err);
  41. if (!result) {
  42. if (g_error_matches(err,
  43. G_DBUS_ERROR,
  44. G_DBUS_ERROR_NAME_HAS_NO_OWNER)) {
  45. return g_new0(char *, 1);
  46. }
  47. error_setg(errp, "Failed to call ListQueuedOwners: %s", err->message);
  48. return NULL;
  49. }
  50. child = g_variant_get_child_value(result, 0);
  51. return g_variant_dup_strv(child, NULL);
  52. }