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.4 2005/07/14 09:20:39 alex 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: epoll (hint size %d, initial maxfd %u, masterfd %d).",
119 ecreate_hint, eventsize, io_masterfd);
120 return io_masterfd >= 0;
123 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
128 if (Conf_MaxConnections >= FD_SETSIZE) {
130 "MaxConnections (%d) exceeds limit (%u), changed 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: kqueue (initial maxfd %u, 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);
169 io_event_setcb(int fd, void (*cbfunc) (int, short))
171 io_event *i = io_event_get(fd);
175 i->callback = cbfunc;
181 io_event_create(int fd, short what, void (*cbfunc) (int, short))
189 if (fd >= FD_SETSIZE) {
191 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
195 #endif /* FD_SETSIZE */
196 #endif /* IO_USE_SELECT */
198 i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
201 "array_alloc failed: could not allocate space for %d io_event structures",
207 i->callback = cbfunc;
210 return io_event_new_epoll(fd, what);
214 return io_event_add_kqueue(fd, what);
218 return io_event_add(fd, what);
225 io_event_new_epoll(int fd, short what)
227 struct epoll_event ev = { 0, {0} };
230 if (what & IO_WANTREAD)
231 ev.events = EPOLLIN | EPOLLPRI;
232 if (what & IO_WANTWRITE)
233 ev.events |= EPOLLOUT;
235 return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
240 io_event_change_epoll(int fd, short what)
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, EPOLL_CTL_MOD, 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);
276 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
278 array_trunc(&io_evcache);
284 io_event_add_kqueue(int fd, short what)
288 unsigned int len = array_length(&io_evcache, sizeof kev);
291 if (what & IO_WANTREAD)
292 filter = EVFILT_READ;
294 if (what & IO_WANTWRITE)
295 filter |= EVFILT_WRITE;
298 ret = io_event_kqueue_commit_cache();
300 array_trunc(&io_evcache);
303 EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
304 return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
310 io_event_add(int fd, short what)
312 io_event *i = io_event_get(fd);
321 Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
327 return io_event_change_epoll(fd, i->what);
331 return io_event_add_kqueue(fd, what);
335 if (fd > select_maxfd)
338 if (what & IO_WANTREAD)
339 FD_SET(fd, &readers);
340 if (what & IO_WANTWRITE)
341 FD_SET(fd, &writers);
349 io_setnonblock(int fd)
351 int flags = fcntl(fd, F_GETFL);
356 #define O_NONBLOCK O_NDELAY
360 return fcntl(fd, F_SETFL, flags) == 0;
367 io_event *i = io_event_get(fd);
369 memset(i, 0, sizeof(io_event));
373 FD_CLR(fd, &writers);
374 FD_CLR(fd, &readers);
376 if (fd == select_maxfd)
380 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
381 io_event_kqueue_commit_cache();
383 return close(fd) == 0; /* both epoll an kqueue will remove fd from all sets automatically */
388 io_event_del(int fd, short what)
394 io_event *i = io_event_get(fd);
396 Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
405 return io_event_change_epoll(fd, i->what);
409 if (what & IO_WANTREAD)
410 filter = EVFILT_READ;
412 if (what & IO_WANTWRITE)
413 filter |= EVFILT_WRITE;
415 EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
416 return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
420 if (what & IO_WANTWRITE)
421 FD_CLR(i->fd, &writers);
423 if (what & IO_WANTREAD)
424 FD_CLR(i->fd, &readers);
433 io_dispatch_select(struct timeval *tv)
435 fd_set readers_tmp = readers;
436 fd_set writers_tmp = writers;
440 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
446 for (i = 0; i <= select_maxfd; i++) {
448 if (FD_ISSET(i, &readers_tmp)) {
453 if (FD_ISSET(i, &writers_tmp)) {
454 what |= IO_WANTWRITE;
458 io_docallback(i, what);
470 io_dispatch_epoll(struct timeval *tv)
472 time_t sec = tv->tv_sec * 1000;
473 int i, total = 0, ret, timeout = tv->tv_usec + sec;
474 struct epoll_event epoll_ev[100];
481 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
486 for (i = 0; i < ret; i++) {
488 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
491 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
494 if (epoll_ev[i].events & EPOLLOUT)
495 type |= IO_WANTWRITE;
497 io_docallback(epoll_ev[i].data.fd, type);
501 } while (ret == 100);
510 io_dispatch_kqueue(struct timeval *tv)
512 int i, total = 0, ret;
513 struct kevent kev[100];
514 struct kevents *newevents;
518 ts.tv_sec = tv->tv_sec;
519 ts.tv_nsec = tv->tv_usec * 1000;
522 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
523 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
524 assert(newevents_len >= 0);
525 if (newevents_len < 0)
532 ret = kevent(io_masterfd, newevents, newevents_len, kev,
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 */