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.31 2008/04/03 20:56:44 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;
114 * this is the first argument for select(), i.e.
115 * the largest fd registered, plus one.
117 static int select_maxfd;
118 static int io_dispatch_select(struct timeval *tv);
121 #define io_masterfd -1
123 #endif /* IO_USE_SELECT */
125 static array io_events;
127 static void io_docallback PARAMS((int fd, short what));
130 static void io_debug(const char *s, int fd, int what)
132 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
135 static inline void io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b) {/*NOTHING*/}
145 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
153 #ifdef IO_USE_DEVPOLL
155 io_dispatch_devpoll(struct timeval *tv)
158 time_t sec = tv->tv_sec * 1000;
159 int i, total, ret, timeout = tv->tv_usec + sec;
161 struct pollfd p[100];
168 dvp.dp_timeout = timeout;
171 ret = ioctl(io_masterfd, DP_POLL, &dvp);
175 for (i=0; i < ret ; i++) {
177 if (p[i].revents & (POLLIN|POLLPRI))
180 if (p[i].revents & POLLOUT)
181 what |= IO_WANTWRITE;
183 if (p[i].revents && !what) {
184 /* other flag is set, probably POLLERR */
187 io_docallback(p[i].fd, what);
189 } while (ret == 100);
196 io_event_change_devpoll(int fd, short what)
202 if (what & IO_WANTREAD)
203 p.events = POLLIN | POLLPRI;
204 if (what & IO_WANTWRITE)
208 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
212 io_close_devpoll(int fd)
215 p.events = POLLREMOVE;
217 write(io_masterfd, &p, sizeof p);
221 io_library_init_devpoll(unsigned int eventsize)
223 io_masterfd = open("/dev/poll", O_RDWR);
224 if (io_masterfd >= 0)
225 library_initialized = true;
226 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
227 eventsize, io_masterfd);
230 static inline void io_close_devpoll(int UNUSED x) {/* NOTHING */}
231 static inline void io_library_init_devpoll(unsigned int UNUSED ev) {/*NOTHING*/}
237 io_dispatch_poll(struct timeval *tv)
239 time_t sec = tv->tv_sec * 1000;
240 int i, ret, timeout = tv->tv_usec + sec;
243 struct pollfd *p = array_start(&pollfds);
248 ret = poll(p, poll_maxfd + 1, timeout);
253 for (i=0; i <= poll_maxfd; i++) {
255 if (p[i].revents & (POLLIN|POLLPRI))
258 if (p[i].revents & POLLOUT)
259 what |= IO_WANTWRITE;
261 if (p[i].revents && !what) {
262 /* other flag is set, probably POLLERR */
267 io_docallback(i, what);
277 io_event_change_poll(int fd, short what)
282 if (what & IO_WANTREAD)
283 events = POLLIN | POLLPRI;
284 if (what & IO_WANTWRITE)
287 p = array_alloc(&pollfds, sizeof *p, fd);
298 io_close_poll(int fd)
301 p = array_get(&pollfds, sizeof *p, fd);
305 if (fd == poll_maxfd) {
306 while (poll_maxfd > 0) {
308 p = array_get(&pollfds, sizeof *p, poll_maxfd);
316 io_library_init_poll(unsigned int eventsize)
319 array_init(&pollfds);
321 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
323 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
326 p = array_start(&pollfds);
327 for (i = 0; i < eventsize; i++)
330 library_initialized = true;
334 static inline void io_close_poll(int UNUSED x) {/* NOTHING */}
335 static inline void io_library_init_poll(unsigned int UNUSED ev) {/*NOTHING*/}
341 io_dispatch_select(struct timeval *tv)
343 fd_set readers_tmp = readers;
344 fd_set writers_tmp = writers;
348 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
354 for (i = 0; i <= select_maxfd; i++) {
356 if (FD_ISSET(i, &readers_tmp)) {
361 if (FD_ISSET(i, &writers_tmp)) {
362 what |= IO_WANTWRITE;
366 io_docallback(i, what);
375 io_library_init_select(unsigned int eventsize)
377 if (library_initialized)
379 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
384 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
386 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
387 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
389 Conf_MaxConnections = FD_SETSIZE - 1;
393 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
394 #endif /* FD_SETSIZE */
395 library_initialized = true;
399 io_close_select(int fd)
403 if (io_masterfd >= 0) /* Are we using epoll()? */
406 FD_CLR(fd, &writers);
407 FD_CLR(fd, &readers);
409 i = io_event_get(fd);
412 if (fd == select_maxfd) {
413 while (select_maxfd>0) {
414 --select_maxfd; /* find largest fd */
415 i = io_event_get(select_maxfd);
416 if (i && i->callback) break;
421 static inline void io_library_init_select(int UNUSED x) {/* NOTHING */}
422 static inline void io_close_select(int UNUSED x) {/* NOTHING */}
428 io_event_change_epoll(int fd, short what, const int action)
430 struct epoll_event ev = { 0, {0} };
433 if (what & IO_WANTREAD)
434 ev.events = EPOLLIN | EPOLLPRI;
435 if (what & IO_WANTWRITE)
436 ev.events |= EPOLLOUT;
438 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
442 io_dispatch_epoll(struct timeval *tv)
444 time_t sec = tv->tv_sec * 1000;
445 int i, total = 0, ret, timeout = tv->tv_usec + sec;
446 struct epoll_event epoll_ev[100];
453 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
458 for (i = 0; i < ret; i++) {
460 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
463 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
466 if (epoll_ev[i].events & EPOLLOUT)
467 type |= IO_WANTWRITE;
469 io_docallback(epoll_ev[i].data.fd, type);
473 } while (ret == 100);
479 io_library_init_epoll(unsigned int eventsize)
481 int ecreate_hint = (int)eventsize;
482 if (ecreate_hint <= 0)
484 io_masterfd = epoll_create(ecreate_hint);
485 if (io_masterfd >= 0) {
486 library_initialized = true;
488 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
489 ecreate_hint, eventsize, io_masterfd);
493 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
497 static inline void io_library_init_epoll(unsigned int UNUSED ev) {/* NOTHING */}
498 #endif /* IO_USE_EPOLL */
503 io_event_kqueue_commit_cache(void)
505 struct kevent *events;
507 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
509 if (!len) /* nothing to do */
515 array_free(&io_evcache);
519 events = array_start(&io_evcache);
521 assert(events != NULL);
523 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
525 array_trunc(&io_evcache);
530 io_event_change_kqueue(int fd, short what, const int action)
535 if (what & IO_WANTREAD) {
536 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
537 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
539 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
542 if (ret && (what & IO_WANTWRITE)) {
543 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
544 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
546 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
549 if (array_length(&io_evcache, sizeof kev) >= 100)
550 io_event_kqueue_commit_cache();
555 io_dispatch_kqueue(struct timeval *tv)
557 int i, total = 0, ret;
558 struct kevent kev[100];
559 struct kevent *newevents;
562 ts.tv_sec = tv->tv_sec;
563 ts.tv_nsec = tv->tv_usec * 1000;
566 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
567 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
568 assert(newevents_len >= 0);
570 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
571 if (newevents && ret != -1)
572 array_trunc(&io_evcache);
578 for (i = 0; i < ret; i++) {
579 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
580 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
581 if (kev[i].flags & EV_ERROR)
582 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
583 (int)kev[i].ident, strerror((int)kev[i].data));
584 io_docallback((int)kev[i].ident, IO_ERROR);
588 switch (kev[i].filter) {
590 io_docallback((int)kev[i].ident, IO_WANTREAD);
593 io_docallback((int)kev[i].ident, IO_WANTWRITE);
596 LogDebug("Unknown kev.filter number %d for fd %d",
597 kev[i].filter, kev[i].ident);
600 io_docallback((int)kev[i].ident, IO_ERROR);
606 } while (ret == 100);
612 io_library_init_kqueue(unsigned int eventsize)
614 io_masterfd = kqueue();
617 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
618 eventsize, io_masterfd);
619 if (io_masterfd >= 0)
620 library_initialized = true;
623 static inline void io_library_init_kqueue(unsigned int UNUSED ev) {/* NOTHING */}
628 io_library_init(unsigned int eventsize)
630 if (library_initialized)
633 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
636 io_library_init_epoll(eventsize);
637 io_library_init_kqueue(eventsize);
638 io_library_init_devpoll(eventsize);
639 io_library_init_poll(eventsize);
640 io_library_init_select(eventsize);
642 return library_initialized;
647 io_library_shutdown(void)
653 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
654 if (io_masterfd >= 0)
659 array_free(&io_evcache);
661 library_initialized = false;
666 io_event_setcb(int fd, void (*cbfunc) (int, short))
668 io_event *i = io_event_get(fd);
672 i->callback = cbfunc;
678 backend_create_ev(int fd, short what)
681 #ifdef IO_USE_DEVPOLL
682 ret = io_event_change_devpoll(fd, what);
685 ret = io_event_change_poll(fd, what);
688 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
691 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
695 ret = io_event_add(fd, what);
702 io_event_create(int fd, short what, void (*cbfunc) (int, short))
708 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
709 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
711 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
716 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
719 "array_alloc failed: could not allocate space for %d io_event structures",
724 i->callback = cbfunc;
726 ret = backend_create_ev(fd, what);
734 io_event_add(int fd, short what)
736 io_event *i = io_event_get(fd);
738 if (!i) return false;
740 if ((i->what & what) == what) /* event type is already registered */
743 io_debug("io_event_add: fd, what", fd, what);
747 if (io_masterfd >= 0)
748 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
751 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
753 #ifdef IO_USE_DEVPOLL
754 return io_event_change_devpoll(fd, i->what);
757 return io_event_change_poll(fd, i->what);
760 if (fd > select_maxfd)
763 if (what & IO_WANTREAD)
764 FD_SET(fd, &readers);
765 if (what & IO_WANTWRITE)
766 FD_SET(fd, &writers);
775 io_setnonblock(int fd)
777 int flags = fcntl(fd, F_GETFL);
781 #define O_NONBLOCK O_NDELAY
785 return fcntl(fd, F_SETFL, flags) == 0;
794 i = io_event_get(fd);
796 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
797 io_event_kqueue_commit_cache();
799 /* both kqueue and epoll remove fd from all sets automatically on the last close
800 * of the descriptor. since we don't know if this is the last close we'll have
801 * to remove the set explicitly. */
803 io_event_change_kqueue(fd, i->what, EV_DELETE);
804 io_event_kqueue_commit_cache();
807 io_close_devpoll(fd);
811 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
817 return close(fd) == 0;
822 io_event_del(int fd, short what)
824 io_event *i = io_event_get(fd);
826 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
827 if (!i) return false;
829 if (!(i->what & what)) /* event is already disabled */
833 #ifdef IO_USE_DEVPOLL
834 return io_event_change_devpoll(fd, i->what);
837 return io_event_change_poll(fd, i->what);
840 if (io_masterfd >= 0)
841 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
844 return io_event_change_kqueue(fd, what, EV_DISABLE);
847 if (what & IO_WANTWRITE)
848 FD_CLR(fd, &writers);
850 if (what & IO_WANTREAD)
851 FD_CLR(fd, &readers);
859 io_dispatch(struct timeval *tv)
862 if (io_masterfd >= 0)
863 return io_dispatch_epoll(tv);
866 return io_dispatch_select(tv);
869 return io_dispatch_kqueue(tv);
871 #ifdef IO_USE_DEVPOLL
872 return io_dispatch_devpoll(tv);
875 return io_dispatch_poll(tv);
881 /* call the callback function inside the struct matching fd */
883 io_docallback(int fd, short what)
885 io_event *i = io_event_get(fd);
887 io_debug("io_docallback; fd, what", fd, what);
889 if (i->callback) { /* callback might be NULL if a previous callback function
890 called io_close on this fd */
891 i->callback(fd, (what & IO_ERROR) ? i->what : what);
893 /* if error indicator is set, we return the event(s) that were registered */