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