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