From e00baea00e68fceddc2cbbd06a486b6b02654cf5 Mon Sep 17 00:00:00 2001 From: Djalal Harouni Date: Mon, 18 Aug 2014 17:13:10 +0100 Subject: [PATCH] test: add conn_recv() that use poll() and msg_recv() Will be used by tests. Signed-off-by: Djalal Harouni --- test/kdbus-util.c | 34 ++++++++++++++++++++++++++++++++++ test/kdbus-util.h | 1 + 2 files changed, 35 insertions(+) diff --git a/test/kdbus-util.c b/test/kdbus-util.c index e59254d..f79d7ec 100644 --- a/test/kdbus-util.c +++ b/test/kdbus-util.c @@ -575,6 +575,40 @@ int msg_recv(struct conn *conn) return 0; } +/* Returns 0 on success, negative errno on failure */ +int conn_recv(struct conn *conn) +{ + int ret; + int cnt = 3; + struct pollfd fd; + + fd.fd = conn->fd; + fd.events = POLLIN | POLLPRI | POLLHUP; + fd.revents = 0; + + while (cnt) { + cnt--; + ret = poll(&fd, 1, 4000); + if (ret == 0) { + ret = -ETIMEDOUT; + break; + } + + if (ret > 0) { + if (fd.revents & POLLIN) + ret = msg_recv(conn); + + if (fd.revents & (POLLHUP | POLLERR)) + ret = -ECONNRESET; + } + + if (ret >= 0 || ret != -EAGAIN) + break; + } + + return ret; +} + int name_acquire(struct conn *conn, const char *name, uint64_t flags) { struct kdbus_cmd_name *cmd_name; diff --git a/test/kdbus-util.h b/test/kdbus-util.h index 342ea9b..0490bd8 100644 --- a/test/kdbus-util.h +++ b/test/kdbus-util.h @@ -59,6 +59,7 @@ int name_list(struct conn *conn, uint64_t flags); int name_release(struct conn *conn, const char *name); int name_acquire(struct conn *conn, const char *name, uint64_t flags); int msg_recv(struct conn *conn); +int conn_recv(struct conn *conn); void msg_dump(const struct conn *conn, const struct kdbus_msg *msg); char *msg_id(uint64_t id, char *buf); int msg_send(const struct conn *conn, const char *name, uint64_t cookie, -- 2.34.1