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.9 2005/08/29 13:58:54 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);
303 if (what & IO_WANTREAD)
304 filter = EVFILT_READ;
306 if (what & IO_WANTWRITE)
307 filter |= EVFILT_WRITE;
310 (void)io_event_kqueue_commit_cache();
313 EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
314 return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
320 io_event_add(int fd, short what)
322 io_event *i = io_event_get(fd);
331 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
337 return io_event_change_epoll(fd, i->what);
341 return io_event_add_kqueue(fd, what);
345 if (fd > select_maxfd)
348 if (what & IO_WANTREAD)
349 FD_SET(fd, &readers);
350 if (what & IO_WANTWRITE)
351 FD_SET(fd, &writers);
359 io_setnonblock(int fd)
361 int flags = fcntl(fd, F_GETFL);
366 #define O_NONBLOCK O_NDELAY
370 return fcntl(fd, F_SETFL, flags) == 0;
377 io_event *i = io_event_get(fd);
379 memset(i, 0, sizeof(io_event));
383 FD_CLR(fd, &writers);
384 FD_CLR(fd, &readers);
386 if (fd == select_maxfd)
390 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
391 io_event_kqueue_commit_cache();
393 return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
398 io_event_del(int fd, short what)
404 io_event *i = io_event_get(fd);
406 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
415 return io_event_change_epoll(fd, i->what);
419 if (what & IO_WANTREAD)
420 filter = EVFILT_READ;
422 if (what & IO_WANTWRITE)
423 filter |= EVFILT_WRITE;
425 EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
426 return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
430 if (what & IO_WANTWRITE)
431 FD_CLR(i->fd, &writers);
433 if (what & IO_WANTREAD)
434 FD_CLR(i->fd, &readers);
443 io_dispatch_select(struct timeval *tv)
445 fd_set readers_tmp = readers;
446 fd_set writers_tmp = writers;
450 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
456 for (i = 0; i <= select_maxfd; i++) {
458 if (FD_ISSET(i, &readers_tmp)) {
463 if (FD_ISSET(i, &writers_tmp)) {
464 what |= IO_WANTWRITE;
468 io_docallback(i, what);
480 io_dispatch_epoll(struct timeval *tv)
482 time_t sec = tv->tv_sec * 1000;
483 int i, total = 0, ret, timeout = tv->tv_usec + sec;
484 struct epoll_event epoll_ev[100];
491 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
496 for (i = 0; i < ret; i++) {
498 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
501 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
504 if (epoll_ev[i].events & EPOLLOUT)
505 type |= IO_WANTWRITE;
507 io_docallback(epoll_ev[i].data.fd, type);
511 } while (ret == 100);
520 io_dispatch_kqueue(struct timeval *tv)
522 int i, total = 0, ret;
523 struct kevent kev[100];
524 struct kevent *newevents;
528 ts.tv_sec = tv->tv_sec;
529 ts.tv_nsec = tv->tv_usec * 1000;
532 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
533 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
534 assert(newevents_len >= 0);
535 if (newevents_len < 0)
542 ret = kevent(io_masterfd, newevents, newevents_len, kev,
544 if ((newevents_len>0) && ret != -1)
545 array_trunc(&io_evcache);
551 for (i = 0; i < ret; i++) {
553 if (kev[i].flags & EV_EOF)
556 if (kev[i].filter & EV_ERROR)
559 if (kev[i].filter & EVFILT_READ)
562 if (kev[i].filter & EVFILT_WRITE)
563 type |= IO_WANTWRITE;
565 io_docallback(kev[i].ident, type);
571 } while (ret == 100);
579 io_dispatch(struct timeval *tv)
582 return io_dispatch_select(tv);
585 return io_dispatch_kqueue(tv);
588 return io_dispatch_epoll(tv);
593 /* call the callback function inside the struct matching fd */
595 io_docallback(int fd, short what)
599 Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
601 i = io_event_get(fd);
604 if (i->callback) /* callback might be 0 if previous callback function called io_close on this fd */
605 i->callback(fd, (what & IO_ERROR) ? i->what : what);
606 /* if error indicator is set, we return the event(s) the app asked for */