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.1 2005/07/07 18:38:35 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);
95 Log(LOG_DEBUG, "io_event_add(): EMPTY FOR fd %d", fd);
104 io_library_init(unsigned int eventsize)
107 int ecreate_hint = (int)eventsize;
108 if (ecreate_hint <= 0)
114 if (eventsize >= FD_SETSIZE)
115 eventsize = FD_SETSIZE - 1;
118 if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
122 io_masterfd = epoll_create(ecreate_hint);
124 "io subsystem: using epoll (hint size %d), initial io_event maxfd: %u, io_masterfd %d",
125 ecreate_hint, eventsize, io_masterfd);
126 return io_masterfd >= 0;
129 Log(LOG_INFO, "io subsystem: using select, initial io_event maxfd: %u",
134 if (Conf_MaxConnections >= FD_SETSIZE) {
136 "Conf_MaxConnections (%d) exceeds limit (%u), changed Conf_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");
148 io_masterfd = kqueue();
151 "io subsystem: using kqueue, initial io_event maxfd: %u, io_masterfd %d",
152 eventsize, io_masterfd);
153 return io_masterfd >= 0;
159 io_library_shutdown(void)
161 unsigned int len = array_length(&io_events, sizeof(io_event));
164 if (NULL == io_event_get(len))
167 #ifndef IO_USE_SELECT
168 close(io_masterfd); /* kqueue, epoll */
175 array_free(&io_evcache);
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))
202 if (fd >= FD_SETSIZE) {
204 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
208 #endif /* FD_SETSIZE */
209 #endif /* IO_USE_SELECT */
211 i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
214 "array_alloc failed: could not allocate space for %d io_event structures",
220 i->callback = cbfunc;
223 return io_event_new_epoll(fd, what);
227 return io_event_add_kqueue(fd, what);
231 return io_event_add(fd, what);
238 io_event_new_epoll(int fd, short what)
240 struct epoll_event ev = { 0, {0} };
243 if (what & IO_WANTREAD)
244 ev.events = EPOLLIN | EPOLLPRI;
245 if (what & IO_WANTWRITE)
246 ev.events |= EPOLLOUT;
248 return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
253 io_event_change_epoll(int fd, short what)
255 struct epoll_event ev = { 0, {0} };
258 if (what & IO_WANTREAD)
259 ev.events = EPOLLIN | EPOLLPRI;
260 if (what & IO_WANTWRITE)
261 ev.events |= EPOLLOUT;
263 return epoll_ctl(io_masterfd, EPOLL_CTL_MOD, fd, &ev) == 0;
269 io_event_kqueue_commit_cache(void)
271 struct kevent *events;
273 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
275 if (!len) /* nothing to do */
281 array_free(&io_evcache);
285 events = array_start(&io_evcache);
289 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
291 array_trunc(&io_evcache);
297 io_event_add_kqueue(int fd, short what)
301 unsigned int len = array_length(&io_evcache, sizeof kev);
304 if (what & IO_WANTREAD)
305 filter = EVFILT_READ;
307 if (what & IO_WANTWRITE)
308 filter |= EVFILT_WRITE;
311 ret = io_event_kqueue_commit_cache();
313 array_trunc(&io_evcache);
316 EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
317 return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
323 io_event_add(int fd, short what)
325 io_event *i = io_event_get(fd);
334 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd,
341 return io_event_change_epoll(fd, i->what);
345 return io_event_add_kqueue(fd, what);
349 if (fd > select_maxfd)
352 if (what & IO_WANTREAD)
353 FD_SET(fd, &readers);
354 if (what & IO_WANTWRITE)
355 FD_SET(fd, &writers);
363 io_setnonblock(int fd)
365 int flags = fcntl(fd, F_GETFL);
370 #define O_NONBLOCK O_NDELAY
374 return fcntl(fd, F_SETFL, flags) == 0;
381 io_event *i = io_event_get(fd);
383 memset(i, 0, sizeof(io_event));
387 FD_CLR(fd, &writers);
388 FD_CLR(fd, &readers);
390 if (fd == select_maxfd)
394 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
395 io_event_kqueue_commit_cache();
397 return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
402 io_event_del(int fd, short what)
408 io_event *i = io_event_get(fd);
410 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d",
420 return io_event_change_epoll(fd, i->what);
424 if (what & IO_WANTREAD)
425 filter = EVFILT_READ;
427 if (what & IO_WANTWRITE)
428 filter |= EVFILT_WRITE;
430 EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
431 return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
435 if (what & IO_WANTWRITE)
436 FD_CLR(i->fd, &writers);
438 if (what & IO_WANTREAD)
439 FD_CLR(i->fd, &readers);
448 io_dispatch_select(struct timeval *tv)
450 fd_set readers_tmp = readers;
451 fd_set writers_tmp = writers;
455 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
461 for (i = 0; i <= select_maxfd; i++) {
463 if (FD_ISSET(i, &readers_tmp)) {
468 if (FD_ISSET(i, &writers_tmp)) {
469 what |= IO_WANTWRITE;
473 io_docallback(i, what);
485 io_dispatch_epoll(struct timeval *tv)
487 time_t sec = tv->tv_sec * 1000;
488 int i, total = 0, ret, timeout = tv->tv_usec + sec;
489 struct epoll_event epoll_ev[100];
496 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
501 for (i = 0; i < ret; i++) {
503 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
506 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
509 if (epoll_ev[i].events & EPOLLOUT)
510 type |= IO_WANTWRITE;
512 io_docallback(epoll_ev[i].data.fd, type);
516 } while (ret == 100);
525 io_dispatch_kqueue(struct timeval *tv)
527 int i, total = 0, ret;
528 struct kevent kev[100];
529 struct kevents *newevents;
533 ts.tv_sec = tv->tv_sec;
534 ts.tv_nsec = tv->tv_usec * 1000;
537 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
538 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
539 assert(newevents_len >= 0);
540 if (newevents_len < 0)
547 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
548 if ((newevents_len>0) && ret != -1)
549 array_trunc(&io_evcache);
555 for (i = 0; i < ret; i++) {
557 if (kev[i].flags & EV_EOF)
560 if (kev[i].filter & EV_ERROR)
563 if (kev[i].filter & EVFILT_READ)
566 if (kev[i].filter & EVFILT_WRITE)
567 type |= IO_WANTWRITE;
569 io_docallback(kev[i].ident, type);
575 } while (ret == 100);
583 io_dispatch(struct timeval *tv)
586 return io_dispatch_select(tv);
589 return io_dispatch_kqueue(tv);
592 return io_dispatch_epoll(tv);
597 /* call the callback function inside the struct matching fd */
599 io_docallback(int fd, short what)
603 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
605 i = io_event_get(fd);
608 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
609 i->callback(fd, (what & IO_ERROR) ? i->what : what);
610 /* if error indicator is set, we return the event(s) the app asked for */