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.15 2006/07/12 19:27:12 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
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);
102 io_library_init(unsigned int eventsize)
106 int ecreate_hint = (int)eventsize;
107 if (ecreate_hint <= 0)
110 if (library_initialized)
115 if (eventsize >= FD_SETSIZE)
116 eventsize = FD_SETSIZE - 1;
119 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
123 io_masterfd = epoll_create(ecreate_hint);
125 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
126 ecreate_hint, eventsize, io_masterfd);
127 ret = io_masterfd >= 0;
128 if (ret) library_initialized = true;
133 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
138 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
140 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
141 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
143 Conf_MaxConnections = FD_SETSIZE - 1;
147 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
148 #endif /* FD_SETSIZE */
149 library_initialized = true;
153 io_masterfd = kqueue();
156 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
157 eventsize, io_masterfd);
158 ret = io_masterfd >= 0;
159 if (ret) library_initialized = true;
167 io_library_shutdown(void)
173 close(io_masterfd); /* kqueue, epoll */
177 array_free(&io_evcache);
179 library_initialized = false;
184 io_event_setcb(int fd, void (*cbfunc) (int, short))
186 io_event *i = io_event_get(fd);
190 i->callback = cbfunc;
196 io_event_create(int fd, short what, void (*cbfunc) (int, short))
205 if (fd >= FD_SETSIZE) {
207 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
211 #endif /* FD_SETSIZE */
212 #endif /* IO_USE_SELECT */
214 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
217 "array_alloc failed: could not allocate space for %d io_event structures",
222 i->callback = cbfunc;
225 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
228 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
231 ret = io_event_add(fd, what);
233 if (ret) i->what = what;
240 io_event_change_epoll(int fd, short what, const int action)
242 struct epoll_event ev = { 0, {0} };
245 if (what & IO_WANTREAD)
246 ev.events = EPOLLIN | EPOLLPRI;
247 if (what & IO_WANTWRITE)
248 ev.events |= EPOLLOUT;
250 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
256 io_event_kqueue_commit_cache(void)
258 struct kevent *events;
260 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
262 if (!len) /* nothing to do */
268 array_free(&io_evcache);
272 events = array_start(&io_evcache);
274 assert(events != NULL);
276 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
278 array_trunc(&io_evcache);
284 io_event_change_kqueue(int fd, short what, const int action)
289 if (what & IO_WANTREAD) {
290 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
291 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
293 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
296 if (ret && (what & IO_WANTWRITE)) {
297 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
298 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
300 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
303 if (array_length(&io_evcache, sizeof kev) >= 100)
304 io_event_kqueue_commit_cache();
311 io_event_add(int fd, short what)
313 io_event *i = io_event_get(fd);
315 if (!i) return false;
316 if (i->what == what) return true;
318 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
322 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
326 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
330 if (fd > select_maxfd)
333 if (what & IO_WANTREAD)
334 FD_SET(fd, &readers);
335 if (what & IO_WANTWRITE)
336 FD_SET(fd, &writers);
344 io_setnonblock(int fd)
346 int flags = fcntl(fd, F_GETFL);
351 #define O_NONBLOCK O_NDELAY
355 return fcntl(fd, F_SETFL, flags) == 0;
364 FD_CLR(fd, &writers);
365 FD_CLR(fd, &readers);
367 if (fd == select_maxfd) {
368 while (select_maxfd>0) {
369 --select_maxfd; /* find largest fd */
370 i = io_event_get(select_maxfd);
371 if (i && i->callback) break;
375 i = io_event_get(fd);
377 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
378 io_event_kqueue_commit_cache();
380 /* both kqueue and epoll remove fd from all sets automatically on the last close
381 * of the descriptor. since we don't know if this is the last close we'll have
382 * to remove the set explicitly. */
384 io_event_change_kqueue(fd, i->what, EV_DELETE);
385 io_event_kqueue_commit_cache();
389 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
395 return close(fd) == 0;
400 io_event_del(int fd, short what)
402 io_event *i = io_event_get(fd);
404 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(fd, &writers);
421 if (what & IO_WANTREAD)
422 FD_CLR(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)
526 assert(newevents != NULL);
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 if (kev[i].flags & EV_EOF) {
540 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
541 kev[i].filter, kev[i].ident);
543 io_docallback((int)kev[i].ident, IO_ERROR);
547 switch (kev[i].filter) {
549 io_docallback((int)kev[i].ident, IO_WANTREAD);
552 io_docallback((int)kev[i].ident, IO_WANTWRITE);
556 LogDebug("Unknown kev.filter number %d for fd %d",
557 kev[i].filter, kev[i].ident); /* Fall through */
560 io_docallback((int)kev[i].ident, IO_ERROR);
566 } while (ret == 100);
574 io_dispatch(struct timeval *tv)
577 return io_dispatch_select(tv);
580 return io_dispatch_kqueue(tv);
583 return io_dispatch_epoll(tv);
588 /* call the callback function inside the struct matching fd */
590 io_docallback(int fd, short what)
594 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
596 i = io_event_get(fd);
598 if (i->callback) { /* callback might be NULL if a previous callback function
599 called io_close on this fd */
600 i->callback(fd, (what & IO_ERROR) ? i->what : what);
602 /* if error indicator is set, we return the event(s) that were registered */