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