]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
timedate: handle gracefully if RTC lost time because of power loss
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Apr 2024 17:20:40 +0000 (19:20 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 20 Apr 2024 00:15:07 +0000 (09:15 +0900)
Apparently some RTC drivers return EINVAL in that case when we try to
read it. Handle that reasonably gracefully.

Fixes: #31854
src/shared/clock-util.c
src/timedate/timedated.c

index b0cbe3007202325f5847034da9f760dc960f7aea..37d02325b749ea61bc03473e59190abda8fb86ed 100644 (file)
@@ -27,10 +27,11 @@ int clock_get_hwclock(struct tm *tm) {
         if (fd < 0)
                 return -errno;
 
-        /* This leaves the timezone fields of struct tm
-         * uninitialized! */
+        /* This leaves the timezone fields of struct tm uninitialized! */
         if (ioctl(fd, RTC_RD_TIME, tm) < 0)
-                return -errno;
+                /* Some drivers return -EINVAL in case the time could not be kept, i.e. power loss
+                 * happened. Let's turn that into a clearly recognizable error */
+                return errno == EINVAL ? -ENODATA : -errno;
 
         /* We don't know daylight saving, so we reset this in order not
          * to confuse mktime(). */
index 4840ba47b1c1b386b8623a2ec0e5c19fe79b6e4b..e3b4367ec0536180d4e6dc5920ab62b47e4dc72c 100644 (file)
@@ -596,6 +596,8 @@ static int property_get_rtc_time(
                 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
         else if (r == -ENOENT)
                 log_debug("/dev/rtc not found.");
+        else if (r == -ENODATA)
+                log_debug("/dev/rtc has no valid time, power loss probably occurred?");
         else if (r < 0)
                 return sd_bus_error_set_errnof(error, r, "Failed to read RTC: %m");
         else