Blame SOURCES/Fix-handling-of-non-EPOLLIN-EPOLLOUT-events.patch

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