item: add kdbus_items_get_str()
authorDaniel Mack <daniel@zonque.org>
Tue, 30 Sep 2014 23:02:20 +0000 (01:02 +0200)
committerDaniel Mack <daniel@zonque.org>
Tue, 30 Sep 2014 23:02:20 +0000 (01:02 +0200)
Introduce a new helper function called kdbus_items_get_str(), which
walks a list of items in order to find one which matches a given type
and return the ->str pointer inside of it.

This allows us to ditch various implementations of similar functions.

Signed-off-by: Daniel Mack <daniel@zonque.org>
domain.c
domain.h
endpoint.c
endpoint.h
handle.c
item.c
item.h
names.c

index c459d3777482330b3a82dbf16a6abd37435a59f8..471eed1012dafb8d14550d8874fff3f9de44fe4d 100644 (file)
--- a/domain.c
+++ b/domain.c
@@ -357,36 +357,6 @@ exit_put:
        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
index beb0e41c26baee2c670d4471ee23a9b0ad09dbd0..9b790532186348ed52fa6ea684d55c0dac111929 100644 (file)
--- a/domain.h
+++ b/domain.h
@@ -95,7 +95,6 @@ struct kdbus_domain *kdbus_domain_unref(struct kdbus_domain *domain);
 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,
index 57e233993f76886e5ebd9bc9e74720cfb6ab4a04..5801137b6ca770155587ec5c4ab8ce13a6e60e15 100644 (file)
@@ -380,33 +380,3 @@ int kdbus_ep_policy_check_own_access(struct kdbus_ep *ep,
 
        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;
-}
index e4c8eccc92b545d1884c0def53276142634a1746..95f36a14d1990afce0c1c964486aadb023f6ad94 100644 (file)
@@ -66,7 +66,6 @@ int kdbus_ep_new(struct kdbus_bus *bus, const char *name,
 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);
index 6b30981e143a57334a9d1468042ec3f583ef0f3c..d7457dc7483f7f97f932f93f5dfe960b3222f98b 100644 (file)
--- a/handle.c
+++ b/handle.c
@@ -336,7 +336,7 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd,
        }
 
        case KDBUS_CMD_DOMAIN_MAKE: {
-               char *name;
+               const char *name;
 
                if (!capable(CAP_IPC_OWNER)) {
                        ret = -EPERM;
@@ -356,7 +356,9 @@ static long kdbus_handle_ioctl_control(struct file *file, unsigned int cmd,
                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;
 
@@ -408,7 +410,7 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd,
                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 */
@@ -430,7 +432,9 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd,
                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;
 
diff --git a/item.c b/item.c
index b26ae9d440e08cdcb88154811fecb0a75089d0ad..abcd1ada55673a5f3432e5aa85b132dce9991bcc 100644 (file)
--- a/item.c
+++ b/item.c
@@ -217,3 +217,40 @@ int kdbus_items_validate(const struct kdbus_item *items, size_t items_size)
 
        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;
+}
diff --git a/item.h b/item.h
index 4ac0aa1380356be3e58bc48dfc958d8e71fac531..3e80dfb9bc3e87dd1432345d36afc05d84d8efc1 100644 (file)
--- a/item.h
+++ b/item.h
@@ -34,5 +34,7 @@
 
 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
diff --git a/names.c b/names.c
index ae51db9caad411e71dd2e79650b7bf6699e4bba0..d01ca801f9eed66e9728dc8e0b078b38ec0bad99 100644 (file)
--- a/names.c
+++ b/names.c
@@ -626,30 +626,6 @@ exit_unlock:
        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
@@ -677,7 +653,8 @@ int kdbus_cmd_name_acquire(struct kdbus_name_registry *reg,
        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;
 
@@ -707,7 +684,8 @@ int kdbus_cmd_name_release(struct kdbus_name_registry *reg,
        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;