Scroll to navigation

SD_BUS_SET_WATCH_BIND(3) sd_bus_set_watch_bind SD_BUS_SET_WATCH_BIND(3)


sd_bus_set_watch_bind, sd_bus_get_watch_bind - Control socket binding watching on bus connections


#include <systemd/sd-bus.h>

int sd_bus_set_watch_bind(sd_bus *bus, int b);

int sd_bus_get_watch_bind(sd_bus *bus);


sd_bus_set_watch_bind() may be used to enable or disable client-side watching of server socket binding for a bus connection object. If the b is true, the feature is enabled, otherwise disabled (which is the default). When enabled, and the selected bus address refers to an AF_UNIX socket in the file system which does not exist while the connection attempt is made an inotify(7) watch is installed on it, waiting for the socket to appear. As soon as the socket appears the connection is made. This functionality is useful in particular in early-boot programs that need to run before the system bus is available, but want to connect to it the instant it may be connected to.

sd_bus_get_watch_bind() may be used to query the current setting of this feature. It returns zero when the feature is disabled, and positive if enabled.

Note that no timeout is applied while we wait for the socket to appear. This means that any synchronous remote operation (such as sd_bus_call(3), sd_bus_add_match(3) or sd_bus_request_name(3)), that is used on a connection with this feature enabled that hasn't been established yet, might block forever if the socket is never created. However, asynchronous remote operations (such as sd_bus_send(3), sd_bus_call_async(3), sd_bus_add_match_async(3)) do not block in this case, and safely enqueue the requested operations to be dispatched the instant the connection is set up.

Use sd_bus_is_ready(3) to determine whether the connection is fully established, i.e. whether the peer socket has been bound, connected to and authenticated. Use sd_bus_set_connected_signal(3) to be notified when the connection is fully established.


On success, these functions return 0 or a positive integer. On failure, they return a negative errno-style error code.


Returned errors may indicate the following problems:


The bus connection has been created in a different process, library or module instance.


Functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file.

The code described here uses getenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call setenv(3) from a parallel thread. It is recommended to only do calls to setenv() from an early phase of the program when no other threads have been started.


Example 1. Create a simple system service that publishes a property on the system bus and can reconnect when D-Bus disconnects and reconnects

/* SPDX-License-Identifier: MIT-0 */
/* Implements a D-Bus service that automatically reconnects when the system bus is restarted.

* 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
* 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"
""> <busconfig>
<policy user="root">
<allow own="org.freedesktop.ReconnectExample"/>
<allow send_destination="org.freedesktop.ReconnectExample"/>
<allow receive_sender="org.freedesktop.ReconnectExample"/>
<policy context="default">
<allow send_destination="org.freedesktop.ReconnectExample"/>
<allow receive_sender="org.freedesktop.ReconnectExample"/>
</policy> </busconfig>
* To get the property via busctl:
* $ busctl --user get-property org.freedesktop.ReconnectExample \
* /org/freedesktop/ReconnectExample \
* org.freedesktop.ReconnectExample \
* Example
* s "example"
*/ #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <systemd/sd-bus.h> #define _cleanup_(f) __attribute__((cleanup(f))) #define check(x) ({ \
int _r = (x); \
errno = _r < 0 ? -_r : 0; \
printf(#x ": %m\n"); \
if (_r < 0) \
return EXIT_FAILURE; \
}) typedef struct object {
const char *example;
sd_bus **bus;
sd_event **event; } object; static int property_get(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
object *o = userdata;
if (strcmp(property, "Example") == 0)
return sd_bus_message_append(reply, "s", o->example);
return sd_bus_error_setf(error,
"Unknown property '%s'",
property); } /* */ static const sd_bus_vtable vtable[] = {
"Example", "s",
SD_BUS_VTABLE_END }; static int setup(object *o); static int on_disconnect(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
check(setup((object *)userdata));
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.
if (*o->bus) {
*o->bus = sd_bus_close_unref(*o->bus);
/* Set up a new bus object for the system bus, configure it to wait for D-Bus to be available
* instead of failing if it is not, and start it. All the following operations are asyncronous
* and will not block waiting for D-Bus to be available.
check(sd_bus_set_address(*o->bus, "unix:path=/run/dbus/system_bus_socket"));
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));
/* Publish an interface on the bus, specifying our well-known object access
* path and public interface name.
/* 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. The callback will check whether the error is
* expected or not, in case it fails.
/* 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.
/* Attach the bus object to the event loop so that calls and signals are processed.
check(sd_bus_attach_event(*o->bus, *o->event, 0));
return 0; } int main(int argc, char **argv) {
/* The bus should be relinquished before the program terminates. The cleanup
* attribute allows us to do it nicely and cleanly whenever we exit the
* block.
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
object o = {
.example = "example",
.bus = &bus,
.event = &event,
/* Create an event loop data structure, with default parameters.
/* By default the event loop will terminate when all sources have disappeared, so
* we have to keep it 'occupied'. Register signal handling to do so.
check(sd_event_add_signal(event, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK, NULL, NULL));
check(sd_event_add_signal(event, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK, NULL, NULL));
/* Enter the main loop, it will exit only on sigint/sigterm.
/* */
check(sd_bus_release_name(bus, "org.freedesktop.ReconnectExample"));
return 0; }

This is particularly useful for services that are configured to survive a soft-reboot, see systemd-soft-reboot.service(8) for more details.


sd_bus_set_watch_bind() and sd_bus_get_watch_bind() were added in version 237.


systemd(1), sd-bus(3), inotify(7), sd_bus_call(3), sd_bus_add_match(3), sd_bus_request_name(3), sd_bus_is_ready(3), sd_bus_set_connected_signal(3)

systemd 255