From: Chris Down Date: Wed, 5 Nov 2025 09:46:40 +0000 (+0800) Subject: systemctl: Support --timestamp for otherwise named properties X-Git-Tag: v259-rc1~155 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ab6925c5e889c238d282fef3061f7651755eb5e;p=thirdparty%2Fsystemd.git systemctl: Support --timestamp for otherwise named properties `systemctl show`'s `--timestamp` flag is supposed to reformat all timestamp-based properties. However, the logic for detecting these properties was incomplete and only checked if the name ended in Timestamp. Expand the check to explicitly include some non-"timestamp" named properties that really are timestamps. Fixes: https://github.com/systemd/systemd/issues/39282 --- diff --git a/src/shared/bus-print-properties.c b/src/shared/bus-print-properties.c index 09986a607af..431b33c100c 100644 --- a/src/shared/bus-print-properties.c +++ b/src/shared/bus-print-properties.c @@ -16,6 +16,14 @@ #include "strv.h" #include "time-util.h" +bool bus_property_is_timestamp(const char *name) { + assert(name); + + /* Trust me, this naming convention is ironclad. Except for these three. Okay four. Well... */ + return endswith(name, "Timestamp") || + STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec"); +} + int bus_print_property_value(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *value) { assert(name); @@ -104,12 +112,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b if (r < 0) return r; - /* Yes, heuristics! But we can change this check - * should it turn out to not be sufficient */ - - if (endswith(name, "Timestamp") || - STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) - + if (bus_property_is_timestamp(name)) bus_print_property_value(name, expected_value, flags, FORMAT_TIMESTAMP(u)); /* Managed OOM pressure default implies "unset" and use the default set in oomd.conf. Without diff --git a/src/shared/bus-print-properties.h b/src/shared/bus-print-properties.h index 507188771ef..be0063a9ed1 100644 --- a/src/shared/bus-print-properties.h +++ b/src/shared/bus-print-properties.h @@ -10,6 +10,8 @@ typedef enum BusPrintPropertyFlags { typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags); +bool bus_property_is_timestamp(const char *name); + int bus_print_property_value(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *value); int bus_print_property_valuef(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *fmt, ...) _printf_(4,5); int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, BusPrintPropertyFlags flags, Set **found_properties); diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index f1c1ab0eff0..2e7d3ffe753 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -1215,7 +1215,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m break; case SD_BUS_TYPE_UINT64: - if (endswith(name, "Timestamp")) { + if (bus_property_is_timestamp(name)) { uint64_t timestamp; r = sd_bus_message_read_basic(m, bus_type, ×tamp); diff --git a/test/units/TEST-26-SYSTEMCTL.sh b/test/units/TEST-26-SYSTEMCTL.sh index d5f9c02a03c..0bfffa54350 100755 --- a/test/units/TEST-26-SYSTEMCTL.sh +++ b/test/units/TEST-26-SYSTEMCTL.sh @@ -287,6 +287,59 @@ for value in pretty us µs utc us+utc µs+utc; do systemctl show -P KernelTimestamp --timestamp="$value" done +# --timestamp with timer properties (issue #39282) +TIMER1="timestamp-test1-$RANDOM.timer" +SERVICE1="${TIMER1%.timer}.service" +cat >"/run/systemd/system/$SERVICE1" <"/run/systemd/system/$TIMER1" < with --timestamp=unix, got: $output" >&2 + exit 1 +fi + +systemctl stop "$TIMER1" +rm -f "/run/systemd/system/$TIMER1" "/run/systemd/system/$SERVICE1" + +TIMER2="timestamp-test2-$RANDOM.timer" +SERVICE2="${TIMER2%.timer}.service" +cat >"/run/systemd/system/$SERVICE2" <"/run/systemd/system/$TIMER2" < with --timestamp=unix, got: $output" >&2 + exit 1 +fi + +systemctl stop "$TIMER2" +rm -f "/run/systemd/system/$TIMER2" "/run/systemd/system/$SERVICE2" +systemctl daemon-reload + # set-default/get-default test_get_set_default() { target="$(systemctl get-default "$@")"