From: Zbigniew Jędrzejewski-Szmek Date: Wed, 12 Jun 2024 08:08:23 +0000 (+0200) Subject: Move two functions only used in pid1 from libshared to the binary X-Git-Tag: v257-rc1~1133^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d47aa8c0435ad7bcd45aea3a091c22027fc0aa8;p=thirdparty%2Fsystemd.git Move two functions only used in pid1 from libshared to the binary Anything that is part of src/shared becomes part of the shared library, which is wasteful, because the library is linked into almost all binaries. The library is also forms a separate link unit, so this prevents the function from being inlined or such. Also, move logging into the function. Now that it's not a library function, there is no reason not to do that. --- diff --git a/src/core/clock-warp.c b/src/core/clock-warp.c new file mode 100644 index 00000000000..d4dcc0440f1 --- /dev/null +++ b/src/core/clock-warp.c @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include +#include + +#include "clock-util.h" +#include "clock-warp.h" +#include "errno-util.h" + +int clock_reset_timewarp(void) { + static const struct timezone tz = { + .tz_minuteswest = 0, + .tz_dsttime = 0, /* DST_NONE */ + }; + + /* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time + * warping is sealed and all later calls behave as expected. */ + return RET_NERRNO(settimeofday(NULL, &tz)); +} + +void clock_apply_epoch(void) { + usec_t epoch_usec; + struct stat st; + int r; + + r = RET_NERRNO(stat(EPOCH_CLOCK_FILE, &st)); + if (r < 0) { + if (r != -ENOENT) + log_warning_errno(r, "Cannot stat " EPOCH_CLOCK_FILE ": %m"); + + epoch_usec = (usec_t) TIME_EPOCH * USEC_PER_SEC; + } else + epoch_usec = timespec_load(&st.st_mtim); + + usec_t now_usec = now(CLOCK_REALTIME); + bool advance; + + 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)) + advance = false; + else + return; /* Nothing to do. */ + + r = RET_NERRNO(clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(epoch_usec))); + if (r < 0 && advance) + return (void) log_error_errno(r, "Current system time is before build time, but cannot correct: %m"); + else if (r < 0) + return (void) log_error_errno(r, "Current system time is further ahead than %s after build time, but cannot correct: %m", + FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); + else if (advance) + log_info("System time was before build time, advanced clock."); + else + log_info("System time was further ahead than %s after build time, reset clock to build time.", + FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); +} diff --git a/src/core/clock-warp.h b/src/core/clock-warp.h new file mode 100644 index 00000000000..78a8093b744 --- /dev/null +++ b/src/core/clock-warp.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +int clock_reset_timewarp(void); +void clock_apply_epoch(void); diff --git a/src/core/main.c b/src/core/main.c index 4b8a315d860..23b6a1119a0 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -30,6 +30,7 @@ #include "cgroup-util.h" #include "chase.h" #include "clock-util.h" +#include "clock-warp.h" #include "conf-parser.h" #include "confidential-virt.h" #include "copy.h" @@ -1705,18 +1706,7 @@ static void initialize_clock(void) { */ (void) clock_reset_timewarp(); - ClockChangeDirection change_dir; - r = clock_apply_epoch(&change_dir); - if (r > 0 && change_dir == CLOCK_CHANGE_FORWARD) - log_info("System time before build time, advancing clock."); - else if (r > 0 && change_dir == CLOCK_CHANGE_BACKWARD) - log_info("System time is further ahead than %s after build time, resetting clock to build time.", - FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); - else if (r < 0 && change_dir == CLOCK_CHANGE_FORWARD) - log_error_errno(r, "Current system time is before build time, but cannot correct: %m"); - else if (r < 0 && change_dir == CLOCK_CHANGE_BACKWARD) - log_error_errno(r, "Current system time is further ahead %s after build time, but cannot correct: %m", - FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); + clock_apply_epoch(); } static void apply_clock_update(void) { diff --git a/src/core/meson.build b/src/core/meson.build index 7a2012a372d..af9928a5994 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -143,6 +143,7 @@ core_includes = [includes, include_directories('.')] systemd_sources = files( 'main.c', 'crash-handler.c', + 'clock-warp.c', ) systemd_executor_sources = files( diff --git a/src/shared/clock-util.c b/src/shared/clock-util.c index 57041d0e767..963a4fd82d9 100644 --- a/src/shared/clock-util.c +++ b/src/shared/clock-util.c @@ -1,10 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include #include -#include -#include -#include #include #include #include @@ -120,47 +116,3 @@ int clock_set_timezone(int *ret_minutesdelta) { return 0; } - -int clock_reset_timewarp(void) { - static const struct timezone tz = { - .tz_minuteswest = 0, - .tz_dsttime = 0, /* DST_NONE */ - }; - - /* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time - * warping is sealed and all later calls behave as expected. */ - return RET_NERRNO(settimeofday(NULL, &tz)); -} - -int clock_apply_epoch(ClockChangeDirection *ret_attempted_change) { - usec_t epoch_usec, now_usec; - struct stat st; - - /* NB: we update *ret_attempted_change in *all* cases, both - * on success and failure, to indicate what we intended to do! */ - - assert(ret_attempted_change); - - if (stat(EPOCH_CLOCK_FILE, &st) < 0) { - if (errno != ENOENT) - log_warning_errno(errno, "Cannot stat " EPOCH_CLOCK_FILE ": %m"); - - epoch_usec = (usec_t) TIME_EPOCH * USEC_PER_SEC; - } else - epoch_usec = timespec_load(&st.st_mtim); - - now_usec = now(CLOCK_REALTIME); - if (now_usec < epoch_usec) - *ret_attempted_change = CLOCK_CHANGE_FORWARD; - else if (CLOCK_VALID_RANGE_USEC_MAX > 0 && now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX)) - *ret_attempted_change = CLOCK_CHANGE_BACKWARD; - else { - *ret_attempted_change = CLOCK_CHANGE_NOOP; - return 0; - } - - if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(epoch_usec)) < 0) - return -errno; - - return 1; -} diff --git a/src/shared/clock-util.h b/src/shared/clock-util.h index 730d1cebe8a..e224a4741c6 100644 --- a/src/shared/clock-util.h +++ b/src/shared/clock-util.h @@ -1,23 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include #include -typedef enum ClockChangeDirection { - CLOCK_CHANGE_NOOP, - CLOCK_CHANGE_FORWARD, - CLOCK_CHANGE_BACKWARD, - _CLOCK_CHANGE_MAX, - _CLOCK_CHANGE_INVALID = -EINVAL, -} ClockChangeDirection; - int clock_is_localtime(const char* adjtime_path); int clock_set_timezone(int *ret_minutesdelta); -int clock_reset_timewarp(void); int clock_get_hwclock(struct tm *tm); int clock_set_hwclock(const struct tm *tm); -int clock_apply_epoch(ClockChangeDirection *ret_attempted_change); #define EPOCH_CLOCK_FILE "/usr/lib/clock-epoch" #define TIMESYNCD_CLOCK_FILE_DIR "/var/lib/systemd/timesync/"