Improve error reporting for ICU chains YAZ-707
[yaz-moved-to-github.git] / src / poll.c
index c877f0a..f3318f5 100644 (file)
@@ -1,13 +1,14 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
- *
- * $Id: poll.c,v 1.1 2007-11-09 16:46:43 adam Exp $
  */
 /**
- * \file 
+ * \file
  * \brief Select, poll wrapper
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <assert.h>
 #include <string.h>
@@ -48,7 +49,7 @@
   cases, e.g. when running IRSpy on a large target database.  So you
   should ensure that YAZ uses ZOOM_yaz_poll_poll() when possible.
 */
-int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int timeout)
+int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
 {
     struct timeval tv;
     fd_set input, output, except;
@@ -59,12 +60,16 @@ int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int timeout)
     FD_ZERO(&output);
     FD_ZERO(&except);
 
-    assert(num_fds > 0);
+    assert(num_fds >= 0);
     for (i = 0; i < num_fds; i++)
     {
         enum yaz_poll_mask mask = fds[i].input_mask;
         int fd = fds[i].fd;
 
+        /* Timeout events */
+        if (fd < 0)
+            continue;
+
         if (mask & yaz_poll_read)
             FD_SET(fd, &input);
         if (mask & yaz_poll_write)
@@ -74,43 +79,42 @@ int yaz_poll_select(struct yaz_poll_fd *fds, int num_fds, int timeout)
         if (max_fd < fd)
             max_fd = fd;
     }
-    tv.tv_sec = timeout;
-    tv.tv_usec = 0;
+    tv.tv_sec = sec;
+    tv.tv_usec = nsec / 1000;
 
-    while ((r = select(max_fd+1, &input, &output, &except,
-                       (timeout == -1 ? 0 : &tv))) < 0 && errno == EINTR)
-    {
-        ;
-    }
-    for (i = 0; i < num_fds; i++)
+    r = select(max_fd+1, &input, &output, &except, (sec == -1 ? 0 : &tv));
+    if (r >= 0)
     {
-        enum yaz_poll_mask mask = 0;
-        int fd = fds[i].fd;
-        if (!r)
-            mask += yaz_poll_timeout;
-        else
+        for (i = 0; i < num_fds; i++)
         {
-            if (FD_ISSET(fd, &input))
-                mask += yaz_poll_read;
-            if (FD_ISSET(fd, &output))
-                mask += yaz_poll_write;
-            if (FD_ISSET(fd, &except))
-                mask += yaz_poll_except;
+            enum yaz_poll_mask mask = yaz_poll_none;
+            int fd = fds[i].fd;
+            if (!r)
+                yaz_poll_add(mask, yaz_poll_timeout);
+            else if (fd >= 0) {
+                if (FD_ISSET(fd, &input))
+                    yaz_poll_add(mask, yaz_poll_read);
+                if (FD_ISSET(fd, &output))
+                    yaz_poll_add(mask, yaz_poll_write);
+                if (FD_ISSET(fd, &except))
+                    yaz_poll_add(mask, yaz_poll_except);
+            }
+            fds[i].output_mask = mask;
         }
-        fds[i].output_mask = mask;
     }
     return r;
 }
 
 #if HAVE_SYS_POLL_H
-int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int timeout)
+int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
 {
-    int r;
-    struct pollfd *pollfds = (struct pollfd *) 
-        xmalloc(num_fds * sizeof *pollfds);
-    int i;
+    int i, r;
+    struct pollfd *pollfds = 0;
 
-    assert(num_fds > 0);
+    if (num_fds > 0)
+        pollfds = (struct pollfd *) xmalloc(num_fds * sizeof *pollfds);
+
+    assert(num_fds >= 0);
     for (i = 0; i < num_fds; i++)
     {
         enum yaz_poll_mask mask = fds[i].input_mask;
@@ -127,27 +131,24 @@ int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int timeout)
         pollfds[i].events = poll_events;
         pollfds[i].revents = 0;
     }
-    while ((r = poll(pollfds, num_fds,
-                     (timeout == -1 ? -1 : timeout * 1000))) < 0
-           && errno == EINTR)
-    {
-        ;
-    }
+    r = poll(pollfds, num_fds, sec == -1 ? -1 : sec*1000 + nsec/1000000);
     if (r >= 0)
     {
         for (i = 0; i < num_fds; i++)
         {
-            enum yaz_poll_mask mask = 0;
+            enum yaz_poll_mask mask = yaz_poll_none;
             if (!r)
-                mask += yaz_poll_timeout;
+                yaz_poll_add(mask, yaz_poll_timeout);
             else
             {
                 if (pollfds[i].revents & POLLIN)
-                    mask += yaz_poll_read;
+                    yaz_poll_add(mask, yaz_poll_read);
                 if (pollfds[i].revents & POLLOUT)
-                    mask += yaz_poll_write;
-                if (pollfds[i].revents & POLLERR)
-                    mask += yaz_poll_except;
+                    yaz_poll_add(mask, yaz_poll_write);
+                if (pollfds[i].revents & ~(POLLIN | POLLOUT))
+                {
+                    yaz_poll_add(mask, yaz_poll_except);
+                }
             }
             fds[i].output_mask = mask;
         }
@@ -157,18 +158,19 @@ int yaz_poll_poll(struct yaz_poll_fd *fds, int num_fds, int timeout)
 }
 #endif
 
-int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int timeout)
+int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
 {
-#if YAZ_HAVE_SYS_POLL_H
-    return yaz_poll_poll(fds, num_fds, timeout);
+#if HAVE_SYS_POLL_H
+    return yaz_poll_poll(fds, num_fds, sec, nsec);
 #else
-    return yaz_poll_select(fds, num_fds, timeout);
+    return yaz_poll_select(fds, num_fds, sec, nsec);
 #endif
 }
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab