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

[libvirt] [PATCH 2/3] util: cache the result of whether logind is available



Similar to how we cache the availability of machined.

Signed-off-by: Ján Tomko <jtomko redhat com>
---
 src/libvirt_private.syms  |  1 +
 src/util/virsystemd.c     | 20 ++++++++++++++++++--
 src/util/virsystemdpriv.h |  1 +
 tests/virsystemdtest.c    |  3 +++
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7a3feb8efa..5940342087 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3135,6 +3135,7 @@ virSystemdCanSuspend;
 virSystemdCreateMachine;
 virSystemdGetActivation;
 virSystemdGetMachineNameByPID;
+virSystemdHasLogindResetCachedValue;
 virSystemdHasMachinedResetCachedValue;
 virSystemdMakeScopeName;
 virSystemdMakeSliceName;
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index b401eda6a2..66d0b7bd53 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -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;
 }
 
diff --git a/src/util/virsystemdpriv.h b/src/util/virsystemdpriv.h
index a49c91e649..736c53d3fa 100644
--- a/src/util/virsystemdpriv.h
+++ b/src/util/virsystemdpriv.h
@@ -28,3 +28,4 @@
 #include "virsystemd.h"
 
 void virSystemdHasMachinedResetCachedValue(void);
+void virSystemdHasLogindResetCachedValue(void);
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index 340b038095..2dafce2764 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -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);
-- 
2.19.2


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