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.12 2006/05/07 10:54:42 fw Exp $";
21 #include <sys/types.h>
31 void (*callback)(int, short);
36 #define INIT_IOEVENT { NULL, -1, 0, NULL }
39 #ifdef HAVE_EPOLL_CREATE
40 #define IO_USE_EPOLL 1
43 #define IO_USE_KQUEUE 1
45 #define IO_USE_SELECT 1
49 static bool library_initialized;
52 #include <sys/epoll.h>
54 static int io_masterfd;
55 static bool io_event_change_epoll(int fd, short what, const int action);
56 static int io_dispatch_epoll(struct timeval *tv);
60 #include <sys/types.h>
61 #include <sys/event.h>
62 static array io_evcache;
63 static int io_masterfd;
65 static int io_dispatch_kqueue(struct timeval *tv);
66 static bool io_event_change_kqueue(int, short, const int action);
70 #include "defines.h" /* for conn.h */
71 #include "conn.h" /* for CONN_IDX (needed by resolve.h) */
72 #include "resolve.h" /* for RES_STAT (needed by conf.h) */
73 #include "conf.h" /* for Conf_MaxConnections */
75 static fd_set readers;
76 static fd_set writers;
77 static int select_maxfd; /* the select() interface sucks badly */
78 static int io_dispatch_select(struct timeval *tv);
81 static array io_events;
83 static void io_docallback PARAMS((int fd, short what));
90 i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
98 io_library_init(unsigned int eventsize)
102 int ecreate_hint = (int)eventsize;
103 if (ecreate_hint <= 0)
106 if (library_initialized)
111 if (eventsize >= FD_SETSIZE)
112 eventsize = FD_SETSIZE - 1;
115 if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
119 io_masterfd = epoll_create(ecreate_hint);
121 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
122 ecreate_hint, eventsize, io_masterfd);
123 ret = io_masterfd >= 0;
124 if (ret) library_initialized = true;
129 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
134 if (Conf_MaxConnections >= FD_SETSIZE) {
136 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
137 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
139 Conf_MaxConnections = FD_SETSIZE - 1;
143 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
144 #endif /* FD_SETSIZE */
145 library_initialized = true;
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;
163 io_library_shutdown(void)
169 close(io_masterfd); /* kqueue, epoll */
173 array_free(&io_evcache);
175 library_initialized = false;
180 io_event_setcb(int fd, void (*cbfunc) (int, short))
182 io_event *i = io_event_get(fd);
186 i->callback = cbfunc;
192 io_event_create(int fd, short what, void (*cbfunc) (int, short))
201 if (fd >= FD_SETSIZE) {
203 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
207 #endif /* FD_SETSIZE */
208 #endif /* IO_USE_SELECT */
210 i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
213 "array_alloc failed: could not allocate space for %d io_event structures",
219 i->callback = cbfunc;
222 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
225 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
228 ret = io_event_add(fd, what);
230 if (ret) i->what = what;
237 io_event_change_epoll(int fd, short what, const int action)
239 struct epoll_event ev = { 0, {0} };
242 if (what & IO_WANTREAD)
243 ev.events = EPOLLIN | EPOLLPRI;
244 if (what & IO_WANTWRITE)
245 ev.events |= EPOLLOUT;
247 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
253 io_event_kqueue_commit_cache(void)
255 struct kevent *events;
257 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
259 if (!len) /* nothing to do */
265 array_free(&io_evcache);
269 events = array_start(&io_evcache);
273 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
275 array_trunc(&io_evcache);
281 io_event_change_kqueue(int fd, short what, const int action)
286 if (what & IO_WANTREAD) {
287 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
288 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
290 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
293 if (ret && (what & IO_WANTWRITE)) {
294 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
295 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
297 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
300 if (array_length(&io_evcache, sizeof kev) >= 100)
301 io_event_kqueue_commit_cache();
308 io_event_add(int fd, short what)
310 io_event *i = io_event_get(fd);
314 if (!i) return false;
315 if (i->what == what) return true;
317 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
321 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
325 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
329 if (fd > select_maxfd)
332 if (what & IO_WANTREAD)
333 FD_SET(fd, &readers);
334 if (what & IO_WANTWRITE)
335 FD_SET(fd, &writers);
343 io_setnonblock(int fd)
345 int flags = fcntl(fd, F_GETFL);
350 #define O_NONBLOCK O_NDELAY
354 return fcntl(fd, F_SETFL, flags) == 0;
363 FD_CLR(fd, &writers);
364 FD_CLR(fd, &readers);
366 if (fd == select_maxfd) {
367 while (select_maxfd>0) {
368 --select_maxfd; /* find largest fd */
369 i = io_event_get(select_maxfd);
370 if (i && (i->fd >= 0)) break;
374 i = io_event_get(fd);
376 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
377 io_event_kqueue_commit_cache();
379 /* both kqueue and epoll remove fd from all sets automatically on the last close
380 * of the descriptor. since we don't know if this is the last close we'll have
381 * to remove the set explicitly. */
383 io_event_change_kqueue(fd, i->what, EV_DELETE);
384 io_event_kqueue_commit_cache();
388 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
391 memset(i, 0, sizeof(io_event));
394 return close(fd) == 0;
399 io_event_del(int fd, short what)
401 io_event *i = io_event_get(fd);
403 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
406 if (!i) return false;
411 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
415 return io_event_change_kqueue(fd, what, EV_DISABLE);
418 if (what & IO_WANTWRITE)
419 FD_CLR(i->fd, &writers);
421 if (what & IO_WANTREAD)
422 FD_CLR(i->fd, &readers);
431 io_dispatch_select(struct timeval *tv)
433 fd_set readers_tmp = readers;
434 fd_set writers_tmp = writers;
438 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
444 for (i = 0; i <= select_maxfd; i++) {
446 if (FD_ISSET(i, &readers_tmp)) {
451 if (FD_ISSET(i, &writers_tmp)) {
452 what |= IO_WANTWRITE;
456 io_docallback(i, what);
468 io_dispatch_epoll(struct timeval *tv)
470 time_t sec = tv->tv_sec * 1000;
471 int i, total = 0, ret, timeout = tv->tv_usec + sec;
472 struct epoll_event epoll_ev[100];
479 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
484 for (i = 0; i < ret; i++) {
486 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
489 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
492 if (epoll_ev[i].events & EPOLLOUT)
493 type |= IO_WANTWRITE;
495 io_docallback(epoll_ev[i].data.fd, type);
499 } while (ret == 100);
508 io_dispatch_kqueue(struct timeval *tv)
510 int i, total = 0, ret;
511 struct kevent kev[100];
512 struct kevent *newevents;
515 ts.tv_sec = tv->tv_sec;
516 ts.tv_nsec = tv->tv_usec * 1000;
519 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
520 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
521 assert(newevents_len >= 0);
522 if (newevents_len < 0)
528 ret = kevent(io_masterfd, newevents, newevents_len, kev,
530 if ((newevents_len>0) && ret != -1)
531 array_trunc(&io_evcache);
537 for (i = 0; i < ret; i++) {
538 switch (kev[i].filter) {
540 io_docallback(kev[i].ident, IO_WANTREAD);
543 io_docallback(kev[i].ident, IO_WANTWRITE);
547 LogDebug("Unknown kev.filter number %d for fd %d",
548 kev[i].filter, kev[i].ident); /* Fall through */
551 io_docallback(kev[i].ident, IO_ERROR);
555 if (kev[i].flags & EV_EOF) {
557 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
558 kev[i].filter, kev[i].ident); /* Fall through */
561 io_docallback(kev[i].ident, IO_ERROR);
567 } while (ret == 100);
575 io_dispatch(struct timeval *tv)
578 return io_dispatch_select(tv);
581 return io_dispatch_kqueue(tv);
584 return io_dispatch_epoll(tv);
589 /* call the callback function inside the struct matching fd */
591 io_docallback(int fd, short what)
595 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
597 i = io_event_get(fd);
600 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
601 i->callback(fd, (what & IO_ERROR) ? i->what : what);
602 /* if error indicator is set, we return the event(s) the app asked for */