X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fsd-bus.c;h=ca687c0a65413a0ac1739b0d37e9d638a0e66ce8;hb=872c8faaf2009422a91d227ae0b5c6f04c9d2c69;hp=870d617ac1e2b962f506f9c34ef9760333c5f641;hpb=3d339fec29178af415cb5367fdf3b53fab3093c2;p=elogind.git diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 870d617ac..ca687c0a6 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -26,34 +26,94 @@ #include #include #include +#include +#include #include "util.h" #include "macro.h" #include "strv.h" #include "set.h" +#include "missing.h" #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" #include "bus-type.h" #include "bus-socket.h" +#include "bus-kernel.h" +#include "bus-control.h" +#include "bus-introspect.h" +#include "bus-signature.h" +#include "bus-objects.h" static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec); +static void bus_close_fds(sd_bus *b) { + assert(b); + + if (b->input_fd >= 0) + close_nointr_nofail(b->input_fd); + + if (b->output_fd >= 0 && b->output_fd != b->input_fd) + close_nointr_nofail(b->output_fd); + + b->input_fd = b->output_fd = -1; +} + +static void bus_node_destroy(sd_bus *b, struct node *n) { + struct node_callback *c; + struct node_vtable *v; + struct node_enumerator *e; + + assert(b); + + if (!n) + return; + + while (n->child) + bus_node_destroy(b, n->child); + + while ((c = n->callbacks)) { + LIST_REMOVE(struct node_callback, callbacks, n->callbacks, c); + free(c); + } + + while ((v = n->vtables)) { + LIST_REMOVE(struct node_vtable, vtables, n->vtables, v); + free(v->interface); + free(v); + } + + while ((e = n->enumerators)) { + LIST_REMOVE(struct node_enumerator, enumerators, n->enumerators, e); + free(e); + } + + if (n->parent) + LIST_REMOVE(struct node, siblings, n->parent->child, n); + + assert_se(hashmap_remove(b->nodes, n->path) == n); + free(n->path); + free(n); +} + static void bus_free(sd_bus *b) { struct filter_callback *f; - struct object_callback *c; + struct node *n; unsigned i; assert(b); - if (b->fd >= 0) - close_nointr_nofail(b->fd); + bus_close_fds(b); + + if (b->kdbus_buffer) + munmap(b->kdbus_buffer, KDBUS_POOL_SIZE); free(b->rbuffer); free(b->unique_name); - free(b->auth_uid); + free(b->auth_buffer); free(b->address); + free(b->kernel); free(b->exec_path); strv_free(b->exec_argv); @@ -77,12 +137,19 @@ static void bus_free(sd_bus *b) { free(f); } - while ((c = hashmap_steal_first(b->object_callbacks))) { - free(c->path); - free(c); - } + bus_match_free(&b->match_callbacks); - hashmap_free(b->object_callbacks); + hashmap_free_free(b->vtable_methods); + hashmap_free_free(b->vtable_properties); + + while ((n = hashmap_first(b->nodes))) + bus_node_destroy(b, n); + + hashmap_free(b->nodes); + + bus_kernel_flush_memfd(b); + + assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0); free(b); } @@ -97,10 +164,13 @@ int sd_bus_new(sd_bus **ret) { if (!r) return -ENOMEM; - r->n_ref = 1; - r->fd = -1; + r->n_ref = REFCNT_INIT; + r->input_fd = r->output_fd = -1; r->message_version = 1; - r->negotiate_fds = true; + r->hello_flags |= KDBUS_HELLO_ACCEPT_FD; + r->original_pid = getpid(); + + assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0); /* We guarantee that wqueue always has space for at least one * entry */ @@ -123,6 +193,8 @@ int sd_bus_set_address(sd_bus *bus, const char *address) { return -EPERM; if (!address) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; a = strdup(address); if (!a) @@ -134,15 +206,20 @@ int sd_bus_set_address(sd_bus *bus, const char *address) { return 0; } -int sd_bus_set_fd(sd_bus *bus, int fd) { +int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) { if (!bus) return -EINVAL; if (bus->state != BUS_UNSET) return -EPERM; - if (fd < 0) + if (input_fd < 0) + return -EINVAL; + if (output_fd < 0) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; - bus->fd = fd; + bus->input_fd = input_fd; + bus->output_fd = output_fd; return 0; } @@ -157,6 +234,8 @@ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) { return -EINVAL; if (strv_isempty(argv)) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; p = strdup(path); if (!p) @@ -182,33 +261,148 @@ int sd_bus_set_bus_client(sd_bus *bus, int b) { return -EINVAL; if (bus->state != BUS_UNSET) return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; bus->bus_client = !!b; return 0; } -int sd_bus_set_negotiate_fds(sd_bus *bus, int b) { +int sd_bus_negotiate_fds(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b); + return 0; +} + +int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b); + return 0; +} + +int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b); + return 0; +} + +int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b); + return 0; +} + +int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b); + return 0; +} + +int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b); + return 0; +} + +int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b); + return 0; +} + +int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) { + if (!bus) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b); + return 0; +} + +int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) { + if (!bus) + return -EINVAL; + if (!b && !sd_id128_equal(server_id, SD_ID128_NULL)) + return -EINVAL; + if (bus->state != BUS_UNSET) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; + + bus->is_server = !!b; + bus->server_id = server_id; + return 0; +} + +int sd_bus_set_anonymous(sd_bus *bus, int b) { if (!bus) return -EINVAL; if (bus->state != BUS_UNSET) return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; - bus->negotiate_fds = !!b; + bus->anonymous_auth = !!b; return 0; } -static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) { +static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { const char *s; int r; assert(bus); assert(bus->state == BUS_HELLO); - - if (error != 0) - return -error; - assert(reply); + r = bus_message_to_errno(reply); + if (r < 0) + return r; + r = sd_bus_message_read(reply, "s", &s); if (r < 0) return r; @@ -231,7 +425,7 @@ static int bus_send_hello(sd_bus *bus) { assert(bus); - if (!bus->bus_client) + if (!bus->bus_client || bus->is_kernel) return 0; r = sd_bus_message_new_method_call( @@ -250,7 +444,7 @@ static int bus_send_hello(sd_bus *bus) { int bus_start_running(sd_bus *bus) { assert(bus); - if (bus->bus_client) { + if (bus->bus_client && !bus->is_kernel) { bus->state = BUS_HELLO; return 1; } @@ -408,8 +602,11 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) { static int parse_tcp_address(sd_bus *b, const char **p, char **guid) { _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL; - struct addrinfo hints, *result; int r; + struct addrinfo *result, hints = { + .ai_socktype = SOCK_STREAM, + .ai_flags = AI_ADDRCONFIG, + }; assert(b); assert(p); @@ -447,10 +644,6 @@ static int parse_tcp_address(sd_bus *b, const char **p, char **guid) { if (!host || !port) return -EINVAL; - zero(hints); - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_ADDRCONFIG; - if (family) { if (streq(family, "ipv4")) hints.ai_family = AF_INET; @@ -569,6 +762,41 @@ fail: return r; } +static int parse_kernel_address(sd_bus *b, const char **p, char **guid) { + _cleanup_free_ char *path = NULL; + int r; + + assert(b); + assert(p); + assert(*p); + assert(guid); + + while (**p != 0 && **p != ';') { + r = parse_address_key(p, "guid", guid); + if (r < 0) + return r; + else if (r > 0) + continue; + + r = parse_address_key(p, "path", &path); + if (r < 0) + return r; + else if (r > 0) + continue; + + skip_address_key(p); + } + + if (!path) + return -EINVAL; + + free(b->kernel); + b->kernel = path; + path = NULL; + + return 0; +} + static void bus_reset_parsed_address(sd_bus *b) { assert(b); @@ -578,7 +806,9 @@ static void bus_reset_parsed_address(sd_bus *b) { free(b->exec_path); b->exec_path = NULL; b->exec_argv = NULL; - b->peer = SD_ID128_NULL; + b->server_id = SD_ID128_NULL; + free(b->kernel); + b->kernel = NULL; } static int bus_parse_next_address(sd_bus *b) { @@ -630,6 +860,14 @@ static int bus_parse_next_address(sd_bus *b) { break; + } else if (startswith(a, "kernel:")) { + + a += 7; + r = parse_kernel_address(b, &a, &guid); + if (r < 0) + return r; + + break; } a = strchr(a, ';'); @@ -638,7 +876,7 @@ static int bus_parse_next_address(sd_bus *b) { } if (guid) { - r = sd_id128_from_string(guid, &b->peer); + r = sd_id128_from_string(guid, &b->server_id); if (r < 0) return r; } @@ -653,10 +891,7 @@ static int bus_start_address(sd_bus *b) { assert(b); for (;;) { - if (b->fd >= 0) { - close_nointr_nofail(b->fd); - b->fd = -1; - } + sd_bus_close(b); if (b->sockaddr.sa.sa_family != AF_UNSPEC) { @@ -672,6 +907,13 @@ static int bus_start_address(sd_bus *b) { if (r >= 0) return r; + b->last_connect_error = -r; + } else if (b->kernel) { + + r = bus_kernel_connect(b); + if (r >= 0) + return r; + b->last_connect_error = -r; } @@ -691,19 +933,38 @@ int bus_next_address(sd_bus *b) { } static int bus_start_fd(sd_bus *b) { + struct stat st; int r; assert(b); + assert(b->input_fd >= 0); + assert(b->output_fd >= 0); - r = fd_nonblock(b->fd, true); + r = fd_nonblock(b->input_fd, true); if (r < 0) return r; - r = fd_cloexec(b->fd, true); + r = fd_cloexec(b->input_fd, true); if (r < 0) return r; - return bus_socket_take_fd(b); + if (b->input_fd != b->output_fd) { + r = fd_nonblock(b->output_fd, true); + if (r < 0) + return r; + + r = fd_cloexec(b->output_fd, true); + if (r < 0) + return r; + } + + if (fstat(b->input_fd, &st) < 0) + return -errno; + + if (S_ISCHR(b->input_fd)) + return bus_kernel_take_fd(b); + else + return bus_socket_take_fd(b); } int sd_bus_start(sd_bus *bus) { @@ -713,12 +974,17 @@ int sd_bus_start(sd_bus *bus) { return -EINVAL; if (bus->state != BUS_UNSET) return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; bus->state = BUS_OPENING; - if (bus->fd >= 0) + if (bus->is_server && bus->bus_client) + return -EINVAL; + + if (bus->input_fd >= 0) r = bus_start_fd(bus); - else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path) + else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel) r = bus_start_address(bus); else return -EINVAL; @@ -741,7 +1007,7 @@ int sd_bus_open_system(sd_bus **ret) { if (r < 0) return r; - e = getenv("DBUS_SYSTEM_BUS_ADDRESS"); + e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); if (e) { r = sd_bus_set_address(b, e); if (r < 0) @@ -779,13 +1045,13 @@ int sd_bus_open_user(sd_bus **ret) { if (r < 0) return r; - e = getenv("DBUS_SESSION_BUS_ADDRESS"); + e = secure_getenv("DBUS_SESSION_BUS_ADDRESS"); if (e) { r = sd_bus_set_address(b, e); if (r < 0) goto fail; } else { - e = getenv("XDG_RUNTIME_DIR"); + e = secure_getenv("XDG_RUNTIME_DIR"); if (!e) { r = -ENOENT; goto fail; @@ -819,20 +1085,29 @@ fail: void sd_bus_close(sd_bus *bus) { if (!bus) return; - if (bus->fd < 0) + if (bus->state == BUS_CLOSED) + return; + if (bus_pid_changed(bus)) return; - close_nointr_nofail(bus->fd); - bus->fd = -1; + bus->state = BUS_CLOSED; + + if (!bus->is_kernel) + bus_close_fds(bus); + + /* We'll leave the fd open in case this is a kernel bus, since + * there might still be memblocks around that reference this + * bus, and they might need to invoke the + * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are + * freed. */ } sd_bus *sd_bus_ref(sd_bus *bus) { if (!bus) return NULL; - assert(bus->n_ref > 0); + assert_se(REFCNT_INC(bus->n_ref) >= 2); - bus->n_ref++; return bus; } @@ -840,10 +1115,7 @@ sd_bus *sd_bus_unref(sd_bus *bus) { if (!bus) return NULL; - assert(bus->n_ref > 0); - bus->n_ref--; - - if (bus->n_ref <= 0) + if (REFCNT_DEC(bus->n_ref) <= 0) bus_free(bus); return NULL; @@ -852,8 +1124,10 @@ sd_bus *sd_bus_unref(sd_bus *bus) { int sd_bus_is_open(sd_bus *bus) { if (!bus) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; - return bus->state != BUS_UNSET && bus->fd >= 0; + return BUS_IS_OPEN(bus->state); } int sd_bus_can_send(sd_bus *bus, char type) { @@ -861,11 +1135,13 @@ int sd_bus_can_send(sd_bus *bus, char type) { if (!bus) return -EINVAL; - if (bus->fd < 0) + if (bus->state == BUS_UNSET) return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; if (type == SD_BUS_TYPE_UNIX_FD) { - if (!bus->negotiate_fds) + if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) return 0; r = bus_ensure_running(bus); @@ -878,19 +1154,21 @@ int sd_bus_can_send(sd_bus *bus, char type) { return bus_type_is_valid(type); } -int sd_bus_get_peer(sd_bus *bus, sd_id128_t *peer) { +int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) { int r; if (!bus) return -EINVAL; - if (!peer) + if (!server_id) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; r = bus_ensure_running(bus); if (r < 0) return r; - *peer = bus->peer; + *server_id = bus->server_id; return 0; } @@ -912,19 +1190,20 @@ static int dispatch_wqueue(sd_bus *bus) { assert(bus); assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - if (bus->fd < 0) - return -ENOTCONN; - while (bus->wqueue_size > 0) { - r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex); + if (bus->is_kernel) + r = bus_kernel_write_message(bus, bus->wqueue[0]); + else + r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex); + if (r < 0) { sd_bus_close(bus); return r; } else if (r == 0) /* Didn't do anything this time */ return ret; - else if (bus->windex >= bus->wqueue[0]->size) { + else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) { /* Fully written. Let's drop the entry from * the queue. * @@ -955,9 +1234,6 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { assert(m); assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); - if (bus->fd < 0) - return -ENOTCONN; - if (bus->rqueue_size > 0) { /* Dispatch a queued message */ @@ -969,7 +1245,11 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { /* Try to read a new message */ do { - r = bus_socket_read_message(bus, &z); + if (bus->is_kernel) + r = bus_kernel_read_message(bus, &z); + else + r = bus_socket_read_message(bus, &z); + if (r < 0) { sd_bus_close(bus); return r; @@ -977,11 +1257,11 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { if (r == 0) return ret; - r = 1; + ret = 1; } while (!z); *m = z; - return 1; + return ret; } int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) { @@ -989,12 +1269,12 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) { if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; if (!m) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; if (m->n_fds > 0) { r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD); @@ -1021,11 +1301,15 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) { if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) { size_t idx = 0; - r = bus_socket_write_message(bus, m, &idx); + if (bus->is_kernel) + r = bus_kernel_write_message(bus, m); + else + r = bus_socket_write_message(bus, m, &idx); + if (r < 0) { sd_bus_close(bus); return r; - } else if (idx < m->size) { + } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) { /* Wasn't fully written. So let's remember how * much was written. Note that the first entry * of the wqueue array is always allocated so @@ -1088,7 +1372,7 @@ static int timeout_compare(const void *a, const void *b) { int sd_bus_send_with_reply( sd_bus *bus, sd_bus_message *m, - sd_message_handler_t callback, + sd_bus_message_handler_t callback, void *userdata, uint64_t usec, uint64_t *serial) { @@ -1098,9 +1382,7 @@ int sd_bus_send_with_reply( if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; if (!m) return -EINVAL; @@ -1110,6 +1392,8 @@ int sd_bus_send_with_reply( return -EINVAL; if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func); if (r < 0) @@ -1125,7 +1409,7 @@ int sd_bus_send_with_reply( if (r < 0) return r; - c = new(struct reply_callback, 1); + c = new0(struct reply_callback, 1); if (!c) return -ENOMEM; @@ -1165,6 +1449,8 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) { return -EINVAL; if (serial == 0) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; c = hashmap_remove(bus->reply_callbacks, &serial); if (!c) @@ -1182,11 +1468,8 @@ int bus_ensure_running(sd_bus *bus) { assert(bus); - if (bus->fd < 0) - return -ENOTCONN; - if (bus->state == BUS_UNSET) + if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED) return -ENOTCONN; - if (bus->state == BUS_RUNNING) return 1; @@ -1219,9 +1502,7 @@ int sd_bus_send_with_reply_and_block( if (!bus) return -EINVAL; - if (bus->fd < 0) - return -ENOTCONN; - if (bus->state == BUS_UNSET) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; if (!m) return -EINVAL; @@ -1231,6 +1512,8 @@ int sd_bus_send_with_reply_and_block( return -EINVAL; if (bus_error_is_dirty(error)) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; r = bus_ensure_running(bus); if (r < 0) @@ -1263,7 +1546,10 @@ int sd_bus_send_with_reply_and_block( room = true; } - r = bus_socket_read_message(bus, &incoming); + if (bus->is_kernel) + r = bus_kernel_read_message(bus, &incoming); + else + r = bus_socket_read_message(bus, &incoming); if (r < 0) return r; if (incoming) { @@ -1272,7 +1558,12 @@ int sd_bus_send_with_reply_and_block( /* Found a match! */ if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) { - *reply = incoming; + + if (reply) + *reply = incoming; + else + sd_bus_message_unref(incoming); + return 0; } @@ -1329,11 +1620,14 @@ int sd_bus_send_with_reply_and_block( int sd_bus_get_fd(sd_bus *bus) { if (!bus) return -EINVAL; - - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; + if (bus->input_fd != bus->output_fd) + return -EPERM; + if (bus_pid_changed(bus)) + return -ECHILD; - return bus->fd; + return bus->input_fd; } int sd_bus_get_events(sd_bus *bus) { @@ -1341,16 +1635,16 @@ int sd_bus_get_events(sd_bus *bus) { if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; if (bus->state == BUS_OPENING) flags |= POLLOUT; else if (bus->state == BUS_AUTHENTICATING) { - if (bus->auth_index < ELEMENTSOF(bus->auth_iovec)) + if (bus_socket_auth_needs_write(bus)) flags |= POLLOUT; flags |= POLLIN; @@ -1372,28 +1666,33 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) { return -EINVAL; if (!timeout_usec) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; if (bus->state == BUS_AUTHENTICATING) { *timeout_usec = bus->auth_timeout; return 1; } - if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) + if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) { + *timeout_usec = (uint64_t) -1; return 0; + } c = prioq_peek(bus->reply_callbacks_prioq); - if (!c) + if (!c) { + *timeout_usec = (uint64_t) -1; return 0; + } *timeout_usec = c->timeout; return 1; } static int process_timeout(sd_bus *bus) { + _cleanup_bus_message_unref_ sd_bus_message* m = NULL; struct reply_callback *c; usec_t n; int r; @@ -1408,10 +1707,18 @@ static int process_timeout(sd_bus *bus) { if (c->timeout > n) return 0; + r = bus_message_new_synthetic_error( + bus, + c->serial, + &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"), + &m); + if (r < 0) + return r; + assert_se(prioq_pop(bus->reply_callbacks_prioq) == c); hashmap_remove(bus->reply_callbacks, &c->serial); - r = c->callback(bus, ETIMEDOUT, NULL, c->userdata); + r = c->callback(bus, m, c->userdata); free(c); return r < 0 ? r : 1; @@ -1426,8 +1733,8 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) { /* 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. */ + * here (we leave that to the usual 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) @@ -1457,7 +1764,11 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { if (c->timeout != 0) prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx); - r = c->callback(bus, 0, m, c->userdata); + r = sd_bus_message_rewind(m, true); + if (r < 0) + return r; + + r = c->callback(bus, m, c->userdata); free(c); return r; @@ -1467,216 +1778,94 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) { struct filter_callback *l; int r; - LIST_FOREACH(callbacks, l, bus->filter_callbacks) { - r = l->callback(bus, 0, m, l->userdata); - if (r != 0) - return r; - } - - return 0; -} - -static int process_builtin(sd_bus *bus, sd_bus_message *m) { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - int r; - assert(bus); assert(m); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) - return 0; - - if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer")) - return 0; - - if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) - return 1; + do { + bus->filter_callbacks_modified = false; - if (streq_ptr(m->member, "Ping")) - r = sd_bus_message_new_method_return(bus, m, &reply); - else if (streq_ptr(m->member, "GetMachineId")) { - sd_id128_t id; - char sid[33]; + LIST_FOREACH(callbacks, l, bus->filter_callbacks) { - r = sd_id128_get_machine(&id); - if (r < 0) - return r; + if (bus->filter_callbacks_modified) + break; - r = sd_bus_message_new_method_return(bus, m, &reply); - if (r < 0) - return r; + /* Don't run this more than once per iteration */ + if (l->last_iteration == bus->iteration_counter) + continue; - r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid)); - } else { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT; + l->last_iteration = bus->iteration_counter; - sd_bus_error_set(&error, - "org.freedesktop.DBus.Error.UnknownMethod", - "Unknown method '%s' on interface '%s'.", m->member, m->interface); + r = sd_bus_message_rewind(m, true); + if (r < 0) + return r; - r = sd_bus_message_new_method_error(bus, m, &error, &reply); - } + r = l->callback(bus, m, l->userdata); + if (r != 0) + return r; - if (r < 0) - return r; + } - r = sd_bus_send(bus, reply, NULL); - if (r < 0) - return r; + } while (bus->filter_callbacks_modified); - return 1; + return 0; } -static int process_object(sd_bus *bus, sd_bus_message *m) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT; - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - struct object_callback *c; - char *p; +static int process_match(sd_bus *bus, sd_bus_message *m) { int r; - bool found = false; assert(bus); assert(m); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) - return 0; - - if (hashmap_isempty(bus->object_callbacks)) - return 0; + do { + bus->match_callbacks_modified = false; - c = hashmap_get(bus->object_callbacks, m->path); - if (c) { - r = c->callback(bus, 0, m, c->userdata); + r = bus_match_run(bus, &bus->match_callbacks, m); if (r != 0) return r; - found = true; - } - - /* Look for fallback prefixes */ - p = strdupa(m->path); - for (;;) { - char *e; - - e = strrchr(p, '/'); - if (e == p || !e) - break; + } while (bus->match_callbacks_modified); - *e = 0; - - c = hashmap_get(bus->object_callbacks, p); - if (c && c->is_fallback) { - r = c->callback(bus, 0, m, c->userdata); - if (r != 0) - return r; - - found = true; - } - } - - /* We found some handlers but none wanted to take this, then - * return this -- with one exception, we can handle - * introspection minimally ourselves */ - if (!found || sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) - return 0; - - sd_bus_error_set(&error, - "org.freedesktop.DBus.Error.UnknownMethod", - "Unknown method '%s' or interface '%s'.", m->member, m->interface); - - r = sd_bus_message_new_method_error(bus, m, &error, &reply); - if (r < 0) - return r; - - r = sd_bus_send(bus, reply, NULL); - if (r < 0) - return r; - - return 1; + return 0; } -static int process_introspect(sd_bus *bus, sd_bus_message *m) { +static int process_builtin(sd_bus *bus, sd_bus_message *m) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_free_ char *introspection = NULL; - _cleanup_set_free_free_ Set *s = NULL; - _cleanup_fclose_ FILE *f = NULL; - struct object_callback *c; - Iterator i; - size_t size = 0; - char *node; int r; assert(bus); assert(m); - if (!sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) + if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) return 0; - if (!m->path) + if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer")) return 0; - s = set_new(string_hash_func, string_compare_func); - if (!s) - return -ENOMEM; - - HASHMAP_FOREACH(c, bus->object_callbacks, i) { - const char *e; - char *a, *p; - - if (streq(c->path, "/")) - continue; - - if (streq(m->path, "/")) - e = c->path; - else { - e = startswith(c->path, m->path); - if (!e || *e != '/') - continue; - } - - a = strdup(e+1); - if (!a) - return -ENOMEM; - - p = strchr(a, '/'); - if (p) - *p = 0; - - r = set_put(s, a); - if (r < 0) { - free(a); + if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) + return 1; - if (r != -EEXIST) - return r; - } - } + if (streq_ptr(m->member, "Ping")) + r = sd_bus_message_new_method_return(bus, m, &reply); + else if (streq_ptr(m->member, "GetMachineId")) { + sd_id128_t id; + char sid[33]; - f = open_memstream(&introspection, &size); - if (!f) - return -ENOMEM; + r = sd_id128_get_machine(&id); + if (r < 0) + return r; - fputs(SD_BUS_INTROSPECT_DOCTYPE, f); - fputs("\n", f); - fputs(SD_BUS_INTROSPECT_INTERFACE_PEER, f); - fputs(SD_BUS_INTROSPECT_INTERFACE_INTROSPECTABLE, f); + r = sd_bus_message_new_method_return(bus, m, &reply); + if (r < 0) + return r; - while ((node = set_steal_first(s))) { - fprintf(f, " \n", node); - free(node); + r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid)); + } else { + r = sd_bus_message_new_method_errorf( + bus, m, &reply, + "org.freedesktop.DBus.Error.UnknownMethod", + "Unknown method '%s' on interface '%s'.", m->member, m->interface); } - fputs("\n", f); - - fflush(f); - - if (ferror(f)) - return -ENOMEM; - - r = sd_bus_message_new_method_return(bus, m, &reply); - if (r < 0) - return r; - - r = sd_bus_message_append(reply, "s", introspection); if (r < 0) return r; @@ -1693,6 +1882,8 @@ static int process_message(sd_bus *bus, sd_bus_message *m) { assert(bus); assert(m); + bus->iteration_counter++; + r = process_hello(bus, m); if (r != 0) return r; @@ -1705,15 +1896,15 @@ static int process_message(sd_bus *bus, sd_bus_message *m) { if (r != 0) return r; - r = process_builtin(bus, m); + r = process_match(bus, m); if (r != 0) return r; - r = process_object(bus, m); + r = process_builtin(bus, m); if (r != 0) return r; - return process_introspect(bus, m); + return bus_process_object(bus, m); } static int process_running(sd_bus *bus, sd_bus_message **ret) { @@ -1742,22 +1933,21 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) { goto null_message; if (ret) { + r = sd_bus_message_rewind(m, true); + if (r < 0) + return r; + *ret = m; m = NULL; return 1; } if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) { - _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT; - - sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path); - - r = sd_bus_message_new_method_error(bus, m, &error, &reply); - if (r < 0) - return r; - r = sd_bus_send(bus, reply, NULL); + r = sd_bus_reply_method_errorf( + bus, m, + "org.freedesktop.DBus.Error.UnknownObject", + "Unknown object '%s'.", m->path); if (r < 0) return r; } @@ -1781,12 +1971,17 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { if (!bus) return -EINVAL; - if (bus->fd < 0) - return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; + + /* We don't allow recursively invoking sd_bus_process(). */ + if (bus->processing) + return -EBUSY; switch (bus->state) { case BUS_UNSET: + case BUS_CLOSED: return -ENOTCONN; case BUS_OPENING: @@ -1809,21 +2004,25 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { case BUS_RUNNING: case BUS_HELLO: - return process_running(bus, ret); + bus->processing = true; + r = process_running(bus, ret); + bus->processing = false; + + return r; } assert_not_reached("Unknown state"); } static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) { - struct pollfd p; - int r, e; + struct pollfd p[2] = {}; + int r, e, n; struct timespec ts; usec_t until, m; assert(bus); - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; e = sd_bus_get_events(bus); @@ -1839,19 +2038,26 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) { if (r == 0) m = (uint64_t) -1; else { - usec_t n; - n = now(CLOCK_MONOTONIC); - m = until > n ? until - n : 0; + usec_t nw; + nw = now(CLOCK_MONOTONIC); + m = until > nw ? until - nw : 0; } if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m)) m = timeout_usec; - zero(p); - p.fd = bus->fd; - p.events = e; + p[0].fd = bus->input_fd; + if (bus->output_fd == bus->input_fd) { + p[0].events = e; + n = 1; + } else { + p[0].events = e & POLLIN; + p[1].fd = bus->output_fd; + p[1].events = e & POLLOUT; + n = 2; + } - r = ppoll(&p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL); + r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL); if (r < 0) return -errno; @@ -1862,10 +2068,11 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) { if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; + if (bus->rqueue_size > 0) return 0; @@ -1877,10 +2084,10 @@ int sd_bus_flush(sd_bus *bus) { if (!bus) return -EINVAL; - if (bus->state == BUS_UNSET) - return -ENOTCONN; - if (bus->fd < 0) + if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; r = bus_ensure_running(bus); if (r < 0) @@ -1903,34 +2110,40 @@ int sd_bus_flush(sd_bus *bus) { } } -int sd_bus_add_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata) { +int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) { struct filter_callback *f; if (!bus) return -EINVAL; if (!callback) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; - f = new(struct filter_callback, 1); + f = new0(struct filter_callback, 1); if (!f) return -ENOMEM; f->callback = callback; f->userdata = userdata; + bus->filter_callbacks_modified = true; LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f); return 0; } -int sd_bus_remove_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata) { +int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) { struct filter_callback *f; if (!bus) return -EINVAL; if (!callback) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; LIST_FOREACH(callbacks, f, bus->filter_callbacks) { if (f->callback == callback && f->userdata == userdata) { + bus->filter_callbacks_modified = true; LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f); free(f); return 1; @@ -1940,94 +2153,76 @@ int sd_bus_remove_filter(sd_bus *bus, sd_message_handler_t callback, void *userd return 0; } -static int bus_add_object( - sd_bus *bus, - bool fallback, - const char *path, - sd_message_handler_t callback, - void *userdata) { - - struct object_callback *c; - int r; +int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) { + struct bus_match_component *components = NULL; + unsigned n_components = 0; + uint64_t cookie = 0; + int r = 0; if (!bus) return -EINVAL; - if (!path) - return -EINVAL; - if (!callback) + if (!match) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; - r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func); + r = bus_match_parse(match, &components, &n_components); if (r < 0) - return r; + goto finish; - c = new(struct object_callback, 1); - if (!c) - return -ENOMEM; + if (bus->bus_client) { + cookie = ++bus->match_cookie; - c->path = strdup(path); - if (!c->path) { - free(c); - return -ENOMEM; + r = bus_add_match_internal(bus, match, components, n_components, cookie); + if (r < 0) + goto finish; } - c->callback = callback; - c->userdata = userdata; - c->is_fallback = fallback; - - r = hashmap_put(bus->object_callbacks, c->path, c); + bus->match_callbacks_modified = true; + r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL); if (r < 0) { - free(c->path); - free(c); - return r; + if (bus->bus_client) + bus_remove_match_internal(bus, match, cookie); } - return 0; +finish: + bus_match_parse_free(components, n_components); + return r; } -static int bus_remove_object( - sd_bus *bus, - bool fallback, - const char *path, - sd_message_handler_t callback, - void *userdata) { - - struct object_callback *c; +int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) { + struct bus_match_component *components = NULL; + unsigned n_components = 0; + int r = 0, q = 0; + uint64_t cookie = 0; if (!bus) return -EINVAL; - if (!path) - return -EINVAL; - if (!callback) + if (!match) return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; - c = hashmap_get(bus->object_callbacks, path); - if (!c) - return 0; - - if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback) - return 0; + r = bus_match_parse(match, &components, &n_components); + if (r < 0) + return r; - assert_se(c == hashmap_remove(bus->object_callbacks, c->path)); + bus->match_callbacks_modified = true; + r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie); - free(c->path); - free(c); + if (bus->bus_client) + q = bus_remove_match_internal(bus, match, cookie); - return 1; -} + bus_match_parse_free(components, n_components); -int sd_bus_add_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) { - return bus_add_object(bus, false, path, callback, userdata); + return r < 0 ? r : q; } -int sd_bus_remove_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) { - return bus_remove_object(bus, false, path, callback, userdata); -} +bool bus_pid_changed(sd_bus *bus) { + assert(bus); -int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) { - return bus_add_object(bus, true, prefix, callback, userdata); -} + /* We don't support people creating a bus connection and + * keeping it around over a fork(). Let's complain. */ -int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) { - return bus_remove_object(bus, true, prefix, callback, userdata); + return bus->original_pid != getpid(); }