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.21 2006/09/19 18:21:30 fw Exp $";
21 #include <sys/types.h>
29 /* Enables extra debug messages in event add/delete/callback code. */
30 /* #define DEBUG_IO */
33 void (*callback)(int, short);
37 #define INIT_IOEVENT { NULL, -1, 0, NULL }
40 #ifdef HAVE_EPOLL_CREATE
41 #define IO_USE_EPOLL 1
44 #define IO_USE_KQUEUE 1
46 # ifdef HAVE_SYS_DEVPOLL_H
47 #define IO_USE_DEVPOLL 1
52 #define IO_USE_SELECT 1
53 # endif /* HAVE_POLL */
54 # endif /* HAVE_SYS_DEVPOLL_H */
55 # endif /* HAVE_KQUEUE */
56 #endif /* HAVE_EPOLL_CREATE */
58 static bool library_initialized;
61 #include <sys/epoll.h>
63 static int io_masterfd;
64 static bool io_event_change_epoll(int fd, short what, const int action);
65 static int io_dispatch_epoll(struct timeval *tv);
69 #include <sys/types.h>
70 #include <sys/event.h>
71 static array io_evcache;
72 static int io_masterfd;
74 static int io_dispatch_kqueue(struct timeval *tv);
75 static bool io_event_change_kqueue(int, short, const int action);
82 static int poll_maxfd;
84 static bool io_event_change_poll(int fd, short what);
88 #include <sys/devpoll.h>
89 static int io_masterfd;
91 static bool io_event_change_devpoll(int fd, short what);
95 #include "defines.h" /* for conn.h */
96 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
97 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
98 #include "conf.h" /* for Conf_MaxConnections */
100 static fd_set readers;
101 static fd_set writers;
102 static int select_maxfd; /* the select() interface sucks badly */
103 static int io_dispatch_select(struct timeval *tv);
106 static array io_events;
108 static void io_docallback PARAMS((int fd, short what));
117 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
125 #ifdef IO_USE_DEVPOLL
127 io_library_init_devpoll(unsigned int eventsize)
129 io_masterfd = open("/dev/poll", O_RDWR);
130 if (io_masterfd >= 0)
131 library_initialized = true;
132 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
133 eventsize, io_masterfd);
140 io_library_init_poll(unsigned int eventsize)
143 array_init(&pollfds);
145 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
147 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
150 p = array_start(&pollfds);
151 for (i = 0; i < eventsize; i++)
154 library_initialized = true;
162 io_library_init_select(unsigned int eventsize)
164 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
169 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
171 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
172 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
174 Conf_MaxConnections = FD_SETSIZE - 1;
176 #endif /* FD_SETSIZE */
177 library_initialized = true;
184 io_library_init_epoll(unsigned int eventsize)
186 int ecreate_hint = (int)eventsize;
187 if (ecreate_hint <= 0)
189 io_masterfd = epoll_create(ecreate_hint);
191 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
192 ecreate_hint, eventsize, io_masterfd);
193 if (io_masterfd >= 0)
194 library_initialized = true;
201 io_library_init_kqueue(unsigned int eventsize)
203 io_masterfd = kqueue();
206 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
207 eventsize, io_masterfd);
208 if (io_masterfd >= 0)
209 library_initialized = true;
215 io_library_init(unsigned int eventsize)
217 if (library_initialized)
222 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
224 if (eventsize >= FD_SETSIZE)
225 eventsize = FD_SETSIZE - 1;
226 #endif /* FD_SETSIZE */
227 #endif /* IO_USE_SELECT */
228 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
231 io_library_init_epoll(eventsize);
234 io_library_init_kqueue(eventsize);
236 #ifdef IO_USE_DEVPOLL
237 io_library_init_devpoll(eventsize);
240 io_library_init_poll(eventsize);
243 io_library_init_select(eventsize);
245 return library_initialized;
250 io_library_shutdown(void)
263 array_free(&io_evcache);
265 library_initialized = false;
270 io_event_setcb(int fd, void (*cbfunc) (int, short))
272 io_event *i = io_event_get(fd);
276 i->callback = cbfunc;
282 io_event_create(int fd, short what, void (*cbfunc) (int, short))
288 #if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
289 if (fd >= FD_SETSIZE) {
291 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
296 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
299 "array_alloc failed: could not allocate space for %d io_event structures",
304 i->callback = cbfunc;
306 #ifdef IO_USE_DEVPOLL
307 ret = io_event_change_devpoll(fd, what);
310 ret = io_event_change_poll(fd, what);
313 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
316 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
319 ret = io_event_add(fd, what);
321 if (ret) i->what = what;
326 #ifdef IO_USE_DEVPOLL
328 io_event_change_devpoll(int fd, short what)
334 if (what & IO_WANTREAD)
335 p.events = POLLIN | POLLPRI;
336 if (what & IO_WANTWRITE)
340 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
348 io_event_change_poll(int fd, short what)
353 if (what & IO_WANTREAD)
354 events = POLLIN | POLLPRI;
355 if (what & IO_WANTWRITE)
358 p = array_alloc(&pollfds, sizeof *p, fd);
371 io_event_change_epoll(int fd, short what, const int action)
373 struct epoll_event ev = { 0, {0} };
376 if (what & IO_WANTREAD)
377 ev.events = EPOLLIN | EPOLLPRI;
378 if (what & IO_WANTWRITE)
379 ev.events |= EPOLLOUT;
381 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
387 io_event_kqueue_commit_cache(void)
389 struct kevent *events;
391 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
393 if (!len) /* nothing to do */
399 array_free(&io_evcache);
403 events = array_start(&io_evcache);
405 assert(events != NULL);
407 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
409 array_trunc(&io_evcache);
415 io_event_change_kqueue(int fd, short what, const int action)
420 if (what & IO_WANTREAD) {
421 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
422 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
424 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
427 if (ret && (what & IO_WANTWRITE)) {
428 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
429 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
431 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
434 if (array_length(&io_evcache, sizeof kev) >= 100)
435 io_event_kqueue_commit_cache();
442 io_event_add(int fd, short what)
444 io_event *i = io_event_get(fd);
446 if (!i) return false;
447 if (i->what == what) return true;
449 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
453 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
457 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
459 #ifdef IO_USE_DEVPOLL
460 return io_event_change_devpoll(fd, i->what);
463 return io_event_change_poll(fd, i->what);
466 if (fd > select_maxfd)
469 if (what & IO_WANTREAD)
470 FD_SET(fd, &readers);
471 if (what & IO_WANTWRITE)
472 FD_SET(fd, &writers);
480 io_setnonblock(int fd)
482 int flags = fcntl(fd, F_GETFL);
487 #define O_NONBLOCK O_NDELAY
491 return fcntl(fd, F_SETFL, flags) == 0;
495 #ifdef IO_USE_DEVPOLL
497 io_close_devpoll(int fd)
500 p.events = POLLREMOVE;
502 write(io_masterfd, &p, sizeof p);
505 static inline void io_close_devpoll(int UNUSED x) { /* NOTHING */ }
512 io_close_poll(int fd)
515 p = array_get(&pollfds, sizeof *p, fd);
519 if (fd == poll_maxfd) {
520 while (poll_maxfd > 0) {
522 p = array_get(&pollfds, sizeof *p, poll_maxfd);
529 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
535 io_close_select(int fd)
538 FD_CLR(fd, &writers);
539 FD_CLR(fd, &readers);
541 i = io_event_get(fd);
544 if (fd == select_maxfd) {
545 while (select_maxfd>0) {
546 --select_maxfd; /* find largest fd */
547 i = io_event_get(select_maxfd);
548 if (i && i->callback) break;
553 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
562 i = io_event_get(fd);
564 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
565 io_event_kqueue_commit_cache();
567 /* both kqueue and epoll remove fd from all sets automatically on the last close
568 * of the descriptor. since we don't know if this is the last close we'll have
569 * to remove the set explicitly. */
571 io_event_change_kqueue(fd, i->what, EV_DELETE);
572 io_event_kqueue_commit_cache();
576 io_close_devpoll(fd);
581 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
587 return close(fd) == 0;
592 io_event_del(int fd, short what)
594 io_event *i = io_event_get(fd);
596 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
598 if (!i) return false;
602 #ifdef IO_USE_DEVPOLL
603 return io_event_change_devpoll(fd, i->what);
606 return io_event_change_poll(fd, i->what);
609 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
613 return io_event_change_kqueue(fd, what, EV_DISABLE);
616 if (what & IO_WANTWRITE)
617 FD_CLR(fd, &writers);
619 if (what & IO_WANTREAD)
620 FD_CLR(fd, &readers);
629 io_dispatch_select(struct timeval *tv)
631 fd_set readers_tmp = readers;
632 fd_set writers_tmp = writers;
636 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
642 for (i = 0; i <= select_maxfd; i++) {
644 if (FD_ISSET(i, &readers_tmp)) {
649 if (FD_ISSET(i, &writers_tmp)) {
650 what |= IO_WANTWRITE;
654 io_docallback(i, what);
664 #ifdef IO_USE_DEVPOLL
666 io_dispatch_devpoll(struct timeval *tv)
669 time_t sec = tv->tv_sec * 1000;
670 int i, total, ret, timeout = tv->tv_usec + sec;
672 struct pollfd p[100];
679 dvp.dp_timeout = timeout;
682 ret = ioctl(io_masterfd, DP_POLL, &dvp);
686 for (i=0; i < ret ; i++) {
688 if (p[i].revents & (POLLIN|POLLPRI))
691 if (p[i].revents & POLLOUT)
692 what |= IO_WANTWRITE;
694 if (p[i].revents && !what) {
695 /* other flag is set, probably POLLERR */
698 io_docallback(p[i].fd, what);
700 } while (ret == 100);
709 io_dispatch_poll(struct timeval *tv)
711 time_t sec = tv->tv_sec * 1000;
712 int i, ret, timeout = tv->tv_usec + sec;
715 struct pollfd *p = array_start(&pollfds);
720 ret = poll(p, poll_maxfd + 1, timeout);
725 for (i=0; i <= poll_maxfd; 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 */
739 io_docallback(i, what);
752 io_dispatch_epoll(struct timeval *tv)
754 time_t sec = tv->tv_sec * 1000;
755 int i, total = 0, ret, timeout = tv->tv_usec + sec;
756 struct epoll_event epoll_ev[100];
763 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
768 for (i = 0; i < ret; i++) {
770 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
773 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
776 if (epoll_ev[i].events & EPOLLOUT)
777 type |= IO_WANTWRITE;
779 io_docallback(epoll_ev[i].data.fd, type);
783 } while (ret == 100);
792 io_dispatch_kqueue(struct timeval *tv)
794 int i, total = 0, ret;
795 struct kevent kev[100];
796 struct kevent *newevents;
799 ts.tv_sec = tv->tv_sec;
800 ts.tv_nsec = tv->tv_usec * 1000;
803 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
804 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
805 assert(newevents_len >= 0);
806 if (newevents_len < 0)
810 assert(newevents != NULL);
812 ret = kevent(io_masterfd, newevents, newevents_len, kev,
814 if ((newevents_len>0) && ret != -1)
815 array_trunc(&io_evcache);
821 for (i = 0; i < ret; i++) {
822 if (kev[i].flags & EV_EOF) {
824 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
825 kev[i].filter, kev[i].ident);
827 io_docallback((int)kev[i].ident, IO_ERROR);
831 switch (kev[i].filter) {
833 io_docallback((int)kev[i].ident, IO_WANTREAD);
836 io_docallback((int)kev[i].ident, IO_WANTWRITE);
840 LogDebug("Unknown kev.filter number %d for fd %d",
841 kev[i].filter, kev[i].ident); /* Fall through */
844 io_docallback((int)kev[i].ident, IO_ERROR);
850 } while (ret == 100);
858 io_dispatch(struct timeval *tv)
861 return io_dispatch_select(tv);
864 return io_dispatch_kqueue(tv);
866 #ifdef IO_USE_DEVPOLL
867 return io_dispatch_devpoll(tv);
870 return io_dispatch_poll(tv);
873 return io_dispatch_epoll(tv);
878 /* call the callback function inside the struct matching fd */
880 io_docallback(int fd, short what)
884 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
886 i = io_event_get(fd);
888 if (i->callback) { /* callback might be NULL if a previous callback function
889 called io_close on this fd */
890 i->callback(fd, (what & IO_ERROR) ? i->what : what);
892 /* if error indicator is set, we return the event(s) that were registered */