chiark / gitweb /
bus: always pass valid timeout to kdbus
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 0a2ce3eb8dda1efc48af91a189e4315fda52f898..060dcc14364745bba68809aef9d602cf471af50a 100644 (file)
@@ -764,6 +764,9 @@ static int parse_container_address(sd_bus *b, const char **p, char **guid) {
         if (!machine)
                 return -EINVAL;
 
+        if (!filename_is_safe(machine))
+                return -EINVAL;
+
         free(b->machine);
         b->machine = machine;
         machine = NULL;
@@ -1139,12 +1142,17 @@ _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
 
         assert_return(machine, -EINVAL);
         assert_return(ret, -EINVAL);
+        assert_return(filename_is_safe(machine), -EINVAL);
 
         e = bus_address_escape(machine);
         if (!e)
                 return -ENOMEM;
 
+#ifdef ENABLE_KDBUS
+        p = strjoin("kernel:path=/dev/kdbus/ns/machine-", e, "/0-system/bus;x-container:machine=", e, NULL);
+#else
         p = strjoin("x-container:machine=", e, NULL);
+#endif
         if (!p)
                 return -ENOMEM;
 
@@ -1268,11 +1276,16 @@ _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
         return 0;
 }
 
-static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
+static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
         assert(b);
         assert(m);
 
-        if (m->header->version > b->message_version)
+        if (b->message_version != 0 &&
+            m->header->version != b->message_version)
+                return -EPERM;
+
+        if (b->message_endian != 0 &&
+            m->header->endian != b->message_endian)
                 return -EPERM;
 
         if (m->sealed) {
@@ -1283,16 +1296,16 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
                 return 0;
         }
 
-        return bus_message_seal(m, ++b->serial);
+        if (timeout == 0)
+                timeout = BUS_DEFAULT_TIMEOUT;
+
+        return bus_message_seal(m, ++b->serial, timeout);
 }
 
 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
         assert(b);
         assert(m);
 
-        if (m->header->version > b->message_version)
-                return -EPERM;
-
         /* The bus specification says the serial number cannot be 0,
          * hence let's fill something in for synthetic messages. Since
          * synthetic messages might have a fake sender and we don't
@@ -1301,7 +1314,7 @@ int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
          * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
          * even though kdbus can do 64bit. */
 
-        return bus_message_seal(m, 0xFFFFFFFFULL);
+        return bus_message_seal(m, 0xFFFFFFFFULL, 0);
 }
 
 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
@@ -1431,7 +1444,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if (!serial && !m->sealed)
                 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
 
-        r = bus_seal_message(bus, m);
+        r = bus_seal_message(bus, m, 0);
         if (r < 0)
                 return r;
 
@@ -1506,9 +1519,6 @@ static usec_t calc_elapse(uint64_t usec) {
         if (usec == (uint64_t) -1)
                 return 0;
 
-        if (usec == 0)
-                usec = BUS_DEFAULT_TIMEOUT;
-
         return now(CLOCK_MONOTONIC) + usec;
 }
 
@@ -1553,13 +1563,11 @@ _public_ int sd_bus_call_async(
         if (r < 0)
                 return r;
 
-        if (usec != (uint64_t) -1) {
-                r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
-                if (r < 0)
-                        return r;
-        }
+        r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
+        if (r < 0)
+                return r;
 
-        r = bus_seal_message(bus, m);
+        r = bus_seal_message(bus, m, usec);
         if (r < 0)
                 return r;
 
@@ -1570,7 +1578,7 @@ _public_ int sd_bus_call_async(
         c->callback = callback;
         c->userdata = userdata;
         c->serial = BUS_MESSAGE_SERIAL(m);
-        c->timeout = calc_elapse(usec);
+        c->timeout = calc_elapse(m->timeout);
 
         r = hashmap_put(bus->reply_callbacks, &c->serial, c);
         if (r < 0) {
@@ -1665,11 +1673,15 @@ _public_ int sd_bus_call(
 
         i = bus->rqueue_size;
 
+        r = bus_seal_message(bus, m, usec);
+        if (r < 0)
+                return r;
+
         r = sd_bus_send(bus, m, &serial);
         if (r < 0)
                 return r;
 
-        timeout = calc_elapse(usec);
+        timeout = calc_elapse(m->timeout);
 
         for (;;) {
                 usec_t left;
@@ -1746,6 +1758,8 @@ _public_ int sd_bus_call(
                 r = bus_poll(bus, true, left);
                 if (r < 0)
                         return r;
+                if (r == 0)
+                        return -ETIMEDOUT;
 
                 r = dispatch_wqueue(bus);
                 if (r < 0) {