From 1b4470bd0abf6845a066cb77ca4a1b9beb6d3414 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 8 Aug 2023 23:27:38 +0200 Subject: [PATCH] readutmp: Use classical implementation for files != /var/run/utmp. * lib/readutmp.c (read_utmp_from_systemd): Renamed from read_utmp [READUTMP_USE_SYSTEMD]. Remove file argument. (read_utmp): Call it when the file argument is "/var/run/utmp". --- ChangeLog | 7 +++++++ lib/readutmp.c | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 62e281fa97..199c818c18 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-08-08 Bruno Haible + + readutmp: Use classical implementation for files != /var/run/utmp. + * lib/readutmp.c (read_utmp_from_systemd): Renamed from read_utmp + [READUTMP_USE_SYSTEMD]. Remove file argument. + (read_utmp): Call it when the file argument is "/var/run/utmp". + 2023-08-08 Bruno Haible readutmp: Get the boot time with higher precision. diff --git a/lib/readutmp.c b/lib/readutmp.c index f7e43eb4a6..66b25bc7ae 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -439,17 +439,9 @@ guess_pty_name (uid_t uid, const struct timespec at) return NULL; } -int -read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, - int options) +static int +read_utmp_from_systemd (idx_t *n_entries, STRUCT_UTMP **utmp_buf, int options) { - /* The current implementation can imitate only UTMP_FILE. */ - if (strcmp (file, UTMP_FILE) != 0) - { - errno = ENOTSUP; - return -1; - } - /* Fill entries, simulating what a utmp file would contain. */ struct utmp_alloc a = {0}; @@ -602,7 +594,9 @@ read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, return 0; } -# elif defined UTMP_NAME_FUNCTION /* glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, IRIX, Solaris, Cygwin, Android */ +# endif + +# if defined UTMP_NAME_FUNCTION /* glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, IRIX, Solaris, Cygwin, Android */ # if !HAVE_UTMPX_H && HAVE_UTMP_H && !HAVE_DECL_GETUTENT struct utmp *getutent (void); @@ -612,6 +606,12 @@ int read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, int options) { +# if READUTMP_USE_SYSTEMD + if (strcmp (file, UTMP_FILE) == 0) + /* Imitate reading UTMP_FILE, using systemd and Linux APIs. */ + return read_utmp_from_systemd (n_entries, utmp_buf, options); +# endif + /* Ignore the return value for now. Solaris' utmpname returns 1 upon success -- which is contrary to what the GNU libc version does. In addition, older GNU libc -- 2.39.5