]> arthur.barton.de Git - ngircd.git/blobdiff - src/ngircd/io.c
kqueue: check for EV_ERROR in .flags
[ngircd.git] / src / ngircd / io.c
index 147fe66233704e47498b228d4009106a6992ad33..2c887bd019313853f77c99970b748e5a3663bd67 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.19 2006/09/16 16:47:27 fw Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.27 2007/12/27 18:25:26 fw Exp $";
 
 #include <assert.h>
 #include <stdlib.h>
@@ -30,33 +30,48 @@ static char UNUSED id[] = "$Id: io.c,v 1.19 2006/09/16 16:47:27 fw Exp $";
 /* #define DEBUG_IO */
 
 typedef struct {
+#ifdef PROTOTYPES
  void (*callback)(int, short);
+#else
+ void (*callback)();
+#endif
  short what;
 } io_event;
 
-#define INIT_IOEVENT    { NULL, -1, 0, NULL }
-#define IO_ERROR        4
+#define INIT_IOEVENT           { NULL, -1, 0, NULL }
+#define IO_ERROR               4
 
 #ifdef HAVE_EPOLL_CREATE
-#define IO_USE_EPOLL    1
+#  define IO_USE_EPOLL         1
+#  ifdef HAVE_SELECT
+#    define IO_USE_SELECT      1
+#  endif
 #else
-# ifdef HAVE_KQUEUE
-#define IO_USE_KQUEUE   1
-# else
-#   ifdef HAVE_POLL
-#define IO_USE_POLL     1
-#   else
-#define IO_USE_SELECT   1
-#   endif /* HAVE_POLL */
-# endif /* HAVE_KQUEUE */
+#  ifdef HAVE_KQUEUE
+#    define IO_USE_KQUEUE      1
+#  else
+#    ifdef HAVE_SYS_DEVPOLL_H
+#      define IO_USE_DEVPOLL   1
+#    else
+#      ifdef HAVE_POLL
+#        define IO_USE_POLL    1
+#      else
+#        ifdef HAVE_SELECT
+#          define IO_USE_SELECT        1
+#        else
+#          error "no IO API available!?"
+#        endif /* HAVE_SELECT */
+#      endif /* HAVE_POLL */
+#    endif /* HAVE_SYS_DEVPOLL_H */
+#  endif /* HAVE_KQUEUE */
 #endif /* HAVE_EPOLL_CREATE */
 
-static bool library_initialized;
+static bool library_initialized = false;
 
 #ifdef IO_USE_EPOLL
 #include <sys/epoll.h>
 
-static int io_masterfd;
+static int io_masterfd = -1;
 static bool io_event_change_epoll(int fd, short what, const int action);
 static int io_dispatch_epoll(struct timeval *tv);
 #endif
@@ -73,10 +88,18 @@ static bool io_event_change_kqueue(int, short, const int action);
 
 #ifdef IO_USE_POLL
 #include <poll.h>
+
 static array pollfds;
 static int poll_maxfd;
 
-static bool io_event_change_poll(int fd, short what);
+static bool io_event_change_poll PARAMS((int fd, short what));
+#endif
+
+#ifdef IO_USE_DEVPOLL
+#include <sys/devpoll.h>
+static int io_masterfd;
+
+static bool io_event_change_devpoll(int fd, short what);
 #endif
 
 #ifdef IO_USE_SELECT
@@ -89,7 +112,11 @@ static fd_set readers;
 static fd_set writers;
 static int select_maxfd;               /* the select() interface sucks badly */
 static int io_dispatch_select(struct timeval *tv);
+
+#ifndef IO_USE_EPOLL
+#define io_masterfd -1
 #endif
+#endif /* IO_USE_SELECT */
 
 static array io_events;
 
@@ -110,6 +137,19 @@ io_event_get(int fd)
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static void
+io_library_init_devpoll(unsigned int eventsize)
+{
+       io_masterfd = open("/dev/poll", O_RDWR);
+       if (io_masterfd >= 0)
+               library_initialized = true;
+       Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
+               eventsize, io_masterfd);
+}
+#endif
+
+
 #ifdef IO_USE_POLL
 static void
 io_library_init_poll(unsigned int eventsize)
@@ -162,11 +202,12 @@ io_library_init_epoll(unsigned 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);
-       if (io_masterfd >= 0)
+       if (io_masterfd >= 0) {
                library_initialized = true;
+               Log(LOG_INFO,
+                   "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
+                   ecreate_hint, eventsize, io_masterfd);
+       }
 }
 #endif
 
@@ -204,15 +245,23 @@ io_library_init(unsigned int eventsize)
                eventsize = 0;
 #ifdef IO_USE_EPOLL
        io_library_init_epoll(eventsize);
