diff options
author | Daniel Mack <daniel@zonque.org> | 2014-10-22 19:04:27 +0200 |
---|---|---|
committer | Daniel Mack <daniel@zonque.org> | 2014-10-22 19:45:07 +0200 |
commit | 370d7a9c0fe558c5846a2abd919e397639b69506 (patch) | |
tree | f4ca6bf67823855d119939792a1d416933358185 | |
parent | a1783d61a8a74dd91028cd065b8a23067419fe0a (diff) |
sd-bus: factor out creds item iterator
We will re-use the code to walk items in order to populate a creds object,
so let's factor it out first.
-rw-r--r-- | src/libsystemd/sd-bus/bus-control.c | 193 |
1 files changed, 95 insertions, 98 deletions
diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c index d344387bb3..b66606763d 100644 --- a/src/libsystemd/sd-bus/bus-control.c +++ b/src/libsystemd/sd-bus/bus-control.c @@ -392,70 +392,16 @@ _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatabl return bus_list_names_dbus1(bus, acquired, activatable); } -static int bus_get_owner_kdbus( - sd_bus *bus, - const char *name, - uint64_t mask, - sd_bus_creds **creds) { +static int bus_populate_creds_from_items(sd_bus *bus, + struct kdbus_info *info, + uint64_t mask, + sd_bus_creds *c) { - _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL; - struct kdbus_cmd_info *cmd; - struct kdbus_info *conn_info; struct kdbus_item *item; - size_t size, l; - uint64_t m, id; + uint64_t m; int r; - r = bus_kernel_parse_unique_name(name, &id); - if (r < 0) - return r; - if (r > 0) { - size = offsetof(struct kdbus_cmd_info, items); - cmd = alloca0_align(size, 8); - cmd->id = id; - } else { - l = strlen(name) + 1; - size = offsetof(struct kdbus_cmd_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; - kdbus_translate_attach_flags(mask, (uint64_t*) &cmd->flags); - - r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd); - if (r < 0) - return -errno; - - conn_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset); - - /* Non-activated names are considered not available */ - if (conn_info->flags & KDBUS_HELLO_ACTIVATOR) { - if (name[0] == ':') - r = -ENXIO; - else - r = -ESRCH; - goto fail; - } - - c = bus_creds_new(); - 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) { - r = -ENOMEM; - goto fail; - } - - c->mask |= SD_BUS_CREDS_UNIQUE_NAME; - } - - KDBUS_ITEM_FOREACH(item, conn_info, items) { + KDBUS_ITEM_FOREACH(item, info, items) { switch (item->type) { @@ -484,10 +430,8 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_PID_COMM: if (mask & SD_BUS_CREDS_COMM) { c->comm = strdup(item->str); - if (!c->comm) { - r = -ENOMEM; - goto fail; - } + if (!c->comm) + return -ENOMEM; c->mask |= SD_BUS_CREDS_COMM; } @@ -496,10 +440,8 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_TID_COMM: if (mask & SD_BUS_CREDS_TID_COMM) { c->tid_comm = strdup(item->str); - if (!c->tid_comm) { - r = -ENOMEM; - goto fail; - } + if (!c->tid_comm) + return -ENOMEM; c->mask |= SD_BUS_CREDS_TID_COMM; } @@ -508,10 +450,8 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_EXE: if (mask & SD_BUS_CREDS_EXE) { c->exe = strdup(item->str); - if (!c->exe) { - r = -ENOMEM; - goto fail; - } + if (!c->exe) + return -ENOMEM; c->mask |= SD_BUS_CREDS_EXE; } @@ -521,10 +461,8 @@ static int bus_get_owner_kdbus( if (mask & SD_BUS_CREDS_CMDLINE) { c->cmdline_size = item->size - KDBUS_ITEM_HEADER_SIZE; c->cmdline = memdup(item->data, c->cmdline_size); - if (!c->cmdline) { - r = -ENOMEM; - goto fail; - } + if (!c->cmdline) + return -ENOMEM; c->mask |= SD_BUS_CREDS_CMDLINE; } @@ -537,20 +475,16 @@ static int bus_get_owner_kdbus( if (m) { c->cgroup = strdup(item->str); - if (!c->cgroup) { - r = -ENOMEM; - goto fail; - } + if (!c->cgroup) + return -ENOMEM; r = bus_get_root_path(bus); if (r < 0) - goto fail; + return r; c->cgroup_root = strdup(bus->cgroup_root); - if (!c->cgroup_root) { - r = -ENOMEM; - goto fail; - } + if (!c->cgroup_root) + return -ENOMEM; c->mask |= m; } @@ -563,10 +497,8 @@ static int bus_get_owner_kdbus( if (m) { c->capability_size = item->size - KDBUS_ITEM_HEADER_SIZE; c->capability = memdup(item->data, c->capability_size); - if (!c->capability) { - r = -ENOMEM; - goto fail; - } + if (!c->capability) + return -ENOMEM; c->mask |= m; } @@ -575,10 +507,8 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_SECLABEL: if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) { c->label = strdup(item->str); - if (!c->label) { - r = -ENOMEM; - goto fail; - } + if (!c->label) + return -ENOMEM; c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; } @@ -598,7 +528,7 @@ static int bus_get_owner_kdbus( if ((mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) && service_name_is_valid(item->name.name)) { r = strv_extend(&c->well_known_names, item->name.name); if (r < 0) - goto fail; + return r; c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES; } @@ -607,10 +537,8 @@ static int bus_get_owner_kdbus( case KDBUS_ITEM_CONN_NAME: if ((mask & SD_BUS_CREDS_CONNECTION_NAME)) { c->conn_name = strdup(item->str); - if (!c->conn_name) { - r = -ENOMEM; - goto fail; - } + if (!c->conn_name) + return -ENOMEM; c->mask |= SD_BUS_CREDS_CONNECTION_NAME; } @@ -618,6 +546,75 @@ static int bus_get_owner_kdbus( } } + return 0; +} + +static int bus_get_owner_kdbus( + sd_bus *bus, + const char *name, + uint64_t mask, + sd_bus_creds **creds) { + + _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL; + struct kdbus_cmd_info *cmd; + struct kdbus_info *conn_info; + size_t size, l; + uint64_t id; + int r; + + r = bus_kernel_parse_unique_name(name, &id); + if (r < 0) + return r; + if (r > 0) { + size = offsetof(struct kdbus_cmd_info, items); + cmd = alloca0_align(size, 8); + cmd->id = id; + } else { + l = strlen(name) + 1; + size = offsetof(struct kdbus_cmd_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; + kdbus_translate_attach_flags(mask, (uint64_t*) &cmd->flags); + + r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd); + if (r < 0) + return -errno; + + conn_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset); + + /* Non-activated names are considered not available */ + if (conn_info->flags & KDBUS_HELLO_ACTIVATOR) { + if (name[0] == ':') + r = -ENXIO; + else + r = -ESRCH; + goto fail; + } + + c = bus_creds_new(); + 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) { + r = -ENOMEM; + goto fail; + } + + c->mask |= SD_BUS_CREDS_UNIQUE_NAME; + } + + r = bus_populate_creds_from_items(bus, conn_info, mask, c); + if (r < 0) + goto fail; + if (creds) { *creds = c; c = NULL; |