Check whether a dbus caller is still alive

David Llewellyn-Jones david at flypig.co.uk
Mon Oct 2 07:55:36 UTC 2017


On 01/10/17 20:55, David Llewellyn-Jones wrote:
> On 01/10/17 18:28, Thiago Macieira wrote:
>> On Sunday, 1 October 2017 06:25:51 PDT 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.
>>
>> Sure. You can watch the NameOwnerChanged signal to see if that particular 
>> unique connection ID changes owner to "", indicating that it disconnected.
> 
> Thank you Thiago. I can now catch the name changes using
> 
> g_dbus_connection_signal_subscribe(connection, "org.freedesktop.DBus",
> "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus",
> NULL, G_DBUS_SIGNAL_FLAGS_NONE, signal_callback, user_data, NULL);
> 
> and I see separate signals when the owner connects and dies for each
> invocation.
> 
> However, the signal gives me a connection, which is the same for each
> invocation. I can't see a way to tie the owner to the
> GDBusMethodInvocation instance.

Sorry for replying to my own message, but after a bit more digging I
think I have an answer.

I got lost focussing on the connection, when I should have been
focussing on the message.

The following will turn my invocation into its unique sender name:

const gchar * unique_name;
unique_name = g_dbus_message_get_sender
    (g_dbus_method_invocation_get_message (invocation));

Now waiting on a "NameOwnerChanged" signal with old_owner matching
unique_name and new_owner matching "" indicates that the owner of that
message is no longer interested.

Hopefully this looks cogent; please correct me if not and thank you again.

David
-- 
Website: http://www.flypig.co.uk


More information about the dbus mailing list