test-util: fix kdbus_msg_recv_poll()
authorDaniel Mack <daniel@zonque.org>
Wed, 1 Oct 2014 00:04:02 +0000 (02:04 +0200)
committerDaniel Mack <daniel@zonque.org>
Wed, 1 Oct 2014 00:06:12 +0000 (02:06 +0200)
Implement kdbus_msg_recv_poll() so that it really takes the time before
and after poll() is called, so that we know how ofter we have to repeat
the loop before we give up.

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

index a0b676cdfa6f46e85b8ad28898896f895c825643..0aa23c75c6e4ec75944d020b7acd32fc76695c72 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <linux/unistd.h>
 #ifndef MFD_CLOEXEC
 #include <linux/memfd.h>
@@ -693,21 +694,24 @@ int kdbus_msg_recv(struct kdbus_conn *conn,
  * We must return the result of kdbus_msg_recv()
  */
 int kdbus_msg_recv_poll(struct kdbus_conn *conn,
-                       unsigned int timeout_ms,
+                       int timeout_ms,
                        struct kdbus_msg **msg_out,
                        uint64_t *offset)
 {
-       int cnt = 3;
-       struct pollfd fd;
        int ret;
 
-       fd.fd = conn->fd;
-       fd.events = POLLIN | POLLPRI | POLLHUP;
-       fd.revents = 0;
+       do {
+               struct timeval before, after, diff;
+               struct pollfd fd;
 
-       while (cnt) {
-               cnt--;
+               fd.fd = conn->fd;
+               fd.events = POLLIN | POLLPRI | POLLHUP;
+               fd.revents = 0;
+
+               gettimeofday(&before, NULL);
                ret = poll(&fd, 1, timeout_ms);
+               gettimeofday(&after, NULL);
+
                if (ret == 0) {
                        ret = -ETIMEDOUT;
                        break;
@@ -724,15 +728,10 @@ int kdbus_msg_recv_poll(struct kdbus_conn *conn,
                if (ret == 0 || ret != -EAGAIN)
                        break;
 
-               /*
-                * recv failed with -EAGAIN (Resource temporarily
-                * unavailable), so just try again.
-                *
-                * Add the following even if poll will return
-                * immediately.
-                */
-               timeout_ms = 0;
-       }
+               timersub(&after, &before, &diff);
+               timeout_ms -= diff.tv_sec * 1000UL +
+                             diff.tv_usec / 1000UL;
+       } while (timeout_ms > 0);
 
        return ret;
 }
index 497d1d95a7b8873e17da7ed257a7ce41b46be5f3..3248e3ec497bbd8cccef63ef6fb103ef8d5cc40a 100644 (file)
@@ -58,7 +58,7 @@ int kdbus_name_acquire(struct kdbus_conn *conn, const char *name,
 void kdbus_msg_free(struct kdbus_msg *msg);
 int kdbus_msg_recv(struct kdbus_conn *conn,
                   struct kdbus_msg **msg, uint64_t *offset);
-int kdbus_msg_recv_poll(struct kdbus_conn *conn, unsigned int timeout_ms,
+int kdbus_msg_recv_poll(struct kdbus_conn *conn, int timeout_ms,
                        struct kdbus_msg **msg_out, uint64_t *offset);
 int kdbus_free(const struct kdbus_conn *conn, uint64_t offset);
 void kdbus_msg_dump(const struct kdbus_conn *conn,