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 <kukuk@suse.com>
This commit is contained in:
Thorsten Kukuk 2023-03-07 10:30:50 +01:00 committed by Craig Small
parent 1f910eeb0e
commit 6d5d4d2804

View File

@ -31,6 +31,12 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <utmp.h> #include <utmp.h>
#ifdef WITH_SYSTEMD
#include <systemd/sd-login.h>
#endif
#ifdef WITH_ELOGIND
#include <elogind/sd-login.h>
#endif
#include "misc.h" #include "misc.h"
#include "procps-private.h" #include "procps-private.h"
@ -45,6 +51,13 @@ static int count_users(void)
int numuser = 0; int numuser = 0;
struct utmp *ut; struct utmp *ut;
#if defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)
numuser = sd_get_sessions(NULL);
if (numuser >= 0 || numuser != ENOENT)
return numuser;
#endif
setutent(); setutent();
while ((ut = getutent())) { while ((ut = getutent())) {
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0')) if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
@ -136,8 +149,13 @@ PROCPS_EXPORT char *procps_uptime_sprint(void)
users = count_users(); users = count_users();
procps_loadavg(&av1, &av5, &av15); procps_loadavg(&av1, &av5, &av15);
pos += sprintf(upbuf + pos, "%2d %s, load average: %.2f, %.2f, %.2f", if (users < 0)
users, users > 1 ? "users" : "user", pos += sprintf(upbuf + pos, " ? ");
else
pos += sprintf(upbuf + pos, "%2d ", users);
pos += sprintf(upbuf + pos, "%s, load average: %.2f, %.2f, %.2f",
users > 1 ? "users" : "user",
av1, av5, av15); av1, av5, av15);
return upbuf; return upbuf;
@ -248,4 +266,3 @@ PROCPS_EXPORT char *procps_uptime_sprint_short(void)
} }
return shortbuf; return shortbuf;
} }