From 4dafc5d610bb9d242b348cb4539f7ab0926acda2 Mon Sep 17 00:00:00 2001 From: Evan Goode Date: Tue, 23 Jan 2024 18:38:28 +0000 Subject: [PATCH] needs-restarting: get systemd boot time from UnitsLoadStartTimestamp For https://github.com/rpm-software-management/dnf5/issues/1184 Following the reproducers in that issue, UnitsLoadStartTimestamp seems to consistently have the correct boot time even when UserspaceTimestamp is incorrect due to `set-local-rtc 1`. The two times are within a few seconds of each other. I have no idea why UnitsLoadStartTimestamp is correct, maybe systemd reads /etc/adjtime and adjusts the clock after setting UserspaceTimestamp but before setting UnitsLoadStartTimestamp. --- .../needs_restarting_plugin/needs_restarting.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp b/dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp index 280fae545..d179d580e 100644 --- a/dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp +++ b/dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp @@ -90,10 +90,11 @@ void NeedsRestartingCommand::configure() { time_t NeedsRestartingCommand::get_boot_time(Context & ctx) { // We have three sources from which to derive the boot time. These values // vary depending on containerization, existing of a Real Time Clock, etc: - // - UserspaceTimestamp property on /org/freedesktop/systemd1 + // - UnitsLoadStartTimestamp property on /org/freedesktop/systemd1 // The start time of the service manager, according to systemd itself. - // Works unless the system was not booted with systemd, such as in (most) - // containers. + // Seems to be more reliable than UserspaceTimestamp when the RTC is + // in local time. Works unless the system was not booted with systemd, + // such as in (most) containers. // - st_mtime of /proc/1 // Reflects the time the first process was run after booting. This // works for all known cases except (1) machines without a RTC---they @@ -118,7 +119,7 @@ time_t NeedsRestartingCommand::get_boot_time(Context & ctx) { auto proxy = sdbus::createProxy(SYSTEMD_DESTINATION_NAME, SYSTEMD_OBJECT_PATH); const uint64_t systemd_boot_time_us = - proxy->getProperty("UserspaceTimestamp").onInterface(SYSTEMD_MANAGER_INTERFACE); + proxy->getProperty("UnitsLoadStartTimestamp").onInterface(SYSTEMD_MANAGER_INTERFACE); const time_t systemd_boot_time = static_cast(systemd_boot_time_us) / (1000L * 1000L);