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.2 2005/07/09 20:23:00 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
51 #include <sys/epoll.h>
53 static int io_masterfd;
54 static bool io_event_new_epoll(int fd, short what);
55 static bool io_event_change_epoll(int fd, short what);
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_add_kqueue(int, short);
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)
101 int ecreate_hint = (int)eventsize;
102 if (ecreate_hint <= 0)
108 if (eventsize >= FD_SETSIZE)
109 eventsize = FD_SETSIZE - 1;
112 if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
116 io_masterfd = epoll_create(ecreate_hint);
118 "io subsystem: using epoll (hint size %d), initial io_event maxfd: %u, io_masterfd %d",
119 ecreate_hint, eventsize, io_masterfd);
120 return io_masterfd >= 0;
123 Log(LOG_INFO, "io subsystem: using select, initial io_event maxfd: %u",
128 if (Conf_MaxConnections >= FD_SETSIZE) {
130 "Conf_MaxConnections (%d) exceeds limit (%u), changed Conf_MaxConnections to %u",
131 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
133 Conf_MaxConnections = FD_SETSIZE - 1;
137 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform");
142 io_masterfd = kqueue();
145 "io subsystem: using kqueue, initial io_event maxfd: %u, io_masterfd %d",
146 eventsize, io_masterfd);
147 return io_masterfd >= 0;
153 io_library_shutdown(void)
159 close(io_masterfd); /* kqueue, epoll */
163 array_free(&io_evcache);
170 io_event_setcb(int fd, void (*cbfunc) (int, short))
172 io_event *i = io_event_get(fd);
176 i->callback = cbfunc;
182 io_event_create(int fd, short what, void (*cbfunc) (int, short))
190 if (fd >= FD_SETSIZE) {
192 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
196 #endif /* FD_SETSIZE */
197 #endif /* IO_USE_SELECT */
199 i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
202 "array_alloc failed: could not allocate space for %d io_event structures",
208 i->callback = cbfunc;
211 return io_event_new_epoll(fd, what);
215 return io_event_add_kqueue(fd, what);
219 return io_event_add(fd, what);
226 io_event_new_epoll(int fd, short what)
228 struct epoll_event ev = { 0, {0} };
231 if (what & IO_WANTREAD)
232 ev.events = EPOLLIN | EPOLLPRI;
233 if (what & IO_WANTWRITE)
234 ev.events |= EPOLLOUT;
236 return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
241 io_event_change_epoll(int fd, short what)
243 struct epoll_event ev = { 0, {0} };
246 if (what & IO_WANTREAD)
247 ev.events = EPOLLIN | EPOLLPRI;
248 if (what & IO_WANTWRITE)
249 ev.events |= EPOLLOUT;
251 return epoll_ctl(io_masterfd, EPOLL_CTL_MOD, fd, &ev) == 0;
257 io_event_kqueue_commit_cache(void)
259 struct kevent *events;
261 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
263 if (!len) /* nothing to do */
269 array_free(&io_evcache);
273 events = array_start(&io_evcache);
277 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
279 array_trunc(&io_evcache);
285 io_event_add_kqueue(int fd, short what)
289 unsigned int len = array_length(&io_evcache, sizeof kev);
292 if (what & IO_WANTREAD)
293 filter = EVFILT_READ;
295 if (what & IO_WANTWRITE)
296 filter |= EVFILT_WRITE;
299 ret = io_event_kqueue_commit_cache();
301 array_trunc(&io_evcache);
304 EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
305 return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
311 io_event_add(int fd, short what)
313 io_event *i = io_event_get(fd);
322 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
328 return io_event_change_epoll(fd, i->what);
332 return io_event_add_kqueue(fd, what);
336 if (fd > select_maxfd)
339 if (what & IO_WANTREAD)
340 FD_SET(fd, &readers);
341 if (what & IO_WANTWRITE)
342 FD_SET(fd, &writers);
350 io_setnonblock(int fd)
352 int flags = fcntl(fd, F_GETFL);
357 #define O_NONBLOCK O_NDELAY
361 return fcntl(fd, F_SETFL, flags) == 0;
368 io_event *i = io_event_get(fd);
370 memset(i, 0, sizeof(io_event));
374 FD_CLR(fd, &writers);
375 FD_CLR(fd, &readers);
377 if (fd == select_maxfd)
381 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
382 io_event_kqueue_commit_cache();
384 return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
389 io_event_del(int fd, short what)
395 io_event *i = io_event_get(fd);
397 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
406 return io_event_change_epoll(fd, i->what);
410 if (what & IO_WANTREAD)
411 filter = EVFILT_READ;
413 if (what & IO_WANTWRITE)
414 filter |= EVFILT_WRITE;
416 EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
417 return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
421 if (what & IO_WANTWRITE)
422 FD_CLR(i->fd, &writers);
424 if (what & IO_WANTREAD)
425 FD_CLR(i->fd, &readers);
434 io_dispatch_select(struct timeval *tv)
436 fd_set readers_tmp = readers;
437 fd_set writers_tmp = writers;
441 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
447 for (i = 0; i <= select_maxfd; i++) {
449 if (FD_ISSET(i, &readers_tmp)) {
454 if (FD_ISSET(i, &writers_tmp)) {
455 what |= IO_WANTWRITE;
459 io_docallback(i, what);
471 io_dispatch_epoll(struct timeval *tv)
473 time_t sec = tv->tv_sec * 1000;
474 int i, total = 0, ret, timeout = tv->tv_usec + sec;
475 struct epoll_event epoll_ev[100];
482 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
487 for (i = 0; i < ret; i++) {
489 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
492 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
495 if (epoll_ev[i].events & EPOLLOUT)
496 type |= IO_WANTWRITE;
498 io_docallback(epoll_ev[i].data.fd, type);
502 } while (ret == 100);
511 io_dispatch_kqueue(struct timeval *tv)
513 int i, total = 0, ret;
514 struct kevent kev[100];
515 struct kevents *newevents;
519 ts.tv_sec = tv->tv_sec;
520 ts.tv_nsec = tv->tv_usec * 1000;
523 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
524 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
525 assert(newevents_len >= 0);
526 if (newevents_len < 0)
533 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
534 if ((newevents_len>0) && ret != -1)
535 array_trunc(&io_evcache);
541 for (i = 0; i < ret; i++) {
543 if (kev[i].flags & EV_EOF)
546 if (kev[i].filter & EV_ERROR)
549 if (kev[i].filter & EVFILT_READ)
552 if (kev[i].filter & EVFILT_WRITE)
553 type |= IO_WANTWRITE;
555 io_docallback(kev[i].ident, type);
561 } while (ret == 100);
569 io_dispatch(struct timeval *tv)
572 return io_dispatch_select(tv);
575 return io_dispatch_kqueue(tv);
578 return io_dispatch_epoll(tv);
583 /* call the callback function inside the struct matching fd */
585 io_docallback(int fd, short what)
589 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
591 i = io_event_get(fd);
594 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
595 i->callback(fd, (what & IO_ERROR) ? i->what : what);
596 /* if error indicator is set, we return the event(s) the app asked for */