]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: cache the result of whether logind is available
authorJán Tomko <jtomko@redhat.com>
Tue, 13 Aug 2019 14:34:30 +0000 (16:34 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 14 Aug 2019 14:22:13 +0000 (16:22 +0200)
Similar to how we cache the availability of machined.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virsystemd.c
src/util/virsystemdpriv.h
tests/virsystemdtest.c

index 7a3feb8efa0bebc8dbc31b0b4c4409f9d1f8dc52..594034208727cde569b21297c887c38f0ffa0a9a 100644 (file)
@@ -3135,6 +3135,7 @@ virSystemdCanSuspend;
 virSystemdCreateMachine;
 virSystemdGetActivation;
 virSystemdGetMachineNameByPID;
+virSystemdHasLogindResetCachedValue;
 virSystemdHasMachinedResetCachedValue;
 virSystemdMakeScopeName;
 virSystemdMakeSliceName;
index b401eda6a207509032d626572fbafaaf68d2b7d1..66d0b7bd53a89cfe3f26f55fbde1b8311e0d1157 100644 (file)
@@ -140,6 +140,7 @@ char *virSystemdMakeSliceName(const char *partition)
 }
 
 static int virSystemdHasMachinedCachedValue = -1;
+static int virSystemdHasLogindCachedValue = -1;
 
 /* Reset the cache from tests for testing the underlying dbus calls
  * as well */
@@ -148,6 +149,12 @@ void virSystemdHasMachinedResetCachedValue(void)
     virSystemdHasMachinedCachedValue = -1;
 }
 
+void virSystemdHasLogindResetCachedValue(void)
+{
+    virSystemdHasLogindCachedValue = -1;
+}
+
+
 /* -2 = machine1 is not supported on this machine
  * -1 = error
  *  0 = machine1 is available
@@ -178,14 +185,23 @@ static int
 virSystemdHasLogind(void)
 {
     int ret;
+    int val;
+
+    val = virAtomicIntGet(&virSystemdHasLogindCachedValue);
+    if (val != -1)
+        return val;
 
     ret = virDBusIsServiceEnabled("org.freedesktop.login1");
-    if (ret < 0)
+    if (ret < 0) {
+        if (ret == -2)
+            virAtomicIntSet(&virSystemdHasLogindCachedValue, -2);
         return ret;
+    }
 
-    if ((ret = virDBusIsServiceRegistered("org.freedesktop.login1")) < 0)
+    if ((ret = virDBusIsServiceRegistered("org.freedesktop.login1")) == -1)
         return ret;
 
+    virAtomicIntSet(&virSystemdHasLogindCachedValue, ret);
     return ret;
 }
 
index a49c91e649a82dcd55fb3c60e6fa3c4fe2db4afe..736c53d3fadbdb6bf09f0c5fd67ee4e2695160cd 100644 (file)
@@ -28,3 +28,4 @@
 #include "virsystemd.h"
 
 void virSystemdHasMachinedResetCachedValue(void);
+void virSystemdHasLogindResetCachedValue(void);
index 340b038095b60392913d74f458afd557c0d106e0..2dafce2764d0d5a93dab1e630718fc6c76a9b9cc 100644 (file)
@@ -751,12 +751,15 @@ mymain(void)
         }; \
         if (virTestRun("Test " name " ", testPMSupportHelper, &data) < 0) \
             ret = -1; \
+        virSystemdHasLogindResetCachedValue(); \
         if (virTestRun("Test " name " no systemd ", \
                        testPMSupportHelperNoSystemd, &data) < 0) \
             ret = -1; \
+        virSystemdHasLogindResetCachedValue(); \
         if (virTestRun("Test systemd " name " not running ", \
                        testPMSupportSystemdNotRunning, &data) < 0) \
             ret = -1; \
+        virSystemdHasLogindResetCachedValue(); \
     } while (0)
 
     TESTS_PM_SUPPORT_HELPER("canSuspend", &virSystemdCanSuspend);