test-sync: implement send_reply()
authorDaniel Mack <daniel@zonque.org>
Tue, 21 Oct 2014 18:17:45 +0000 (20:17 +0200)
committerDaniel Mack <daniel@zonque.org>
Tue, 21 Oct 2014 20:13:25 +0000 (22:13 +0200)
Implement a new helper function to reply to pending messages.

Formerly, the test abused the timeout parameter to respond, knowing
that its value will eventually end up in the kdbus message in the
same union as the cookie_reply field.

In the process of switching to absolute timeouts, however, this bites
us, so move this hack out of the way first.

Signed-off-by: Daniel Mack <daniel@zonque.org>
test/test-sync.c

index ffd445f70b8870f08373d93b2014b894281ab1d3..ec706d676e0a20d375826adfd9ec997115e0026c 100644 (file)
@@ -23,6 +23,53 @@ 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_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);
+
+       ret = ioctl(conn->fd, KDBUS_CMD_MSG_SEND, msg);
+       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,
                          int sa_flags)
@@ -88,7 +135,7 @@ static void *run_thread_reply(void *data)
        ret = kdbus_msg_recv_poll(conn_a, 3000, NULL, NULL);
        if (ret == 0) {
                kdbus_printf("Thread received message, sending reply ...\n");
-               kdbus_msg_send(conn_a, NULL, 0, 0, cookie, 0, conn_b->id);
+               send_reply(conn_a, cookie, conn_b->id);
        }
 
        pthread_exit(NULL);