return ret;
}
-/**
- * kdbus_domain_make_user() - create domain data from user data
- * @cmd: The command as passed in by the ioctl
- * @name: The name of the domain to create
- *
- * Return: 0 on success, negative errno on failure
- */
-int kdbus_domain_make_user(struct kdbus_cmd_make *cmd, char **name)
-{
- const struct kdbus_item *item;
- const char *n = NULL;
-
- KDBUS_ITEMS_FOREACH(item, cmd->items, KDBUS_ITEMS_SIZE(cmd, items)) {
- switch (item->type) {
- case KDBUS_ITEM_MAKE_NAME:
- if (n)
- return -EEXIST;
-
- n = item->str;
- continue;
- }
- }
-
- if (!name)
- return -EBADMSG;
-
- *name = (char *)n;
- return 0;
-}
-
/**
* kdbus_domain_user_assign_id() - allocate ID and assign it to the
* domain user
void kdbus_domain_disconnect(struct kdbus_domain *domain);
int kdbus_domain_new(struct kdbus_domain *parent, const char *name,
umode_t mode, struct kdbus_domain **domain);
-int kdbus_domain_make_user(struct kdbus_cmd_make *cmd, char **name);
struct kdbus_domain *kdbus_domain_find_by_major(unsigned int major);
int kdbus_domain_get_user_unlocked(struct kdbus_domain *domain,
return 0;
}
-
-/**
- * kdbus_ep_make_user() - create endpoint data from user data
- * @make: The returned copy of user data
- * @name: The name of the endpoint to create
- *
- * Return: 0 on success, negative errno on failure.
- */
-int kdbus_ep_make_user(const struct kdbus_cmd_make *make, char **name)
-{
- const struct kdbus_item *item;
- const char *n = NULL;
-
- KDBUS_ITEMS_FOREACH(item, make->items, KDBUS_ITEMS_SIZE(make, items)) {
- switch (item->type) {
- case KDBUS_ITEM_MAKE_NAME:
- if (n)
- return -EEXIST;
-
- n = item->str;
- continue;
- }
- }
-
- if (!n)
- return -EBADMSG;
-
- *name = (char *)n;
- return 0;
-}
struct kdbus_ep *kdbus_ep_ref(struct kdbus_ep *ep);
struct kdbus_ep *kdbus_ep_unref(struct kdbus_ep *ep);
void kdbus_ep_disconnect(struct kdbus_ep *ep);
-int kdbus_ep_make_user(const struct kdbus_cmd_make *make, char **name);
int kdbus_ep_policy_set(struct kdbus_ep *ep,
const struct kdbus_item *items,
size_t items_size);
}
case KDBUS_CMD_DOMAIN_MAKE: {
- char *name;
+ const char *name;
if (!capable(CAP_IPC_OWNER)) {
ret = -EPERM;
if (ret < 0)
break;
- ret = kdbus_domain_make_user(make, &name);
+ ret = kdbus_items_get_str(make->items,
+ KDBUS_ITEMS_SIZE(make, items),
+ KDBUS_ITEM_MAKE_NAME, &name);
if (ret < 0)
break;
struct kdbus_cmd_make *make;
umode_t mode = 0;
kgid_t gid = KGIDT_INIT(0);
- char *name;
+ const char *name;
struct kdbus_ep *ep;
/* creating custom endpoints is a privileged operation */
if (ret < 0)
break;
- ret = kdbus_ep_make_user(make, &name);
+ ret = kdbus_items_get_str(make->items,
+ KDBUS_ITEMS_SIZE(make, items),
+ KDBUS_ITEM_MAKE_NAME, &name);
if (ret < 0)
break;
return 0;
}
+
+/**
+ * kdbus_items_get_str() - get string from a list of items
+ * @items: The items to walk
+ * @items_size: The size of all items
+ * @item_type: The item type to look for
+ * @str_ret: A pointer to store the found name
+ *
+ * This function walks a list of items and searches for items of type
+ * @item_type. If it finds exactly one such item, @str_ret will be set to
+ * the .str member of the item.
+ *
+ * Return: 0 if the item was found exactly once, -EEXIST if the item was
+ * found more than once, and -EBADMSG if there was no item of the given type.
+ */
+int kdbus_items_get_str(const struct kdbus_item *items, size_t items_size,
+ unsigned int item_type, const char **str_ret)
+{
+ const struct kdbus_item *item;
+ const char *n = NULL;
+
+ KDBUS_ITEMS_FOREACH(item, items, items_size) {
+ if (item->type == item_type) {
+ if (n)
+ return -EEXIST;
+
+ n = item->str;
+ continue;
+ }
+ }
+
+ if (!n)
+ return -EBADMSG;
+
+ *str_ret = n;
+ return 0;
+}
int kdbus_item_validate_name(const struct kdbus_item *item);
int kdbus_items_validate(const struct kdbus_item *items, size_t items_size);
+int kdbus_items_get_str(const struct kdbus_item *items, size_t items_size,
+ unsigned int item_type, const char **str_ret);
#endif
return ret;
}
-static int kdbus_cmd_name_get_name(const struct kdbus_cmd_name *cmd,
- const char **name)
-{
- const struct kdbus_item *item;
- const char *n = NULL;
-
- KDBUS_ITEMS_FOREACH(item, cmd->items, KDBUS_ITEMS_SIZE(cmd, items)) {
- switch (item->type) {
- case KDBUS_ITEM_NAME:
- if (n)
- return -EINVAL;
-
- n = item->str;
- break;
- }
- }
-
- if (!n)
- return -EINVAL;
-
- *name = n;
- return 0;
-}
-
/**
* kdbus_cmd_name_acquire() - acquire a name from a ioctl command buffer
* @reg: The name registry
if ((cmd->flags & ~allowed) != 0)
return -EINVAL;
- ret = kdbus_cmd_name_get_name(cmd, &name);
+ ret = kdbus_items_get_str(cmd->items, KDBUS_ITEMS_SIZE(cmd, items),
+ KDBUS_ITEM_NAME, &name);
if (ret < 0)
return -EINVAL;
int ret;
const char *name;
- ret = kdbus_cmd_name_get_name(cmd, &name);
+ ret = kdbus_items_get_str(cmd->items, KDBUS_ITEMS_SIZE(cmd, items),
+ KDBUS_ITEM_NAME, &name);
if (ret < 0)
return -EINVAL;