X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fbus-control.c;h=2682439427b606621ffe2cc41aa5518a5cb6db77;hp=de5c20e3f8f6b0bee3d967627d92fd7a57dc84fd;hb=29a07cdb4a317f2e1ea160b79bfe6eb1be2e6e01;hpb=e7176abbe818c75c6acd90227a7a84c3e05fee31 diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c index de5c20e3f..268243942 100644 --- a/src/libsystemd-bus/bus-control.c +++ b/src/libsystemd-bus/bus-control.c @@ -27,7 +27,6 @@ #include #include "strv.h" - #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" @@ -50,7 +49,7 @@ _public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) { return 0; } -static int bus_request_name_kernel(sd_bus *bus, const char *name, unsigned flags) { +static int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags) { struct kdbus_cmd_name *n; size_t l; int r; @@ -78,14 +77,21 @@ static int bus_request_name_kernel(sd_bus *bus, const char *name, unsigned flags return 1; } -static int bus_request_name_dbus1(sd_bus *bus, const char *name, unsigned flags) { +static int bus_request_name_dbus1(sd_bus *bus, const char *name, uint64_t flags) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - uint32_t ret; + uint32_t ret, param = 0; int r; assert(bus); assert(name); + if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT) + param |= BUS_NAME_ALLOW_REPLACEMENT; + if (flags & SD_BUS_NAME_REPLACE_EXISTING) + param |= BUS_NAME_REPLACE_EXISTING; + if (!(flags & SD_BUS_NAME_QUEUE)) + param |= BUS_NAME_DO_NOT_QUEUE; + r = sd_bus_call_method( bus, "org.freedesktop.DBus", @@ -96,7 +102,7 @@ static int bus_request_name_dbus1(sd_bus *bus, const char *name, unsigned flags) &reply, "su", name, - flags); + param); if (r < 0) return r; @@ -104,25 +110,25 @@ static int bus_request_name_dbus1(sd_bus *bus, const char *name, unsigned flags) if (r < 0) return r; - if (ret == SD_BUS_NAME_ALREADY_OWNER) + if (ret == BUS_NAME_ALREADY_OWNER) return -EALREADY; - else if (ret == SD_BUS_NAME_EXISTS) + else if (ret == BUS_NAME_EXISTS) return -EEXIST; - else if (ret == SD_BUS_NAME_IN_QUEUE) + else if (ret == BUS_NAME_IN_QUEUE) return 0; - else - return -EIO; + else if (ret == BUS_NAME_PRIMARY_OWNER) + return 1; - return 1; + return -EIO; } -_public_ int sd_bus_request_name(sd_bus *bus, const char *name, unsigned flags) { +_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_DO_NOT_QUEUE)), -EINVAL); + assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL); if (bus->is_kernel) return bus_request_name_kernel(bus, name, flags); @@ -177,11 +183,11 @@ static int bus_release_name_dbus1(sd_bus *bus, const char *name) { r = sd_bus_message_read(reply, "u", &ret); if (r < 0) return r; - if (ret == SD_BUS_NAME_NON_EXISTENT) + if (ret == BUS_NAME_NON_EXISTENT) return -ENOENT; - if (ret == SD_BUS_NAME_NOT_OWNER) + if (ret == BUS_NAME_NOT_OWNER) return -EADDRNOTAVAIL; - if (ret == SD_BUS_NAME_RELEASED) + if (ret == BUS_NAME_RELEASED) return 0; return -EINVAL; @@ -200,102 +206,145 @@ _public_ int sd_bus_release_name(sd_bus *bus, const char *name) { return bus_release_name_dbus1(bus, name); } -static int bus_list_names_kernel(sd_bus *bus, char ***l) { - _cleanup_free_ struct kdbus_cmd_name_list *cmd = NULL; +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; struct kdbus_cmd_name *name; - char **x = NULL; + uint64_t previous_id = 0; int r; - cmd = malloc0(sizeof(struct kdbus_cmd_name_list)); - if (!cmd) - return -ENOMEM; + /* Caller will free half-constructed list on failure... */ - cmd->size = sizeof(struct kdbus_cmd_name_list); - cmd->flags = KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES; + cmd.flags = flags; - r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_LIST, cmd); + r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd); if (r < 0) return -errno; - name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd->offset); + name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset); - KDBUS_PART_FOREACH(name, name_list, names) { - char *n; + KDBUS_ITEM_FOREACH(name, name_list, names) { - if (name->size > sizeof(*name)) - n = name->name; - else - asprintf(&n, ":1.%llu", (unsigned long long) name->id); + if ((flags & KDBUS_NAME_LIST_UNIQUE) && name->id != previous_id) { + char *n; - r = strv_extend(&x, n); - if (r < 0) - return -ENOMEM; + if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0) + return -ENOMEM; + + r = strv_push(x, n); + if (r < 0) { + free(n); + return -ENOMEM; + } + + previous_id = name->id; + } + + if (name->size > sizeof(*name)) { + r = strv_extend(x, name->name); + if (r < 0) + return -ENOMEM; + } } - r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_FREE, &cmd->offset); + r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_FREE, &cmd.offset); if (r < 0) return -errno; - *l = x; return 0; } -static int bus_list_names_dbus1(sd_bus *bus, char ***l) { - _cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL; - char **x = NULL; +static int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) { + _cleanup_strv_free_ char **x = NULL, **y = NULL; int r; - r = sd_bus_call_method( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "ListNames", - NULL, - &reply1, - NULL); - if (r < 0) - return r; + if (acquired) { + r = kernel_get_list(bus, KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES, &x); + if (r < 0) + return r; + } - r = sd_bus_call_method( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "ListActivatableNames", - NULL, - &reply2, - NULL); - if (r < 0) - return r; + if (activatable) { + r = kernel_get_list(bus, KDBUS_NAME_LIST_STARTERS, &y); + if (r < 0) + return r; - r = bus_message_read_strv_extend(reply1, &x); - if (r < 0) { - strv_free(x); - return r; + *activatable = y; + y = NULL; } - r = bus_message_read_strv_extend(reply2, &x); - if (r < 0) { - strv_free(x); - return r; + if (acquired) { + *acquired = x; + x = NULL; } - *l = strv_uniq(x); return 0; } -_public_ int sd_bus_list_names(sd_bus *bus, char ***l) { +static int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatable) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + _cleanup_strv_free_ char **x = NULL, **y = NULL; + int r; + + if (acquired) { + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "ListNames", + NULL, + &reply, + NULL); + if (r < 0) + return r; + + r = sd_bus_message_read_strv(reply, &x); + if (r < 0) + return r; + + reply = sd_bus_message_unref(reply); + } + + if (activatable) { + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "ListActivatableNames", + NULL, + &reply, + NULL); + if (r < 0) + return r; + + r = sd_bus_message_read_strv(reply, &y); + if (r < 0) + return r; + + *activatable = y; + y = NULL; + } + + if (acquired) { + *acquired = x; + x = NULL; + } + + return 0; +} + +_public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) { assert_return(bus, -EINVAL); - assert_return(l, -EINVAL); + assert_return(acquired || activatable, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); if (bus->is_kernel) - return bus_list_names_kernel(bus, l); + return bus_list_names_kernel(bus, acquired, activatable); else - return bus_list_names_dbus1(bus, l); + return bus_list_names_dbus1(bus, acquired, activatable); } static int bus_get_owner_kdbus( @@ -324,6 +373,7 @@ static int bus_get_owner_kdbus( cmd = alloca0(size); strcpy(cmd->name, name); } + cmd->flags = KDBUS_ATTACH_NAMES; cmd->size = size; r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd); @@ -343,7 +393,7 @@ static int bus_get_owner_kdbus( c->mask |= SD_BUS_CREDS_UNIQUE_NAME; } - KDBUS_PART_FOREACH(item, conn_info, items) { + KDBUS_ITEM_FOREACH(item, conn_info, items) { switch (item->type) { @@ -399,7 +449,7 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_CMDLINE: if (mask & SD_BUS_CREDS_CMDLINE) { - c->cmdline_size = item->size - KDBUS_PART_HEADER_SIZE; + c->cmdline_size = item->size - KDBUS_ITEM_HEADER_SIZE; c->cmdline = memdup(item->data, c->cmdline_size); if (!c->cmdline) { r = -ENOMEM; @@ -431,7 +481,7 @@ static int bus_get_owner_kdbus( SD_BUS_CREDS_INHERITABLE_CAPS | SD_BUS_CREDS_BOUNDING_CAPS) & mask; if (m) { - c->capability_size = item->size - KDBUS_PART_HEADER_SIZE; + c->capability_size = item->size - KDBUS_ITEM_HEADER_SIZE; c->capability = memdup(item->data, c->capability_size); if (!c->capability) { r = -ENOMEM; @@ -464,14 +514,11 @@ static int bus_get_owner_kdbus( } break; - case KDBUS_ITEM_NAMES: + case KDBUS_ITEM_NAME: if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) { - c->well_known_names_size = item->size - KDBUS_PART_HEADER_SIZE; - c->well_known_names = memdup(item->data, c->well_known_names_size); - if (!c->well_known_names) { - r = -ENOMEM; + r = strv_extend(&c->well_known_names, item->name.name); + if (r < 0) goto fail; - } c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES; } @@ -973,7 +1020,7 @@ static int bus_add_match_internal_kernel( item->type = KDBUS_MATCH_BLOOM; memcpy(item->data64, bloom, BLOOM_SIZE); - item = KDBUS_PART_NEXT(item); + item = KDBUS_ITEM_NEXT(item); } if (sender) {