]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
io_event_add: return if eventtype is already registered.
[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.24 2007/01/18 00:25:26 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 #  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
467         if ((i->what & what) == what) /* event type is already registered */
468                 return true;
469 #ifdef DEBUG_IO
470         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
471 #endif
472         i->what |= what;
473 #ifdef IO_USE_EPOLL
474         if (io_masterfd >= 0)
475                 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
476 #endif
477
478 #ifdef IO_USE_KQUEUE
479         return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
480 #endif
481 #ifdef IO_USE_DEVPOLL
482         return io_event_change_devpoll(fd, i->what);
483 #endif
484 #ifdef IO_USE_POLL
485         return io_event_change_poll(fd, i->what);
486 #endif
487 #ifdef IO_USE_SELECT
488         if (fd > select_maxfd)
489                 select_maxfd = fd;
490
491         if (what & IO_WANTREAD)
492                 FD_SET(fd, &readers);
493         if (what & IO_WANTWRITE)
494                 FD_SET(fd, &writers);
495
496         return true;
497 #endif
498 }
499
500
501 bool
502 io_setnonblock(int fd)
503 {
504         int flags = fcntl(fd, F_GETFL);
505         if (flags == -1)
506                 return false;
507
508 #ifndef O_NONBLOCK
509 #define O_NONBLOCK O_NDELAY
510 #endif
511         flags |= O_NONBLOCK;
512
513         return fcntl(fd, F_SETFL, flags) == 0;
514 }
515
516
517 #ifdef IO_USE_DEVPOLL
518 static void
519 io_close_devpoll(int fd)
520 {
521         struct pollfd p;
522         p.events = POLLREMOVE;
523         p.fd = fd;
524         write(io_masterfd, &p, sizeof p);
525 }
526 #else
527 static inline void io_close_devpoll(int UNUSED x) { /* NOTHING */ }
528 #endif
529
530
531
532 #ifdef IO_USE_POLL
533 static void
534 io_close_poll(int fd)
535 {
536         struct pollfd *p;
537         p = array_get(&pollfds, sizeof *p, fd);
538         if (!p) return;
539
540         p->fd = -1;
541         if (fd == poll_maxfd) {
542                 while (poll_maxfd > 0) {
543                         --poll_maxfd;
544                         p = array_get(&pollfds, sizeof *p, poll_maxfd);
545                         if (p && p->fd >= 0)
546                                 break;
547                 }
548         }
549 }
550 #else
551 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
552 #endif
553
554
555 #ifdef IO_USE_SELECT
556 static void
557 io_close_select(int fd)
558 {
559         io_event *i;
560
561         if (io_masterfd >= 0)   /* Are we using epoll()? */
562                 return;
563
564         FD_CLR(fd, &writers);
565         FD_CLR(fd, &readers);
566
567         i = io_event_get(fd);
568         if (!i) return;
569
570         if (fd == select_maxfd) {
571                 while (select_maxfd>0) {
572                         --select_maxfd; /* find largest fd */
573                         i = io_event_get(select_maxfd);
574                         if (i && i->callback) break;
575                 }
576         }
577 }
578 #else
579 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
580 #endif
581
582
583 bool
584 io_close(int fd)
585 {
586         io_event *i;
587
588         i = io_event_get(fd);
589 #ifdef IO_USE_KQUEUE
590         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
591                 io_event_kqueue_commit_cache();
592
593         /* both kqueue and epoll remove fd from all sets automatically on the last close
594          * of the descriptor. since we don't know if this is the last close we'll have
595          * to remove the set explicitly. */
596         if (i) {
597                 io_event_change_kqueue(fd, i->what, EV_DELETE);
598                 io_event_kqueue_commit_cache();
599         }
600 #endif
601
602         io_close_devpoll(fd);
603         io_close_poll(fd);
604         io_close_select(fd);
605
606 #ifdef IO_USE_EPOLL
607         io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
608 #endif
609         if (i) {
610                 i->callback = NULL;
611                 i->what = 0;
612         }
613         return close(fd) == 0;
614 }
615
616
617 bool
618 io_event_del(int fd, short what)
619 {
620         io_event *i = io_event_get(fd);
621 #ifdef DEBUG_IO
622         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
623 #endif
624         if (!i) return false;
625
626         i->what &= ~what;
627
628 #ifdef IO_USE_DEVPOLL
629         return io_event_change_devpoll(fd, i->what);
630 #endif
631 #ifdef IO_USE_POLL
632         return io_event_change_poll(fd, i->what);
633 #endif
634 #ifdef IO_USE_EPOLL
635         if (io_masterfd >= 0)
636                 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
637 #endif
638
639 #ifdef IO_USE_KQUEUE
640         return io_event_change_kqueue(fd, what, EV_DISABLE);
641 #endif
642 #ifdef IO_USE_SELECT
643         if (what & IO_WANTWRITE)
644                 FD_CLR(fd, &writers);
645
646         if (what & IO_WANTREAD)
647                 FD_CLR(fd, &readers);
648
649         return true;
650 #endif
651 }
652
653
654 #ifdef IO_USE_SELECT
655 static int
656 io_dispatch_select(struct timeval *tv)
657 {
658         fd_set readers_tmp = readers;
659         fd_set writers_tmp = writers;
660         short what;
661         int ret, i;
662         int fds_ready;
663         ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
664         if (ret <= 0)
665                 return ret;
666
667         fds_ready = ret;
668
669         for (i = 0; i <= select_maxfd; i++) {
670                 what = 0;
671                 if (FD_ISSET(i, &readers_tmp)) {
672                         what = IO_WANTREAD;
673                         fds_ready--;
674                 }
675
676                 if (FD_ISSET(i, &writers_tmp)) {
677                         what |= IO_WANTWRITE;
678                         fds_ready--;
679                 }
680                 if (what)
681                         io_docallback(i, what);
682                 if (fds_ready <= 0)
683                         break;
684         }
685
686         return ret;
687 }
688 #endif
689
690
691 #ifdef IO_USE_DEVPOLL
692 static int
693 io_dispatch_devpoll(struct timeval *tv)
694 {
695         struct dvpoll dvp;
696         time_t sec = tv->tv_sec * 1000;
697         int i, total, ret, timeout = tv->tv_usec + sec;
698         short what;
699         struct pollfd p[100];
700
701         if (timeout < 0)
702                 timeout = 1000;
703
704         total = 0;
705         do {
706                 dvp.dp_timeout = timeout;
707                 dvp.dp_nfds = 100;
708                 dvp.dp_fds = p;
709                 ret = ioctl(io_masterfd, DP_POLL, &dvp);
710                 total += ret;
711                 if (ret <= 0)
712                         return total;
713                 for (i=0; i < ret ; i++) {
714                         what = 0;
715                         if (p[i].revents & (POLLIN|POLLPRI))
716                                 what = IO_WANTREAD;
717
718                         if (p[i].revents & POLLOUT)
719                                 what |= IO_WANTWRITE;
720
721                         if (p[i].revents && !what) {
722                                 /* other flag is set, probably POLLERR */
723                                 what = IO_ERROR;
724                         }
725                         io_docallback(p[i].fd, what);
726                 }
727         } while (ret == 100);
728
729         return total;
730 }
731 #endif
732
733
734 #ifdef IO_USE_POLL
735 static int
736 io_dispatch_poll(struct timeval *tv)
737 {
738         time_t sec = tv->tv_sec * 1000;
739         int i, ret, timeout = tv->tv_usec + sec;
740         int fds_ready;
741         short what;
742         struct pollfd *p = array_start(&pollfds);
743
744         if (timeout < 0)
745                 timeout = 1000;
746
747         ret = poll(p, poll_maxfd + 1, timeout);
748         if (ret <= 0)
749                 return ret;
750
751         fds_ready = ret;
752         for (i=0; i <= poll_maxfd; i++) {
753                 what = 0;
754                 if (p[i].revents & (POLLIN|POLLPRI))
755                         what = IO_WANTREAD;
756
757                 if (p[i].revents & POLLOUT)
758                         what |= IO_WANTWRITE;
759
760                 if (p[i].revents && !what) {
761                         /* other flag is set, probably POLLERR */
762                         what = IO_ERROR;
763                 }
764                 if (what) {
765                         fds_ready--;
766                         io_docallback(i, what);
767                 }
768                 if (fds_ready <= 0)
769                         break;
770         }
771
772         return ret;
773 }
774 #endif
775
776
777 #ifdef IO_USE_EPOLL
778 static int
779 io_dispatch_epoll(struct timeval *tv)
780 {
781         time_t sec = tv->tv_sec * 1000;
782         int i, total = 0, ret, timeout = tv->tv_usec + sec;
783         struct epoll_event epoll_ev[100];
784         short type;
785
786         if (timeout < 0)
787                 timeout = 1000;
788
789         do {
790                 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
791                 total += ret;
792                 if (ret <= 0)
793                         return total;
794
795                 for (i = 0; i < ret; i++) {
796                         type = 0;
797                         if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
798                                 type = IO_ERROR;
799
800                         if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
801                                 type |= IO_WANTREAD;
802
803                         if (epoll_ev[i].events & EPOLLOUT)
804                                 type |= IO_WANTWRITE;
805
806                         io_docallback(epoll_ev[i].data.fd, type);
807                 }
808
809                 timeout = 0;
810         } while (ret == 100);
811
812         return total;
813 }
814 #endif
815
816
817 #ifdef IO_USE_KQUEUE
818 static int
819 io_dispatch_kqueue(struct timeval *tv)
820 {
821         int i, total = 0, ret;
822         struct kevent kev[100];
823         struct kevent *newevents;
824         struct timespec ts;
825         int newevents_len;
826         ts.tv_sec = tv->tv_sec;
827         ts.tv_nsec = tv->tv_usec * 1000;
828
829         do {
830                 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
831                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
832                 assert(newevents_len >= 0);
833                 if (newevents_len < 0)
834                         newevents_len = 0;
835 #ifdef DEBUG
836                 if (newevents_len)
837                         assert(newevents != NULL);
838 #endif
839                 ret = kevent(io_masterfd, newevents, newevents_len, kev,
840                              100, &ts);
841                 if ((newevents_len>0) && ret != -1)
842                         array_trunc(&io_evcache);
843
844                 total += ret;
845                 if (ret <= 0)
846                         return total;
847
848                 for (i = 0; i < ret; i++) {
849                         if (kev[i].flags & EV_EOF) {
850 #ifdef DEBUG
851                                 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
852                                         kev[i].filter, kev[i].ident);
853 #endif
854                                 io_docallback((int)kev[i].ident, IO_ERROR);
855                                 continue;
856                         }
857
858                         switch (kev[i].filter) {
859                                 case EVFILT_READ:
860                                         io_docallback((int)kev[i].ident, IO_WANTREAD);
861                                         break;
862                                 case EVFILT_WRITE:
863                                         io_docallback((int)kev[i].ident, IO_WANTWRITE);
864                                         break;
865                                 default:
866 #ifdef DEBUG
867                                         LogDebug("Unknown kev.filter number %d for fd %d",
868                                                 kev[i].filter, kev[i].ident); /* Fall through */
869 #endif
870                                 case EV_ERROR:
871                                         io_docallback((int)kev[i].ident, IO_ERROR);
872                                         break;
873                         }
874                 }
875                 ts.tv_sec = 0;
876                 ts.tv_nsec = 0;
877         } while (ret == 100);
878
879         return total;
880 }
881 #endif
882
883
884 int
885 io_dispatch(struct timeval *tv)
886 {
887 #ifdef IO_USE_EPOLL
888         if (io_masterfd >= 0)
889                 return io_dispatch_epoll(tv);
890 #endif
891 #ifdef IO_USE_SELECT
892         return io_dispatch_select(tv);
893 #endif
894 #ifdef IO_USE_KQUEUE
895         return io_dispatch_kqueue(tv);
896 #endif
897 #ifdef IO_USE_DEVPOLL
898         return io_dispatch_devpoll(tv);
899 #endif
900 #ifdef IO_USE_POLL
901         return io_dispatch_poll(tv);
902 #endif
903 }
904
905
906 /* call the callback function inside the struct matching fd */
907 static void
908 io_docallback(int fd, short what)
909 {
910         io_event *i;
911 #ifdef DEBUG_IO
912         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
913 #endif
914         i = io_event_get(fd);
915
916         if (i->callback) {      /* callback might be NULL if a previous callback function
917                                    called io_close on this fd */
918                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
919         }
920         /* if error indicator is set, we return the event(s) that were registered */
921 }