[libvirt PATCH 1/7] util: Refactor virSystemdHas{Machined,Logind}

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

 



To reduce code duplication both function now use a common
virSystemdHasService helper.

Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx>
---
 src/util/virsystemd.c | 76 ++++++++++++++++++++-----------------------
 1 file changed, 36 insertions(+), 40 deletions(-)

diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index cd4de0eef8..21892aca02 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -141,66 +141,62 @@ void virSystemdHasLogindResetCachedValue(void)
 }
 
 
-/* -2 = machine1 is not supported on this machine
- * -1 = error
- *  0 = machine1 is available
+/**
+ * virSystemdHasService:
+ *
+ * Check whether a DBus @service is enabled and either the service itself or
+ * systemd1 service is registered. If @requireSystemd == true, the systemd1
+ * service has to be registered even if @service is registered.
+ *
+ * Returns
+ *   -2 when service is not supported on this machine
+ *   -1 on error
+ *    0 when service is available
  */
-int
-virSystemdHasMachined(void)
+static int
+virSystemdHasService(const char *service,
+                     bool requireSystemd,
+                     int *cached)
 {
     int ret;
     int val;
 
-    val = g_atomic_int_get(&virSystemdHasMachinedCachedValue);
+    val = g_atomic_int_get(cached);
     if (val != -1)
         return val;
 
-    if ((ret = virGDBusIsServiceEnabled("org.freedesktop.machine1")) < 0) {
+    if ((ret = virGDBusIsServiceEnabled(service)) < 0) {
         if (ret == -2)
-            g_atomic_int_set(&virSystemdHasMachinedCachedValue, -2);
+            g_atomic_int_set(cached, -2);
         return ret;
     }
 
-    if ((ret = virGDBusIsServiceRegistered("org.freedesktop.systemd1")) == -1)
+    if ((ret = virGDBusIsServiceRegistered(service)) == -1)
         return ret;
-    g_atomic_int_set(&virSystemdHasMachinedCachedValue, ret);
+
+    if (requireSystemd || ret == -2) {
+        if ((ret = virGDBusIsServiceRegistered("org.freedesktop.systemd1")) == -1)
+            return ret;
+    }
+
+    g_atomic_int_set(cached, ret);
     return ret;
 }
 
+
 int
-virSystemdHasLogind(void)
+virSystemdHasMachined(void)
 {
-    int ret;
-    int val;
-
-    val = g_atomic_int_get(&virSystemdHasLogindCachedValue);
-    if (val != -1)
-        return val;
-
-    ret = virGDBusIsServiceEnabled("org.freedesktop.login1");
-    if (ret < 0) {
-        if (ret == -2)
-            g_atomic_int_set(&virSystemdHasLogindCachedValue, -2);
-        return ret;
-    }
-
-    /*
-     * Want to use logind if:
-     *   - logind is already running
-     * Or
-     *   - logind is not running, but this is a systemd host
-     *     (rely on dbus activation)
-     */
-    if ((ret = virGDBusIsServiceRegistered("org.freedesktop.login1")) == -1)
-        return ret;
+    return virSystemdHasService("org.freedesktop.machine1", true,
+                                &virSystemdHasMachinedCachedValue);
+}
 
-    if (ret == -2) {
-        if ((ret = virGDBusIsServiceRegistered("org.freedesktop.systemd1")) == -1)
-            return ret;
-    }
 
-    g_atomic_int_set(&virSystemdHasLogindCachedValue, ret);
-    return ret;
+int
+virSystemdHasLogind(void)
+{
+    return virSystemdHasService("org.freedesktop.login1", false,
+                                &virSystemdHasLogindCachedValue);
 }
 
 
-- 
2.43.0
_______________________________________________
Devel mailing list -- devel@xxxxxxxxxxxxxxxxx
To unsubscribe send an email to devel-leave@xxxxxxxxxxxxxxxxx




[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