From: Lennart Poettering Date: Tue, 19 Dec 2017 14:54:30 +0000 (+0100) Subject: networkd,resolved: make use of watch_bind feature to connect to the bus X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=ef02e13a94aa70904b62c70236ef82ec0e4f2fec;p=elogind.git networkd,resolved: make use of watch_bind feature to connect to the bus The changes both networkd and resolved to make use of the watch_bind feature of sd-bus to connect to the system bus. This way, both daemons can be started during early boot, and automatically and instantly connect to the system bus as it becomes available. This replaces prior code that used a time-based retry logic to connect to the bus. --- diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 1f9aa8dbe..f301ab185 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -1616,3 +1616,54 @@ int bus_track_add_name_many(sd_bus_track *t, char **l) { return r; } #endif // 0 + +int bus_open_system_watch_bind(sd_bus **ret) { + _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; + const char *e; + int r; + + assert(ret); + + /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */ + + r = sd_bus_new(&bus); + if (r < 0) + return r; + + e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); + if (!e) + e = DEFAULT_SYSTEM_BUS_ADDRESS; + + r = sd_bus_set_address(bus, e); + if (r < 0) + return r; + + r = sd_bus_set_bus_client(bus, true); + if (r < 0) + return r; + + r = sd_bus_set_trusted(bus, true); + if (r < 0) + return r; + + r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS); + if (r < 0) + return r; + + r = sd_bus_set_watch_bind(bus, true); + if (r < 0) + return r; + + r = sd_bus_set_connected_signal(bus, true); + if (r < 0) + return r; + + r = sd_bus_start(bus); + if (r < 0) + return r; + + *ret = bus; + bus = NULL; + + return 0; +} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 4c22ee0a8..e0addd71c 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -182,3 +182,5 @@ int bus_property_get_rlimit(sd_bus *bus, const char *path, const char *interface int bus_track_add_name_many(sd_bus_track *t, char **l); #endif // 0 + +int bus_open_system_watch_bind(sd_bus **ret); diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index 3c40a53dc..5ac08e99e 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -152,6 +152,8 @@ int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b); int sd_bus_get_exit_on_disconnect(sd_bus *bus); int sd_bus_set_watch_bind(sd_bus *bus, int b); int sd_bus_get_watch_bind(sd_bus *bus); +int sd_bus_set_connected_signal(sd_bus *bus, int b); +int sd_bus_get_connected_signal(sd_bus *bus); int sd_bus_start(sd_bus *bus);