[procps] Re: [PATCH] improve check if systemd is running

  • From: "Thorsten Kukuk" <dmarc-noreply@xxxxxxxxxxxxx> ("kukuk")
  • To: procps@xxxxxxxxxxxxx
  • Date: Tue, 30 May 2023 15:28:50 +0000


Hi,

What about this patch? I haven't seen any comment on it?

Thanks,
   Thorsten

On Fri, Apr 14, Thorsten Kukuk wrote:

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 <kukuk@xxxxxxxx>

-- 
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)
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 <kukuk@xxxxxxxx>
---
 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

Other related posts: