From: Paul Eggert Date: Sat, 12 Aug 2023 22:46:43 +0000 (-0700) Subject: boot-time: do not depend on timespec_get X-Git-Tag: v1.0~921 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=19cb99e334bb741a719aa2ee7b3ebf8bfa8bf02b;p=gnulib.git boot-time: do not depend on timespec_get This is for Emacs, which does not use timespec_get now and which likes to minimize dependencies. Also, treat musl libc like recent glibc, and fix a timespec_get return value typo. * lib/boot-time-aux.h (get_linux_uptime): Assume musl libc supports CLOCK_BOOTTIME. (get_linux_boot_time_final_fallback): Likewise for musl libc and CLOCK_REALTIME. Do not rely on the timespec_get module, to break the dependency. Consider 0 to be a failure return from timespec_get. Fall back on gettimeofday if timespec_get does not exist. * modules/boot-time (Depends-on): Remove timespec_get. --- diff --git a/ChangeLog b/ChangeLog index 4330034fbe..03120ec167 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2023-08-12 Paul Eggert + + boot-time: do not depend on timespec_get + This is for Emacs, which does not use timespec_get now + and which likes to minimize dependencies. + Also, treat musl libc like recent glibc, + and fix a timespec_get return value typo. + * lib/boot-time-aux.h (get_linux_uptime): + Assume musl libc supports CLOCK_BOOTTIME. + (get_linux_boot_time_final_fallback): + Likewise for musl libc and CLOCK_REALTIME. + Do not rely on the timespec_get module, to break the dependency. + Consider 0 to be a failure return from timespec_get. + Fall back on gettimeofday if timespec_get does not exist. + * modules/boot-time (Depends-on): Remove timespec_get. + 2023-08-12 Bruno Haible readutmp, boot-time: Fix parsing of /proc/uptime. diff --git a/lib/boot-time-aux.h b/lib/boot-time-aux.h index 4c5474c6c8..d980682a59 100644 --- a/lib/boot-time-aux.h +++ b/lib/boot-time-aux.h @@ -27,9 +27,9 @@ static int get_linux_uptime (struct timespec *p_uptime) { /* The clock_gettime facility returns the uptime with a resolution of 1 µsec. - It is available with glibc >= 2.14. In glibc < 2.17 it required linking - with librt. */ -# if (__GLIBC__ + (__GLIBC_MINOR__ >= 17) > 2) || defined __ANDROID__ + It is available with glibc >= 2.14, Android, or musl libc. + In glibc < 2.17 it required linking with librt. */ +# if !defined __GLIBC__ || 2 < __GLIBC__ + (17 <= __GLIBC_MINOR__) if (clock_gettime (CLOCK_BOOTTIME, p_uptime) >= 0) return 0; # endif @@ -115,21 +115,33 @@ get_linux_boot_time_final_fallback (struct timespec *p_boot_time) if (get_linux_uptime (&uptime) >= 0) { struct timespec result; - /* equivalent to: - if (clock_gettime (CLOCK_REALTIME, &result) >= 0) +# if !defined __GLIBC__ || 2 < __GLIBC__ + (16 <= __GLIBC_MINOR__) + /* Better than: + if (0 <= clock_gettime (CLOCK_REALTIME, &result)) + because timespec_get does not need -lrt in glibc 2.16. */ - if (timespec_get (&result, TIME_UTC) >= 0) + if (! timespec_get (&result, TIME_UTC)) + return -1; +# else + /* Fall back on lower-res approach that does not need -lrt. + This is good enough; on these hosts UPTIME is even lower-res. */ + struct timeval tv; + int r = gettimeofday (&tv, NULL); + if (r < 0) + return r; + result.tv_sec = tv.tv_sec; + result.tv_nsec = tv.tv_usec * 1000; +# endif + + if (result.tv_nsec < uptime.tv_nsec) { - 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; - *p_boot_time = result; - return 0; + result.tv_nsec += 1000000000; + result.tv_sec -= 1; } + result.tv_sec -= uptime.tv_sec; + result.tv_nsec -= uptime.tv_nsec; + *p_boot_time = result; + return 0; } return -1; } diff --git a/modules/boot-time b/modules/boot-time index 8890406d6c..749b9014cd 100644 --- a/modules/boot-time +++ b/modules/boot-time @@ -16,7 +16,6 @@ idx stat-time stdbool time-h -timespec_get unlocked-io-internal configure.ac: