summaryrefslogtreecommitdiffstats
path: root/src/timesync/timesyncd.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-01-19 20:36:49 +0100
committerLennart Poettering <lennart@poettering.net>2023-01-20 22:33:05 +0100
commit84447fe79d75801317a6a65694667f48c08dde98 (patch)
tree0aa1302ce7b49dfeef12fe38b598936e65d9abdc /src/timesync/timesyncd.c
parenttimesyncd: write structured log messages whenever we bump the clock based on ... (diff)
downloadsystemd-84447fe79d75801317a6a65694667f48c08dde98.tar.xz
systemd-84447fe79d75801317a6a65694667f48c08dde98.zip
timesyncd: modernize load_clock_timestamp() a bit
Let's log more.
Diffstat (limited to '')
-rw-r--r--src/timesync/timesyncd.c55
1 files changed, 26 insertions, 29 deletions
diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
index 709c64375e..c34f0dba81 100644
--- a/src/timesync/timesyncd.c
+++ b/src/timesync/timesyncd.c
@@ -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;
-
- /* 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);
- }
+ if (fd < 0) {
+ if (errno != ENOENT)
+ log_debug_errno(errno, "Unable to open timestamp file '" CLOCK_FILE "', ignoring: %m");
- } 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;