chiark / gitweb /
bus: properly set up bus connections with sd_bus_set_exec()
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 5f2ede088a32bcb471edc232af62a2561d7804be..870d617ac1e2b962f506f9c34ef9760333c5f641 100644 (file)
@@ -244,11 +244,7 @@ static int bus_send_hello(sd_bus *bus) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, NULL);
-        if (r < 0)
-                return r;
-
-        return r;
+        return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
 }
 
 int bus_start_running(sd_bus *bus) {
@@ -507,7 +503,7 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
 
                         errno = 0;
                         ul = strtoul(*p + 4, (char**) p, 10);
-                        if (errno != 0 || **p != '=' || ul > 256) {
+                        if (errno > 0 || **p != '=' || ul > 256) {
                                 r = -EINVAL;
                                 goto fail;
                         }
@@ -722,7 +718,7 @@ int sd_bus_start(sd_bus *bus) {
 
         if (bus->fd >= 0)
                 r = bus_start_fd(bus);
-        else if (bus->address)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -1421,6 +1417,28 @@ static int process_timeout(sd_bus *bus) {
         return r < 0 ? r : 1;
 }
 
+static int process_hello(sd_bus *bus, sd_bus_message *m) {
+        assert(bus);
+        assert(m);
+
+        if (bus->state != BUS_HELLO)
+                return 0;
+
+        /* Let's make sure the first message on the bus is the HELLO
+         * reply. But note that we don't actually parse the message
+         * here (we leave that to the usual reply handling), we just
+         * verify we don't let any earlier msg through. */
+
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
+            m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+                return -EIO;
+
+        if (m->reply_serial != bus->hello_serial)
+                return -EIO;
+
+        return 0;
+}
+
 static int process_reply(sd_bus *bus, sd_bus_message *m) {
         struct reply_callback *c;
         int r;
@@ -1675,6 +1693,10 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
+        r = process_hello(bus, m);
+        if (r != 0)
+                return r;
+
         r = process_reply(bus, m);
         if (r != 0)
                 return r;
@@ -1944,7 +1966,7 @@ static int bus_add_object(
                 return -ENOMEM;
 
         c->path = strdup(path);
-        if (!path) {
+        if (!c->path) {
                 free(c);
                 return -ENOMEM;
         }