]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/io.c
use io_close instead of plain close in Conn_ExitListeners()
[ngircd-alex.git] / src / ngircd / io.c
index 1f98af46f3fcc8ea16adb0dee5099660122e99ae..5c8d8c8fe5e564d9938950782e7d9c80dd3e4298 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.4 2005/07/14 09:20:39 alex Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.11 2005/09/04 13:38:59 fw Exp $";
 
 #include <assert.h>
 #include <stdlib.h>
@@ -46,6 +46,7 @@ typedef struct {
 #endif
 #endif
 
+static bool library_initialized;
 
 #ifdef IO_USE_EPOLL
 #include <sys/epoll.h>
@@ -97,12 +98,16 @@ io_event_get(int fd)
 bool
 io_library_init(unsigned int eventsize)
 {
+       bool ret;
 #ifdef IO_USE_EPOLL
        int ecreate_hint = (int)eventsize;
        if (ecreate_hint <= 0)
                ecreate_hint = 128;
 #endif
 
+       if (library_initialized)
+               return true;
+
 #ifdef IO_USE_SELECT
 #ifdef FD_SETSIZE
        if (eventsize >= FD_SETSIZE)
@@ -117,7 +122,10 @@ io_library_init(unsigned int eventsize)
        Log(LOG_INFO,
            "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
            ecreate_hint, eventsize, io_masterfd);
-       return io_masterfd >= 0;
+       ret = io_masterfd >= 0;
+       if (ret) library_initialized = true;
+
+       return ret;
 #endif
 #ifdef IO_USE_SELECT
        Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
@@ -135,16 +143,20 @@ io_library_init(unsigned int eventsize)
 #else
        Log(LOG_WARNING,
            "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
-#endif
+#endif /* FD_SETSIZE */
+       library_initialized = true;
        return true;
-#endif
+#endif /* SELECT */
 #ifdef IO_USE_KQUEUE
        io_masterfd = kqueue();
 
        Log(LOG_INFO,
            "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
            eventsize, io_masterfd);
-       return io_masterfd >= 0;
+       ret = io_masterfd >= 0;
+       if (ret) library_initialized = true;
+
+       return ret;
 #endif
 }
 
@@ -162,6 +174,7 @@ io_library_shutdown(void)
 #ifdef IO_USE_KQUEUE
        array_free(&io_evcache);
 #endif
+       library_initialized = false;
 }
 
 
@@ -285,8 +298,7 @@ io_event_add_kqueue(int fd, short what)
 {
        struct kevent kev;
        short filter = 0;
-       unsigned int len = array_length(&io_evcache, sizeof kev);
-       bool ret;
+       size_t len = array_length(&io_evcache, sizeof kev);
 
        if (what & IO_WANTREAD)
                filter = EVFILT_READ;
@@ -295,9 +307,7 @@ io_event_add_kqueue(int fd, short what)
                filter |= EVFILT_WRITE;
 
        if (len >= 100) {
-               ret = io_event_kqueue_commit_cache();
-               if (ret)
-                       array_trunc(&io_evcache);
+               (void)io_event_kqueue_commit_cache();
        }
 
        EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
@@ -380,7 +390,10 @@ io_close(int fd)
        if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
                io_event_kqueue_commit_cache();
 #endif
-       return close(fd) == 0;  /* both epoll an kqueue will remove fd from all sets automatically */
+#ifdef IO_USE_EPOLL
+       epoll_ctl(io_masterfd, EPOLL_CTL_DEL, fd, NULL);
+#endif
+       return close(fd) == 0;  /* kqueue will remove fd from all sets automatically */
 }
 
 
@@ -511,7 +524,7 @@ io_dispatch_kqueue(struct timeval *tv)
 {
        int i, total = 0, ret;
        struct kevent kev[100];
-       struct kevents *newevents;
+       struct kevent *newevents;
        struct timespec ts;
        int newevents_len;
        short type;
@@ -519,7 +532,7 @@ io_dispatch_kqueue(struct timeval *tv)
        ts.tv_nsec = tv->tv_usec * 1000;
        
        do {
-               newevents_len = array_length(&io_evcache, sizeof (struct kevent));
+               newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
                newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
                assert(newevents_len >= 0);
                if (newevents_len < 0)