68bf20
From a2a5789d6410e12469ea0f81c9a31ce70bac9ede Mon Sep 17 00:00:00 2001
1f3433
From: Alexander Scheel <alexander.m.scheel@gmail.com>
1f3433
Date: Thu, 14 Sep 2017 11:16:42 -0500
1f3433
Subject: [PATCH] Fix handling of non-EPOLLIN/EPOLLOUT events
1f3433
1f3433
Signed-off-by: Alexander Scheel <alexander.m.scheel@gmail.com>
1f3433
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
1f3433
Merges: #213
1f3433
(cherry picked from commit b8f5b2f75612a11753cf742ee0477b98df8e6b02)
1f3433
---
68bf20
 proxy/src/client/gpm_common.c | 49 +++++++++++++++++++++++++----------
1f3433
 1 file changed, 35 insertions(+), 14 deletions(-)
1f3433
1f3433
diff --git a/proxy/src/client/gpm_common.c b/proxy/src/client/gpm_common.c
1f3433
index 7d1158e..b14e846 100644
1f3433
--- a/proxy/src/client/gpm_common.c
1f3433
+++ b/proxy/src/client/gpm_common.c
1f3433
@@ -283,26 +283,47 @@ static int gpm_epoll_wait(struct gpm_ctx *gpmctx, uint32_t event_flags) {
1f3433
         gpm_epoll_close(gpmctx);
1f3433
     } else if (epoll_ret == 1 && events[0].data.fd == gpmctx->timerfd) {
1f3433
         /* Got an event which is only our timer */
1f3433
-        ret = read(gpmctx->timerfd, &timer_read, sizeof(uint64_t));
1f3433
-        if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
1f3433
-            /* In the case when reading from the timer failed, don't hide the
1f3433
-             * timer error behind ETIMEDOUT such that it isn't retried */
1f3433
-            ret = errno;
1f3433
+        if ((events[0].events & EPOLLIN) == 0) {
1f3433
+            /* We got an event which was not EPOLLIN; assume this is an error,
1f3433
+             * and exit with EBADF: epoll_wait said timerfd had an event,
1f3433
+             * but that event is not an EPOLIN event. */
1f3433
+            ret = EBADF;
1f3433
         } else {
1f3433
-            /* If ret == 0, then we definitely timed out. Else, if ret == -1
1f3433
-             * and errno == EAGAIN or errno == EWOULDBLOCK, we're in a weird
1f3433
-             * edge case where epoll thinks the timer can be read, but it
1f3433
-             * is blocking more; treat it like a TIMEOUT and retry, as
1f3433
-             * nothing around us would handle EAGAIN from timer and retry
1f3433
-             * it. */
1f3433
-            ret = ETIMEDOUT;
1f3433
+            ret = read(gpmctx->timerfd, &timer_read, sizeof(uint64_t));
1f3433
+            if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
1f3433
+                /* In the case when reading from the timer failed, don't hide the
1f3433
+                 * timer error behind ETIMEDOUT such that it isn't retried */
1f3433
+                ret = errno;
1f3433
+            } else {
1f3433
+                /* If ret == 0, then we definitely timed out. Else, if ret == -1
1f3433
+                 * and errno == EAGAIN or errno == EWOULDBLOCK, we're in a weird
1f3433
+                 * edge case where epoll thinks the timer can be read, but it
1f3433
+                 * is blocking more; treat it like a TIMEOUT and retry, as
1f3433
+                 * nothing around us would handle EAGAIN from timer and retry
1f3433
+                 * it. */
1f3433
+                ret = ETIMEDOUT;
1f3433
+            }
1f3433
         }
1f3433
         gpm_epoll_close(gpmctx);
1f3433
     } else {
1f3433
         /* If ret == 2, then we ignore the timerfd; that way if the next
1f3433
          * operation cannot be performed immediately, we timeout and retry.
1f3433
-         * If ret == 1 and data.fd == gpmctx->fd, return 0. */
1f3433
-        ret = 0;
1f3433
+         * Always check the returned event of the socket fd. */
1f3433
+        int fd_index = 0;
1f3433
+        if (epoll_ret == 2 && events[fd_index].data.fd != gpmctx->fd) {
1f3433
+            fd_index = 1;
1f3433
+        }
1f3433
+
1f3433
+        if ((events[fd_index].events & event_flags) == 0) {
1f3433
+            /* We cannot call EPOLLIN/EPOLLOUT at this time; assume that this
1f3433
+             * is a fatal error; return with EBADFD to distinguish from
1f3433
+             * EBADF in timer_fd case. */
1f3433
+            ret = EBADFD;
1f3433
+            gpm_epoll_close(gpmctx);
1f3433
+        } else {
1f3433
+            /* We definintely got a EPOLLIN/EPOLLOUT event; return success. */
1f3433
+            ret = 0;
1f3433
+        }
1f3433
     }
1f3433
 
1f3433
     epoll_ret = epoll_ctl(gpmctx->epollfd, EPOLL_CTL_DEL, gpmctx->fd, NULL);