]> Savannah Git Hosting - gnulib.git/commitdiff
readutmp: Fix the boot time returned on Minix.
authorBruno Haible <bruno@clisp.org>
Fri, 11 Aug 2023 13:22:43 +0000 (15:22 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 11 Aug 2023 13:22:43 +0000 (15:22 +0200)
* 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
lib/readutmp.c

index dfca665e9d51ae63e5a18af4537698aad9635985..7f308b747323cb79c1b2351f36d5fc2b1f33b7b8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-08-11  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        readutmp: On Cygwin and Windows, return the boot time.
index c19ec2f6b158b18f639279563e34debf7e5c9468..40cee0e58cc042b77da38b6cc5af53774e484d2e 100644 (file)
@@ -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");