[PATCH] query logind/elogind for number of users
"Thorsten Kukuk" <[email protected]> ("kukuk") Tue, 7 Mar 2023 11:32:21 +0100
| Newsgroups | gmane.linux.procps.devel |
|---|---|
| Message-ID | <[email protected]> |
--UugvWAfsgieZRqgk Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hi, the utmp implementation of glibc has a Y2038 problem, even on 64bit archictures, due to the 32bit userland compat mode. More background about this can be found on my blog (https://www.thkukuk.de/blog/Y2038_glibc_utmp_64bit/) and a more technical analysis, which applications are affected (https://github.com/thkukuk/utmpx/blob/main/Y2038.md) libprocps uses utmp to count the number of currently logged in users. Since libprocps uses already libsystemd to get similar informations from logind/elogind, I propose the attached patch. This also solves the issue, that, due to the fake entries, counting the users in utmp is not reliable. E.g. xterm creates fake entries, GNOME does not. Same for e.g. screen and tmux, the first one creates fake entries, the second one not. Comments? I know, w is also using utmp, one step after the other. 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) --UugvWAfsgieZRqgk Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-library-use-sd_get_sessions-instead-of-utmp.patch" From 790bc92fe15065cf87b27a95132ba557bffb1af9 Mon Sep 17 00:00:00 2001 From: Thorsten Kukuk <[email protected]> Date: Tue, 7 Mar 2023 10:30:50 +0100 Subject: [PATCH 1/1] library: use sd_get_sessions() instead of utmp The utmp format of glibc is not Y2038 safe, not even on 64bit systems. Query logind/elogind for the number of users if we use libsystemd. Signed-off-by: Thorsten Kukuk <[email protected]> --- library/uptime.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/uptime.c b/library/uptime.c index 16f1b05d..1fc5c347 100644 --- a/library/uptime.c +++ b/library/uptime.c @@ -30,7 +30,15 @@ #include <string.h> #include <time.h> #include <unistd.h> +#if !defined(WITH_SYSTEMD) && !defined(WITH_ELOGIND) #include <utmp.h> +#endif +#ifdef WITH_SYSTEMD +#include <systemd/sd-login.h> +#endif +#ifdef WITH_ELOGIND +#include <elogind/sd-login.h> +#endif #include "misc.h" #include "procps-private.h" @@ -42,6 +50,9 @@ static __thread char shortbuf[256]; static int count_users(void) { +#if defined(WITH_SYSTEMD) || defined(WITH_ELOGIND) + return sd_get_sessions(NULL); +#else int numuser = 0; struct utmp *ut; @@ -53,6 +64,7 @@ static int count_users(void) endutent(); return numuser; +#endif } /* @@ -248,4 +260,3 @@ PROCPS_EXPORT char *procps_uptime_sprint_short(void) } return shortbuf; } - -- 2.39.2 --UugvWAfsgieZRqgk--