From f3067de49182e38bdc100e938a20822f88615590 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 11 Aug 2023 15:22:43 +0200 Subject: [PATCH] readutmp: Fix the boot time returned on Minix. * lib/readutmp.c (read_utmp_from_file): [__minix] When the time of the BOOT_TIME entry is very close to the Epoch, replace it with the time from the "run-level m" entry. --- ChangeLog | 7 +++++++ lib/readutmp.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index dfca665e9d..7f308b7473 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-08-11 Bruno Haible + + readutmp: Fix the boot time returned on Minix. + * lib/readutmp.c (read_utmp_from_file): [__minix] When the time of the + BOOT_TIME entry is very close to the Epoch, replace it with the time + from the "run-level m" entry. + 2023-08-11 Bruno Haible readutmp: On Cygwin and Windows, return the boot time. diff --git a/lib/readutmp.c b/lib/readutmp.c index c19ec2f6b1..40cee0e58c 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -373,7 +373,7 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, SET_UTMP_ENT (); -# if defined __linux__ && !defined __ANDROID__ +# if (defined __linux__ && !defined __ANDROID__) || defined __minix bool file_is_utmp = (strcmp (file, UTMP_FILE) == 0); /* Timestamp of the "runlevel" entry, if any. */ struct timespec runlevel_ts = {0}; @@ -461,6 +461,12 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, && memcmp (UT_USER (ut), "runlevel", strlen ("runlevel") + 1) == 0 && memcmp (ut->ut_line, "~", strlen ("~") + 1) == 0) runlevel_ts = ts; +# endif +# if defined __minix + if (file_is_utmp + && UT_USER (ut)[0] == '\0' + && memcmp (ut->ut_line, "run-level ", strlen ("run-level ")) == 0) + runlevel_ts = ts; # endif } @@ -571,6 +577,30 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, } # endif +# if defined __minix + /* On Minix, during boot, + 1. an entry gets written into /var/run/utmp, with ut_type = BOOT_TIME, + ut_user = "", ut_line = "system boot", time = 1970-01-01 00:00:00, + 2. an entry gets written into /var/run/utmp, with + ut_user = "", ut_line = "run-level m", time = correct value. + In this case, copy the time from the "run-level m" entry to the + "system boot" entry. */ + if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 + && file_is_utmp) + { + for (idx_t i = 0; i < a.filled; i++) + { + struct gl_utmp *ut = &a.utmp[i]; + if (UT_TYPE_BOOT_TIME (ut)) + { + if (ut->ut_ts.tv_sec <= 60 && runlevel_ts.tv_sec != 0) + ut->ut_ts = runlevel_ts; + break; + } + } + } +# endif + # else /* old FreeBSD, OpenBSD, HP-UX */ FILE *f = fopen (file, "re"); -- 2.39.5