[PATCH 1/2] virgdbus: Introduce virGDBusHasSessionBus()

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

 



This is just like virGDBusHasSystemBus() except it checks for the
session bus instead of the system one.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virgdbus.c      | 50 ++++++++++++++++++++++++++++++++++++----
 src/util/virgdbus.h      |  3 +++
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bd9564f2a7..30a9f806f0 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2496,6 +2496,7 @@ virGDBusCloseSystemBus;
 virGDBusErrorIsUnknownMethod;
 virGDBusGetSessionBus;
 virGDBusGetSystemBus;
+virGDBusHasSessionBus;
 virGDBusHasSystemBus;
 virGDBusIsServiceEnabled;
 virGDBusIsServiceRegistered;
diff --git a/src/util/virgdbus.c b/src/util/virgdbus.c
index 71ca3cd43b..dd3c6d77b4 100644
--- a/src/util/virgdbus.c
+++ b/src/util/virgdbus.c
@@ -113,8 +113,8 @@ virGDBusSessionBusInit(void)
 }
 
 
-GDBusConnection *
-virGDBusGetSessionBus(void)
+static GDBusConnection *
+virGDBusGetSessionBusInternal(void)
 {
     if (virOnce(&sessionOnce, virGDBusSessionBusInit) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -122,14 +122,56 @@ virGDBusGetSessionBus(void)
         return NULL;
     }
 
-    if (!sessionBus) {
+    return sessionBus;
+}
+
+
+GDBusConnection *
+virGDBusGetSessionBus(void)
+{
+    GDBusConnection *bus = virGDBusGetSessionBusInternal();
+
+    if (!bus) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unable to get session bus connection: %1$s"),
                        sessionError->message);
         return NULL;
     }
 
-    return sessionBus;
+    return bus;
+}
+
+
+/**
+ * virGDBusHasSessionBus:
+ *
+ * Check if DBus session bus is running. This does not imply that we have
+ * a connection. DBus might be running and refusing connections due to its
+ * client limit. The latter must be treated as a fatal error.
+ *
+ * Return false if dbus is not available, true if probably available.
+ */
+bool
+virGDBusHasSessionBus(void)
+{
+    g_autofree char *name = NULL;
+
+    if (virGDBusGetSessionBusInternal())
+        return true;
+
+    if (!g_dbus_error_is_remote_error(sessionError))
+        return false;
+
+    name = g_dbus_error_get_remote_error(sessionError);
+
+    if (name &&
+        (STREQ(name, "org.freedesktop.DBus.Error.FileNotFound") ||
+         STREQ(name, "org.freedesktop.DBus.Error.NoServer"))) {
+        VIR_DEBUG("System bus not available: %s", NULLSTR(sessionError->message));
+        return false;
+    }
+
+    return true;
 }
 
 
diff --git a/src/util/virgdbus.h b/src/util/virgdbus.h
index ca7073e27c..dfe6138112 100644
--- a/src/util/virgdbus.h
+++ b/src/util/virgdbus.h
@@ -36,6 +36,9 @@ virGDBusGetSystemBus(void);
 GDBusConnection *
 virGDBusGetSessionBus(void);
 
+bool
+virGDBusHasSessionBus(void);
+
 bool
 virGDBusHasSystemBus(void);
 
-- 
2.45.3




[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]

  Powered by Linux