]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timesyncd: modernize load_clock_timestamp() a bit
authorLennart Poettering <lennart@poettering.net>
Thu, 19 Jan 2023 19:36:49 +0000 (20:36 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 20 Jan 2023 21:33:05 +0000 (22:33 +0100)
Let's log more.

src/timesync/timesyncd.c

index 709c64375eb928e713f75f39243ec00ee4788766..c34f0dba81bf90f4bed2fece0ef51c2e555f9874 100644 (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;
-
-                /* 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;