]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - libcharybdis/commio.c
[svn] - remove ALL braindead 2.8 I/O artifacts: MASTER_MAX, HARD_FDLIMIT, HARD_FDLIMI...
[irc/rqf/shadowircd.git] / libcharybdis / commio.c
index c30322d5285ca9fbe052b453c5a2073d210864aa..14c9efaf1ef689eef705a3cf114c36741923efcb 100644 (file)
@@ -21,7 +21,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
  *
- *  $Id: commio.c 3229 2007-03-05 17:23:07Z nenolod $
+ *  $Id: commio.c 3354 2007-04-03 09:21:31Z nenolod $
  */
 
 #include "libcharybdis.h"
@@ -56,6 +56,7 @@ static void comm_connect_callback(int fd, int status);
 static PF comm_connect_timeout;
 static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
 static PF comm_connect_tryconnect;
+static int comm_max_connections = 0;
 
 inline fde_t *
 comm_locate_fd(int fd)
@@ -140,10 +141,11 @@ comm_close_all(void)
        int fd;
 #endif
 
-       /* XXX someone tell me why we care about 4 fd's ? */
-       /* XXX btw, fd 3 is used for profiler ! */
-
-       for (i = 4; i < MAXCONNECTIONS; ++i)
+       /*
+         * we start at 4 to avoid giving fds where malloc messages
+         * could be written --nenolod
+         */
+       for (i = 4; i < comm_max_connections; ++i)
        {
                fde_t *F = comm_locate_fd(i);
 
@@ -317,39 +319,51 @@ comm_setflush(int fd, time_t timeout, PF * callback, void *cbdata)
 void
 comm_checktimeouts(void *notused)
 {
-       int fd;
        PF *hdl;
        void *data;
        fde_t *F;
-       for (fd = 0; fd <= highest_fd; fd++)
+       dlink_list *bucket;
+       int i;
+       dlink_node *n, *n2;
+
+       for (i = 0; i <= FD_HASH_SIZE; i++)
        {
-               F = comm_locate_fd(fd);
-               if(F == NULL)
-                       continue;
-               if(!F->flags.open)
-                       continue;
-               if(F->flags.closing)
+               bucket = &fd_table[i];
+
+               if (dlink_list_length(bucket) <= 0)
                        continue;
 
-               /* check flush functions */
-               if(F->flush_handler &&
-                  F->flush_timeout > 0 && F->flush_timeout < CurrentTime)
+               DLINK_FOREACH_SAFE(n, n2, bucket->head)
                {
-                       hdl = F->flush_handler;
-                       data = F->flush_data;
-                       comm_setflush(F->fd, 0, NULL, NULL);
-                       hdl(F->fd, data);
-               }
+                       F = (fde_t *) n->data;
 
-               /* check timeouts */
-               if(F->timeout_handler &&
-                  F->timeout > 0 && F->timeout < CurrentTime)
-               {
-                       /* Call timeout handler */
-                       hdl = F->timeout_handler;
-                       data = F->timeout_data;
-                       comm_settimeout(F->fd, 0, NULL, NULL);
-                       hdl(F->fd, data);
+                       if(F == NULL)
+                               continue;
+                       if(!F->flags.open)
+                               continue;
+                       if(F->flags.closing)
+                               continue;
+
+                       /* check flush functions */
+                       if(F->flush_handler &&
+                          F->flush_timeout > 0 && F->flush_timeout < CurrentTime)
+                       {
+                               hdl = F->flush_handler;
+                               data = F->flush_data;
+                               comm_setflush(F->fd, 0, NULL, NULL);
+                               hdl(F->fd, data);
+                       }
+
+                       /* check timeouts */
+                       if(F->timeout_handler &&
+                          F->timeout > 0 && F->timeout < CurrentTime)
+                       {
+                               /* Call timeout handler */
+                               hdl = F->timeout_handler;
+                               data = F->timeout_data;
+                               comm_settimeout(F->fd, 0, NULL, NULL);
+                               hdl(F->fd, data);
+                       }
                }
        }
 }
@@ -588,7 +602,7 @@ comm_socket(int family, int sock_type, int proto, const char *note)
 {
        int fd;
        /* First, make sure we aren't going to run out of file descriptors */
-       if(number_fd >= MASTER_MAX)
+       if(number_fd >= comm_max_connections)
        {
                errno = ENFILE;
                return -1;
@@ -646,7 +660,7 @@ int
 comm_accept(int fd, struct sockaddr *pn, socklen_t *addrlen)
 {
        int newfd;
-       if(number_fd >= MASTER_MAX)
+       if(number_fd >= comm_max_connections)
        {
                errno = ENFILE;
                return -1;
@@ -710,7 +724,7 @@ fdlist_update_biggest(int fd, int opening)
 {
        if(fd < highest_fd)
                return;
-       s_assert(fd < MAXCONNECTIONS);
+       s_assert(fd < comm_max_connections);
 
        if(fd > highest_fd)
        {
@@ -728,7 +742,7 @@ fdlist_update_biggest(int fd, int opening)
         * re-opening it
         */
        s_assert(!opening);
-       while (highest_fd >= 0 && comm_locate_fd(fd)->flags.open) /* XXX */
+       while (highest_fd >= 0 && comm_locate_fd(fd) != NULL)
                highest_fd--;
 }
 
@@ -737,10 +751,16 @@ void
 fdlist_init(void)
 {
        static int initialized = 0;
+       struct rlimit limit;
 
        if(!initialized)
        {
                memset(&fd_table, '\0', sizeof(dlink_list) * FD_HASH_SIZE);
+
+               /* set up comm_max_connections. */
+               if(!getrlimit(RLIMIT_NOFILE, &limit))
+                       comm_max_connections = limit.rlim_cur;
+
                initialized = 1;
        }
 }
@@ -789,6 +809,7 @@ comm_close(int fd)
        }
        comm_setselect(F->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
        comm_setflush(F->fd, 0, NULL, NULL);
+       F->timeout = 0;
        
        if (F->dns_query != NULL)
        {
@@ -802,7 +823,6 @@ comm_close(int fd)
        number_fd--;
        comm_remove_fd(fd);
 
-       F->timeout = 0;
        /* Unlike squid, we're actually closing the FD here! -- adrian */
        close(fd);
 }
@@ -858,4 +878,10 @@ comm_note(int fd, const char *format, ...)
                F->desc[0] = '\0';
 }
 
+extern int
+comm_get_maxconnections(void)
+{
+       fdlist_init();
 
+       return comm_max_connections;
+}