From: Bruno Haible Date: Fri, 11 Aug 2023 00:31:30 +0000 (+0200) Subject: readutmp: Tweak the Android specific addition. X-Git-Tag: v1.0~938 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=ea7db75976ea4027021263af1fccdd94b7a68915;p=gnulib.git readutmp: Tweak the Android specific addition. * lib/readutmp.c (read_utmp_from_file) [__ANDROID__]: Don't fake a BOOT_TIME entry if the options request to omit it or if a BOOT_TIME entry is already present. --- diff --git a/ChangeLog b/ChangeLog index e5efb85c5f..979761ad16 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-08-10 Bruno Haible + + readutmp: Tweak the Android specific addition. + * lib/readutmp.c (read_utmp_from_file) [__ANDROID__]: Don't fake a + BOOT_TIME entry if the options request to omit it or if a BOOT_TIME + entry is already present. + 2023-08-10 Bruno Haible readutmp: Fix the boot time returned on Raspbian. diff --git a/lib/readutmp.c b/lib/readutmp.c index e383531474..eabd3e7678 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -526,30 +526,45 @@ read_utmp_from_file (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf, to system files. Therefore use the kernel's uptime counter, although it produces wrong values after the date has been bumped in the running system. */ - { - struct timespec uptime; - if (get_linux_uptime (&uptime) >= 0) - { - struct timespec result; - if (clock_gettime (CLOCK_REALTIME, &result) >= 0) - { - if (result.tv_nsec < uptime.tv_nsec) - { - result.tv_nsec += 1000000000; - result.tv_sec -= 1; - } - result.tv_sec -= uptime.tv_sec; - result.tv_nsec -= uptime.tv_nsec; - struct timespec boot_time = result; - a = add_utmp (a, options, - "reboot", strlen ("reboot"), - "", 0, - "", 0, - "", 0, - 0, BOOT_TIME, boot_time, 0, 0, 0); - } - } - } + if ((options & (READ_UTMP_USER_PROCESS | READ_UTMP_NO_BOOT_TIME)) == 0 + && strcmp (file, UTMP_FILE) == 0) + { + bool have_boot_time = false; + for (idx_t i = 0; i < a.filled; i++) + { + struct gl_utmp *ut = &a.utmp[i]; + if (UT_TYPE_BOOT_TIME (ut)) + { + have_boot_time = true; + break; + } + } + if (!have_boot_time) + { + struct timespec uptime; + if (get_linux_uptime (&uptime) >= 0) + { + struct timespec result; + if (clock_gettime (CLOCK_REALTIME, &result) >= 0) + { + if (result.tv_nsec < uptime.tv_nsec) + { + result.tv_nsec += 1000000000; + result.tv_sec -= 1; + } + result.tv_sec -= uptime.tv_sec; + result.tv_nsec -= uptime.tv_nsec; + struct timespec boot_time = result; + a = add_utmp (a, options, + "reboot", strlen ("reboot"), + "", 0, + "", 0, + "", 0, + 0, BOOT_TIME, boot_time, 0, 0, 0); + } + } + } + } # endif # else /* old FreeBSD, OpenBSD, HP-UX */