From: Luca Boccassi Date: Wed, 7 Aug 2024 19:41:20 +0000 (+0100) Subject: busctl: add support for --timeout to monitor verb X-Git-Tag: v257-rc1~717^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=989e843e7543b21b91de4368da44692d674722a5;p=thirdparty%2Fsystemd.git busctl: add support for --timeout to monitor verb Useful in scripts when you want to exit successfully after a certain time --- diff --git a/man/busctl.xml b/man/busctl.xml index 27327ee1928..0938582c8bd 100644 --- a/man/busctl.xml +++ b/man/busctl.xml @@ -392,15 +392,15 @@ - When used with the call command, - specifies the maximum time to wait for method call - completion. If no time unit is specified, assumes - seconds. The usual other units are understood, too (ms, us, - s, min, h, d, w, month, y). Note that this timeout does not - apply if is used, as the - tool does not wait for any reply message then. When not - specified or when set to 0, the default of - 25s is assumed. + When used with the call command, specifies the maximum time to wait for + method call completion. When used with the monitor command, since version v257, + specifies the maximum time to wait for messages before automatically exiting. If no time unit is + specified, assumes seconds. The usual other units are understood, too (ms, us, s, min, h, d, w, + month, y). Note that this timeout does not apply if is used, + when combined with the call command, as the tool does not wait for any reply + message then. When not specified or when set to 0, the default of 25s is + assumed for the call command, and it is disabled for the + monitor command. diff --git a/shell-completion/zsh/_busctl b/shell-completion/zsh/_busctl index d8d7adfb4cc..c8c4f96ed16 100644 --- a/shell-completion/zsh/_busctl +++ b/shell-completion/zsh/_busctl @@ -282,6 +282,6 @@ _arguments \ '--expect-reply=[Expect a method call reply]:boolean:(1 0)' \ '--auto-start=[Auto-start destination service]:boolean:(1 0)' \ '--allow-interactive-authorization=[Allow interactive authorization for operation]:boolean:(1 0)' \ - '--timeout=[Maximum time to wait for method call completion]:timeout (seconds)' \ + '--timeout=[Maximum time to wait for method call completion and monitoring]:timeout (seconds)' \ '--augment-creds=[Extend credential data with data read from /proc/$PID]:boolean:(1 0)' \ '*::busctl command:_busctl_commands' diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 4a5fac76062..31104f36c59 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1373,7 +1373,12 @@ static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f if (r > 0) continue; - r = sd_bus_wait(bus, UINT64_MAX); + r = sd_bus_wait(bus, arg_timeout > 0 ? arg_timeout : UINT64_MAX); + if (r == 0 && arg_timeout > 0) { + if (!arg_quiet && arg_json_format_flags == SD_JSON_FORMAT_OFF) + log_info("Timed out waiting for messages, exiting."); + return 0; + } if (r < 0) return log_error_errno(r, "Failed to wait for bus: %m"); } diff --git a/test/units/TEST-74-AUX-UTILS.busctl.sh b/test/units/TEST-74-AUX-UTILS.busctl.sh index aaf96d08c12..6e997b46c64 100755 --- a/test/units/TEST-74-AUX-UTILS.busctl.sh +++ b/test/units/TEST-74-AUX-UTILS.busctl.sh @@ -108,3 +108,5 @@ busctl get-property -j \ # Invalid argument (! busctl set-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager \ KExecWatchdogUSec t "foo") + +busctl --quiet --timeout 1 --match "interface=org.freedesktop.systemd1.Manager" monitor >/dev/null