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.5 2005/07/14 14:35:38 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_new_epoll(int fd, short what);
56 static bool io_event_change_epoll(int fd, short what);
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_add_kqueue(int, short);
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));
91 i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
99 io_library_init(unsigned int eventsize)
103 int ecreate_hint = (int)eventsize;
104 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))
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, what);
340 return io_event_change_epoll(fd, i->what);
344 return io_event_add_kqueue(fd, what);
348 if (fd > select_maxfd)
351 if (what & IO_WANTREAD)
352 FD_SET(fd, &readers);
353 if (what & IO_WANTWRITE)
354 FD_SET(fd, &writers);
362 io_setnonblock(int fd)
364 int flags = fcntl(fd, F_GETFL);
369 #define O_NONBLOCK O_NDELAY
373 return fcntl(fd, F_SETFL, flags) == 0;
380 io_event *i = io_event_get(fd);
382 memset(i, 0, sizeof(io_event));
386 FD_CLR(fd, &writers);
387 FD_CLR(fd, &readers);
389 if (fd == select_maxfd)
393 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
394 io_event_kqueue_commit_cache();
396 return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
401 io_event_del(int fd, short what)
407 io_event *i = io_event_get(fd);
409 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
418 return io_event_change_epoll(fd, i->what);
422 if (what & IO_WANTREAD)
423 filter = EVFILT_READ;
425 if (what & IO_WANTWRITE)
426 filter |= EVFILT_WRITE;
428 EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
429 return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
433 if (what & IO_WANTWRITE)
434 FD_CLR(i->fd, &writers);
436 if (what & IO_WANTREAD)
437 FD_CLR(i->fd, &readers);
446 io_dispatch_select(struct timeval *tv)
448 fd_set readers_tmp = readers;
449 fd_set writers_tmp = writers;
453 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
459 for (i = 0; i <= select_maxfd; i++) {
461 if (FD_ISSET(i, &readers_tmp)) {
466 if (FD_ISSET(i, &writers_tmp)) {
467 what |= IO_WANTWRITE;
471 io_docallback(i, what);
483 io_dispatch_epoll(struct timeval *tv)
485 time_t sec = tv->tv_sec * 1000;
486 int i, total = 0, ret, timeout = tv->tv_usec + sec;
487 struct epoll_event epoll_ev[100];
494 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
499 for (i = 0; i < ret; i++) {
501 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
504 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
507 if (epoll_ev[i].events & EPOLLOUT)
508 type |= IO_WANTWRITE;
510 io_docallback(epoll_ev[i].data.fd, type);
514 } while (ret == 100);
523 io_dispatch_kqueue(struct timeval *tv)
525 int i, total = 0, ret;
526 struct kevent kev[100];
527 struct kevents *newevents;
531 ts.tv_sec = tv->tv_sec;
532 ts.tv_nsec = tv->tv_usec * 1000;
535 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
536 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
537 assert(newevents_len >= 0);
538 if (newevents_len < 0)
545 ret = kevent(io_masterfd, newevents, newevents_len, kev,
547 if ((newevents_len>0) && ret != -1)
548 array_trunc(&io_evcache);
554 for (i = 0; i < ret; i++) {
556 if (kev[i].flags & EV_EOF)
559 if (kev[i].filter & EV_ERROR)
562 if (kev[i].filter & EVFILT_READ)
565 if (kev[i].filter & EVFILT_WRITE)
566 type |= IO_WANTWRITE;
568 io_docallback(kev[i].ident, type);
574 } while (ret == 100);
582 io_dispatch(struct timeval *tv)
585 return io_dispatch_select(tv);
588 return io_dispatch_kqueue(tv);
591 return io_dispatch_epoll(tv);
596 /* call the callback function inside the struct matching fd */
598 io_docallback(int fd, short what)
602 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
604 i = io_event_get(fd);
607 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
608 i->callback(fd, (what & IO_ERROR) ? i->what : what);
609 /* if error indicator is set, we return the event(s) the app asked for */