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