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.13 2006/05/09 17:02:40 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);
38 #define INIT_IOEVENT { NULL, -1, 0, NULL }
41 #ifdef HAVE_EPOLL_CREATE
42 #define IO_USE_EPOLL 1
45 #define IO_USE_KQUEUE 1
47 #define IO_USE_SELECT 1
51 static bool library_initialized;
54 #include <sys/epoll.h>
56 static int io_masterfd;
57 static bool io_event_change_epoll(int fd, short what, const int action);
58 static int io_dispatch_epoll(struct timeval *tv);
62 #include <sys/types.h>
63 #include <sys/event.h>
64 static array io_evcache;
65 static int io_masterfd;
67 static int io_dispatch_kqueue(struct timeval *tv);
68 static bool io_event_change_kqueue(int, short, const int action);
72 #include "defines.h" /* for conn.h */
73 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
74 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
75 #include "conf.h" /* for Conf_MaxConnections */
77 static fd_set readers;
78 static fd_set writers;
79 static int select_maxfd; /* the select() interface sucks badly */
80 static int io_dispatch_select(struct timeval *tv);
83 static array io_events;
85 static void io_docallback PARAMS((int fd, short what));
92 i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
100 io_library_init(unsigned int eventsize)
104 int ecreate_hint = (int)eventsize;
105 if (ecreate_hint <= 0)
108 if (library_initialized)
113 if (eventsize >= FD_SETSIZE)
114 eventsize = FD_SETSIZE - 1;
117 if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
121 io_masterfd = epoll_create(ecreate_hint);
123 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
124 ecreate_hint, eventsize, io_masterfd);
125 ret = io_masterfd >= 0;
126 if (ret) library_initialized = true;
131 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
136 if (Conf_MaxConnections >= FD_SETSIZE) {
138 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
139 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
141 Conf_MaxConnections = FD_SETSIZE - 1;
145 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
146 #endif /* FD_SETSIZE */
147 library_initialized = true;
151 io_masterfd = kqueue();
154 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
155 eventsize, io_masterfd);
156 ret = io_masterfd >= 0;
157 if (ret) library_initialized = true;
165 io_library_shutdown(void)
171 close(io_masterfd); /* kqueue, epoll */
175 array_free(&io_evcache);
177 library_initialized = false;
182 io_event_setcb(int fd, void (*cbfunc) (int, short))
184 io_event *i = io_event_get(fd);
188 i->callback = cbfunc;
194 io_event_create(int fd, short what, void (*cbfunc) (int, short))
203 if (fd >= FD_SETSIZE) {
205 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
209 #endif /* FD_SETSIZE */
210 #endif /* IO_USE_SELECT */
212 i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
215 "array_alloc failed: could not allocate space for %d io_event structures",
221 i->callback = cbfunc;
224 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
227 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
230 ret = io_event_add(fd, what);
232 if (ret) i->what = what;
239 io_event_change_epoll(int fd, short what, const int action)
241 struct epoll_event ev = { 0, {0} };
244 if (what & IO_WANTREAD)
245 ev.events = EPOLLIN | EPOLLPRI;
246 if (what & IO_WANTWRITE)
247 ev.events |= EPOLLOUT;
249 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
255 io_event_kqueue_commit_cache(void)
257 struct kevent *events;
259 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
261 if (!len) /* nothing to do */
267 array_free(&io_evcache);
271 events = array_start(&io_evcache);
275 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
277 array_trunc(&io_evcache);
283 io_event_change_kqueue(int fd, short what, const int action)
288 if (what & IO_WANTREAD) {
289 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
290 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
292 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
295 if (ret && (what & IO_WANTWRITE)) {
296 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
297 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
299 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
302 if (array_length(&io_evcache, sizeof kev) >= 100)
303 io_event_kqueue_commit_cache();
310 io_event_add(int fd, short what)
312 io_event *i = io_event_get(fd);
316 if (!i) return false;
317 if (i->what == what) return true;
319 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
323 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
327 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
331 if (fd > select_maxfd)
334 if (what & IO_WANTREAD)
335 FD_SET(fd, &readers);
336 if (what & IO_WANTWRITE)
337 FD_SET(fd, &writers);
345 io_setnonblock(int fd)
347 int flags = fcntl(fd, F_GETFL);
352 #define O_NONBLOCK O_NDELAY
356 return fcntl(fd, F_SETFL, flags) == 0;
365 FD_CLR(fd, &writers);
366 FD_CLR(fd, &readers);
368 if (fd == select_maxfd) {
369 while (select_maxfd>0) {
370 --select_maxfd; /* find largest fd */
371 i = io_event_get(select_maxfd);
372 if (i && (i->fd >= 0)) break;
376 i = io_event_get(fd);
378 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
379 io_event_kqueue_commit_cache();
381 /* both kqueue and epoll remove fd from all sets automatically on the last close
382 * of the descriptor. since we don't know if this is the last close we'll have
383 * to remove the set explicitly. */
385 io_event_change_kqueue(fd, i->what, EV_DELETE);
386 io_event_kqueue_commit_cache();
390 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
393 memset(i, 0, sizeof(io_event));
396 return close(fd) == 0;
401 io_event_del(int fd, short what)
403 io_event *i = io_event_get(fd);
405 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
408 if (!i) return false;
413 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
417 return io_event_change_kqueue(fd, what, EV_DISABLE);
420 if (what & IO_WANTWRITE)
421 FD_CLR(i->fd, &writers);
423 if (what & IO_WANTREAD)
424 FD_CLR(i->fd, &readers);
433 io_dispatch_select(struct timeval *tv)
435 fd_set readers_tmp = readers;
436 fd_set writers_tmp = writers;
440 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
446 for (i = 0; i <= select_maxfd; i++) {
448 if (FD_ISSET(i, &readers_tmp)) {
453 if (FD_ISSET(i, &writers_tmp)) {
454 what |= IO_WANTWRITE;
458 io_docallback(i, what);
470 io_dispatch_epoll(struct timeval *tv)
472 time_t sec = tv->tv_sec * 1000;
473 int i, total = 0, ret, timeout = tv->tv_usec + sec;
474 struct epoll_event epoll_ev[100];
481 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
486 for (i = 0; i < ret; i++) {
488 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
491 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
494 if (epoll_ev[i].events & EPOLLOUT)
495 type |= IO_WANTWRITE;
497 io_docallback(epoll_ev[i].data.fd, type);
501 } while (ret == 100);
510 io_dispatch_kqueue(struct timeval *tv)
512 int i, total = 0, ret;
513 struct kevent kev[100];
514 struct kevent *newevents;
517 ts.tv_sec = tv->tv_sec;
518 ts.tv_nsec = tv->tv_usec * 1000;
521 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
522 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
523 assert(newevents_len >= 0);
524 if (newevents_len < 0)
530 ret = kevent(io_masterfd, newevents, newevents_len, kev,
532 if ((newevents_len>0) && ret != -1)
533 array_trunc(&io_evcache);
539 for (i = 0; i < ret; i++) {
540 switch (kev[i].filter) {
542 io_docallback(kev[i].ident, IO_WANTREAD);
545 io_docallback(kev[i].ident, IO_WANTWRITE);
549 LogDebug("Unknown kev.filter number %d for fd %d",
550 kev[i].filter, kev[i].ident); /* Fall through */
553 io_docallback(kev[i].ident, IO_ERROR);
557 if (kev[i].flags & EV_EOF) {
559 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
560 kev[i].filter, kev[i].ident); /* Fall through */
563 io_docallback(kev[i].ident, IO_ERROR);
569 } while (ret == 100);
577 io_dispatch(struct timeval *tv)
580 return io_dispatch_select(tv);
583 return io_dispatch_kqueue(tv);
586 return io_dispatch_epoll(tv);
591 /* call the callback function inside the struct matching fd */
593 io_docallback(int fd, short what)
597 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
599 i = io_event_get(fd);
602 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
603 i->callback(fd, (what & IO_ERROR) ? i->what : what);
604 /* if error indicator is set, we return the event(s) the app asked for */