chiark / gitweb /
networkd,resolved: make use of watch_bind feature to connect to the bus
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Dec 2017 14:54:30 +0000 (15:54 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:49:59 +0000 (07:49 +0200)
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.

src/shared/bus-util.c
src/shared/bus-util.h
src/systemd/sd-bus.h

index 1f9aa8dbeb84424b0ce89ad900db42f83b3e8b15..f301ab185e0b9ff4f81d46fede9cf97c1e341de0 100644 (file)
@@ -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;
+}
index 4c22ee0a89026c8b997b452f0351444e283c02b6..e0addd71cdae6569b20fb05c54809c0d9e9c8a6b 100644 (file)
@@ -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);
index 3c40a53dc70f316c805259a7873c34d80fe74cc8..5ac08e99e5b2b8148570de206d3fe4917b2b381d 100644 (file)
@@ -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);