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.18 2006/09/16 15:00:10 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
49 #define IO_USE_SELECT 1
50 # endif /* HAVE_POLL */
51 # endif /* HAVE_KQUEUE */
52 #endif /* HAVE_EPOLL_CREATE */
54 static bool library_initialized;
57 #include <sys/epoll.h>
59 static int io_masterfd;
60 static bool io_event_change_epoll(int fd, short what, const int action);
61 static int io_dispatch_epoll(struct timeval *tv);
65 #include <sys/types.h>
66 #include <sys/event.h>
67 static array io_evcache;
68 static int io_masterfd;
70 static int io_dispatch_kqueue(struct timeval *tv);
71 static bool io_event_change_kqueue(int, short, const int action);
77 static int poll_maxfd;
79 static bool io_event_change_poll(int fd, short what);
83 #include "defines.h" /* for conn.h */
84 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
85 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
86 #include "conf.h" /* for Conf_MaxConnections */
88 static fd_set readers;
89 static fd_set writers;
90 static int select_maxfd; /* the select() interface sucks badly */
91 static int io_dispatch_select(struct timeval *tv);
94 static array io_events;
96 static void io_docallback PARAMS((int fd, short what));
105 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
115 io_library_init_poll(unsigned int eventsize)
118 array_init(&pollfds);
120 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
122 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
125 p = array_start(&pollfds);
126 for (i = 0; i < eventsize; i++)
129 library_initialized = true;
138 io_library_init_select(unsigned int eventsize)
140 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
145 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
147 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
148 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
150 Conf_MaxConnections = FD_SETSIZE - 1;
152 #endif /* FD_SETSIZE */
153 library_initialized = true;
161 io_library_init_epoll(unsigned int eventsize)
164 int ecreate_hint = (int)eventsize;
165 if (ecreate_hint <= 0)
167 io_masterfd = epoll_create(ecreate_hint);
169 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
170 ecreate_hint, eventsize, io_masterfd);
171 ret = io_masterfd >= 0;
172 if (ret) library_initialized = true;
181 io_library_init_kqueue(unsigned int eventsize)
184 io_masterfd = kqueue();
187 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
188 eventsize, io_masterfd);
189 ret = io_masterfd >= 0;
190 if (ret) library_initialized = true;
197 io_library_init(unsigned int eventsize)
199 if (library_initialized)
204 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
206 if (eventsize >= FD_SETSIZE)
207 eventsize = FD_SETSIZE - 1;
208 #endif /* FD_SETSIZE */
209 #endif /* IO_USE_SELECT */
210 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
213 return io_library_init_epoll(eventsize);
216 return io_library_init_kqueue(eventsize);
219 return io_library_init_poll(eventsize);
222 return io_library_init_select(eventsize);
228 io_library_shutdown(void)
241 array_free(&io_evcache);
243 library_initialized = false;
248 io_event_setcb(int fd, void (*cbfunc) (int, short))
250 io_event *i = io_event_get(fd);
254 i->callback = cbfunc;
260 io_event_create(int fd, short what, void (*cbfunc) (int, short))
266 #if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
267 if (fd >= FD_SETSIZE) {
269 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
274 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
277 "array_alloc failed: could not allocate space for %d io_event structures",
282 i->callback = cbfunc;
285 ret = io_event_change_poll(fd, what);
288 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
291 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
294 ret = io_event_add(fd, what);
296 if (ret) i->what = what;
303 io_event_change_poll(int fd, short what)
308 if (what & IO_WANTREAD)
309 events = POLLIN | POLLPRI;
310 if (what & IO_WANTWRITE)
313 p = array_alloc(&pollfds, sizeof *p, fd);
326 io_event_change_epoll(int fd, short what, const int action)
328 struct epoll_event ev = { 0, {0} };
331 if (what & IO_WANTREAD)
332 ev.events = EPOLLIN | EPOLLPRI;
333 if (what & IO_WANTWRITE)
334 ev.events |= EPOLLOUT;
336 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
342 io_event_kqueue_commit_cache(void)
344 struct kevent *events;
346 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
348 if (!len) /* nothing to do */
354 array_free(&io_evcache);
358 events = array_start(&io_evcache);
360 assert(events != NULL);
362 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
364 array_trunc(&io_evcache);
370 io_event_change_kqueue(int fd, short what, const int action)
375 if (what & IO_WANTREAD) {
376 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
377 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
379 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
382 if (ret && (what & IO_WANTWRITE)) {
383 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
384 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
386 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
389 if (array_length(&io_evcache, sizeof kev) >= 100)
390 io_event_kqueue_commit_cache();
397 io_event_add(int fd, short what)
399 io_event *i = io_event_get(fd);
401 if (!i) return false;
402 if (i->what == what) return true;
404 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
408 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
412 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
415 return io_event_change_poll(fd, i->what);
418 if (fd > select_maxfd)
421 if (what & IO_WANTREAD)
422 FD_SET(fd, &readers);
423 if (what & IO_WANTWRITE)
424 FD_SET(fd, &writers);
432 io_setnonblock(int fd)
434 int flags = fcntl(fd, F_GETFL);
439 #define O_NONBLOCK O_NDELAY
443 return fcntl(fd, F_SETFL, flags) == 0;
449 io_close_poll(int fd)
452 p = array_get(&pollfds, sizeof *p, fd);
456 if (fd == poll_maxfd) {
457 while (poll_maxfd > 0) {
459 p = array_get(&pollfds, sizeof *p, poll_maxfd);
466 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
472 io_close_select(int fd)
475 FD_CLR(fd, &writers);
476 FD_CLR(fd, &readers);
478 i = io_event_get(fd);
481 if (fd == select_maxfd) {
482 while (select_maxfd>0) {
483 --select_maxfd; /* find largest fd */
484 i = io_event_get(select_maxfd);
485 if (i && i->callback) break;
490 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
499 i = io_event_get(fd);
501 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
502 io_event_kqueue_commit_cache();
504 /* both kqueue and epoll remove fd from all sets automatically on the last close
505 * of the descriptor. since we don't know if this is the last close we'll have
506 * to remove the set explicitly. */
508 io_event_change_kqueue(fd, i->what, EV_DELETE);
509 io_event_kqueue_commit_cache();
517 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
523 return close(fd) == 0;
528 io_event_del(int fd, short what)
530 io_event *i = io_event_get(fd);
532 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
534 if (!i) return false;
539 return io_event_change_poll(fd, i->what);
542 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
546 return io_event_change_kqueue(fd, what, EV_DISABLE);
549 if (what & IO_WANTWRITE)
550 FD_CLR(fd, &writers);
552 if (what & IO_WANTREAD)
553 FD_CLR(fd, &readers);
562 io_dispatch_select(struct timeval *tv)
564 fd_set readers_tmp = readers;
565 fd_set writers_tmp = writers;
569 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
575 for (i = 0; i <= select_maxfd; i++) {
577 if (FD_ISSET(i, &readers_tmp)) {
582 if (FD_ISSET(i, &writers_tmp)) {
583 what |= IO_WANTWRITE;
587 io_docallback(i, what);
599 io_dispatch_poll(struct timeval *tv)
601 time_t sec = tv->tv_sec * 1000;
602 int i, ret, timeout = tv->tv_usec + sec;
605 struct pollfd *p = array_start(&pollfds);
610 ret = poll(p, poll_maxfd + 1, timeout);
615 for (i=0; i <= poll_maxfd; i++) {
617 if (p[i].revents & (POLLIN|POLLPRI))
620 if (p[i].revents & POLLOUT)
621 what |= IO_WANTWRITE;
623 if (p[i].revents && !what) {
624 /* other flag is set, probably POLLERR */
629 io_docallback(i, what);
642 io_dispatch_epoll(struct timeval *tv)
644 time_t sec = tv->tv_sec * 1000;
645 int i, total = 0, ret, timeout = tv->tv_usec + sec;
646 struct epoll_event epoll_ev[100];
653 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
658 for (i = 0; i < ret; i++) {
660 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
663 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
666 if (epoll_ev[i].events & EPOLLOUT)
667 type |= IO_WANTWRITE;
669 io_docallback(epoll_ev[i].data.fd, type);
673 } while (ret == 100);
682 io_dispatch_kqueue(struct timeval *tv)
684 int i, total = 0, ret;
685 struct kevent kev[100];
686 struct kevent *newevents;
689 ts.tv_sec = tv->tv_sec;
690 ts.tv_nsec = tv->tv_usec * 1000;
693 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
694 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
695 assert(newevents_len >= 0);
696 if (newevents_len < 0)
700 assert(newevents != NULL);
702 ret = kevent(io_masterfd, newevents, newevents_len, kev,
704 if ((newevents_len>0) && ret != -1)
705 array_trunc(&io_evcache);
711 for (i = 0; i < ret; i++) {
712 if (kev[i].flags & EV_EOF) {
714 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
715 kev[i].filter, kev[i].ident);
717 io_docallback((int)kev[i].ident, IO_ERROR);
721 switch (kev[i].filter) {
723 io_docallback((int)kev[i].ident, IO_WANTREAD);
726 io_docallback((int)kev[i].ident, IO_WANTWRITE);
730 LogDebug("Unknown kev.filter number %d for fd %d",
731 kev[i].filter, kev[i].ident); /* Fall through */
734 io_docallback((int)kev[i].ident, IO_ERROR);
740 } while (ret == 100);
748 io_dispatch(struct timeval *tv)
751 return io_dispatch_select(tv);
754 return io_dispatch_kqueue(tv);
757 return io_dispatch_poll(tv);
760 return io_dispatch_epoll(tv);
765 /* call the callback function inside the struct matching fd */
767 io_docallback(int fd, short what)
771 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
773 i = io_event_get(fd);
775 if (i->callback) { /* callback might be NULL if a previous callback function
776 called io_close on this fd */
777 i->callback(fd, (what & IO_ERROR) ? i->what : what);
779 /* if error indicator is set, we return the event(s) that were registered */