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