am dce3ddf5: Call read on any event, not just on POLLIN.

* commit 'dce3ddf54083ccd0e3752c4c08013688f79baa7a':
  Call read on any event, not just on POLLIN.
diff --git a/clatd.c b/clatd.c
index 56eb66a..dbf725b 100644
--- a/clatd.c
+++ b/clatd.c
@@ -375,27 +375,28 @@
  */
 void event_loop(const struct tun_data *tunnel) {
   time_t last_interface_poll;
-  struct pollfd wait_fd[2];
+  struct pollfd wait_fd[] = {
+    { tunnel->read_fd6, POLLIN, 0 },
+    { tunnel->fd4, POLLIN, 0 },
+  };
 
   // start the poll timer
   last_interface_poll = time(NULL);
 
-  wait_fd[0].fd = tunnel->read_fd6;
-  wait_fd[0].events = POLLIN;
-  wait_fd[0].revents = 0;
-  wait_fd[1].fd = tunnel->fd4;
-  wait_fd[1].events = POLLIN;
-  wait_fd[1].revents = 0;
-
   while(running) {
     if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
       if(errno != EINTR) {
         logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
       }
     } else {
-      int i;
-      for(i = 0; i < 2; i++) {
-        if((wait_fd[i].revents & POLLIN) != 0) {
+      size_t i;
+      for(i = 0; i < ARRAY_SIZE(wait_fd); i++) {
+        // Call read_packet if the socket has data to be read, but also if an
+        // error is waiting. If we don't call read() after getting POLLERR, a
+        // subsequent poll() will return immediately with POLLERR again,
+        // causing this code to spin in a loop. Calling read() will clear the
+        // socket error flag instead.
+        if(wait_fd[i].revents != 0) {
           read_packet(wait_fd[i].fd,tunnel);
         }
       }
diff --git a/clatd.h b/clatd.h
index 0f4809d..ca7369b 100644
--- a/clatd.h
+++ b/clatd.h
@@ -25,6 +25,8 @@
 #define PACKETLEN (MAXMTU+sizeof(struct tun_pi))
 #define CLATD_VERSION "1.3"
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
 // how frequently (in seconds) to poll for an address change while traffic is passing
 #define INTERFACE_POLL_FREQUENCY 30