Query the status of other process using D-Bus
Simon McVittie
smcv at collabora.com
Fri May 26 12:07:40 UTC 2017
On Fri, 26 May 2017 at 16:24:02 +0530, prashantkumar dhotre wrote:
> On Fri, May 26, 2017 at 3:51 PM, Ravi Kumar Kandati
> <Ravikumar.Kandati at lnttechservices.com> wrote:
> > For one use case, I have two processes (written in C) P1, P2 communicating
> > using D-Bus in a system.
> >
> > Is it possible to check whether P2 is started(i.e connected with D-Bus to
> > receive messages) or not while P1 is starting.
>
> check dbus_bus_name_has_owner()
It's usually best not to use that function or its equivalents in other
libraries. Asking "is this running?" before communicating has an inherent
time-of-check/time-of-use[1] bug: what if P2 starts just after dbus-daemon
has replied "no, not available", or P2 exits or crashes just after
dbus-daemon has replied "yes, available"?
Better than that is to just try to communicate with P2 and see what happens.
If it is available, you'll communicate with it. If it isn't, you'll get
a (recoverable!) error reply. You have to cope with error replies when
communicating via D-Bus *anyway* (because P2 might exit, crash, or
send back an error reply), so this isn't any extra complexity; and not
having the separate IPC transaction to check whether P2 is available
gives you less code complexity, more robustness, and faster startup.
This approach is sometimes called "leap before you look" or
"easier to ask forgiveness than permission".
It is also possible to use *autostarting*: if you have provided a
.service file for P2, you can send a method call message to P2 while it
is *not* running yet, and have dbus-daemon automatically start P2 and
deliver that message to it. This is usually the best approach to D-Bus
service interdependencies.
In particular, if you use autostarting, you don't need to worry about
starting P2 and waiting for it to be available before you start P1.
This typically leads to simpler and faster system startup.
Depending how you have written the .service file, dbus-daemon will
either ask systemd to start P2 (this is the preferred mode of operation
on Linux systems with systemd), or start P2 itself (available on all
platforms).
S
[1] https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use
More information about the dbus
mailing list