]> Savannah Git Hosting - gnulib.git/commitdiff
boot-time: do not depend on timespec_get
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2023 22:46:43 +0000 (15:46 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2023 22:47:20 +0000 (15:47 -0700)
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.

ChangeLog
lib/boot-time-aux.h
modules/boot-time

index 4330034fbe7ea050c8b75001d2a8b5d14ed88169..03120ec167c169b1c68fba61bf9e153c6d409d4f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2023-08-12  Paul Eggert  <eggert@cs.ucla.edu>
+
+       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  <bruno@clisp.org>
 
        readutmp, boot-time: Fix parsing of /proc/uptime.
index 4c5474c6c8d4c9662ac34725abf60780a132e047..d980682a5999fb4c81d9610376c57d16f4f7da5b 100644 (file)
@@ -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;
 }
index 8890406d6c470a15fb5d97e730416c96c4f9076e..749b9014cdee453168fe1a0fb20a7b16f19a2a05 100644 (file)
@@ -16,7 +16,6 @@ idx
 stat-time
 stdbool
 time-h
-timespec_get
 unlocked-io-internal
 
 configure.ac: