]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/io.c
cleanup
[ngircd-alex.git] / src / ngircd / io.c
index b5ec13e2abdea92f681f4e1349615e223bfe48b5..39b994b22fb67b19d41f52339e9d3f8f300ead94 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.13 2006/05/09 17:02:40 fw Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.17 2006/09/16 14:49:26 fw Exp $";
 
 #include <assert.h>
 #include <stdlib.h>
@@ -31,7 +31,6 @@ static char UNUSED id[] = "$Id: io.c,v 1.13 2006/05/09 17:02:40 fw Exp $";
 
 typedef struct {
  void (*callback)(int, short);
- int fd;
  short what;
 } io_event;
 
@@ -45,8 +44,8 @@ typedef struct {
 #define IO_USE_KQUEUE   1
 # else
 #define IO_USE_SELECT   1
-#endif
-#endif
+# endif /* HAVE_KQUEUE */
+#endif /* HAVE_EPOLL_CREATE */
 
 static bool library_initialized;
 
@@ -88,66 +87,65 @@ static io_event *
 io_event_get(int fd)
 {
        io_event *i;
+
        assert(fd >= 0);
-       i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
-       assert(i);
+
+       i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
+
+       assert(i != NULL);
 
        return i;
 }
 
 
-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)
-               eventsize = FD_SETSIZE - 1;
-#endif
-#endif
-       if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
-               eventsize = 0;
-
-#ifdef IO_USE_EPOLL
-       io_masterfd = epoll_create(ecreate_hint);
-       Log(LOG_INFO,
-           "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
-           ecreate_hint, eventsize, io_masterfd);
-       ret = io_masterfd >= 0;
-       if (ret) library_initialized = true;
-
-       return ret;
-#endif
 #ifdef IO_USE_SELECT
+static bool
+io_library_init_select(unsigned int eventsize)
+{
        Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
            eventsize);
        FD_ZERO(&readers);
        FD_ZERO(&writers);
 #ifdef FD_SETSIZE
-       if (Conf_MaxConnections >= FD_SETSIZE) {
+       if (Conf_MaxConnections >= (int)FD_SETSIZE) {
                Log(LOG_WARNING,
                    "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
                    Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
 
                Conf_MaxConnections = FD_SETSIZE - 1;
        }
-#else
-       Log(LOG_WARNING,
-           "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
 #endif /* FD_SETSIZE */
        library_initialized = true;
        return true;
+}
 #endif /* SELECT */
+
+
+#ifdef IO_USE_EPOLL
+static bool
+io_library_init_epoll(unsigned int eventsize)
+{
+       bool ret;
+       int ecreate_hint = (int)eventsize;
+       if (ecreate_hint <= 0)
+               ecreate_hint = 128;
+       io_masterfd = epoll_create(ecreate_hint);
+       Log(LOG_INFO,
+           "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
+           ecreate_hint, eventsize, io_masterfd);
+       ret = io_masterfd >= 0;
+       if (ret) library_initialized = true;
+
+       return ret;
+}
+#endif
+
+
 #ifdef IO_USE_KQUEUE
+static bool
+io_library_init_kqueue(unsigned int eventsize)
+{
+       bool ret;
        io_masterfd = kqueue();
 
        Log(LOG_INFO,
@@ -155,8 +153,35 @@ io_library_init(unsigned int eventsize)
            eventsize, io_masterfd);
        ret = io_masterfd >= 0;
        if (ret) library_initialized = true;
-
        return ret;
+}
+#endif
+
+
+bool
+io_library_init(unsigned int eventsize)
+{
+       if (library_initialized)
+               return true;
+#ifdef IO_USE_SELECT
+#ifndef FD_SETSIZE
+       Log(LOG_WARNING,
+           "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
+#else
+       if (eventsize >= FD_SETSIZE)
+               eventsize = FD_SETSIZE - 1;
+#endif /* FD_SETSIZE */
+#endif /* IO_USE_SELECT */
+       if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
+               eventsize = 0;
+#ifdef IO_USE_EPOLL
+       return io_library_init_epoll(eventsize);
+#endif
+#ifdef IO_USE_KQUEUE
+       return io_library_init_kqueue(eventsize);
+#endif
+#ifdef IO_USE_SELECT
+       return io_library_init_select(eventsize);
 #endif
 }
 
@@ -167,11 +192,14 @@ io_library_shutdown(void)
 #ifdef IO_USE_SELECT
        FD_ZERO(&readers);
        FD_ZERO(&writers);
-#else
-       close(io_masterfd);     /* kqueue, epoll */
+#endif
+#ifdef IO_USE_EPOLL
+       close(io_masterfd);
        io_masterfd = -1;
 #endif
 #ifdef IO_USE_KQUEUE
+       close(io_masterfd);
+       io_masterfd = -1;
        array_free(&io_evcache);
 #endif
        library_initialized = false;
@@ -197,19 +225,15 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
        io_event *i;
 
        assert(fd >= 0);
-
-#ifdef IO_USE_SELECT
-#ifdef FD_SETSIZE
+#if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
        if (fd >= FD_SETSIZE) {
                Log(LOG_ERR,
                    "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
                    fd, FD_SETSIZE);
                return false;
        }
-#endif                         /* FD_SETSIZE */
-#endif                         /* IO_USE_SELECT */
-
-       i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
+#endif
+       i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
        if (!i) {
                Log(LOG_WARNING,
                    "array_alloc failed: could not allocate space for %d io_event structures",
@@ -217,7 +241,6 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
                return false;
        }
 
-       i->fd = fd;
        i->callback = cbfunc;
        i->what = 0;
 #ifdef IO_USE_EPOLL
@@ -257,7 +280,7 @@ io_event_kqueue_commit_cache(void)
        struct kevent *events;
        bool ret;
        int len = (int) array_length(&io_evcache, sizeof (struct kevent));
+
        if (!len) /* nothing to do */
                return true;
 
@@ -270,7 +293,7 @@ io_event_kqueue_commit_cache(void)
 
        events = array_start(&io_evcache);
 
-       assert(events);
+       assert(events != NULL);
 
        ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
        if (ret)
@@ -290,7 +313,7 @@ io_event_change_kqueue(int fd, short what, const int action)
                ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
                if (!ret)
                        ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
-       }       
+       }
 
        if (ret && (what & IO_WANTWRITE)) {
                EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
@@ -311,8 +334,6 @@ io_event_add(int fd, short what)
 {
        io_event *i = io_event_get(fd);
 
-       assert(i != NULL);
-
        if (!i) return false;
        if (i->what == what) return true;
 #ifdef DEBUG_IO
@@ -326,7 +347,6 @@ io_event_add(int fd, short what)
 #ifdef IO_USE_KQUEUE
        return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
 #endif
-
 #ifdef IO_USE_SELECT
        if (fd > select_maxfd)
                select_maxfd = fd;
@@ -357,22 +377,35 @@ io_setnonblock(int fd)
 }
 
 
-bool
-io_close(int fd)
+#ifdef IO_USE_SELECT
+static void
+io_close_select(int fd)
 {
        io_event *i;
-#ifdef IO_USE_SELECT
        FD_CLR(fd, &writers);
        FD_CLR(fd, &readers);
 
+       i = io_event_get(fd);
+       if (!i) return;
+
        if (fd == select_maxfd) {
                while (select_maxfd>0) {
-                       --select_maxfd; /* find largest fd */  
+                       --select_maxfd; /* find largest fd */
                        i = io_event_get(select_maxfd);
-                       if (i && (i->fd >= 0)) break;
-               }       
-       }       
+                       if (i && i->callback) break;
+               }
+       }
+}
+#else
+static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
 #endif
+
+
+bool
+io_close(int fd)
+{
+       io_event *i;
+
        i = io_event_get(fd);
 #ifdef IO_USE_KQUEUE
        if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
@@ -384,14 +417,17 @@ io_close(int fd)
        if (i) {
                io_event_change_kqueue(fd, i->what, EV_DELETE);
                io_event_kqueue_commit_cache();
-       }       
+       }
 #endif
+
+       io_close_select(fd);
+
 #ifdef IO_USE_EPOLL
        io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
 #endif
        if (i) {
-               memset(i, 0, sizeof(io_event));
-               i->fd = -1;
+               i->callback = NULL;
+               i->what = 0;
        }
        return close(fd) == 0;
 }
@@ -404,7 +440,6 @@ io_event_del(int fd, short what)
 #ifdef DEBUG_IO
        Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
 #endif
-       assert(i != NULL);
        if (!i) return false;
 
        i->what &= ~what;
@@ -418,10 +453,10 @@ io_event_del(int fd, short what)
 #endif
 #ifdef IO_USE_SELECT
        if (what & IO_WANTWRITE)
-               FD_CLR(i->fd, &writers);
+               FD_CLR(fd, &writers);
 
        if (what & IO_WANTREAD)
-               FD_CLR(i->fd, &readers);
+               FD_CLR(fd, &readers);
 
        return true;
 #endif
@@ -516,7 +551,7 @@ io_dispatch_kqueue(struct timeval *tv)
        int newevents_len;
        ts.tv_sec = tv->tv_sec;
        ts.tv_nsec = tv->tv_usec * 1000;
-       
+
        do {
                newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
                newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
@@ -525,7 +560,7 @@ io_dispatch_kqueue(struct timeval *tv)
                        newevents_len = 0;
 #ifdef DEBUG
                if (newevents_len)
-                       assert(newevents);
+                       assert(newevents != NULL);
 #endif
                ret = kevent(io_masterfd, newevents, newevents_len, kev,
                             100, &ts);
@@ -537,12 +572,21 @@ io_dispatch_kqueue(struct timeval *tv)
                        return total;
 
                for (i = 0; i < ret; i++) {
+                       if (kev[i].flags & EV_EOF) {
+#ifdef DEBUG
+                               LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
+                                       kev[i].filter, kev[i].ident);
+#endif
+                               io_docallback((int)kev[i].ident, IO_ERROR);
+                               continue;
+                       }
+
                        switch (kev[i].filter) {
                                case EVFILT_READ:
-                                       io_docallback(kev[i].ident, IO_WANTREAD);
+                                       io_docallback((int)kev[i].ident, IO_WANTREAD);
                                        break;
                                case EVFILT_WRITE:
-                                       io_docallback(kev[i].ident, IO_WANTWRITE);
+                                       io_docallback((int)kev[i].ident, IO_WANTWRITE);
                                        break;
                                default:
 #ifdef DEBUG
@@ -550,19 +594,9 @@ io_dispatch_kqueue(struct timeval *tv)
                                                kev[i].filter, kev[i].ident); /* Fall through */
 #endif
                                case EV_ERROR:
-                                       io_docallback(kev[i].ident, IO_ERROR);
+                                       io_docallback((int)kev[i].ident, IO_ERROR);
                                        break;
-                                               
                        }
-                       if (kev[i].flags & EV_EOF) {
-#ifdef DEBUG
-                               LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
-                                       kev[i].filter, kev[i].ident); /* Fall through */
-
-#endif                         
-                               io_docallback(kev[i].ident, IO_ERROR);
-
-                       }       
                }
                ts.tv_sec = 0;
                ts.tv_nsec = 0;
@@ -597,9 +631,10 @@ io_docallback(int fd, short what)
        Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
 #endif
        i = io_event_get(fd);
-       assert(i);
 
-       if (i->callback)        /* callback might be 0 if previous callback function called io_close on this fd */
+       if (i->callback) {      /* callback might be NULL if a previous callback function
+                                  called io_close on this fd */
                i->callback(fd, (what & IO_ERROR) ? i->what : what);
-       /* if error indicator is set, we return the event(s) the app asked for */
+       }
+       /* if error indicator is set, we return the event(s) that were registered */
 }