diff options
author | Luca Boccassi <bluca@debian.org> | 2024-02-14 18:14:21 +0100 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2024-02-15 15:19:39 +0100 |
commit | e07e7017bb65cd99aaa3dbb25fb7803759dc3be3 (patch) | |
tree | c813c0c7deeaf79a431d9d6b0b99b2ec9f83124f /man/sd_bus_service_reconnect.c | |
parent | keyring-util: Use reported key size to resize buf (diff) | |
download | systemd-e07e7017bb65cd99aaa3dbb25fb7803759dc3be3.tar.xz systemd-e07e7017bb65cd99aaa3dbb25fb7803759dc3be3.zip |
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
Diffstat (limited to 'man/sd_bus_service_reconnect.c')
-rw-r--r-- | man/sd_bus_service_reconnect.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/man/sd_bus_service_reconnect.c b/man/sd_bus_service_reconnect.c index 10c776b035..0b75086ef1 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': <?xml version="1.0"?> <!--*-nxml-*--> <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" @@ -92,6 +93,23 @@ static int on_disconnect(sd_bus_message *message, void *userdata, sd_bus_error * return 0; } +/* Ensure the event loop exits with a clear error if acquiring the well-known service name fails */ +static int request_name_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { + if (!sd_bus_message_is_method_error(m, NULL)) + return 1; + + const sd_bus_error *error = sd_bus_message_get_error(m); + + if (sd_bus_error_has_names(error, SD_BUS_ERROR_TIMEOUT, SD_BUS_ERROR_NO_REPLY)) + return 1; /* The bus is not available, try again later */ + + printf("Failed to request name: %s\n", error->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. |