]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
clock-util: read timestamp from /usr/lib/clock-epoch
authorDaniel Mack <daniel@zonque.org>
Fri, 28 Aug 2020 14:14:12 +0000 (16:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 28 Aug 2020 16:58:22 +0000 (18:58 +0200)
On systems without an RTC, systemd currently sets the clock to a
compile-time epoch value, derived from the NEWS file in the
repository. This is not ideal as the initial clock hence depends
on the last time systemd was built, not when the image was compiled.

Let's provide a different way here and look at `/usr/lib/clock-epoch`.
If that file exists, it's timestamp for the last modification will be
used instead of the compile-time default.

src/shared/clock-util.c

index 32cce1e1098a4528128feae14e0c75830071c857..0f0dd8281ca6fcdea3098a5ac1c4e722715bd307 100644 (file)
@@ -142,15 +142,25 @@ int clock_reset_timewarp(void) {
         return 0;
 }
 
-#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
+#define EPOCH_FILE "/usr/lib/clock-epoch"
 
 int clock_apply_epoch(void) {
+        struct stat st;
         struct timespec ts;
+        usec_t epoch_usec;
 
-        if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
+        if (stat(EPOCH_FILE, &st) < 0) {
+                if (errno != ENOENT)
+                        log_warning_errno(errno, "Cannot stat %s: %m\n", EPOCH_FILE);
+
+                epoch_usec = ((usec_t) TIME_EPOCH * USEC_PER_SEC);
+        } else
+                epoch_usec = timespec_load(&st.st_mtim);
+
+        if (now(CLOCK_REALTIME) >= epoch_usec)
                 return 0;
 
-        if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
+        if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, epoch_usec)) < 0)
                 return -errno;
 
         return 1;