test: add conn_recv() that use poll() and msg_recv()
authorDjalal Harouni <tixxdz@opendz.org>
Mon, 18 Aug 2014 16:13:10 +0000 (17:13 +0100)
committerDaniel Mack <zonque@gmail.com>
Tue, 19 Aug 2014 07:16:48 +0000 (09:16 +0200)
Will be used by tests.

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

index e59254d458886bc33ae7afe362290dd0c2bc3679..f79d7ecf680b24a36cb97532cd269fd11a13185b 100644 (file)
@@ -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;
index 342ea9bc1a5d0c8b49b402272de6ede04d461db6..0490bd83684ff5a7141a0e8921388ed018fad3b5 100644 (file)
@@ -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,