diff options
author | caoxia2008cxx <78151946+caoxia2008cxx@users.noreply.github.com> | 2021-04-29 11:05:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-29 11:05:01 +0200 |
commit | f813b62316395205f4c744cde43885081b5f88ae (patch) | |
tree | 762f54088037c3f67602cf23b84a72a1419684f3 /src/update-utmp | |
parent | Merge pull request #19449 from yuwata/network-downgrade-log-level (diff) | |
download | systemd-f813b62316395205f4c744cde43885081b5f88ae.tar.xz systemd-f813b62316395205f4c744cde43885081b5f88ae.zip |
set boot time from monotonic time (#19444)
utmp: calculate boot timestamp from monotonic timestamp instead of realtime timestamp
Diffstat (limited to 'src/update-utmp')
-rw-r--r-- | src/update-utmp/update-utmp.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index 38df4cddf0..0995e3557e 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -44,7 +44,7 @@ static void context_clear(Context *c) { #endif } -static usec_t get_startup_time(Context *c) { +static usec_t get_startup_monotonic_time(Context *c) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; usec_t t = 0; int r; @@ -56,7 +56,7 @@ static usec_t get_startup_time(Context *c) { "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "UserspaceTimestamp", + "UserspaceTimestampMonotonic", &error, 't', &t); if (r < 0) { @@ -115,6 +115,7 @@ static int get_current_runlevel(Context *c) { static int on_reboot(Context *c) { int r = 0, q; usec_t t; + usec_t boottime; assert(c); @@ -130,9 +131,15 @@ static int on_reboot(Context *c) { /* If this call fails it will return 0, which * utmp_put_reboot() will then fix to the current time */ - t = get_startup_time(c); - - q = utmp_put_reboot(t); + t = get_startup_monotonic_time(c); + boottime = map_clock_usec(t, CLOCK_MONOTONIC, CLOCK_REALTIME); + /* We query the recorded monotonic time here (instead of the system clock CLOCK_REALTIME), + * even though we actually want the system clock time. That's because there's a likely + * chance that the system clock wasn't set right during early boot. By manually converting + * the monotonic clock to the system clock here we can compensate + * for incorrectly set clocks during early boot. */ + + q = utmp_put_reboot(boottime); if (q < 0) r = log_error_errno(q, "Failed to write utmp record: %m"); |