14 #include "kdbus-api.h"
15 #include "kdbus-test.h"
16 #include "kdbus-util.h"
17 #include "kdbus-enum.h"
19 wur int timeout_msg_recv(struct kdbus_conn *conn, uint64_t type, uint64_t *cookie_reply, uint64_t *seqnum, uint64_t *monotonic_ns, uint64_t *realtime_ns)
21 struct kdbus_cmd_recv recv = { .size = sizeof(recv) };
22 struct kdbus_msg *msg;
25 ret = kdbus_cmd_recv(conn->fd, &recv);
27 kdbus_printf("error receiving message: %d (%m)\n", ret);
31 msg = (struct kdbus_msg *)(conn->buf + recv.msg.offset);
33 ASSERT_RETURN_VAL(msg->payload_type,==,(typeof(msg->payload_type))KDBUS_PAYLOAD_KERNEL, -EINVAL);
34 ASSERT_RETURN_VAL(msg->src_id,==,(typeof(msg->src_id))KDBUS_SRC_ID_KERNEL, -EINVAL);
35 ASSERT_RETURN_VAL(msg->dst_id,==,conn->id, -EINVAL);
38 const struct kdbus_item *item;
39 bool have_type = false;
40 bool have_timestamp = false;
42 KDBUS_ITEM_FOREACH(item, msg, items) {
43 if (item->type == type) {
44 ASSERT_ZERO(have_type);
46 ASSERT_RETURN(item->size,==,2*sizeof(uint64_t));
49 ASSERT_RETURN(item->type,==,(uint64_t)KDBUS_ITEM_TIMESTAMP);
50 ASSERT_ZERO(have_timestamp);
51 have_timestamp = true;
52 ASSERT_RETURN(item->size,==,2*sizeof(uint64_t) + sizeof(struct kdbus_timestamp));
53 #define S(P) if (P) *P = item->timestamp.P;
61 *cookie_reply = msg->cookie_reply;
62 kdbus_printf("Got message timeout for cookie %llu\n",
65 ret = kdbus_free(conn, recv.msg.offset);
72 wur int kdbus_test_timeout(struct kdbus_test_env *env)
74 struct kdbus_conn *conn_a, *conn_b;
76 int ret, i, n_msgs = 4;
77 uint64_t expected = 0;
78 uint64_t cookie = 0xdeadbeef;
80 ASSERT_NONZERO(conn_a = kdbus_hello(env->buspath, 0, NULL, 0));
81 ASSERT_NONZERO(conn_b = kdbus_hello(env->buspath, 0, NULL, 0));
86 * send messages that expect a reply (within 100 msec),
87 * but never answer it.
89 for (i = 0; i < n_msgs; i++, cookie++) {
90 kdbus_printf("Sending message with cookie %llu ...\n",
91 (unsigned long long)cookie);
92 ASSERT_ZERO(kdbus_msg_send(conn_b, NULL, cookie, KDBUS_MSG_EXPECT_REPLY, (i + 1) * 100ULL * 1000000ULL, 0, conn_a->id));
93 expected |= 1ULL << cookie;
97 uint64_t cookie_reply;
98 fd.events = POLLIN | POLLPRI | POLLHUP;
101 ret = poll(&fd, 1, (n_msgs + 1) * 100);
103 kdbus_printf("--- timeout\n");
107 if (fd.revents & POLLIN)
108 ASSERT_ZERO(timeout_msg_recv(conn_b, KDBUS_ITEM_REPLY_TIMEOUT, &cookie_reply, NULL, NULL, NULL));
110 if (!(expected &= ~(1ULL << cookie_reply)))
114 ASSERT_ZERO(expected);
116 kdbus_conn_free(conn_a);
117 kdbus_conn_free(conn_b);