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