]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
busctl: add support for --timeout to monitor verb
authorLuca Boccassi <bluca@debian.org>
Wed, 7 Aug 2024 19:41:20 +0000 (20:41 +0100)
committerLuca Boccassi <bluca@debian.org>
Thu, 8 Aug 2024 08:18:41 +0000 (09:18 +0100)
Useful in scripts when you want to exit successfully after a certain time

man/busctl.xml
shell-completion/zsh/_busctl
src/busctl/busctl.c
test/units/TEST-74-AUX-UTILS.busctl.sh

index 27327ee1928ca76f51bcf66fa946b50c44400be9..0938582c8bd799cdbbc5aca8ec3772128a953a74 100644 (file)
         <term><option>--timeout=<replaceable>SECS</replaceable></option></term>
 
         <listitem>
-          <para>When used with the <command>call</command> 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 <option>--expect-reply=no</option> is used, as the
-          tool does not wait for any reply message then. When not
-          specified or when set to 0, the default of
-          <literal>25s</literal> is assumed.</para>
+          <para>When used with the <command>call</command> command, specifies the maximum time to wait for
+          method call completion. When used with the <command>monitor</command> 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 <option>--expect-reply=no</option> is used,
+          when combined with the <command>call</command> command, as the tool does not wait for any reply
+          message then. When not specified or when set to 0, the default of <literal>25s</literal> is
+          assumed for the <command>call</command> command, and it is disabled for the
+          <command>monitor</command> command.</para>
 
         <xi:include href="version-info.xml" xpointer="v218"/>
         </listitem>
index d8d7adfb4cc5e6ad7d49b3c4ee99dc080038e97c..c8c4f96ed1654b7d890cdbd86749d81b0f2da3a8 100644 (file)
@@ -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'
index 4a5fac76062fe4270f7f0157f601a9294558eb6e..31104f36c5943f41c1973aad6e4b2c4bcc87501e 100644 (file)
@@ -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");
         }
index aaf96d08c1230ae90bbf72d33c472e22090c6ad7..6e997b46c641b24e523ed1b2b2a1e6ef9b722214 100755 (executable)
@@ -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