Use poll() instead of select() in order to work in apps with fd>1023 open
authorZdeněk Salvet <salvet@ics.muni.cz>
Thu, 18 May 2006 09:21:48 +0000 (09:21 +0000)
committerZdeněk Salvet <salvet@ics.muni.cz>
Thu, 18 May 2006 09:21:48 +0000 (09:21 +0000)
(bug #16506).

org.glite.lb.common/src/lb_plain_io.c

index 7915dfa..a1e4dd2 100644 (file)
 #include <netdb.h>
 #include <netinet/in.h>
 #include <errno.h>
+#include <poll.h>
+#ifndef INFTIM
+#define INFTIM (-1)
+#endif
 
 #include "lb_plain_io.h"
 
@@ -68,7 +72,8 @@ int edg_wll_plain_read(
        struct timeval             *to)
 {
        int                             ct, toread = 0;
-       fd_set                  fds;
+       struct pollfd                   pollfds[1];
+       int                             polltime = 0;
        struct timeval  timeout, before, after;
 
 
@@ -89,11 +94,16 @@ int edg_wll_plain_read(
 
        toread = 0;
        do {
-               FD_ZERO(&fds);
-               FD_SET(conn->sock, &fds);
-               switch (select(conn->sock+1, &fds, NULL, NULL, to ? &timeout : NULL)) {
-               case 0: errno = ETIMEDOUT; goto cleanup; break;
-               case -1: goto cleanup; break;
+               pollfds[0].fd = conn->sock;
+               pollfds[0].events = POLLIN;
+               polltime = to ? (timeout.tv_sec*1000+timeout.tv_usec/1000) : INFTIM;
+               switch (poll(pollfds, 1, polltime)) {
+                       case 0: errno = ETIMEDOUT; goto cleanup; break;
+                       case -1: goto cleanup; break;
+                       default: if (!(pollfds[0].revents & POLLIN)) {
+                                       errno = EIO;
+                                       goto cleanup; break;
+                               }
                }
 
                if ( conn->bufUse == conn->bufSize ) {
@@ -173,7 +183,8 @@ int edg_wll_plain_write_full(
 {
        size_t                  written = 0;
        int                             ct = -1;
-       fd_set                  fds;
+       struct pollfd                   pollfds[1];
+       int                             polltime = 0;
        struct timeval  timeout, before, after;
        struct sigaction        sa,osa;
 
@@ -188,12 +199,18 @@ int edg_wll_plain_write_full(
 
        errno = 0;
        while ( written < bufsz ) {
-               FD_ZERO(&fds);
-               FD_SET(conn->sock, &fds);
 
-               switch ( select(conn->sock+1, NULL, &fds, NULL, to? &timeout: NULL) ) {
+               pollfds[0].fd = conn->sock;
+               pollfds[0].events = POLLOUT;
+               polltime = to ? (timeout.tv_sec*1000+timeout.tv_usec/1000) : INFTIM;
+
+               switch (poll(pollfds, 1, polltime)) {
                        case 0: errno = ETIMEDOUT; goto end; break;
                        case -1: goto end; break;
+                       default: if (!(pollfds[0].revents & POLLOUT)) {
+                                       errno = ENOTCONN;
+                                       goto end; break;
+                               }
                }
                if ( (ct=write(conn->sock, ((char*)buf)+written, bufsz-written)) < 0 ) {
                        if ( errno == EINTR ) { errno = 0; continue; }