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.
8 * I/O abstraction interface.
9 * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
15 static char UNUSED id[] = "$Id: io.c,v 1.27 2007/12/27 18:25:26 fw Exp $";
21 #include <sys/types.h>
29 /* Enables extra debug messages in event add/delete/callback code. */
30 /* #define DEBUG_IO */
34 void (*callback)(int, short);
41 #define INIT_IOEVENT { NULL, -1, 0, NULL }
44 #ifdef HAVE_EPOLL_CREATE
45 # define IO_USE_EPOLL 1
47 # define IO_USE_SELECT 1
51 # define IO_USE_KQUEUE 1
53 # ifdef HAVE_SYS_DEVPOLL_H
54 # define IO_USE_DEVPOLL 1
57 # define IO_USE_POLL 1
60 # define IO_USE_SELECT 1
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 */
69 static bool library_initialized = false;
72 #include <sys/epoll.h>
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);
80 #include <sys/types.h>
81 #include <sys/event.h>
82 static array io_evcache;
83 static int io_masterfd;
85 static int io_dispatch_kqueue(struct timeval *tv);
86 static bool io_event_change_kqueue(int, short, const int action);
93 static int poll_maxfd;
95 static bool io_event_change_poll PARAMS((int fd, short what));
99 #include <sys/devpoll.h>
100 static int io_masterfd;
102 static bool io_event_change_devpoll(int fd, short what);
106 #include "defines.h" /* for conn.h */
107 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
108 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
109 #include "conf.h" /* for Conf_MaxConnections */
111 static fd_set readers;
112 static fd_set writers;
113 static int select_maxfd; /* the select() interface sucks badly */
114 static int io_dispatch_select(struct timeval *tv);
117 #define io_masterfd -1
119 #endif /* IO_USE_SELECT */
121 static array io_events;
123 static void io_docallback PARAMS((int fd, short what));
132 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
140 #ifdef IO_USE_DEVPOLL
142 io_library_init_devpoll(unsigned int eventsize)
144 io_masterfd = open("/dev/poll", O_RDWR);
145 if (io_masterfd >= 0)
146 library_initialized = true;
147 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
148 eventsize, io_masterfd);
155 io_library_init_poll(unsigned int eventsize)
158 array_init(&pollfds);
160 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
162 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
165 p = array_start(&pollfds);
166 for (i = 0; i < eventsize; i++)
169 library_initialized = true;
177 io_library_init_select(unsigned int eventsize)
179 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
184 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
186 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
187 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
189 Conf_MaxConnections = FD_SETSIZE - 1;
191 #endif /* FD_SETSIZE */
192 library_initialized = true;
199 io_library_init_epoll(unsigned int eventsize)
201 int ecreate_hint = (int)eventsize;
202 if (ecreate_hint <= 0)
204 io_masterfd = epoll_create(ecreate_hint);
205 if (io_masterfd >= 0) {
206 library_initialized = true;
208 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
209 ecreate_hint, eventsize, io_masterfd);
217 io_library_init_kqueue(unsigned int eventsize)
219 io_masterfd = kqueue();
222 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
223 eventsize, io_masterfd);
224 if (io_masterfd >= 0)
225 library_initialized = true;
231 io_library_init(unsigned int eventsize)
233 if (library_initialized)
238 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
240 if (eventsize >= FD_SETSIZE)
241 eventsize = FD_SETSIZE - 1;
242 #endif /* FD_SETSIZE */
243 #endif /* IO_USE_SELECT */
244 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
247 io_library_init_epoll(eventsize);
250 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
254 io_library_init_kqueue(eventsize);
256 #ifdef IO_USE_DEVPOLL
257 io_library_init_devpoll(eventsize);
260 io_library_init_poll(eventsize);
263 if (! library_initialized)
264 io_library_init_select(eventsize);
266 return library_initialized;
271 io_library_shutdown(void)
278 if (io_masterfd >= 0)
285 array_free(&io_evcache);
287 library_initialized = false;
292 io_event_setcb(int fd, void (*cbfunc) (int, short))
294 io_event *i = io_event_get(fd);
298 i->callback = cbfunc;
304 io_event_create(int fd, short what, void (*cbfunc) (int, short))
310 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
311 if (fd >= FD_SETSIZE) {
313 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
318 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
321 "array_alloc failed: could not allocate space for %d io_event structures",
326 i->callback = cbfunc;
328 #ifdef IO_USE_DEVPOLL
329 ret = io_event_change_devpoll(fd, what);
332 ret = io_event_change_poll(fd, what);
335 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
338 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
342 ret = io_event_add(fd, what);
344 if (ret) i->what = what;
349 #ifdef IO_USE_DEVPOLL
351 io_event_change_devpoll(int fd, short what)
357 if (what & IO_WANTREAD)
358 p.events = POLLIN | POLLPRI;
359 if (what & IO_WANTWRITE)
363 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
371 io_event_change_poll(int fd, short what)
376 if (what & IO_WANTREAD)
377 events = POLLIN | POLLPRI;
378 if (what & IO_WANTWRITE)
381 p = array_alloc(&pollfds, sizeof *p, fd);
394 io_event_change_epoll(int fd, short what, const int action)
396 struct epoll_event ev = { 0, {0} };
399 if (what & IO_WANTREAD)
400 ev.events = EPOLLIN | EPOLLPRI;
401 if (what & IO_WANTWRITE)
402 ev.events |= EPOLLOUT;
404 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
410 io_event_kqueue_commit_cache(void)
412 struct kevent *events;
414 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
416 if (!len) /* nothing to do */
422 array_free(&io_evcache);
426 events = array_start(&io_evcache);
428 assert(events != NULL);
430 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
432 array_trunc(&io_evcache);
438 io_event_change_kqueue(int fd, short what, const int action)
443 if (what & IO_WANTREAD) {
444 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
445 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
447 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
450 if (ret && (what & IO_WANTWRITE)) {
451 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
452 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
454 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
457 if (array_length(&io_evcache, sizeof kev) >= 100)
458 io_event_kqueue_commit_cache();
465 io_event_add(int fd, short what)
467 io_event *i = io_event_get(fd);
469 if (!i) return false;
471 if ((i->what & what) == what) /* event type is already registered */
474 Log(LOG_DEBUG, "io_event_add(): fd %d, what %d.", fd, what);
478 if (io_masterfd >= 0)
479 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
483 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
485 #ifdef IO_USE_DEVPOLL
486 return io_event_change_devpoll(fd, i->what);
489 return io_event_change_poll(fd, i->what);
492 if (fd > select_maxfd)
495 if (what & IO_WANTREAD)
496 FD_SET(fd, &readers);
497 if (what & IO_WANTWRITE)
498 FD_SET(fd, &writers);
506 io_setnonblock(int fd)
508 int flags = fcntl(fd, F_GETFL);
513 #define O_NONBLOCK O_NDELAY
517 return fcntl(fd, F_SETFL, flags) == 0;
521 #ifdef IO_USE_DEVPOLL
523 io_close_devpoll(int fd)
526 p.events = POLLREMOVE;
528 write(io_masterfd, &p, sizeof p);
532 io_close_devpoll(int UNUSED x)
542 io_close_poll(int fd)
545 p = array_get(&pollfds, sizeof *p, fd);
549 if (fd == poll_maxfd) {
550 while (poll_maxfd > 0) {
552 p = array_get(&pollfds, sizeof *p, poll_maxfd);
559 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
565 io_close_select(int fd)
569 if (io_masterfd >= 0) /* Are we using epoll()? */
572 FD_CLR(fd, &writers);
573 FD_CLR(fd, &readers);
575 i = io_event_get(fd);
578 if (fd == select_maxfd) {
579 while (select_maxfd>0) {
580 --select_maxfd; /* find largest fd */
581 i = io_event_get(select_maxfd);
582 if (i && i->callback) break;
588 io_close_select(int UNUSED x)
600 i = io_event_get(fd);
602 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
603 io_event_kqueue_commit_cache();
605 /* both kqueue and epoll remove fd from all sets automatically on the last close
606 * of the descriptor. since we don't know if this is the last close we'll have
607 * to remove the set explicitly. */
609 io_event_change_kqueue(fd, i->what, EV_DELETE);
610 io_event_kqueue_commit_cache();
614 io_close_devpoll(fd);
619 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
625 return close(fd) == 0;
630 io_event_del(int fd, short what)
632 io_event *i = io_event_get(fd);
634 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
636 if (!i) return false;
640 #ifdef IO_USE_DEVPOLL
641 return io_event_change_devpoll(fd, i->what);
644 return io_event_change_poll(fd, i->what);
647 if (io_masterfd >= 0)
648 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
652 return io_event_change_kqueue(fd, what, EV_DISABLE);
655 if (what & IO_WANTWRITE)
656 FD_CLR(fd, &writers);
658 if (what & IO_WANTREAD)
659 FD_CLR(fd, &readers);
668 io_dispatch_select(struct timeval *tv)
670 fd_set readers_tmp = readers;
671 fd_set writers_tmp = writers;
675 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
681 for (i = 0; i <= select_maxfd; i++) {
683 if (FD_ISSET(i, &readers_tmp)) {
688 if (FD_ISSET(i, &writers_tmp)) {
689 what |= IO_WANTWRITE;
693 io_docallback(i, what);
703 #ifdef IO_USE_DEVPOLL
705 io_dispatch_devpoll(struct timeval *tv)
708 time_t sec = tv->tv_sec * 1000;
709 int i, total, ret, timeout = tv->tv_usec + sec;
711 struct pollfd p[100];
718 dvp.dp_timeout = timeout;
721 ret = ioctl(io_masterfd, DP_POLL, &dvp);
725 for (i=0; i < ret ; i++) {
727 if (p[i].revents & (POLLIN|POLLPRI))
730 if (p[i].revents & POLLOUT)
731 what |= IO_WANTWRITE;
733 if (p[i].revents && !what) {
734 /* other flag is set, probably POLLERR */
737 io_docallback(p[i].fd, what);
739 } while (ret == 100);
748 io_dispatch_poll(struct timeval *tv)
750 time_t sec = tv->tv_sec * 1000;
751 int i, ret, timeout = tv->tv_usec + sec;
754 struct pollfd *p = array_start(&pollfds);
759 ret = poll(p, poll_maxfd + 1, timeout);
764 for (i=0; i <= poll_maxfd; i++) {
766 if (p[i].revents & (POLLIN|POLLPRI))
769 if (p[i].revents & POLLOUT)
770 what |= IO_WANTWRITE;
772 if (p[i].revents && !what) {
773 /* other flag is set, probably POLLERR */
778 io_docallback(i, what);
791 io_dispatch_epoll(struct timeval *tv)
793 time_t sec = tv->tv_sec * 1000;
794 int i, total = 0, ret, timeout = tv->tv_usec + sec;
795 struct epoll_event epoll_ev[100];
802 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
807 for (i = 0; i < ret; i++) {
809 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
812 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
815 if (epoll_ev[i].events & EPOLLOUT)
816 type |= IO_WANTWRITE;
818 io_docallback(epoll_ev[i].data.fd, type);
822 } while (ret == 100);
831 io_dispatch_kqueue(struct timeval *tv)
833 int i, total = 0, ret;
834 struct kevent kev[100];
835 struct kevent *newevents;
838 ts.tv_sec = tv->tv_sec;
839 ts.tv_nsec = tv->tv_usec * 1000;
842 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
843 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
844 assert(newevents_len >= 0);
846 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
847 if (newevents && ret != -1)
848 array_trunc(&io_evcache);
854 for (i = 0; i < ret; i++) {
856 LogDebug("fd %d, kev.flags: %x", (int)kev[i].ident, kev[i].flags);
858 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
859 if (kev[i].flags & EV_ERROR)
860 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
861 (int)kev[i].ident, strerror((int)kev[i].data));
862 io_docallback((int)kev[i].ident, IO_ERROR);
866 switch (kev[i].filter) {
868 io_docallback((int)kev[i].ident, IO_WANTREAD);
871 io_docallback((int)kev[i].ident, IO_WANTWRITE);
874 LogDebug("Unknown kev.filter number %d for fd %d",
875 kev[i].filter, kev[i].ident);
878 io_docallback((int)kev[i].ident, IO_ERROR);
884 } while (ret == 100);
892 io_dispatch(struct timeval *tv)
895 if (io_masterfd >= 0)
896 return io_dispatch_epoll(tv);
899 return io_dispatch_select(tv);
902 return io_dispatch_kqueue(tv);
904 #ifdef IO_USE_DEVPOLL
905 return io_dispatch_devpoll(tv);
908 return io_dispatch_poll(tv);
913 /* call the callback function inside the struct matching fd */
915 io_docallback(int fd, short what)
919 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
921 i = io_event_get(fd);
923 if (i->callback) { /* callback might be NULL if a previous callback function
924 called io_close on this fd */
925 i->callback(fd, (what & IO_ERROR) ? i->what : what);
927 /* if error indicator is set, we return the event(s) that were registered */