+#ifdef IO_USE_SELECT
+       if (io_masterfd < 0)
+               Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
+#endif
 #endif
 #ifdef IO_USE_KQUEUE
        io_library_init_kqueue(eventsize);
 #endif
+#ifdef IO_USE_DEVPOLL
+       io_library_init_devpoll(eventsize);
+#endif
 #ifdef IO_USE_POLL
        io_library_init_poll(eventsize);
 #endif
 #ifdef IO_USE_SELECT
-       io_library_init_select(eventsize);
+       if (! library_initialized)
+               io_library_init_select(eventsize);
 #endif
        return library_initialized;
 }
@@ -226,7 +275,8 @@ io_library_shutdown(void)
        FD_ZERO(&writers);
 #endif
 #ifdef IO_USE_EPOLL
-       close(io_masterfd);
+       if (io_masterfd >= 0)
+               close(io_masterfd);
        io_masterfd = -1;
 #endif
 #ifdef IO_USE_KQUEUE
@@ -257,7 +307,7 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
        io_event *i;
 
        assert(fd >= 0);
-#if defined(IO_USE_SELECT) || defined(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)",
@@ -275,6 +325,9 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
 
        i->callback = cbfunc;
        i->what = 0;
+#ifdef IO_USE_DEVPOLL
+       ret = io_event_change_devpoll(fd, what);
+#endif
 #ifdef IO_USE_POLL
        ret = io_event_change_poll(fd, what);
 #endif
@@ -285,13 +338,34 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
        ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
 #endif
 #ifdef IO_USE_SELECT
-       ret = io_event_add(fd, what);
+       if (io_masterfd < 0)
+               ret = io_event_add(fd, what);
 #endif
        if (ret) i->what = what;
        return ret;
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static bool
+io_event_change_devpoll(int fd, short what)
+{
+       struct pollfd p;
+
+       p.events = 0;
+
+       if (what & IO_WANTREAD)
+               p.events = POLLIN | POLLPRI;
+       if (what & IO_WANTWRITE)
+               p.events |= POLLOUT;
+
+       p.fd = fd;
+       return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
+}
+#endif
+
+
+
 #ifdef IO_USE_POLL
 static bool
 io_event_change_poll(int fd, short what)
@@ -393,18 +467,24 @@ io_event_add(int fd, short what)
        io_event *i = io_event_get(fd);
 
        if (!i) return false;
-       if (i->what == what) return true;
+
+       if ((i->what & what) == what) /* event type is already registered */
+               return true;
 #ifdef DEBUG_IO
-       Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
+       Log(LOG_DEBUG, "io_event_add(): fd %d, what %d.", fd, what);
 #endif
        i->what |= what;
 #ifdef IO_USE_EPOLL
-       return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
+       if (io_masterfd >= 0)
+               return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
 #endif
 
 #ifdef IO_USE_KQUEUE
        return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
 #endif
+#ifdef IO_USE_DEVPOLL
+       return io_event_change_devpoll(fd, i->what);
+#endif
 #ifdef IO_USE_POLL
        return io_event_change_poll(fd, i->what);
 #endif
@@ -438,6 +518,25 @@ io_setnonblock(int fd)
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static void
+io_close_devpoll(int fd)
+{
+       struct pollfd p;
+       p.events = POLLREMOVE;
+       p.fd = fd;
+       write(io_masterfd, &p, sizeof p);
+}
+#else
+static inline void
+io_close_devpoll(int UNUSED x)
+{ 
+       /* NOTHING */
+}
+#endif
+
+
+
 #ifdef IO_USE_POLL
 static void
 io_close_poll(int fd)
@@ -466,6 +565,10 @@ static void
 io_close_select(int fd)
 {
        io_event *i;
+
+       if (io_masterfd >= 0)   /* Are we using epoll()? */
+               return;
+
        FD_CLR(fd, &writers);
        FD_CLR(fd, &readers);
 
@@ -481,7 +584,11 @@ io_close_select(int fd)
        }
 }
 #else
-static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
+static inline void
+io_close_select(int UNUSED x)
+{ 
+       /* NOTHING */
+}
 #endif
 
 
@@ -504,6 +611,7 @@ io_close(int fd)
        }
 #endif
 
+       io_close_devpoll(fd);
        io_close_poll(fd);
        io_close_select(fd);
 
@@ -529,11 +637,15 @@ io_event_del(int fd, short what)
 
        i->what &= ~what;
 
+#ifdef IO_USE_DEVPOLL
+       return io_event_change_devpoll(fd, i->what);
+#endif
 #ifdef IO_USE_POLL
        return io_event_change_poll(fd, i->what);
 #endif
 #ifdef IO_USE_EPOLL
