Skip to content

Commit 1637701

Browse files
poetteringbluca
authored andcommitted
timedate: handle gracefully if RTC lost time because of power loss
Apparently some RTC drivers return EINVAL in that case when we try to read it. Handle that reasonably gracefully. Fixes: #31854 (cherry picked from commit 5c81de9) (cherry picked from commit b858433)
1 parent 7e20ff0 commit 1637701

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/shared/clock-util.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ int clock_get_hwclock(struct tm *tm) {
2727
if (fd < 0)
2828
return -errno;
2929

30-
/* This leaves the timezone fields of struct tm
31-
* uninitialized! */
30+
/* This leaves the timezone fields of struct tm uninitialized! */
3231
if (ioctl(fd, RTC_RD_TIME, tm) < 0)
33-
return -errno;
32+
/* Some drivers return -EINVAL in case the time could not be kept, i.e. power loss
33+
* happened. Let's turn that into a clearly recognizable error */
34+
return errno == EINVAL ? -ENODATA : -errno;
3435

3536
/* We don't know daylight saving, so we reset this in order not
3637
* to confuse mktime(). */

src/timedate/timedated.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ static int property_get_rtc_time(
591591
log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
592592
else if (r == -ENOENT)
593593
log_debug("/dev/rtc not found.");
594+
else if (r == -ENODATA)
595+
log_debug("/dev/rtc has no valid time, power loss probably occurred?");
594596
else if (r < 0)
595597
return sd_bus_error_set_errnof(error, r, "Failed to read RTC: %m");
596598
else

0 commit comments

Comments
 (0)