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.17 2006/09/16 14:49:26 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 #define IO_USE_SELECT 1
47 # endif /* HAVE_KQUEUE */
48 #endif /* HAVE_EPOLL_CREATE */
50 static bool library_initialized;
53 #include <sys/epoll.h>
55 static int io_masterfd;
56 static bool io_event_change_epoll(int fd, short what, const int action);
57 static int io_dispatch_epoll(struct timeval *tv);
61 #include <sys/types.h>
62 #include <sys/event.h>
63 static array io_evcache;
64 static int io_masterfd;
66 static int io_dispatch_kqueue(struct timeval *tv);
67 static bool io_event_change_kqueue(int, short, const int action);
71 #include "defines.h" /* for conn.h */
72 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
73 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
74 #include "conf.h" /* for Conf_MaxConnections */
76 static fd_set readers;
77 static fd_set writers;
78 static int select_maxfd; /* the select() interface sucks badly */
79 static int io_dispatch_select(struct timeval *tv);
82 static array io_events;
84 static void io_docallback PARAMS((int fd, short what));
93 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
103 io_library_init_select(unsigned int eventsize)
105 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
110 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
112 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
113 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
115 Conf_MaxConnections = FD_SETSIZE - 1;
117 #endif /* FD_SETSIZE */
118 library_initialized = true;
126 io_library_init_epoll(unsigned int eventsize)
129 int ecreate_hint = (int)eventsize;
130 if (ecreate_hint <= 0)
132 io_masterfd = epoll_create(ecreate_hint);
134 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
135 ecreate_hint, eventsize, io_masterfd);
136 ret = io_masterfd >= 0;
137 if (ret) library_initialized = true;
146 io_library_init_kqueue(unsigned int eventsize)
149 io_masterfd = kqueue();
152 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
153 eventsize, io_masterfd);
154 ret = io_masterfd >= 0;
155 if (ret) library_initialized = true;
162 io_library_init(unsigned int eventsize)
164 if (library_initialized)
169 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
171 if (eventsize >= FD_SETSIZE)
172 eventsize = FD_SETSIZE - 1;
173 #endif /* FD_SETSIZE */
174 #endif /* IO_USE_SELECT */
175 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
178 return io_library_init_epoll(eventsize);
181 return io_library_init_kqueue(eventsize);
184 return io_library_init_select(eventsize);
190 io_library_shutdown(void)
203 array_free(&io_evcache);
205 library_initialized = false;
210 io_event_setcb(int fd, void (*cbfunc) (int, short))
212 io_event *i = io_event_get(fd);
216 i->callback = cbfunc;
222 io_event_create(int fd, short what, void (*cbfunc) (int, short))
228 #if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
229 if (fd >= FD_SETSIZE) {
231 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
236 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
239 "array_alloc failed: could not allocate space for %d io_event structures",
244 i->callback = cbfunc;
247 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
250 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
253 ret = io_event_add(fd, what);
255 if (ret) i->what = what;
262 io_event_change_epoll(int fd, short what, const int action)
264 struct epoll_event ev = { 0, {0} };
267 if (what & IO_WANTREAD)
268 ev.events = EPOLLIN | EPOLLPRI;
269 if (what & IO_WANTWRITE)
270 ev.events |= EPOLLOUT;
272 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
278 io_event_kqueue_commit_cache(void)
280 struct kevent *events;
282 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
284 if (!len) /* nothing to do */
290 array_free(&io_evcache);
294 events = array_start(&io_evcache);
296 assert(events != NULL);
298 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
300 array_trunc(&io_evcache);
306 io_event_change_kqueue(int fd, short what, const int action)
311 if (what & IO_WANTREAD) {
312 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
313 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
315 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
318 if (ret && (what & IO_WANTWRITE)) {
319 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
320 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
322 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
325 if (array_length(&io_evcache, sizeof kev) >= 100)
326 io_event_kqueue_commit_cache();
333 io_event_add(int fd, short what)
335 io_event *i = io_event_get(fd);
337 if (!i) return false;
338 if (i->what == what) return true;
340 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
344 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
348 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
351 if (fd > select_maxfd)
354 if (what & IO_WANTREAD)
355 FD_SET(fd, &readers);
356 if (what & IO_WANTWRITE)
357 FD_SET(fd, &writers);
365 io_setnonblock(int fd)
367 int flags = fcntl(fd, F_GETFL);
372 #define O_NONBLOCK O_NDELAY
376 return fcntl(fd, F_SETFL, flags) == 0;
382 io_close_select(int fd)
385 FD_CLR(fd, &writers);
386 FD_CLR(fd, &readers);
388 i = io_event_get(fd);
391 if (fd == select_maxfd) {
392 while (select_maxfd>0) {
393 --select_maxfd; /* find largest fd */
394 i = io_event_get(select_maxfd);
395 if (i && i->callback) break;
400 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
409 i = io_event_get(fd);
411 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
412 io_event_kqueue_commit_cache();
414 /* both kqueue and epoll remove fd from all sets automatically on the last close
415 * of the descriptor. since we don't know if this is the last close we'll have
416 * to remove the set explicitly. */
418 io_event_change_kqueue(fd, i->what, EV_DELETE);
419 io_event_kqueue_commit_cache();
426 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
432 return close(fd) == 0;
437 io_event_del(int fd, short what)
439 io_event *i = io_event_get(fd);
441 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
443 if (!i) return false;
448 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
452 return io_event_change_kqueue(fd, what, EV_DISABLE);
455 if (what & IO_WANTWRITE)
456 FD_CLR(fd, &writers);
458 if (what & IO_WANTREAD)
459 FD_CLR(fd, &readers);
468 io_dispatch_select(struct timeval *tv)
470 fd_set readers_tmp = readers;
471 fd_set writers_tmp = writers;
475 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
481 for (i = 0; i <= select_maxfd; i++) {
483 if (FD_ISSET(i, &readers_tmp)) {
488 if (FD_ISSET(i, &writers_tmp)) {
489 what |= IO_WANTWRITE;
493 io_docallback(i, what);
505 io_dispatch_epoll(struct timeval *tv)
507 time_t sec = tv->tv_sec * 1000;
508 int i, total = 0, ret, timeout = tv->tv_usec + sec;
509 struct epoll_event epoll_ev[100];
516 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
521 for (i = 0; i < ret; i++) {
523 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
526 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
529 if (epoll_ev[i].events & EPOLLOUT)
530 type |= IO_WANTWRITE;
532 io_docallback(epoll_ev[i].data.fd, type);
536 } while (ret == 100);
545 io_dispatch_kqueue(struct timeval *tv)
547 int i, total = 0, ret;
548 struct kevent kev[100];
549 struct kevent *newevents;
552 ts.tv_sec = tv->tv_sec;
553 ts.tv_nsec = tv->tv_usec * 1000;
556 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
557 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
558 assert(newevents_len >= 0);
559 if (newevents_len < 0)
563 assert(newevents != NULL);
565 ret = kevent(io_masterfd, newevents, newevents_len, kev,
567 if ((newevents_len>0) && ret != -1)
568 array_trunc(&io_evcache);
574 for (i = 0; i < ret; i++) {
575 if (kev[i].flags & EV_EOF) {
577 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
578 kev[i].filter, kev[i].ident);
580 io_docallback((int)kev[i].ident, IO_ERROR);
584 switch (kev[i].filter) {
586 io_docallback((int)kev[i].ident, IO_WANTREAD);
589 io_docallback((int)kev[i].ident, IO_WANTWRITE);
593 LogDebug("Unknown kev.filter number %d for fd %d",
594 kev[i].filter, kev[i].ident); /* Fall through */
597 io_docallback((int)kev[i].ident, IO_ERROR);
603 } while (ret == 100);
611 io_dispatch(struct timeval *tv)
614 return io_dispatch_select(tv);
617 return io_dispatch_kqueue(tv);
620 return io_dispatch_epoll(tv);
625 /* call the callback function inside the struct matching fd */
627 io_docallback(int fd, short what)
631 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
633 i = io_event_get(fd);
635 if (i->callback) { /* callback might be NULL if a previous callback function
636 called io_close on this fd */
637 i->callback(fd, (what & IO_ERROR) ? i->what : what);
639 /* if error indicator is set, we return the event(s) that were registered */