[PATCH] improve check if systemd is running

"Thorsten Kukuk" <[email protected]> ("kukuk") Fri, 14 Apr 2023 08:23:34 +0000
Newsgroups gmane.linux.procps.devel
Message-ID <[email protected]>
--qMm9M+Fa2AknHoGS
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


Hi,

in the first patch we assumed, that sd_get_sessions will return ENOENT
if systemd is not running. But the non-existing of the directory does
not mean that systemd is not running, but only that there is no session.

Using sd_booted() is the recommended and more robust way to test if
systemd is in use.

  Thorsten

-- 
Thorsten Kukuk, Distinguished Engineer, Senior Architect, Future Technologies
SUSE Software Solutions Germany GmbH, Frankenstraße 146, 90461 Nuernberg, Germany
Managing Director: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)

--qMm9M+Fa2AknHoGS
Content-Type: text/x-patch; charset=us-ascii
Content-Disposition: attachment; filename="0001-library-use-sd_booted-if-systemd-is-in-use.patch"

library: use sd_booted() if systemd is in use

sd_get_sessions() will not return ENOENT as error, as this only means
that there is no session, not that systemd is not used. Use the
recommended sd_booted() function for this.

Signed-off-by: Thorsten Kukuk <[email protected]>
---
 library/uptime.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/library/uptime.c b/library/uptime.c
index bb9344a2..1826343d 100644
--- a/library/uptime.c
+++ b/library/uptime.c
@@ -32,9 +32,11 @@
 #include <unistd.h>
 #include <utmp.h>
 #ifdef WITH_SYSTEMD
+#include <systemd/sd-daemon.h>
 #include <systemd/sd-login.h>
 #endif
 #ifdef WITH_ELOGIND
+#include <elogind/sd-daemon.h>
 #include <elogind/sd-login.h>
 #endif
 
@@ -52,10 +54,8 @@ static int count_users(void)
     struct utmp *ut;
 
 #if defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)
-    numuser = sd_get_sessions(NULL);
-
-    if (numuser >= 0 || numuser != ENOENT)
-      return numuser;
+    if (sd_booted() > 0)
+      return sd_get_sessions(NULL);
 #endif
 
     setutent();
-- 
2.40.0


--qMm9M+Fa2AknHoGS--