refactoring: generalize and use setting string item 50/202550/6
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 29 Mar 2019 15:00:31 +0000 (16:00 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Wed, 3 Apr 2019 07:07:01 +0000 (09:07 +0200)
Change-Id: Iabedbee561b73df704f97392ae064cfcc97806a5

src/kdbus.cpp

index c920145..fe9b043 100644 (file)
@@ -81,6 +81,13 @@ static int kdbus_open_bus(kdbus_connection *conn, const char *path)
        return conn->fd;
 }
 
+static void set_item_str(struct kdbus_item *item, const char *str, __u64 str_length, __u64 type)
+{
+       item->size = KDBUS_ITEM_SIZE(str_length);
+       item->type = type;
+       memcpy(item->str, str, str_length);
+}
+
 static int kdbus_hello(struct kdbus_connection *conn,
                                           uint64_t hello_flags,
                                           uint64_t attach_flags_send,
@@ -93,10 +100,8 @@ static int kdbus_hello(struct kdbus_connection *conn,
        cmd->attach_flags_recv = attach_flags_recv;
        cmd->pool_size = KDBUS_POOL_SIZE;
 
-       struct kdbus_item *item = cmd->items;
-       item->size = offsetof(struct kdbus_item, data) + sizeof(CONNECTION_LABEL);
-       item->type = KDBUS_ITEM_CONN_DESCRIPTION;
-       memcpy(item->str, CONNECTION_LABEL, sizeof(CONNECTION_LABEL));
+       set_item_str(cmd->items, CONNECTION_LABEL, sizeof(CONNECTION_LABEL), KDBUS_ITEM_CONN_DESCRIPTION);
+
        if (ioctl(conn->fd, KDBUS_CMD_HELLO, cmd) < 0)
                return -errno;
 
@@ -155,14 +160,6 @@ static uint64_t kdbus_unique_id(char const *name)
        return res;
 }
 
-static void set_item_info(struct kdbus_item *item, const char *str, __u64 str_length, __u64 type)
-{
-       item->size = (__u64)offsetof(struct kdbus_item, data) + str_length + (__u64)1;
-       item->type = type;
-       *(uint64_t*)ALIGNDN8((uintptr_t)item->str + str_length) = 0; /* trailing zero + padding */
-       memcpy(item->str, str, str_length+1);
-}
-
 int kdbus_get_conn_info(kdbus_connection *conn,
                                                const char *destination,
                                                struct kdbus_cmd_param *info,
@@ -187,7 +184,7 @@ int kdbus_get_conn_info(kdbus_connection *conn,
                auto l = strlen(destination);
                cmd->size += KDBUS_ITEM_SIZE_ALIGN(ALIGN8(l+1));
                cmd->id = 0;
-               set_item_info(cmd->items, destination, l, KDBUS_ITEM_NAME);
+               set_item_str(cmd->items, destination, l+1, KDBUS_ITEM_NAME);
        }
        if (ioctl(conn->fd, KDBUS_CMD_CONN_INFO, cmd) < 0) {
                if (errno == ENXIO || errno == ESRCH)