test: add kdbus_msg_send_reply()
authorDjalal Harouni <tixxdz@opendz.org>
Sun, 4 Jan 2015 13:55:40 +0000 (14:55 +0100)
committerDjalal Harouni <tixxdz@opendz.org>
Sun, 4 Jan 2015 13:55:40 +0000 (14:55 +0100)
Move send_reply() from test-sync.c to kdbus-util.c and rename it to
kdbus_msg_send_reply().

Currently there is only one user, will add a quota test for sync
messages later that will use it.

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test/kdbus-util.c
test/kdbus-util.h
test/test-sync.c

index 7bf3649a92a16a4ca0d6c4ab79562b1b9a431c63..013b5ba80d0e52e9705a8c1763dbbc058865d907 100644 (file)
@@ -631,6 +631,55 @@ int kdbus_msg_send_sync(const struct kdbus_conn *conn, const char *name,
                                dst_id, KDBUS_SEND_SYNC_REPLY, cancel_fd);
 }
 
+int kdbus_msg_send_reply(const struct kdbus_conn *conn,
+                        uint64_t reply_cookie,
+                        uint64_t dst_id)
+{
+       struct kdbus_cmd_send cmd = {};
+       struct kdbus_msg *msg;
+       const char ref1[1024 * 128 + 3] = "0123456789_0";
+       struct kdbus_item *item;
+       uint64_t size;
+       int ret;
+
+       size = sizeof(struct kdbus_msg);
+       size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
+
+       msg = malloc(size);
+       if (!msg) {
+               ret = -errno;
+               kdbus_printf("unable to malloc()!?\n");
+               return ret;
+       }
+
+       memset(msg, 0, size);
+       msg->size = size;
+       msg->src_id = conn->id;
+       msg->dst_id = dst_id;
+       msg->cookie_reply = reply_cookie;
+       msg->payload_type = KDBUS_PAYLOAD_DBUS;
+
+       item = msg->items;
+
+       item->type = KDBUS_ITEM_PAYLOAD_VEC;
+       item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);
+       item->vec.address = (uintptr_t)&ref1;
+       item->vec.size = sizeof(ref1);
+       item = KDBUS_ITEM_NEXT(item);
+
+       cmd.size = sizeof(cmd);
+       cmd.msg_address = (uintptr_t)msg;
+
+       ret = ioctl(conn->fd, KDBUS_CMD_SEND, &cmd);
+       if (ret < 0) {
+               ret = -errno;
+               kdbus_printf("error sending message: %d (%m)\n", ret);
+       }
+
+       free(msg);
+
+       return ret;
+}
 static char *msg_id(uint64_t id, char *buf)
 {
        if (id == 0)
index a568fc677eebb1b2adc92ec190f3d2132e46eb54..5f30aa8ea20c2d7cb594325c369b73e292d69a10 100644 (file)
@@ -171,6 +171,9 @@ int kdbus_msg_send(const struct kdbus_conn *conn, const char *name,
 int kdbus_msg_send_sync(const struct kdbus_conn *conn, const char *name,
                        uint64_t cookie, uint64_t flags, uint64_t timeout,
                        int64_t priority, uint64_t dst_id, int cancel_fd);
+int kdbus_msg_send_reply(const struct kdbus_conn *conn,
+                        uint64_t reply_cookie,
+                        uint64_t dst_id);
 struct kdbus_conn *kdbus_hello(const char *path, uint64_t hello_flags,
                               const struct kdbus_item *item,
                               size_t item_size);
index fd7b006f31e65b4cd4a7176d078ba1f4aa340d88..464509fe19f1f93a0136488157f652e7d109b1cb 100644 (file)
@@ -24,57 +24,6 @@ static unsigned int cookie = 0xdeadbeef;
 
 static void nop_handler(int sig) {}
 
-static int send_reply(const struct kdbus_conn *conn,
-                     uint64_t reply_cookie,
-                     uint64_t dst_id)
-{
-       struct kdbus_cmd_send cmd = {};
-       struct kdbus_msg *msg;
-       const char ref1[1024 * 128 + 3] = "0123456789_0";
-       struct kdbus_item *item;
-       uint64_t size;
-       int ret;
-
-       size = sizeof(struct kdbus_msg);
-       size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
-
-       msg = malloc(size);
-       if (!msg) {
-               ret = -errno;
-               kdbus_printf("unable to malloc()!?\n");
-               return ret;
-       }
-
-       memset(msg, 0, size);
-       msg->size = size;
-       msg->src_id = conn->id;
-       msg->dst_id = dst_id;
-       msg->cookie_reply = reply_cookie;
-       msg->payload_type = KDBUS_PAYLOAD_DBUS;
-
-       item = msg->items;
-
-       item->type = KDBUS_ITEM_PAYLOAD_VEC;
-       item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);
-       item->vec.address = (uintptr_t)&ref1;
-       item->vec.size = sizeof(ref1);
-       item = KDBUS_ITEM_NEXT(item);
-
-       cmd.size = sizeof(cmd);
-       cmd.msg_address = (uintptr_t)msg;
-
-       ret = ioctl(conn->fd, KDBUS_CMD_SEND, &cmd);
-       if (ret < 0) {
-               ret = -errno;
-               kdbus_printf("error sending message: %d (%m)\n", ret);
-               return ret;
-       }
-
-       free(msg);
-
-       return 0;
-}
-
 static int interrupt_sync(struct kdbus_conn *conn_src,
                          struct kdbus_conn *conn_dst)
 {
@@ -261,7 +210,7 @@ static int no_cancel_sync(struct kdbus_conn *conn_src,
 
        kdbus_msg_free(msg);
 
-       ret = send_reply(conn_src, cookie, conn_dst->id);
+       ret = kdbus_msg_send_reply(conn_src, cookie, conn_dst->id);
        ASSERT_RETURN_VAL(ret >= 0, ret);
 
        ret = waitpid(pid, &status, 0);
@@ -285,13 +234,13 @@ static void *run_thread_reply(void *data)
        kdbus_printf("Thread received message, sending reply ...\n");
 
        /* using an unknown cookie must fail */
-       ret = send_reply(conn_a, ~cookie, conn_b->id);
+       ret = kdbus_msg_send_reply(conn_a, ~cookie, conn_b->id);
        if (ret != -EPERM) {
                status = TEST_ERR;
                goto exit_thread;
        }
 
-       ret = send_reply(conn_a, cookie, conn_b->id);
+       ret = kdbus_msg_send_reply(conn_a, cookie, conn_b->id);
        if (ret != 0) {
                status = TEST_ERR;
                goto exit_thread;