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