c0a4658
Author: Anton Kortunov <toshic.toshic@gmail.com>
c0a4658
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/libfcgi/+bug/933417
c0a4658
Description: use poll in os_unix.c instead of select to avoid problem with > 1024 connections
c0a4658
Forwarded: yes, fastcgi-developers@mailman.fastcgi.com
c0a4658
c0a4658
diff --git a/libfcgi/os_unix.c b/libfcgi/os_unix.c
c0a4658
index 73e6a7f..af35aee 100755
c0a4658
--- a/libfcgi/os_unix.c
c0a4658
+++ b/libfcgi/os_unix.c
c0a4658
@@ -42,6 +42,7 @@ static const char rcsid[] = "$Id: os_unix.c,v 1.37 2002/03/05 19:14:49 robs Exp
c0a4658
 #include <sys/time.h>
c0a4658
 #include <sys/un.h>
c0a4658
 #include <signal.h>
c0a4658
+#include <poll.h>
c0a4658
 
c0a4658
 #ifdef HAVE_NETDB_H
c0a4658
 #include <netdb.h>
c0a4658
@@ -103,6 +104,9 @@ static int volatile maxFd = -1;
c0a4658
 static int shutdownPending = FALSE;
c0a4658
 static int shutdownNow = FALSE;
c0a4658
 
c0a4658
+static int libfcgiOsClosePollTimeout = 2000;
c0a4658
+static int libfcgiIsAfUnixKeeperPollTimeout = 2000;
c0a4658
+
c0a4658
 void OS_ShutdownPending()
c0a4658
 {
c0a4658
     shutdownPending = TRUE;
c0a4658
@@ -168,6 +172,16 @@ int OS_LibInit(int stdioFds[3])
c0a4658
     if(libInitialized)
c0a4658
         return 0;
c0a4658
 
c0a4658
+    char *libfcgiOsClosePollTimeoutStr = getenv( "LIBFCGI_OS_CLOSE_POLL_TIMEOUT" );
c0a4658
+    if(libfcgiOsClosePollTimeoutStr) {
c0a4658
+        libfcgiOsClosePollTimeout = atoi(libfcgiOsClosePollTimeoutStr);
c0a4658
+    }
c0a4658
+
c0a4658
+    char *libfcgiIsAfUnixKeeperPollTimeoutStr = getenv( "LIBFCGI_IS_AF_UNIX_KEEPER_POLL_TIMEOUT" );
c0a4658
+    if(libfcgiIsAfUnixKeeperPollTimeoutStr) {
c0a4658
+        libfcgiIsAfUnixKeeperPollTimeout = atoi(libfcgiIsAfUnixKeeperPollTimeoutStr);
c0a4658
+    }
c0a4658
+
c0a4658
     asyncIoTable = (AioInfo *)malloc(asyncIoTableSize * sizeof(AioInfo));
c0a4658
     if(asyncIoTable == NULL) {
c0a4658
         errno = ENOMEM;
c0a4658
@@ -755,19 +769,16 @@ int OS_Close(int fd)
c0a4658
 
c0a4658
     if (shutdown(fd, 1) == 0)
c0a4658
     {
c0a4658
-        struct timeval tv;
c0a4658
-        fd_set rfds;
c0a4658
+        struct pollfd pfd;
c0a4658
         int rv;
c0a4658
         char trash[1024];
c0a4658
 
c0a4658
-        FD_ZERO(&rfds);
c0a4658
+        pfd.fd = fd;
c0a4658
+        pfd.events = POLLIN;
c0a4658
 
c0a4658
         do 
c0a4658
         {
c0a4658
-            FD_SET(fd, &rfds);
c0a4658
-            tv.tv_sec = 2;
c0a4658
-            tv.tv_usec = 0;
c0a4658
-            rv = select(fd + 1, &rfds, NULL, NULL, &tv;;
c0a4658
+            rv = poll(&pfd, 1, libfcgiOsClosePollTimeout);
c0a4658
         }
c0a4658
         while (rv > 0 && read(fd, trash, sizeof(trash)) > 0);
c0a4658
     }
c0a4658
@@ -1116,13 +1127,11 @@ static int is_reasonable_accept_errno (const int error)
c0a4658
  */
c0a4658
 static int is_af_unix_keeper(const int fd)
c0a4658
 {
c0a4658
-    struct timeval tval = { READABLE_UNIX_FD_DROP_DEAD_TIMEVAL };
c0a4658
-    fd_set read_fds;
c0a4658
-
c0a4658
-    FD_ZERO(&read_fds);
c0a4658
-    FD_SET(fd, &read_fds);
c0a4658
+    struct pollfd pfd;
c0a4658
+    pfd.fd = fd;
c0a4658
+    pfd.events = POLLIN;
c0a4658
 
c0a4658
-    return select(fd + 1, &read_fds, NULL, NULL, &tval) >= 0 && FD_ISSET(fd, &read_fds);
c0a4658
+    return poll(&pfd, 1, libfcgiIsAfUnixKeeperPollTimeout) >= 0 && (pfd.revents & POLLIN);
c0a4658
 }
c0a4658
 
c0a4658
 /*