timesyncd: modernize load_clock_timestamp() a bit

Let's log more.
This commit is contained in:
Lennart Poettering
2023-01-20 22:33:05 +01:00
parent 29920c5b1f
commit 84447fe79d
+26 -29
View File
@@ -27,48 +27,45 @@ static int load_clock_timestamp(uid_t uid, gid_t gid) {
_cleanup_close_ int fd = -EBADF;
int r;
/* Let's try to make sure that the clock is always
* monotonically increasing, by saving the clock whenever we
* have a new NTP time, or when we shut down, and restoring it
* when we start again. This is particularly helpful on
* systems lacking a battery backed RTC. We also will adjust
* the time to at least the build time of systemd. */
/* Let's try to make sure that the clock is always monotonically increasing, by saving the clock
* whenever we have a new NTP time, or when we shut down, and restoring it when we start again. This
* is particularly helpful on systems lacking a battery backed RTC. We also will adjust the time to
* at least the build time of systemd. */
fd = open(CLOCK_FILE, O_RDWR|O_CLOEXEC, 0644);
if (fd >= 0) {
struct stat st;
usec_t stamp;
if (fd < 0) {
if (errno != ENOENT)
log_debug_errno(errno, "Unable to open timestamp file '" CLOCK_FILE "', ignoring: %m");
/* check if the recorded time is later than the compiled-in one */
if (fstat(fd, &st) >= 0) {
stamp = timespec_load(&st.st_mtim);
if (stamp > min)
min = stamp;
}
if (geteuid() == 0) {
/* Try to fix the access mode, so that we can still
touch the file after dropping privileges */
r = fchmod_and_chown(fd, 0644, uid, gid);
if (r < 0)
log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
}
} else {
r = mkdir_safe_label(STATE_DIR, 0755, uid, gid,
MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
if (r < 0) {
if (r < 0)
log_debug_errno(r, "Failed to create state directory, ignoring: %m");
goto settime;
}
/* create stamp file with the compiled-in date */
r = touch_file(CLOCK_FILE, /* parents= */ false, min, uid, gid, 0644);
if (r < 0)
log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE);
} else {
struct stat st;
usec_t stamp;
/* check if the recorded time is later than the compiled-in one */
if (fstat(fd, &st) < 0)
return log_error_errno(errno, "Unable to stat timestamp file '" CLOCK_FILE "': %m");
stamp = timespec_load(&st.st_mtim);
if (stamp > min)
min = stamp;
/* Try to fix the access mode, so that we can still touch the file after dropping
* privileges */
r = fchmod_and_chown(fd, 0644, uid, gid);
if (r < 0)
log_full_errno(ERRNO_IS_PRIVILEGE(r) ? LOG_DEBUG : LOG_WARNING, r,
"Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
}
settime:
ct = now(CLOCK_REALTIME);
if (ct > min)
return 0;