chiark / gitweb /
sd-bus: log about bus state changes
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Dec 2017 11:50:43 +0000 (12:50 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:50:00 +0000 (07:50 +0200)
Let's unify all state changes in a new helper function, from which we
can then debug log all state changes

src/libelogind/sd-bus/bus-internal.h
src/libelogind/sd-bus/bus-socket.c
src/libelogind/sd-bus/sd-bus.c

index 7124b8edc106f4599653b01b1ac10358995ca741..a2261309118a285966e1af8737e62c3fb88025b0 100644 (file)
@@ -166,7 +166,8 @@ enum bus_state {
         BUS_HELLO,           /* we are waiting for the Hello() response */
         BUS_RUNNING,
         BUS_CLOSING,
-        BUS_CLOSED
+        BUS_CLOSED,
+        _BUS_STATE_MAX,
 };
 
 static inline bool BUS_IS_OPEN(enum bus_state state) {
@@ -417,3 +418,5 @@ int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
         } while (false)
 
 void bus_enter_closing(sd_bus *bus);
+
+void bus_set_state(sd_bus *bus, enum bus_state state);
index 8e1d5467a3f69d0b8109299ef39c284be84d7031..f930ba93fc999ba7e53e7cf3636b919aae061d69 100644 (file)
@@ -675,7 +675,7 @@ int bus_socket_start_auth(sd_bus *b) {
 
         bus_get_peercred(b);
 
-        b->state = BUS_AUTHENTICATING;
+        bus_set_state(b, BUS_AUTHENTICATING);
         b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_AUTH_TIMEOUT;
 
         if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
@@ -894,7 +894,7 @@ int bus_socket_connect(sd_bus *b) {
                                 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
                                  * we start parsing the address string. The only reason we set the state explicitly
                                  * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
-                                b->state = BUS_OPENING;
+                                bus_set_state(b, BUS_OPENING);
                                 return 1;
                         }
 
@@ -910,7 +910,7 @@ int bus_socket_connect(sd_bus *b) {
                                 if (inotify_done) {
                                         /* inotify set up already, don't do it again, just return now, and remember
                                          * that we are waiting for inotify events now. */
-                                        b->state = BUS_WATCH_BIND;
+                                        bus_set_state(b, BUS_WATCH_BIND);
                                         return 1;
                                 }
 
index ca4075af61911cf3b1916beb98fd92dfc82de565..ccbe3d57eb79d5b9e933948b343624b19895911c 100644 (file)
@@ -457,6 +457,29 @@ static int synthesize_connected_signal(sd_bus *bus) {
         return 0;
 }
 
+void bus_set_state(sd_bus *bus, enum bus_state state) {
+
+        static const char * const table[_BUS_STATE_MAX] = {
+                [BUS_UNSET] = "UNSET",
+                [BUS_WATCH_BIND] = "WATCH_BIND",
+                [BUS_OPENING] = "OPENING",
+                [BUS_AUTHENTICATING] = "AUTHENTICATING",
+                [BUS_HELLO] = "HELLO",
+                [BUS_RUNNING] = "RUNNING",
+                [BUS_CLOSING] = "CLOSING",
+                [BUS_CLOSED] = "CLOSED",
+        };
+
+        assert(bus);
+        assert(state < _BUS_STATE_MAX);
+
+        if (state == bus->state)
+                return;
+
+        log_debug("Bus %s: changing state %s → %s", strna(bus->description), table[bus->state], table[state]);
+        bus->state = state;
+}
+
 static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
         const char *s;
         sd_bus *bus;
@@ -483,7 +506,7 @@ static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *e
                 return -ENOMEM;
 
         if (bus->state == BUS_HELLO) {
-                bus->state = BUS_RUNNING;
+                bus_set_state(bus, BUS_RUNNING);
 
                 r = synthesize_connected_signal(bus);
                 if (r < 0)
@@ -537,11 +560,11 @@ int bus_start_running(sd_bus *bus) {
         }
 
         if (bus->bus_client) {
-                bus->state = BUS_HELLO;
+                bus_set_state(bus, BUS_HELLO);
                 return 1;
         }
 
-        bus->state = BUS_RUNNING;
+        bus_set_state(bus, BUS_RUNNING);
 
         r = synthesize_connected_signal(bus);
         if (r < 0)
@@ -1095,7 +1118,7 @@ _public_ int sd_bus_start(sd_bus *bus) {
         assert_return(bus->state == BUS_UNSET, -EPERM);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        bus->state = BUS_OPENING;
+        bus_set_state(bus, BUS_OPENING);
 
         if (bus->is_server && bus->bus_client)
                 return -EINVAL;
@@ -1418,7 +1441,7 @@ _public_ void sd_bus_close(sd_bus *bus) {
         if (bus_pid_changed(bus))
                 return;
 
-        bus->state = BUS_CLOSED;
+        bus_set_state(bus, BUS_CLOSED);
 
         sd_bus_detach_event(bus);
 
@@ -1447,7 +1470,7 @@ void bus_enter_closing(sd_bus *bus) {
         if (!IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING, BUS_HELLO, BUS_RUNNING))
                 return;
 
-        bus->state = BUS_CLOSING;
+        bus_set_state(bus, BUS_CLOSING);
 }
 
 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {