Check whether a dbus caller is still alive

Bastien Nocera hadess at hadess.net
Thu Oct 5 14:53:04 UTC 2017


On Sun, 2017-10-01 at 14:25 +0100, David Llewellyn-Jones wrote:
> Hi,
> 
> I have an application that connects to a systemd service via the
> system
> bus. The application makes a synchronous call which blocks, until the
> service responds, which may take some time.
> 
> My concern is that the application may get closed before the service
> responds. Is there any way for the service to check whether the
> invocation is still valid? If the application closes, I'd like the
> service to stop the long-running operation.
> 
> For context, the service uses gdbus and a GMainLoop. When it receives
> an
> invocation a function like this gets called:
> 
> static gboolean on_handle_function(FlypigInterface * object,
> GDBusMethodInvocation * invocation, gint handle, gpointer user_data)
> {
>     // ...perform long running task
> }
> 
> Then, when it's done with the task, it calls something like this
> (created by gdbus-codegen) to respond back to the application:
> 
> void flypig_interface_complete_function (
>     FlypigInterface *object,
>     GDBusMethodInvocation *invocation,
>     gboolean success);
> 
> My constraints are that the application must be synchronous, so can't
> do
> anything else while it's waiting, whereas the service has much more
> flexibility.

g_bus_watch_name () and associated (see "Watching Bus Names" page in
the GIO API docs) is what you want.

This is an example of a service which implements the Freedesktop
Inhibit spec by calling gnome-session's API:
https://git.gnome.org//browse/gnome-settings-daemon/tree/plugins/screensaver-proxy/gsd-screensaver-proxy-manager.c

As gnome-session isn't watching the real caller, we need to make sure
our long running proxy disables the inhibition if the real client goes
away. This is done using a watch on the peer name.

HTH


More information about the dbus mailing list