]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
removed errouneous FD_SETSIZE limit when not using select()
[ngircd-alex.git] / src / ngircd / io.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * Please read the file COPYING, README and AUTHORS for more information.
7  *
8  * I/O abstraction interface.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  *
11  */
12
13 #include "portab.h"
14
15 static char UNUSED id[] = "$Id: io.c,v 1.22 2006/12/16 22:48:34 fw Exp $";
16
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24
25 #include "array.h"
26 #include "io.h"
27 #include "log.h"
28
29 /* Enables extra debug messages in event add/delete/callback code. */
30 /* #define DEBUG_IO */
31
32 typedef struct {
33  void (*callback)(int, short);
34  short what;
35 } io_event;
36
37 #define INIT_IOEVENT    { NULL, -1, 0, NULL }
38 #define IO_ERROR        4
39
40 #ifdef HAVE_EPOLL_CREATE
41 #define IO_USE_EPOLL    1
42 #else
43 # ifdef HAVE_KQUEUE
44 #define IO_USE_KQUEUE   1
45 # else
46 #  ifdef HAVE_SYS_DEVPOLL_H
47 #define IO_USE_DEVPOLL  1
48 #   else
49 #    ifdef HAVE_POLL
50 #define IO_USE_POLL     1
51 #        else
52 #define IO_USE_SELECT   1
53 #      endif /* HAVE_POLL */
54 #    endif /* HAVE_SYS_DEVPOLL_H */
55 # endif /* HAVE_KQUEUE */
56 #endif /* HAVE_EPOLL_CREATE */
57
58 static bool library_initialized;
59
60 #ifdef IO_USE_EPOLL
61 #include <sys/epoll.h>
62
63 static int io_masterfd;
64 static bool io_event_change_epoll(int fd, short what, const int action);
65 static int io_dispatch_epoll(struct timeval *tv);
66 #endif
67
68 #ifdef IO_USE_KQUEUE
69 #include <sys/types.h>
70 #include <sys/event.h>
71 static array io_evcache;
72 static int io_masterfd;
73
74 static int io_dispatch_kqueue(struct timeval *tv);
75 static bool io_event_change_kqueue(int, short, const int action);
76 #endif
77
78 #ifdef IO_USE_POLL
79 #include <poll.h>
80
81 static array pollfds;
82 static int poll_maxfd;
83
84 static bool io_event_change_poll(int fd, short what);
85 #endif
86
87 #ifdef IO_USE_DEVPOLL
88 #include <sys/devpoll.h>
89 static int io_masterfd;
90
91 static bool io_event_change_devpoll(int fd, short what);
92 #endif
93
94 #ifdef IO_USE_SELECT
95 #include "defines.h"    /* for conn.h */
96 #include "conn.h"       /* for CONN_IDX (needed by resolve.h) */
97 #include "resolve.h"    /* for RES_STAT (needed by conf.h) */
98 #include "conf.h"       /* for Conf_MaxConnections */
99
100 static fd_set readers;
101 static fd_set writers;
102 static int select_maxfd;                /* the select() interface sucks badly */
103 static int io_dispatch_select(struct timeval *tv);
104 #endif
105
106 static array io_events;
107
108 static void io_docallback PARAMS((int fd, short what));
109
110 static io_event *
111 io_event_get(int fd)
112 {
113         io_event *i;
114
115         assert(fd >= 0);
116
117         i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
118
119         assert(i != NULL);
120
121         return i;
122 }
123
124
125 #ifdef IO_USE_DEVPOLL
126 static void
127 io_library_init_devpoll(unsigned int eventsize)
128 {
129         io_masterfd = open("/dev/poll", O_RDWR);
130         if (io_masterfd >= 0)
131                 library_initialized = true;
132         Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
133                 eventsize, io_masterfd);
134 }
135 #endif
136
137
138 #ifdef IO_USE_POLL
139 static void
140 io_library_init_poll(unsigned int eventsize)
141 {
142         struct pollfd *p;
143         array_init(&pollfds);
144         poll_maxfd = 0;
145         Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
146             eventsize);
147         p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
148         if (p) {
149                 unsigned i;
150                 p = array_start(&pollfds);
151                 for (i = 0; i < eventsize; i++)
152                         p[i].fd = -1;
153
154                 library_initialized = true;
155         }
156 }
157 #endif
158
159
160 #ifdef IO_USE_SELECT
161 static void
162 io_library_init_select(unsigned int eventsize)
163 {
164         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
165             eventsize);
166         FD_ZERO(&readers);
167         FD_ZERO(&writers);
168 #ifdef FD_SETSIZE
169         if (Conf_MaxConnections >= (int)FD_SETSIZE) {
170                 Log(LOG_WARNING,
171                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
172                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
173
174                 Conf_MaxConnections = FD_SETSIZE - 1;
175         }
176 #endif /* FD_SETSIZE */
177         library_initialized = true;
178 }
179 #endif /* SELECT */
180
181
182 #ifdef IO_USE_EPOLL
183 static void
184 io_library_init_epoll(unsigned int eventsize)
185 {
186         int ecreate_hint = (int)eventsize;
187         if (ecreate_hint <= 0)
188                 ecreate_hint = 128;
189         io_masterfd = epoll_create(ecreate_hint);
190         Log(LOG_INFO,
191             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
192             ecreate_hint, eventsize, io_masterfd);
193         if (io_masterfd >= 0)
194                 library_initialized = true;
195 }
196 #endif
197
198
199 #ifdef IO_USE_KQUEUE
200 static void
201 io_library_init_kqueue(unsigned int eventsize)
202 {
203         io_masterfd = kqueue();
204
205         Log(LOG_INFO,
206             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
207             eventsize, io_masterfd);
208         if (io_masterfd >= 0)
209                 library_initialized = true;
210 }
211 #endif
212
213
214 bool
215 io_library_init(unsigned int eventsize)
216 {
217         if (library_initialized)
218                 return true;
219 #ifdef IO_USE_SELECT
220 #ifndef FD_SETSIZE
221         Log(LOG_WARNING,
222             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
223 #else
224         if (eventsize >= FD_SETSIZE)
225                 eventsize = FD_SETSIZE - 1;
226 #endif /* FD_SETSIZE */
227 #endif /* IO_USE_SELECT */
228         if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
229                 eventsize = 0;
230 #ifdef IO_USE_EPOLL
231         io_library_init_epoll(eventsize);
232 #endif
233 #ifdef IO_USE_KQUEUE
234         io_library_init_kqueue(eventsize);
235 #endif
236 #ifdef IO_USE_DEVPOLL
237         io_library_init_devpoll(eventsize);
238 #endif
239 #ifdef IO_USE_POLL
240         io_library_init_poll(eventsize);
241 #endif
242 #ifdef IO_USE_SELECT
243         io_library_init_select(eventsize);
244 #endif
245         return library_initialized;
246 }
247
248
249 void
250 io_library_shutdown(void)
251 {
252 #ifdef IO_USE_SELECT
253         FD_ZERO(&readers);
254         FD_ZERO(&writers);
255 #endif
256 #ifdef IO_USE_EPOLL
257         close(io_masterfd);
258         io_masterfd = -1;
259 #endif
260 #ifdef IO_USE_KQUEUE
261         close(io_masterfd);
262         io_masterfd = -1;
263         array_free(&io_evcache);
264 #endif
265         library_initialized = false;
266 }
267
268
269 bool
270 io_event_setcb(int fd, void (*cbfunc) (int, short))
271 {
272         io_event *i = io_event_get(fd);
273         if (!i)
274                 return false;
275
276         i->callback = cbfunc;
277         return true;
278 }
279
280
281 bool
282 io_event_create(int fd, short what, void (*cbfunc) (int, short))
283 {
284         bool ret;
285         io_event *i;
286
287         assert(fd >= 0);
288 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
289         if (fd >= FD_SETSIZE) {
290                 Log(LOG_ERR,
291                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
292                     fd, FD_SETSIZE);
293                 return false;
294         }
295 #endif
296         i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
297         if (!i) {
298                 Log(LOG_WARNING,
299                     "array_alloc failed: could not allocate space for %d io_event structures",
300                     fd);
301                 return false;
302         }
303
304         i->callback = cbfunc;
305         i->what = 0;
306 #ifdef IO_USE_DEVPOLL
307         ret = io_event_change_devpoll(fd, what);
308 #endif
309 #ifdef IO_USE_POLL
310         ret = io_event_change_poll(fd, what);
311 #endif
312 #ifdef IO_USE_EPOLL
313         ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
314 #endif
315 #ifdef IO_USE_KQUEUE
316         ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
317 #endif
318 #ifdef IO_USE_SELECT
319         ret = io_event_add(fd, what);
320 #endif
321         if (ret) i->what = what;
322         return ret;
323 }
324
325
326 #ifdef IO_USE_DEVPOLL
327 static bool
328 io_event_change_devpoll(int fd, short what)
329 {
330         struct pollfd p;
331
332         p.events = 0;
333
334         if (what & IO_WANTREAD)
335                 p.events = POLLIN | POLLPRI;
336         if (what & IO_WANTWRITE)
337                 p.events |= POLLOUT;
338
339         p.fd = fd;
340         return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
341 }
342 #endif
343
344
345
346 #ifdef IO_USE_POLL
347 static bool
348 io_event_change_poll(int fd, short what)
349 {
350         struct pollfd *p;
351         short events = 0;
352
353         if (what & IO_WANTREAD)
354                 events = POLLIN | POLLPRI;
355         if (what & IO_WANTWRITE)
356                 events |= POLLOUT;
357
358         p = array_alloc(&pollfds, sizeof *p, fd);
359         if (p) {
360                 p->events = events;
361                 p->fd = fd;
362                 if (fd > poll_maxfd)
363                         poll_maxfd = fd;
364         }
365         return p != NULL;
366 }
367 #endif
368
369 #ifdef IO_USE_EPOLL
370 static bool
371 io_event_change_epoll(int fd, short what, const int action)
372 {
373         struct epoll_event ev = { 0, {0} };
374         ev.data.fd = fd;
375
376         if (what & IO_WANTREAD)
377                 ev.events = EPOLLIN | EPOLLPRI;
378         if (what & IO_WANTWRITE)
379                 ev.events |= EPOLLOUT;
380
381         return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
382 }
383 #endif
384
385 #ifdef IO_USE_KQUEUE
386 static bool
387 io_event_kqueue_commit_cache(void)
388 {
389         struct kevent *events;
390         bool ret;
391         int len = (int) array_length(&io_evcache, sizeof (struct kevent));
392
393         if (!len) /* nothing to do */
394                 return true;
395
396         assert(len>0);
397
398         if (len < 0) {
399                 array_free(&io_evcache);
400                 return false;
401         }
402
403         events = array_start(&io_evcache);
404
405         assert(events != NULL);
406
407         ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
408         if (ret)
409                 array_trunc(&io_evcache);
410         return ret;
411 }
412
413
414 static bool
415 io_event_change_kqueue(int fd, short what, const int action)
416 {
417         struct kevent kev;
418         bool ret = true;
419
420         if (what & IO_WANTREAD) {
421                 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
422                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
423                 if (!ret)
424                         ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
425         }
426
427         if (ret && (what & IO_WANTWRITE)) {
428                 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
429                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
430                 if (!ret)
431                         ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
432         }
433
434         if (array_length(&io_evcache, sizeof kev) >= 100)
435                 io_event_kqueue_commit_cache();
436         return ret;
437 }
438 #endif
439
440
441 bool
442 io_event_add(int fd, short what)
443 {
444         io_event *i = io_event_get(fd);
445
446         if (!i) return false;
447         if (i->what == what) return true;
448 #ifdef DEBUG_IO
449         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
450 #endif
451         i->what |= what;
452 #ifdef IO_USE_EPOLL
453         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
454 #endif
455
456 #ifdef IO_USE_KQUEUE
457         return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
458 #endif
459 #ifdef IO_USE_DEVPOLL
460         return io_event_change_devpoll(fd, i->what);
461 #endif
462 #ifdef IO_USE_POLL
463         return io_event_change_poll(fd, i->what);
464 #endif
465 #ifdef IO_USE_SELECT
466         if (fd > select_maxfd)
467                 select_maxfd = fd;
468
469         if (what & IO_WANTREAD)
470                 FD_SET(fd, &readers);
471         if (what & IO_WANTWRITE)
472                 FD_SET(fd, &writers);
473
474         return true;
475 #endif
476 }
477
478
479 bool
480 io_setnonblock(int fd)
481 {
482         int flags = fcntl(fd, F_GETFL);
483         if (flags == -1)
484                 return false;
485
486 #ifndef O_NONBLOCK
487 #define O_NONBLOCK O_NDELAY
488 #endif
489         flags |= O_NONBLOCK;
490
491         return fcntl(fd, F_SETFL, flags) == 0;
492 }
493
494
495 #ifdef IO_USE_DEVPOLL
496 static void
497 io_close_devpoll(int fd)
498 {
499         struct pollfd p;
500         p.events = POLLREMOVE;
501         p.fd = fd;
502         write(io_masterfd, &p, sizeof p);
503 }
504 #else
505 static inline void io_close_devpoll(int UNUSED x) { /* NOTHING */ }
506 #endif
507
508
509
510 #ifdef IO_USE_POLL
511 static void
512 io_close_poll(int fd)
513 {
514         struct pollfd *p;
515         p = array_get(&pollfds, sizeof *p, fd);
516         if (!p) return;
517
518         p->fd = -1;
519         if (fd == poll_maxfd) {
520                 while (poll_maxfd > 0) {
521                         --poll_maxfd;
522                         p = array_get(&pollfds, sizeof *p, poll_maxfd);
523                         if (p && p->fd >= 0)
524                                 break;
525                 }
526         }
527 }
528 #else
529 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
530 #endif
531
532
533 #ifdef IO_USE_SELECT
534 static void
535 io_close_select(int fd)
536 {
537         io_event *i;
538         FD_CLR(fd, &writers);
539         FD_CLR(fd, &readers);
540
541         i = io_event_get(fd);
542         if (!i) return;
543
544         if (fd == select_maxfd) {
545                 while (select_maxfd>0) {
546                         --select_maxfd; /* find largest fd */
547                         i = io_event_get(select_maxfd);
548                         if (i && i->callback) break;
549                 }
550         }
551 }
552 #else
553 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
554 #endif
555
556
557 bool
558 io_close(int fd)
559 {
560         io_event *i;
561
562         i = io_event_get(fd);
563 #ifdef IO_USE_KQUEUE
564         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
565                 io_event_kqueue_commit_cache();
566
567         /* both kqueue and epoll remove fd from all sets automatically on the last close
568          * of the descriptor. since we don't know if this is the last close we'll have
569          * to remove the set explicitly. */
570         if (i) {
571                 io_event_change_kqueue(fd, i->what, EV_DELETE);
572                 io_event_kqueue_commit_cache();
573         }
574 #endif
575
576         io_close_devpoll(fd);
577         io_close_poll(fd);
578         io_close_select(fd);
579
580 #ifdef IO_USE_EPOLL
581         io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
582 #endif
583         if (i) {
584                 i->callback = NULL;
585                 i->what = 0;
586         }
587         return close(fd) == 0;
588 }
589
590
591 bool
592 io_event_del(int fd, short what)
593 {
594         io_event *i = io_event_get(fd);
595 #ifdef DEBUG_IO
596         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
597 #endif
598         if (!i) return false;
599
600         i->what &= ~what;
601
602 #ifdef IO_USE_DEVPOLL
603         return io_event_change_devpoll(fd, i->what);
604 #endif
605 #ifdef IO_USE_POLL
606         return io_event_change_poll(fd, i->what);
607 #endif
608 #ifdef IO_USE_EPOLL
609         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
610 #endif
611
612 #ifdef IO_USE_KQUEUE
613         return io_event_change_kqueue(fd, what, EV_DISABLE);
614 #endif
615 #ifdef IO_USE_SELECT
616         if (what & IO_WANTWRITE)
617                 FD_CLR(fd, &writers);
618
619         if (what & IO_WANTREAD)
620                 FD_CLR(fd, &readers);
621
622         return true;
623 #endif
624 }
625
626
627 #ifdef IO_USE_SELECT
628 static int
629 io_dispatch_select(struct timeval *tv)
630 {
631         fd_set readers_tmp = readers;
632         fd_set writers_tmp = writers;
633         short what;
634         int ret, i;
635         int fds_ready;
636         ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
637         if (ret <= 0)
638                 return ret;
639
640         fds_ready = ret;
641
642         for (i = 0; i <= select_maxfd; i++) {
643                 what = 0;
644                 if (FD_ISSET(i, &readers_tmp)) {
645                         what = IO_WANTREAD;
646                         fds_ready--;
647                 }
648
649                 if (FD_ISSET(i, &writers_tmp)) {
650                         what |= IO_WANTWRITE;
651                         fds_ready--;
652                 }
653                 if (what)
654                         io_docallback(i, what);
655                 if (fds_ready <= 0)
656                         break;
657         }
658
659         return ret;
660 }
661 #endif
662
663
664 #ifdef IO_USE_DEVPOLL
665 static int
666 io_dispatch_devpoll(struct timeval *tv)
667 {
668         struct dvpoll dvp;
669         time_t sec = tv->tv_sec * 1000;
670         int i, total, ret, timeout = tv->tv_usec + sec;
671         short what;
672         struct pollfd p[100];
673
674         if (timeout < 0)
675                 timeout = 1000;
676
677         total = 0;
678         do {
679                 dvp.dp_timeout = timeout;
680                 dvp.dp_nfds = 100;
681                 dvp.dp_fds = p;
682                 ret = ioctl(io_masterfd, DP_POLL, &dvp);
683                 total += ret;
684                 if (ret <= 0)
685                         return total;
686                 for (i=0; i < ret ; i++) {
687                         what = 0;
688                         if (p[i].revents & (POLLIN|POLLPRI))
689                                 what = IO_WANTREAD;
690
691                         if (p[i].revents & POLLOUT)
692                                 what |= IO_WANTWRITE;
693
694                         if (p[i].revents && !what) {
695                                 /* other flag is set, probably POLLERR */
696                                 what = IO_ERROR;
697                         }
698                         io_docallback(p[i].fd, what);
699                 }
700         } while (ret == 100);
701
702         return total;
703 }
704 #endif
705
706
707 #ifdef IO_USE_POLL
708 static int
709 io_dispatch_poll(struct timeval *tv)
710 {
711         time_t sec = tv->tv_sec * 1000;
712         int i, ret, timeout = tv->tv_usec + sec;
713         int fds_ready;
714         short what;
715         struct pollfd *p = array_start(&pollfds);
716
717         if (timeout < 0)
718                 timeout = 1000;
719
720         ret = poll(p, poll_maxfd + 1, timeout);
721         if (ret <= 0)
722                 return ret;
723
724         fds_ready = ret;
725         for (i=0; i <= poll_maxfd; i++) {
726                 what = 0;
727                 if (p[i].revents & (POLLIN|POLLPRI))
728                         what = IO_WANTREAD;
729
730                 if (p[i].revents & POLLOUT)
731                         what |= IO_WANTWRITE;
732
733                 if (p[i].revents && !what) {
734                         /* other flag is set, probably POLLERR */
735                         what = IO_ERROR;
736                 }
737                 if (what) {
738                         fds_ready--;
739                         io_docallback(i, what);
740                 }
741                 if (fds_ready <= 0)
742                         break;
743         }
744
745         return ret;
746 }
747 #endif
748
749
750 #ifdef IO_USE_EPOLL
751 static int
752 io_dispatch_epoll(struct timeval *tv)
753 {
754         time_t sec = tv->tv_sec * 1000;
755         int i, total = 0, ret, timeout = tv->tv_usec + sec;
756         struct epoll_event epoll_ev[100];
757         short type;
758
759         if (timeout < 0)
760                 timeout = 1000;
761
762         do {
763                 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
764                 total += ret;
765                 if (ret <= 0)
766                         return total;
767
768                 for (i = 0; i < ret; i++) {
769                         type = 0;
770                         if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
771                                 type = IO_ERROR;
772
773                         if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
774                                 type |= IO_WANTREAD;
775
776                         if (epoll_ev[i].events & EPOLLOUT)
777                                 type |= IO_WANTWRITE;
778
779                         io_docallback(epoll_ev[i].data.fd, type);
780                 }
781
782                 timeout = 0;
783         } while (ret == 100);
784
785         return total;
786 }
787 #endif
788
789
790 #ifdef IO_USE_KQUEUE
791 static int
792 io_dispatch_kqueue(struct timeval *tv)
793 {
794         int i, total = 0, ret;
795         struct kevent kev[100];
796         struct kevent *newevents;
797         struct timespec ts;
798         int newevents_len;
799         ts.tv_sec = tv->tv_sec;
800         ts.tv_nsec = tv->tv_usec * 1000;
801
802         do {
803                 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
804                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
805                 assert(newevents_len >= 0);
806                 if (newevents_len < 0)
807                         newevents_len = 0;
808 #ifdef DEBUG
809                 if (newevents_len)
810                         assert(newevents != NULL);
811 #endif
812                 ret = kevent(io_masterfd, newevents, newevents_len, kev,
813                              100, &ts);
814                 if ((newevents_len>0) && ret != -1)
815                         array_trunc(&io_evcache);
816
817                 total += ret;
818                 if (ret <= 0)
819                         return total;
820
821                 for (i = 0; i < ret; i++) {
822                         if (kev[i].flags & EV_EOF) {
823 #ifdef DEBUG
824                                 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
825                                         kev[i].filter, kev[i].ident);
826 #endif
827                                 io_docallback((int)kev[i].ident, IO_ERROR);
828                                 continue;
829                         }
830
831                         switch (kev[i].filter) {
832                                 case EVFILT_READ:
833                                         io_docallback((int)kev[i].ident, IO_WANTREAD);
834                                         break;
835                                 case EVFILT_WRITE:
836                                         io_docallback((int)kev[i].ident, IO_WANTWRITE);
837                                         break;
838                                 default:
839 #ifdef DEBUG
840                                         LogDebug("Unknown kev.filter number %d for fd %d",
841                                                 kev[i].filter, kev[i].ident); /* Fall through */
842 #endif
843                                 case EV_ERROR:
844                                         io_docallback((int)kev[i].ident, IO_ERROR);
845                                         break;
846                         }
847                 }
848                 ts.tv_sec = 0;
849                 ts.tv_nsec = 0;
850         } while (ret == 100);
851
852         return total;
853 }
854 #endif
855
856
857 int
858 io_dispatch(struct timeval *tv)
859 {
860 #ifdef IO_USE_SELECT
861         return io_dispatch_select(tv);
862 #endif
863 #ifdef IO_USE_KQUEUE
864         return io_dispatch_kqueue(tv);
865 #endif
866 #ifdef IO_USE_DEVPOLL
867         return io_dispatch_devpoll(tv);
868 #endif
869 #ifdef IO_USE_POLL
870         return io_dispatch_poll(tv);
871 #endif
872 #ifdef IO_USE_EPOLL
873         return io_dispatch_epoll(tv);
874 #endif
875 }
876
877
878 /* call the callback function inside the struct matching fd */
879 static void
880 io_docallback(int fd, short what)
881 {
882         io_event *i;
883 #ifdef DEBUG_IO
884         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
885 #endif
886         i = io_event_get(fd);
887
888         if (i->callback) {      /* callback might be NULL if a previous callback function
889                                    called io_close on this fd */
890                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
891         }
892         /* if error indicator is set, we return the event(s) that were registered */
893 }