dbus-monitor: Add an option to break the loop on first match
Is your feature request related to a problem? Please describe.
Currently, dbus-monitor
isn't exactly script-friendly. It is interesting to watch what's going on interactively and then interrupt it when we're done. However, from within scripts, we usually don't want to keep monitoring forever but exit when certain conditions are met.
Describe the solution you'd like
An extra option akin to --chgexit
of watch
(with a better name) would be very convenient to have. It should break the dbus_connection_read_write_dispatch()
loop as soon as the first match arrives and return 0.
Describe alternatives you've considered
One way around in shell scripts is to pipe the output to a while
loop and do something like this:
dbus-monitor ... \
| while read -r line
do case $line in
*PropertiesChanged) break;;
*) continue
esac
done
It's obviously not ideal, as a second call to write()
is needed in order to trigger SIGPIPE
, else it hangs indefinitely. With GNU grep -m 1
, it's possible to stop processing after the first match but it's the same story here as well, not to mention it's not portable. pkill dbus-monitor
and the likes work, though, because they close the write end and cause read()
to encounter EOF
, thus the whole pipeline is closed immediately.
Additional context
On entrance, the following two signals are always listed:
#type timestamp serial sender destination path interface member
# in_reply_to
sig 1670589765.544835 2 org.freedesktop.DBus :1.720 /org/freedesktop/DBus org.freedesktop.DBus NameAcquired
sig 1670589765.544993 4 org.freedesktop.DBus :1.720 /org/freedesktop/DBus org.freedesktop.DBus NameLost
They should be ignored so as to not end the loop prematurely.