free(n);
}
+static void bus_reset_queues(sd_bus *b) {
+ unsigned i;
+
+ assert(b);
+
+ for (i = 0; i < b->rqueue_size; i++)
+ sd_bus_message_unref(b->rqueue[i]);
+ free(b->rqueue);
+
+ for (i = 0; i < b->wqueue_size; i++)
+ sd_bus_message_unref(b->wqueue[i]);
+ free(b->wqueue);
+
+ b->rqueue = b->wqueue = NULL;
+ b->rqueue_size = b->wqueue_size = 0;
+}
+
static void bus_free(sd_bus *b) {
struct filter_callback *f;
struct node *n;
- unsigned i;
assert(b);
close_many(b->fds, b->n_fds);
free(b->fds);
- for (i = 0; i < b->rqueue_size; i++)
- sd_bus_message_unref(b->rqueue[i]);
- free(b->rqueue);
-
- for (i = 0; i < b->wqueue_size; i++)
- sd_bus_message_unref(b->wqueue[i]);
- free(b->wqueue);
+ bus_reset_queues(b);
hashmap_free_free(b->reply_callbacks);
prioq_free(b->reply_callbacks_prioq);
r->input_fd = r->output_fd = -1;
r->message_version = 1;
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
+ r->attach_flags |= KDBUS_ATTACH_NAMES;
r->original_pid = getpid();
assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_TIMESTAMP, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CREDS, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b);
+ SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
return 0;
}
-_public_ int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) {
+_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
assert_return(bus, -EINVAL);
+ assert_return(mask <= _SD_BUS_CREDS_MAX, -EINVAL);
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b);
- return 0;
-}
-
-_public_ int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) {
- assert_return(bus, -EINVAL);
- assert_return(bus->state == BUS_UNSET, -EPERM);
- assert_return(!bus_pid_changed(bus), -ECHILD);
-
- SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b);
- return 0;
+ return kdbus_translate_attach_flags(mask, &bus->creds_mask);
}
_public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
return 0;
}
-static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
+static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
const char *s;
int r;
assert(bus);
- assert(bus->state == BUS_HELLO);
+ assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
assert(reply);
r = sd_bus_message_get_errno(reply);
if (!bus->unique_name)
return -ENOMEM;
- bus->state = BUS_RUNNING;
+ if (bus->state == BUS_HELLO)
+ bus->state = BUS_RUNNING;
return 1;
}
}
_public_ void sd_bus_close(sd_bus *bus) {
+
if (!bus)
return;
if (bus->state == BUS_CLOSED)
sd_bus_detach_event(bus);
+ /* Drop all queued messages so that they drop references to
+ * the bus object and the bus may be freed */
+ bus_reset_queues(bus);
+
if (!bus->is_kernel)
bus_close_fds(bus);
}
static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
+ assert(b);
assert(m);
if (m->header->version > b->message_version)
return bus_message_seal(m, ++b->serial);
}
+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
+ * want to interfere with the real sender's serial numbers we
+ * pick a fixed, artifical one. We use (uint32_t) -1 rather
+ * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
+ * even though kdbus can do 64bit. */
+
+ return bus_message_seal(m, 0xFFFFFFFFULL);
+}
+
static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
int r;
return ret;
}
-static int bus_read_message(sd_bus *bus, sd_bus_message **m) {
- int r;
-
+static int bus_read_message(sd_bus *bus) {
assert(bus);
- assert(m);
if (bus->is_kernel)
- r = bus_kernel_read_message(bus, m);
+ return bus_kernel_read_message(bus);
else
- r = bus_socket_read_message(bus, m);
+ return bus_socket_read_message(bus);
+}
- return r;
+int bus_rqueue_make_room(sd_bus *bus) {
+ sd_bus_message **q;
+ unsigned x;
+
+ x = bus->rqueue_size + 1;
+
+ if (bus->rqueue_allocated >= x)
+ return 0;
+
+ if (x > BUS_RQUEUE_MAX)
+ return -ENOBUFS;
+
+ q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
+ if (!q)
+ return -ENOMEM;
+
+ bus->rqueue = q;
+ bus->rqueue_allocated = x;
+
+ return 0;
}
static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
- sd_bus_message *z = NULL;
int r, ret = 0;
assert(bus);
assert(m);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->rqueue_size > 0) {
- /* Dispatch a queued message */
+ for (;;) {
+ if (bus->rqueue_size > 0) {
+ /* Dispatch a queued message */
- *m = bus->rqueue[0];
- bus->rqueue_size --;
- memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
- return 1;
- }
+ *m = bus->rqueue[0];
+ bus->rqueue_size --;
+ memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
+ return 1;
+ }
- /* Try to read a new message */
- do {
- r = bus_read_message(bus, &z);
+ /* Try to read a new message */
+ r = bus_read_message(bus);
if (r < 0)
return r;
if (r == 0)
return ret;
ret = 1;
- } while (!z);
-
- *m = z;
- return ret;
+ }
}
_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
sd_bus_error *error,
sd_bus_message **reply) {
- int r;
usec_t timeout;
uint64_t serial;
- bool room = false;
+ unsigned i;
+ int r;
assert_return(bus, -EINVAL);
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
return r;
timeout = calc_elapse(usec);
+ i = bus->rqueue_size;
for (;;) {
usec_t left;
- sd_bus_message *incoming = NULL;
-
- if (!room) {
- sd_bus_message **q;
- if (bus->rqueue_size >= BUS_RQUEUE_MAX)
- return -ENOBUFS;
-
- /* Make sure there's room for queuing this
- * locally, before we read the message */
-
- q = realloc(bus->rqueue, (bus->rqueue_size + 1) * sizeof(sd_bus_message*));
- if (!q)
- return -ENOMEM;
-
- bus->rqueue = q;
- room = true;
- }
-
- r = bus_read_message(bus, &incoming);
+ r = bus_read_message(bus);
if (r < 0)
return r;
- if (incoming) {
+ while (i < bus->rqueue_size) {
+ sd_bus_message *incoming = NULL;
+
+ incoming = bus->rqueue[i];
if (incoming->reply_serial == serial) {
/* Found a match! */
+ memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
+ bus->rqueue_size--;
+
if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
if (reply)
incoming->sender &&
streq(bus->unique_name, incoming->sender)) {
+ memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
+ bus->rqueue_size--;
+
/* Our own message? Somebody is trying
* to send its own client a message,
* let's not dead-lock, let's fail
return -ELOOP;
}
- /* There's already guaranteed to be room for
- * this, so need to resize things here */
- bus->rqueue[bus->rqueue_size ++] = incoming;
- room = false;
-
/* Try to read more, right-away */
- continue;
+ i++;
}
- if (r != 0)
+
+ if (r > 0)
continue;
if (timeout > 0) {
}
static int process_timeout(sd_bus *bus) {
+ _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message* m = NULL;
struct reply_callback *c;
usec_t n;
r = bus_message_new_synthetic_error(
bus,
c->serial,
- &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
+ &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
&m);
if (r < 0)
return r;
- r = bus_seal_message(bus, m);
+ m->sender = "org.freedesktop.DBus";
+
+ r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
bus->current = m;
bus->iteration_counter ++;
- r = c->callback(bus, m, c->userdata);
+ r = c->callback(bus, m, c->userdata, &error_buffer);
+ r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
bus->current = NULL;
- return r < 0 ? r : 1;
+ return r;
}
static int process_hello(sd_bus *bus, sd_bus_message *m) {
}
static int process_reply(sd_bus *bus, sd_bus_message *m) {
+ _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
struct reply_callback *c;
int r;
if (r < 0)
return r;
- r = c->callback(bus, m, c->userdata);
+ r = c->callback(bus, m, c->userdata, &error_buffer);
+ r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
- return r < 0 ? r : 1;
+ return r;
}
static int process_filter(sd_bus *bus, sd_bus_message *m) {
+ _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
struct filter_callback *l;
int r;
if (r < 0)
return r;
- r = l->callback(bus, m, l->userdata);
+ r = l->callback(bus, m, l->userdata, &error_buffer);
+ r = bus_maybe_reply_error(m, r, &error_buffer);
if (r != 0)
return r;
c = hashmap_first(bus->reply_callbacks);
if (c) {
+ _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
+
/* First, fail all outstanding method calls */
r = bus_message_new_synthetic_error(
bus,
c->serial,
- &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
+ &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
&m);
if (r < 0)
return r;
- r = bus_seal_message(bus, m);
+ r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
bus->current = m;
bus->iteration_counter++;
- r = c->callback(bus, m, c->userdata);
+ r = c->callback(bus, m, c->userdata, &error_buffer);
+ r = bus_maybe_reply_error(m, r, &error_buffer);
free(c);
- if (r >= 0)
- r = 1;
-
goto finish;
}
if (r < 0)
return r;
- r = bus_seal_message(bus, m);
+ m->sender = "org.freedesktop.DBus.Local";
+
+ r = bus_seal_synthetic_message(bus, m);
if (r < 0)
return r;
return 0;
}
+_public_ sd_event* sd_bus_get_event(sd_bus *bus) {
+ assert_return(bus, NULL);
+
+ return bus->event;
+}
+
_public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
assert_return(bus, NULL);
return r;
}
+
+_public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
+ sd_bus_creds *c;
+ pid_t pid = 0;
+ int r;
+
+ assert_return(bus, -EINVAL);
+ assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
+ assert_return(ret, -EINVAL);
+ assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+ assert_return(!bus_pid_changed(bus), -ECHILD);
+ assert_return(!bus->is_kernel, -ENOTSUP);
+
+ if (!bus->ucred_valid && !isempty(bus->label))
+ return -ENODATA;
+
+ c = bus_creds_new();
+ if (!c)
+ return -ENOMEM;
+
+ if (bus->ucred_valid) {
+ pid = c->pid = bus->ucred.pid;
+ c->uid = bus->ucred.uid;
+ c->gid = bus->ucred.gid;
+
+ c->mask |= ((SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask) & bus->creds_mask;
+ }
+
+ if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
+ c->label = strdup(bus->label);
+ if (!c->label) {
+ sd_bus_creds_unref(c);
+ return -ENOMEM;
+ }
+
+ c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT | bus->creds_mask;
+ }
+
+ r = bus_creds_add_more(c, mask, pid, 0);
+ if (r < 0)
+ return r;
+
+ *ret = c;
+ return 0;
+}