X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd%2Fsd-bus%2Fbus-control.c;h=fa321d5315c79d6aa57aba84c860dbfc0fdd876c;hb=659b937e670a5cb423a274b97b1eb60abd7d218f;hp=846307d6647d1f23d94804efba66b5f067103d96;hpb=b28ff39f42877daef31383748fd2f313d7fd67c1;p=elogind.git diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c index 846307d66..fa321d531 100644 --- a/src/libsystemd/sd-bus/bus-control.c +++ b/src/libsystemd/sd-bus/bus-control.c @@ -58,12 +58,15 @@ static int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags assert(bus); assert(name); - l = strlen(name); - size = offsetof(struct kdbus_cmd_name, name) + l + 1; - n = alloca0(size); + l = strlen(name) + 1; + size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l); + n = alloca0_align(size, 8); n->size = size; kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags); - memcpy(n->name, name, l+1); + + n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l; + n->items[0].type = KDBUS_ITEM_NAME; + memcpy(n->items[0].str, name, l); #ifdef HAVE_VALGRIND_MEMCHECK_H VALGRIND_MAKE_MEM_DEFINED(n, n->size); @@ -128,12 +131,14 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) assert_return(bus, -EINVAL); assert_return(name, -EINVAL); assert_return(bus->bus_client, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL); assert_return(service_name_is_valid(name), -EINVAL); assert_return(name[0] != ':', -EINVAL); + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus->is_kernel) return bus_request_name_kernel(bus, name, flags); else @@ -142,16 +147,20 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags) static int bus_release_name_kernel(sd_bus *bus, const char *name) { struct kdbus_cmd_name *n; - size_t l; + size_t size, l; int r; assert(bus); assert(name); - l = strlen(name); - n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1); - n->size = offsetof(struct kdbus_cmd_name, name) + l + 1; - memcpy(n->name, name, l+1); + l = strlen(name) + 1; + size = offsetof(struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(l); + n = alloca0_align(size, 8); + n->size = size; + + n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l; + n->items[0].type = KDBUS_ITEM_NAME; + memcpy(n->items[0].str, name, l); #ifdef HAVE_VALGRIND_MEMCHECK_H VALGRIND_MAKE_MEM_DEFINED(n, n->size); @@ -160,7 +169,7 @@ static int bus_release_name_kernel(sd_bus *bus, const char *name) { if (r < 0) return -errno; - return n->flags; + return 0; } static int bus_release_name_dbus1(sd_bus *bus, const char *name) { @@ -201,17 +210,36 @@ _public_ int sd_bus_release_name(sd_bus *bus, const char *name) { assert_return(bus, -EINVAL); assert_return(name, -EINVAL); assert_return(bus->bus_client, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(service_name_is_valid(name), -EINVAL); assert_return(name[0] != ':', -EINVAL); + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus->is_kernel) return bus_release_name_kernel(bus, name); else return bus_release_name_dbus1(bus, name); } +static int kernel_cmd_free(sd_bus *bus, uint64_t offset) +{ + struct kdbus_cmd_free cmd; + int r; + + assert(bus); + + cmd.flags = 0; + cmd.offset = offset; + + r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd); + if (r < 0) + return -errno; + + return 0; +} + static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) { struct kdbus_cmd_name_list cmd = {}; struct kdbus_name_list *name_list; @@ -231,33 +259,42 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) { KDBUS_ITEM_FOREACH(name, name_list, names) { + struct kdbus_item *item; + const char *entry_name = NULL; + if ((flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != previous_id) { char *n; - if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0) - return -ENOMEM; - - r = strv_push(x, n); - if (r < 0) { - free(n); - return -ENOMEM; + if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0) { + r = -ENOMEM; + goto fail; } + r = strv_consume(x, n); + if (r < 0) + goto fail; + previous_id = name->owner_id; } - if (name->size > sizeof(*name) && service_name_is_valid(name->name)) { - r = strv_extend(x, name->name); - if (r < 0) - return -ENOMEM; + KDBUS_ITEM_FOREACH(item, name, items) + if (item->type == KDBUS_ITEM_NAME) + entry_name = item->str; + + if (entry_name && service_name_is_valid(entry_name)) { + r = strv_extend(x, entry_name); + if (r < 0) { + r = -ENOMEM; + goto fail; + } } } - r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset); - if (r < 0) - return -errno; + r = 0; - return 0; +fail: + kernel_cmd_free(bus, cmd.offset); + return r; } static int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) { @@ -344,9 +381,11 @@ static int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatab _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) { assert_return(bus, -EINVAL); assert_return(acquired || activatable, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus->is_kernel) return bus_list_names_kernel(bus, acquired, activatable); else @@ -363,7 +402,7 @@ static int bus_get_owner_kdbus( struct kdbus_cmd_conn_info *cmd; struct kdbus_conn_info *conn_info; struct kdbus_item *item; - size_t size; + size_t size, l; uint64_t m, id; int r; @@ -371,13 +410,16 @@ static int bus_get_owner_kdbus( if (r < 0) return r; if (r > 0) { - size = offsetof(struct kdbus_cmd_conn_info, name); - cmd = alloca0(size); + size = offsetof(struct kdbus_cmd_conn_info, items); + cmd = alloca0_align(size, 8); cmd->id = id; } else { - size = offsetof(struct kdbus_cmd_conn_info, name) + strlen(name) + 1; - cmd = alloca0(size); - strcpy(cmd->name, name); + l = strlen(name) + 1; + size = offsetof(struct kdbus_cmd_conn_info, items) + KDBUS_ITEM_SIZE(l); + cmd = alloca0_align(size, 8); + cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + l; + cmd->items[0].type = KDBUS_ITEM_NAME; + memcpy(cmd->items[0].str, name, l); } cmd->size = size; @@ -390,16 +432,25 @@ static int bus_get_owner_kdbus( conn_info = (struct kdbus_conn_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset); /* Non-activated names are considered not available */ - if (conn_info->flags & KDBUS_HELLO_ACTIVATOR) - return name[0] == ':' ? -ENXIO : -ENOENT; + if (conn_info->flags & KDBUS_HELLO_ACTIVATOR) { + if (name[0] == ':') + r = -ENXIO; + else + r = -ESRCH; + goto fail; + } c = bus_creds_new(); - if (!c) - return -ENOMEM; + if (!c) { + r = -ENOMEM; + goto fail; + } if (mask & SD_BUS_CREDS_UNIQUE_NAME) { - if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0) - return -ENOMEM; + if (asprintf(&c->unique_name, ":1.%llu", (unsigned long long) conn_info->id) < 0) { + r = -ENOMEM; + goto fail; + } c->mask |= SD_BUS_CREDS_UNIQUE_NAME; } @@ -491,11 +542,9 @@ static int bus_get_owner_kdbus( goto fail; } - if (!bus->cgroup_root) { - r = cg_get_root_path(&bus->cgroup_root); - if (r < 0) - goto fail; - } + r = bus_get_root_path(bus); + if (r < 0) + goto fail; c->cgroup_root = strdup(bus->cgroup_root); if (!c->cgroup_root) { @@ -577,7 +626,7 @@ static int bus_get_owner_kdbus( r = 0; fail: - ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd->offset); + kernel_cmd_free(bus, cmd->offset); return r; } @@ -687,8 +736,8 @@ static int bus_get_owner_dbus1( } if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) { - const void *p; - size_t sz; + const void *p = NULL; + size_t sz = 0; r = sd_bus_call_method( bus, @@ -737,11 +786,13 @@ _public_ int sd_bus_get_owner( assert_return(name, -EINVAL); assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP); assert_return(mask == 0 || creds, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(service_name_is_valid(name), -EINVAL); assert_return(bus->bus_client, -ENODATA); + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus->is_kernel) return bus_get_owner_kdbus(bus, name, mask, creds); else @@ -823,7 +874,7 @@ static int add_name_change_match(sd_bus *bus, offsetof(struct kdbus_notify_name_change, name) + l); - m = alloca0(sz); + m = alloca0_align(sz, 8); m->size = sz; m->cookie = cookie; @@ -833,8 +884,8 @@ static int add_name_change_match(sd_bus *bus, offsetof(struct kdbus_notify_name_change, name) + l; - item->name_change.old.id = old_owner_id; - item->name_change.new.id = new_owner_id; + item->name_change.old_id.id = old_owner_id; + item->name_change.new_id.id = new_owner_id; if (name) memcpy(item->name_change.name, name, l); @@ -883,7 +934,7 @@ static int add_name_change_match(sd_bus *bus, offsetof(struct kdbus_item, id_change) + sizeof(struct kdbus_notify_id_change)); - m = alloca0(sz); + m = alloca0_align(sz, 8); m->size = sz; m->cookie = cookie; @@ -919,7 +970,6 @@ static int add_name_change_match(sd_bus *bus, int bus_add_match_internal_kernel( sd_bus *bus, - uint64_t id, struct bus_match_component *components, unsigned n_components, uint64_t cookie) { @@ -1054,10 +1104,9 @@ int bus_add_match_internal_kernel( if (using_bloom) sz += ALIGN8(offsetof(struct kdbus_item, data64) + bus->bloom_size); - m = alloca0(sz); + m = alloca0_align(sz, 8); m->size = sz; m->cookie = cookie; - m->owner_id = id; item = m->items; @@ -1100,13 +1149,22 @@ int bus_add_match_internal_kernel( return 0; } +#define internal_match(bus, m) \ + ((bus)->hello_flags & KDBUS_HELLO_MONITOR \ + ? (isempty(m) ? "eavesdrop='true'" : strappenda((m), ",eavesdrop='true'")) \ + : (m)) + static int bus_add_match_internal_dbus1( sd_bus *bus, const char *match) { + const char *e; + assert(bus); assert(match); + e = internal_match(bus, match); + return sd_bus_call_method( bus, "org.freedesktop.DBus", @@ -1116,7 +1174,7 @@ static int bus_add_match_internal_dbus1( NULL, NULL, "s", - match); + e); } int bus_add_match_internal( @@ -1127,17 +1185,15 @@ int bus_add_match_internal( uint64_t cookie) { assert(bus); - assert(match); if (bus->is_kernel) - return bus_add_match_internal_kernel(bus, 0, components, n_components, cookie); + return bus_add_match_internal_kernel(bus, components, n_components, cookie); else return bus_add_match_internal_dbus1(bus, match); } int bus_remove_match_internal_kernel( sd_bus *bus, - uint64_t id, uint64_t cookie) { struct kdbus_cmd_match m; @@ -1148,7 +1204,6 @@ int bus_remove_match_internal_kernel( zero(m); m.size = offsetof(struct kdbus_cmd_match, items); m.cookie = cookie; - m.owner_id = id; r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m); if (r < 0) @@ -1161,9 +1216,13 @@ static int bus_remove_match_internal_dbus1( sd_bus *bus, const char *match) { + const char *e; + assert(bus); assert(match); + e = internal_match(bus, match); + return sd_bus_call_method( bus, "org.freedesktop.DBus", @@ -1173,7 +1232,7 @@ static int bus_remove_match_internal_dbus1( NULL, NULL, "s", - match); + e); } int bus_remove_match_internal( @@ -1182,10 +1241,9 @@ int bus_remove_match_internal( uint64_t cookie) { assert(bus); - assert(match); if (bus->is_kernel) - return bus_remove_match_internal_kernel(bus, 0, cookie); + return bus_remove_match_internal_kernel(bus, cookie); else return bus_remove_match_internal_dbus1(bus, match); } @@ -1198,19 +1256,22 @@ _public_ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128 assert_return(bus, -EINVAL); assert_return(name, -EINVAL); assert_return(machine, -EINVAL); - assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); assert_return(service_name_is_valid(name), -EINVAL); + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (streq_ptr(name, bus->unique_name)) return sd_id128_get_machine(machine); r = sd_bus_message_new_method_call( bus, + &m, name, "/", "org.freedesktop.DBus.Peer", - "GetMachineId", &m); + "GetMachineId"); if (r < 0) return r;