X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fio.c;h=cce6ef536557e7376bf93a1a8df9f912218ae0e4;hp=7e78e49f8dd70aefa557f0bb38c98f90921894c8;hb=32f63abb59b5c9f47b4d517e0bbf9cc73fd044dc;hpb=2a7dd06ebd9cc72d45a6a4becdbef5213d7b7800 diff --git a/src/ngircd/io.c b/src/ngircd/io.c index 7e78e49f..cce6ef53 100644 --- a/src/ngircd/io.c +++ b/src/ngircd/io.c @@ -41,6 +41,7 @@ typedef struct { #define INIT_IOEVENT { NULL, -1, 0, NULL } #define IO_ERROR 4 +#define MAX_EVENTS 100 #ifdef HAVE_EPOLL_CREATE # define IO_USE_EPOLL 1 @@ -54,7 +55,7 @@ typedef struct { # ifdef HAVE_SYS_DEVPOLL_H # define IO_USE_DEVPOLL 1 # else -# ifdef HAVE_POLL +# if defined(HAVE_POLL) && defined(HAVE_POLL_H) # define IO_USE_POLL 1 # else # ifdef HAVE_SELECT @@ -85,6 +86,20 @@ static int io_masterfd; static int io_dispatch_kqueue(struct timeval *tv); static bool io_event_change_kqueue(int, short, const int action); + +#ifndef EV_SET +/* Taken from /usr/include/sys/event.h of FreeBSD 8.1 and required by all + * platforms that have kqueue but lack EV_SET() -- for example FreeBSD 4. */ +#define EV_SET(kevp, a, b, c, d, e, f) do { \ + struct kevent *__kevp__ = (kevp); \ + __kevp__->ident = (a); \ + __kevp__->filter = (b); \ + __kevp__->flags = (c); \ + __kevp__->fflags = (d); \ + __kevp__->data = (e); \ + __kevp__->udata = (f); \ +} while(0) +#endif #endif #ifdef IO_USE_POLL @@ -160,39 +175,34 @@ 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; + int i, ret, timeout = tv->tv_usec + sec; short what; - struct pollfd p[100]; + struct pollfd p[MAX_EVENTS]; 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); + dvp.dp_timeout = timeout; + dvp.dp_nfds = MAX_EVENTS; + dvp.dp_fds = p; + ret = ioctl(io_masterfd, DP_POLL, &dvp); + + 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; } - } while (ret == 100); + io_docallback(p[i].fd, what); + } - return total; + return ret; } @@ -462,37 +472,30 @@ static int io_dispatch_epoll(struct timeval *tv) { time_t sec = tv->tv_sec * 1000; - int i, total = 0, ret, timeout = tv->tv_usec + sec; - struct epoll_event epoll_ev[100]; + int i, ret, timeout = tv->tv_usec + sec; + struct epoll_event epoll_ev[MAX_EVENTS]; short type; if (timeout < 0) timeout = 1000; - do { - ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout); - total += ret; - if (ret <= 0) - return total; + ret = epoll_wait(io_masterfd, epoll_ev, MAX_EVENTS, timeout); - for (i = 0; i < ret; i++) { - type = 0; - if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP)) - type = IO_ERROR; + for (i = 0; i < ret; i++) { + type = 0; + if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP)) + type = IO_ERROR; - if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI)) - type |= IO_WANTREAD; + if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI)) + type |= IO_WANTREAD; - if (epoll_ev[i].events & EPOLLOUT) - type |= IO_WANTWRITE; + if (epoll_ev[i].events & EPOLLOUT) + type |= IO_WANTWRITE; - io_docallback(epoll_ev[i].data.fd, type); - } - - timeout = 0; - } while (ret == 100); + io_docallback(epoll_ev[i].data.fd, type); + } - return total; + return ret; } static void @@ -576,58 +579,50 @@ io_event_change_kqueue(int fd, short what, const int action) static int io_dispatch_kqueue(struct timeval *tv) { - int i, total = 0, ret; - struct kevent kev[100]; + int i, ret; + struct kevent kev[MAX_EVENTS]; struct kevent *newevents; struct timespec ts; 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; - assert(newevents_len >= 0); - - ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts); - if (newevents && ret != -1) - array_trunc(&io_evcache); - - total += ret; - if (ret <= 0) - return total; - - for (i = 0; i < ret; i++) { - io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags); - 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: - 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; - } + newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent)); + newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL; + assert(newevents_len >= 0); + + ret = kevent(io_masterfd, newevents, newevents_len, kev, MAX_EVENTS, &ts); + if (newevents && ret != -1) + array_trunc(&io_evcache); + + for (i = 0; i < ret; i++) { + io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags); + 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: + 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; - ts.tv_nsec = 0; - } while (ret == 100); + } - return total; + return ret; } static void