From: Zbigniew Jędrzejewski-Szmek Date: Wed, 5 Jun 2024 11:21:45 +0000 (+0200) Subject: manager: apply clock epoch on updates too X-Git-Tag: v257-rc1~1133^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20fa2bb8939cd258e4f09fb3466d7a8a85af049d;p=thirdparty%2Fsystemd.git manager: apply clock epoch on updates too If we're updating on a system with an invalid clock, and we're installing a newer system version with a higher update, adjust the clock. This way the invariant that the clock is always later than max(compile time, timestamp file, other timestamp file) is maintained. Also, adjust the wording of messages. When /usr/lib/clock-epoch was introduced, "build time" stopped being acurate. Just say "epoch" instead. --- diff --git a/src/core/clock-warp.c b/src/core/clock-warp.c index eff06ea1b8e..8957d61b8b7 100644 --- a/src/core/clock-warp.c +++ b/src/core/clock-warp.c @@ -18,7 +18,7 @@ int clock_reset_timewarp(void) { return RET_NERRNO(settimeofday(NULL, &tz)); } -void clock_apply_epoch(void) { +void clock_apply_epoch(bool allow_backwards) { usec_t epoch_usec = 0, timesyncd_usec = 0; struct stat st; int r; @@ -47,7 +47,9 @@ void clock_apply_epoch(void) { if (now_usec < epoch_usec) advance = true; - else if (CLOCK_VALID_RANGE_USEC_MAX > 0 && now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX)) + else if (CLOCK_VALID_RANGE_USEC_MAX > 0 && + now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX) && + allow_backwards) advance = false; else return; /* Nothing to do. */ diff --git a/src/core/clock-warp.h b/src/core/clock-warp.h index 78a8093b744..402c7eee042 100644 --- a/src/core/clock-warp.h +++ b/src/core/clock-warp.h @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include + int clock_reset_timewarp(void); -void clock_apply_epoch(void); +void clock_apply_epoch(bool allow_backwards); diff --git a/src/core/main.c b/src/core/main.c index 23b6a1119a0..3422bac1555 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1671,7 +1671,7 @@ static int become_shutdown(int objective, int retval) { return -errno; } -static void initialize_clock(void) { +static void initialize_clock_timewarp(void) { int r; /* This is called very early on, before we parse the kernel command line or otherwise figure out why @@ -1683,7 +1683,7 @@ static void initialize_clock(void) { /* The very first call of settimeofday() also does a time warp in the kernel. * * In the rtc-in-local time mode, we set the kernel's timezone, and rely on external tools to - * take care of maintaining the RTC and do all adjustments. This matches the behavior of + * take care of maintaining the RTC and do all adjustments. This matches the behavior of * Windows, which leaves the RTC alone if the registry tells that the RTC runs in UTC. */ r = clock_set_timezone(&min); @@ -1705,13 +1705,11 @@ static void initialize_clock(void) { * time concepts will be treated as UTC that way. */ (void) clock_reset_timewarp(); - - clock_apply_epoch(); } static void apply_clock_update(void) { - /* This is called later than initialize_clock(), i.e. after we parsed configuration files/kernel - * command line and such. */ + /* This is called later than clock_apply_epoch(), i.e. after we have parsed + * configuration files/kernel command line and such. */ if (arg_clock_usec == 0) return; @@ -3038,7 +3036,9 @@ int main(int argc, char *argv[]) { } if (!skip_setup) - initialize_clock(); + initialize_clock_timewarp(); + + clock_apply_epoch(/* allow_backwards= */ !skip_setup); /* Set the default for later on, but don't actually open the logs like this for * now. Note that if we are transitioning from the initrd there might still be