-       return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
+       if (io_masterfd >= 0)
+               return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
 #endif
 
 #ifdef IO_USE_KQUEUE
@@ -588,6 +700,49 @@ io_dispatch_select(struct timeval *tv)
 #endif
 
 
+#ifdef IO_USE_DEVPOLL
+static int
+io_dispatch_devpoll(struct timeval *tv)
+{
+       struct dvpoll dvp;
+       time_t sec = tv->tv_sec * 1000;
+       int i, total, ret, timeout = tv->tv_usec + sec;
+       short what;
+       struct pollfd p[100];
+
+       if (timeout < 0)
+               timeout = 1000;
+
+       total = 0;
+       do {
+               dvp.dp_timeout = timeout;
+               dvp.dp_nfds = 100;
+               dvp.dp_fds = p;
+               ret = ioctl(io_masterfd, DP_POLL, &dvp);
+               total += ret;
+               if (ret <= 0)
+                       return total;
+               for (i=0; i < ret ; i++) {
+                       what = 0;
+                       if (p[i].revents & (POLLIN|POLLPRI))
+                               what = IO_WANTREAD;
+
+                       if (p[i].revents & POLLOUT)
+                               what |= IO_WANTWRITE;
+
+                       if (p[i].revents && !what) {
+                               /* other flag is set, probably POLLERR */
+                               what = IO_ERROR;
+                       }
+                       io_docallback(p[i].fd, what);
+               }
+       } while (ret == 100);
+
+       return total;
+}
+#endif
+
+
 #ifdef IO_USE_POLL
 static int
 io_dispatch_poll(struct timeval *tv)
@@ -687,15 +842,9 @@ io_dispatch_kqueue(struct timeval *tv)
                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)
-                       newevents_len = 0;
-#ifdef DEBUG
-               if (newevents_len)
-                       assert(newevents != NULL);
-#endif
-               ret = kevent(io_masterfd, newevents, newevents_len, kev,
-                            100, &ts);
-               if ((newevents_len>0) && ret != -1)
+
+               ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
+               if (newevents && ret != -1)
                        array_trunc(&io_evcache);
 
                total += ret;
@@ -703,30 +852,31 @@ 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);
+#ifdef DEBUG_IO
+                       LogDebug("fd %d, kev.flags: %x", (int)kev[i].ident, kev[i].flags);
 #endif
+                       if (kev[i].flags & (EV_EOF|EV_ERROR)) {
+                               if (kev[i].flags & EV_ERROR)
+                                       Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
+                                               (int)kev[i].ident, strerror((int)kev[i].data));
                                io_docallback((int)kev[i].ident, IO_ERROR);
                                continue;
                        }
 
                        switch (kev[i].filter) {
-                               case EVFILT_READ:
-                                       io_docallback((int)kev[i].ident, IO_WANTREAD);
-                                       break;
-                               case EVFILT_WRITE:
-                                       io_docallback((int)kev[i].ident, IO_WANTWRITE);
-                                       break;
-                               default:
-#ifdef DEBUG
-                                       LogDebug("Unknown kev.filter number %d for fd %d",
-                                               kev[i].filter, kev[i].ident); /* Fall through */
-#endif
-                               case EV_ERROR:
-                                       io_docallback((int)kev[i].ident, IO_ERROR);
-                                       break;
+                       case EVFILT_READ:
+                               io_docallback((int)kev[i].ident, IO_WANTREAD);
+                               break;
+                       case EVFILT_WRITE:
+                               io_docallback((int)kev[i].ident, IO_WANTWRITE);
+                               break;
+                       default:
+                               LogDebug("Unknown kev.filter number %d for fd %d",
+                                       kev[i].filter, kev[i].ident);
+                               /* Fall through */
+                       case EV_ERROR:
+                               io_docallback((int)kev[i].ident, IO_ERROR);
+                               break;
                        }
                }
                ts.tv_sec = 0;
@@ -741,18 +891,22 @@ io_dispatch_kqueue(struct timeval *tv)
 int
 io_dispatch(struct timeval *tv)
 {
+#ifdef IO_USE_EPOLL
+       if (io_masterfd >= 0)
+               return io_dispatch_epoll(tv);
+#endif
 #ifdef IO_USE_SELECT
        return io_dispatch_select(tv);
 #endif
 #ifdef IO_USE_KQUEUE
        return io_dispatch_kqueue(tv);
 #endif
+#ifdef IO_USE_DEVPOLL
+       return io_dispatch_devpoll(tv);
+#endif
 #ifdef IO_USE_POLL
        return io_dispatch_poll(tv);
 #endif
-#ifdef IO_USE_EPOLL
-       return io_dispatch_epoll(tv);
-#endif
 }