]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: add new sd_bus_set_connected_signal() API
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Dec 2017 14:50:05 +0000 (15:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 5 Jan 2018 12:58:32 +0000 (13:58 +0100)
With this new API sd-bus can synthesize a local "Connected" signal when
the connection is fully established. It mirrors the local "Disconnected"
signal that is already generated when the connection is terminated. This
is useful to be notified when connection setup is done, in order to
start method calls then, in particular when using "slow" connection
methods (for example slow TCP, or most importantly the "watch_bind"
inotify logic).

Note that one could also use hook into the initial NameAcquired signal
received from the bus broker, but that scheme works only if we actually
connect to a bus. The benefit of "Connected" OTOH is that it works with
any kind of connection.

Ideally, we'd just generate this message unconditionally, but in order
not to break clients that do not expect this message it is opt-in.

src/libsystemd/libsystemd.sym
src/libsystemd/sd-bus/bus-internal.h
src/libsystemd/sd-bus/sd-bus.c

index af4226c73af9acd5dcb4a75bfab152104a1daa88..27f3219ee2337652b9b73474ff643f112aa2697b 100644 (file)
@@ -541,4 +541,6 @@ global:
         sd_bus_match_signal;
         sd_bus_match_signal_async;
         sd_bus_is_ready;
+        sd_bus_set_connected_signal;
+        sd_bus_get_connected_signal;
 } LIBSYSTEMD_236;
index 29366c4c5683c43d6df518459db12b90e54fcebc..abda27fd561887343c9bea0331381886e4b9537c 100644 (file)
@@ -219,6 +219,7 @@ struct sd_bus {
         bool is_monitor:1;
         bool accept_fd:1;
         bool attach_timestamp:1;
+        bool connected_signal:1;
 
         int use_memfd;
 
index 7967aac7c67d78c005f3bcc2fbf6f276e187729a..74d54f785fe10bb5fa8a08a96d5ebfb29d48c6ef 100644 (file)
@@ -392,6 +392,66 @@ _public_ int sd_bus_get_watch_bind(sd_bus *bus) {
         return bus->watch_bind;
 }
 
+_public_ int sd_bus_set_connected_signal(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        bus->connected_signal = b;
+        return 0;
+}
+
+_public_ int sd_bus_get_connected_signal(sd_bus *bus) {
+        assert_return(bus, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        return bus->connected_signal;
+}
+
+static int synthesize_connected_signal(sd_bus *bus) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+        int r;
+
+        assert(bus);
+
+        /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
+         * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
+         * Hello() reply. Or in other words, whenver we enter BUS_RUNNING state.
+         *
+         * This is useful so that clients can start doing stuff whenver the connection is fully established in a way
+         * that works independently from whether we connected to a full bus or just a direct connection. */
+
+        if (!bus->connected_signal)
+                return 0;
+
+        r = sd_bus_message_new_signal(
+                        bus,
+                        &m,
+                        "/org/freedesktop/DBus/Local",
+                        "org.freedesktop.DBus.Local",
+                        "Connected");
+        if (r < 0)
+                return r;
+
+        bus_message_set_sender_local(bus, m);
+
+        r = bus_seal_synthetic_message(bus, m);
+        if (r < 0)
+                return r;
+
+        r = bus_rqueue_make_room(bus);
+        if (r < 0)
+                return r;
+
+        /* Insert at the very front */
+        memmove(bus->rqueue + 1, bus->rqueue, sizeof(sd_bus_message*) * bus->rqueue_size);
+        bus->rqueue[0] = m;
+        m = NULL;
+        bus->rqueue_size++;
+
+        return 0;
+}
+
 static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
         const char *s;
         sd_bus *bus;
@@ -417,9 +477,14 @@ static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *e
         if (!bus->unique_name)
                 return -ENOMEM;
 
-        if (bus->state == BUS_HELLO)
+        if (bus->state == BUS_HELLO) {
                 bus->state = BUS_RUNNING;
 
+                r = synthesize_connected_signal(bus);
+                if (r < 0)
+                        return r;
+        }
+
         return 1;
 }
 
@@ -449,6 +514,7 @@ int bus_start_running(sd_bus *bus) {
         struct reply_callback *c;
         Iterator i;
         usec_t n;
+        int r;
 
         assert(bus);
         assert(bus->state < BUS_HELLO);
@@ -471,6 +537,11 @@ int bus_start_running(sd_bus *bus) {
         }
 
         bus->state = BUS_RUNNING;
+
+        r = synthesize_connected_signal(bus);
+        if (r < 0)
+                return r;
+
         return 1;
 }