#include "bus-objects.h"
#include "bus-util.h"
#include "bus-container.h"
+#include "bus-protocol.h"
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
return 0;
}
+_public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
+ assert_return(bus, -EINVAL);
+ assert_return(bus->state == BUS_UNSET, -EPERM);
+ assert_return(!bus_pid_changed(bus), -ECHILD);
+
+ bus->trusted = !!b;
+ return 0;
+}
+
static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
const char *s;
int r;
if (!machine)
return -EINVAL;
+ if (!filename_is_safe(machine))
+ return -EINVAL;
+
free(b->machine);
b->machine = machine;
machine = NULL;
b->bus_client = true;
+ /* Let's do per-method access control on the system bus. We
+ * need the caller's UID and capability set for that. */
+ b->trusted = false;
+ b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
+
r = sd_bus_start(b);
if (r < 0)
goto fail;
b->bus_client = true;
+ /* We don't do any per-method access control on the user
+ * bus. */
+ b->trusted = true;
+
r = sd_bus_start(b);
if (r < 0)
goto fail;
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;
}
_public_ sd_bus *sd_bus_unref(sd_bus *bus) {
- assert_return(bus, NULL);
+
+ if (!bus)
+ return NULL;
if (REFCNT_DEC(bus->n_ref) <= 0)
bus_free(bus);
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) {
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
* 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) {
/* If the serial number isn't kept, then we know that no reply
* is expected */
if (!serial && !m->sealed)
- m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+ 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;
if (usec == (uint64_t) -1)
return 0;
- if (usec == 0)
- usec = BUS_DEFAULT_TIMEOUT;
-
return now(CLOCK_MONOTONIC) + usec;
}
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
assert_return(m, -EINVAL);
assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
- assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
+ assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
assert_return(callback, -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
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;
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) {
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
assert_return(m, -EINVAL);
assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
- assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
+ assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
assert_return(!bus_error_is_dirty(error), -EINVAL);
assert_return(!bus_pid_changed(bus), -ECHILD);
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;
r = bus_poll(bus, true, left);
if (r < 0)
return r;
+ if (r == 0)
+ return -ETIMEDOUT;
r = dispatch_wqueue(bus);
if (r < 0) {
if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
return 0;
- if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+ if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
return 1;
if (streq_ptr(m->member, "Ping"))
_public_ int sd_bus_detach_event(sd_bus *bus) {
assert_return(bus, -EINVAL);
- assert_return(bus->event, -ENXIO);
+
+ if (!bus->event)
+ return 0;
if (bus->input_io_event_source) {
sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
if (bus->event)
bus->event = sd_event_unref(bus->event);
- return 0;
+ return 1;
}
_public_ sd_event* sd_bus_get_event(sd_bus *bus) {