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