]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
update-utmp: modernize get_startup_monotonic_time()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Apr 2023 19:26:19 +0000 (04:26 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 24 May 2023 11:06:07 +0000 (20:06 +0900)
This also downgrade error level, as the failure will be ignored.

No functional change, just refactoring.

src/update-utmp/update-utmp.c

index 8f2fa17e6bf0d1064c50f638bc7204867b6f3ec1..7121f304c759e939e946fa2b73d6825967205cce 100644 (file)
@@ -45,20 +45,23 @@ static void context_clear(Context *c) {
 #endif
 }
 
-static usec_t get_startup_monotonic_time(Context *c) {
+static int get_startup_monotonic_time(Context *c, usec_t *ret) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        usec_t t = 0;
         int r;
 
         assert(c);
+        assert(ret);
+
+        r = bus_get_property_trivial(
+                        c->bus,
+                        bus_systemd_mgr,
+                        "UserspaceTimestampMonotonic",
+                        &error,
+                        't', ret);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to get timestamp, ignoring: %s", bus_error_message(&error, r));
 
-        r = bus_get_property_trivial(c->bus, bus_systemd_mgr, "UserspaceTimestampMonotonic", &error, 't', &t);
-        if (r < 0) {
-                log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
-                return 0;
-        }
-
-        return t;
+        return 0;
 }
 
 static int get_current_runlevel(Context *c) {
@@ -106,7 +109,7 @@ static int get_current_runlevel(Context *c) {
 
 static int on_reboot(int argc, char *argv[], void *userdata) {
         Context *c = ASSERT_PTR(userdata);
-        usec_t t, boottime;
+        usec_t t = 0, boottime;
         int r = 0, q;
 
         /* We finished start-up, so let's write the utmp record and send the audit msg. */
@@ -118,8 +121,8 @@ static int on_reboot(int argc, char *argv[], void *userdata) {
                         r = log_error_errno(errno, "Failed to send audit message: %m");
 #endif
 
-        /* If this call fails it will return 0, which utmp_put_reboot() will then fix to the current time. */
-        t = get_startup_monotonic_time(c);
+        /* If this call fails, then utmp_put_reboot() will fix to the current time. */
+        (void) get_startup_monotonic_time(c, &t);
         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