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