resolved: apply epoch to system time from PID 1
authorLennart Poettering <lennart@poettering.net>
Wed, 3 Feb 2016 17:28:40 +0000 (18:28 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 3 Feb 2016 22:58:24 +0000 (23:58 +0100)
For use in timesyncd we already defined a compile-time "epoch" value, which is based on the mtime of the NEWS file, and
specifies a point in time we know lies in the past at runtime. timesyncd uses this to filter out nonsensical timestamp
file data, and bump the system clock to a time that is after the build time of systemd. This patch adds similar bumping
code to earliest PID 1 initialization, so that the system never continues operation with a clock that is in the 1970ies
or even 1930s.

src/basic/clock-util.c
src/basic/clock-util.h
src/core/main.c

index 05788a3..c64b2e5 100644 (file)
@@ -146,3 +146,17 @@ int clock_reset_timewarp(void) {
 
         return 0;
 }
+
+#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
+
+int clock_apply_epoch(void) {
+        struct timespec ts;
+
+        if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
+                return 0;
+
+        if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
+                return -errno;
+
+        return 1;
+}
index fef2d47..09d4675 100644 (file)
@@ -28,3 +28,4 @@ int clock_set_timezone(int *min);
 int clock_reset_timewarp(void);
 int clock_get_hwclock(struct tm *tm);
 int clock_set_hwclock(const struct tm *tm);
+int clock_apply_epoch(void);
index 99ef723..fb27e89 100644 (file)
@@ -1424,8 +1424,14 @@ int main(int argc, char *argv[]) {
                                  * saving time change. All kernel local time concepts will be treated
                                  * as UTC that way.
                                  */
-                                clock_reset_timewarp();
+                                (void) clock_reset_timewarp();
                         }
+
+                        r = clock_apply_epoch();
+                        if (r < 0)
+                                log_error_errno(r, "Current system time is before build time, but cannot correct: %m");
+                        else if (r > 0)
+                                log_info("System time before build time, advancing clock.");
                 }
 
                 /* Set the default for later on, but don't actually