From e07e7017bb65cd99aaa3dbb25fb7803759dc3be3 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Wed, 14 Feb 2024 17:14:21 +0000 Subject: [PATCH] man: enchance sd_bus_set_watch_bind() example to handle one more failure In case the D-Bus policy is not set up correctly the example just loops forever. Check the return of sd_bus_request_name_async() in a callback and exit if the error is not temporary. Follow-up for 34bbda18a5f07fa5a52e8d85d20637ce1c00c4ec --- man/sd_bus_service_reconnect.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/man/sd_bus_service_reconnect.c b/man/sd_bus_service_reconnect.c index 10c776b0355..0b75086ef1b 100644 --- a/man/sd_bus_service_reconnect.c +++ b/man/sd_bus_service_reconnect.c @@ -5,7 +5,8 @@ * Compile with 'cc sd_bus_service_reconnect.c $(pkg-config --libs --cflags libsystemd)' * * To allow the program to take ownership of the name 'org.freedesktop.ReconnectExample', - * add the following as /etc/dbus-1/system.d/org.freedesktop.ReconnectExample.conf: + * add the following as /etc/dbus-1/system.d/org.freedesktop.ReconnectExample.conf + * and then reload the broker with 'systemctl reload dbus': message); + object *o = userdata; + check(sd_event_exit(*o->event, -sd_bus_error_get_errno(error))); + + return 1; +} + static int setup(object *o) { /* If we are reconnecting, then the bus object needs to be closed, detached from * the event loop and recreated. @@ -119,7 +137,6 @@ static int setup(object *o) { check(sd_bus_set_bus_client(*o->bus, 1)); check(sd_bus_negotiate_creds(*o->bus, 1, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS)); check(sd_bus_set_watch_bind(*o->bus, 1)); - check(sd_bus_set_connected_signal(*o->bus, 1)); check(sd_bus_start(*o->bus)); /* Publish an interface on the bus, specifying our well-known object access @@ -135,15 +152,16 @@ static int setup(object *o) { o)); /* By default the service is only assigned an ephemeral name. Also add a well-known * one, so that clients know whom to call. This needs to be asynchronous, as - * D-Bus might not be yet available. + * D-Bus might not be yet available. The callback will check whether the error is + * expected or not, in case it fails. * https://www.freedesktop.org/software/systemd/man/sd_bus_request_name.html */ check(sd_bus_request_name_async(*o->bus, NULL, "org.freedesktop.ReconnectExample", 0, - NULL, - NULL)); + request_name_callback, + o)); /* When D-Bus is disconnected this callback will be invoked, which will * set up the connection again. This needs to be asynchronous, as D-Bus might not * yet be available.