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.31 2008/04/03 20:56:44 fw Exp $";
21 #include <sys/types.h>
29 /* Enables extra debug messages in event add/delete/callback code. */
30 /* #define DEBUG_IO */
34 void (*callback)(int, short);
41 #define INIT_IOEVENT { NULL, -1, 0, NULL }
44 #ifdef HAVE_EPOLL_CREATE
45 # define IO_USE_EPOLL 1
47 # define IO_USE_SELECT 1
51 # define IO_USE_KQUEUE 1
53 # ifdef HAVE_SYS_DEVPOLL_H
54 # define IO_USE_DEVPOLL 1
57 # define IO_USE_POLL 1
60 # define IO_USE_SELECT 1
62 # error "no IO API available!?"
63 # endif /* HAVE_SELECT */
64 # endif /* HAVE_POLL */
65 # endif /* HAVE_SYS_DEVPOLL_H */
66 # endif /* HAVE_KQUEUE */
67 #endif /* HAVE_EPOLL_CREATE */
69 static bool library_initialized = false;
72 #include <sys/epoll.h>
74 static int io_masterfd = -1;
75 static bool io_event_change_epoll(int fd, short what, const int action);
76 static int io_dispatch_epoll(struct timeval *tv);
80 #include <sys/types.h>
81 #include <sys/event.h>
82 static array io_evcache;
83 static int io_masterfd;
85 static int io_dispatch_kqueue(struct timeval *tv);
86 static bool io_event_change_kqueue(int, short, const int action);
93 static int poll_maxfd;
95 static bool io_event_change_poll PARAMS((int fd, short what));
99 #include <sys/devpoll.h>
100 static int io_masterfd;
102 static bool io_event_change_devpoll(int fd, short what);
106 #include "defines.h" /* for conn.h */
107 #include "proc.h" /* for PROC_STAT (needed by conf.h) */
108 #include "conf.h" /* for Conf_MaxConnections */
110 static fd_set readers;
111 static fd_set writers;
113 * this is the first argument for select(), i.e.
114 * the largest fd registered, plus one.
116 static int select_maxfd;
117 static int io_dispatch_select(struct timeval *tv);
120 #define io_masterfd -1
122 #endif /* IO_USE_SELECT */
124 static array io_events;
126 static void io_docallback PARAMS((int fd, short what));
129 static void io_debug(const char *s, int fd, int what)
131 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
134 static inline void io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b) {/*NOTHING*/}
144 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
152 #ifdef IO_USE_DEVPOLL
154 io_dispatch_devpoll(struct timeval *tv)
157 time_t sec = tv->tv_sec * 1000;
158 int i, total, ret, timeout = tv->tv_usec + sec;
160 struct pollfd p[100];
167 dvp.dp_timeout = timeout;
170 ret = ioctl(io_masterfd, DP_POLL, &dvp);
174 for (i=0; i < ret ; i++) {
176 if (p[i].revents & (POLLIN|POLLPRI))
179 if (p[i].revents & POLLOUT)
180 what |= IO_WANTWRITE;
182 if (p[i].revents && !what) {
183 /* other flag is set, probably POLLERR */
186 io_docallback(p[i].fd, what);
188 } while (ret == 100);
195 io_event_change_devpoll(int fd, short what)
201 if (what & IO_WANTREAD)
202 p.events = POLLIN | POLLPRI;
203 if (what & IO_WANTWRITE)
207 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
211 io_close_devpoll(int fd)
214 p.events = POLLREMOVE;
216 write(io_masterfd, &p, sizeof p);
220 io_library_init_devpoll(unsigned int eventsize)
222 io_masterfd = open("/dev/poll", O_RDWR);
223 if (io_masterfd >= 0)
224 library_initialized = true;
225 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
226 eventsize, io_masterfd);
229 static inline void io_close_devpoll(int UNUSED x) {/* NOTHING */}
230 static inline void io_library_init_devpoll(unsigned int UNUSED ev) {/*NOTHING*/}
236 io_dispatch_poll(struct timeval *tv)
238 time_t sec = tv->tv_sec * 1000;
239 int i, ret, timeout = tv->tv_usec + sec;
242 struct pollfd *p = array_start(&pollfds);
247 ret = poll(p, poll_maxfd + 1, timeout);
252 for (i=0; i <= poll_maxfd; i++) {
254 if (p[i].revents & (POLLIN|POLLPRI))
257 if (p[i].revents & POLLOUT)
258 what |= IO_WANTWRITE;
260 if (p[i].revents && !what) {
261 /* other flag is set, probably POLLERR */
266 io_docallback(i, what);
276 io_event_change_poll(int fd, short what)
281 if (what & IO_WANTREAD)
282 events = POLLIN | POLLPRI;
283 if (what & IO_WANTWRITE)
286 p = array_alloc(&pollfds, sizeof *p, fd);
297 io_close_poll(int fd)
300 p = array_get(&pollfds, sizeof *p, fd);
304 if (fd == poll_maxfd) {
305 while (poll_maxfd > 0) {
307 p = array_get(&pollfds, sizeof *p, poll_maxfd);
315 io_library_init_poll(unsigned int eventsize)
318 array_init(&pollfds);
320 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
322 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
325 p = array_start(&pollfds);
326 for (i = 0; i < eventsize; i++)
329 library_initialized = true;
333 static inline void io_close_poll(int UNUSED x) {/* NOTHING */}
334 static inline void io_library_init_poll(unsigned int UNUSED ev) {/*NOTHING*/}
340 io_dispatch_select(struct timeval *tv)
342 fd_set readers_tmp = readers;
343 fd_set writers_tmp = writers;
347 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
353 for (i = 0; i <= select_maxfd; i++) {
355 if (FD_ISSET(i, &readers_tmp)) {
360 if (FD_ISSET(i, &writers_tmp)) {
361 what |= IO_WANTWRITE;
365 io_docallback(i, what);
374 io_library_init_select(unsigned int eventsize)
376 if (library_initialized)
378 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
383 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
385 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
386 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
388 Conf_MaxConnections = FD_SETSIZE - 1;
392 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
393 #endif /* FD_SETSIZE */
394 library_initialized = true;
398 io_close_select(int fd)
402 if (io_masterfd >= 0) /* Are we using epoll()? */
405 FD_CLR(fd, &writers);
406 FD_CLR(fd, &readers);
408 i = io_event_get(fd);
411 if (fd == select_maxfd) {
412 while (select_maxfd>0) {
413 --select_maxfd; /* find largest fd */
414 i = io_event_get(select_maxfd);
415 if (i && i->callback) break;
420 static inline void io_library_init_select(int UNUSED x) {/* NOTHING */}
421 static inline void io_close_select(int UNUSED x) {/* NOTHING */}
427 io_event_change_epoll(int fd, short what, const int action)
429 struct epoll_event ev = { 0, {0} };
432 if (what & IO_WANTREAD)
433 ev.events = EPOLLIN | EPOLLPRI;
434 if (what & IO_WANTWRITE)
435 ev.events |= EPOLLOUT;
437 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
441 io_dispatch_epoll(struct timeval *tv)
443 time_t sec = tv->tv_sec * 1000;
444 int i, total = 0, ret, timeout = tv->tv_usec + sec;
445 struct epoll_event epoll_ev[100];
452 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
457 for (i = 0; i < ret; i++) {
459 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
462 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
465 if (epoll_ev[i].events & EPOLLOUT)
466 type |= IO_WANTWRITE;
468 io_docallback(epoll_ev[i].data.fd, type);
472 } while (ret == 100);
478 io_library_init_epoll(unsigned int eventsize)
480 int ecreate_hint = (int)eventsize;
481 if (ecreate_hint <= 0)
483 io_masterfd = epoll_create(ecreate_hint);
484 if (io_masterfd >= 0) {
485 library_initialized = true;
487 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
488 ecreate_hint, eventsize, io_masterfd);
492 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
496 static inline void io_library_init_epoll(unsigned int UNUSED ev) {/* NOTHING */}
497 #endif /* IO_USE_EPOLL */
502 io_event_kqueue_commit_cache(void)
504 struct kevent *events;
506 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
508 if (!len) /* nothing to do */
514 array_free(&io_evcache);
518 events = array_start(&io_evcache);
520 assert(events != NULL);
522 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
524 array_trunc(&io_evcache);
529 io_event_change_kqueue(int fd, short what, const int action)
534 if (what & IO_WANTREAD) {
535 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
536 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
538 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
541 if (ret && (what & IO_WANTWRITE)) {
542 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
543 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
545 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
548 if (array_length(&io_evcache, sizeof kev) >= 100)
549 io_event_kqueue_commit_cache();
554 io_dispatch_kqueue(struct timeval *tv)
556 int i, total = 0, ret;
557 struct kevent kev[100];
558 struct kevent *newevents;
561 ts.tv_sec = tv->tv_sec;
562 ts.tv_nsec = tv->tv_usec * 1000;
565 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
566 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
567 assert(newevents_len >= 0);
569 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
570 if (newevents && ret != -1)
571 array_trunc(&io_evcache);
577 for (i = 0; i < ret; i++) {
578 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
579 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
580 if (kev[i].flags & EV_ERROR)
581 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
582 (int)kev[i].ident, strerror((int)kev[i].data));
583 io_docallback((int)kev[i].ident, IO_ERROR);
587 switch (kev[i].filter) {
589 io_docallback((int)kev[i].ident, IO_WANTREAD);
592 io_docallback((int)kev[i].ident, IO_WANTWRITE);
595 LogDebug("Unknown kev.filter number %d for fd %d",
596 kev[i].filter, kev[i].ident);
599 io_docallback((int)kev[i].ident, IO_ERROR);
605 } while (ret == 100);
611 io_library_init_kqueue(unsigned int eventsize)
613 io_masterfd = kqueue();
616 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
617 eventsize, io_masterfd);
618 if (io_masterfd >= 0)
619 library_initialized = true;
622 static inline void io_library_init_kqueue(unsigned int UNUSED ev) {/* NOTHING */}
627 io_library_init(unsigned int eventsize)
629 if (library_initialized)
632 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
635 io_library_init_epoll(eventsize);
636 io_library_init_kqueue(eventsize);
637 io_library_init_devpoll(eventsize);
638 io_library_init_poll(eventsize);
639 io_library_init_select(eventsize);
641 return library_initialized;
646 io_library_shutdown(void)
652 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
653 if (io_masterfd >= 0)
658 array_free(&io_evcache);
660 library_initialized = false;
665 io_event_setcb(int fd, void (*cbfunc) (int, short))
667 io_event *i = io_event_get(fd);
671 i->callback = cbfunc;
677 backend_create_ev(int fd, short what)
680 #ifdef IO_USE_DEVPOLL
681 ret = io_event_change_devpoll(fd, what);
684 ret = io_event_change_poll(fd, what);
687 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
690 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
694 ret = io_event_add(fd, what);
701 io_event_create(int fd, short what, void (*cbfunc) (int, short))
707 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
708 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
710 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
715 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
718 "array_alloc failed: could not allocate space for %d io_event structures",
723 i->callback = cbfunc;
725 ret = backend_create_ev(fd, what);
733 io_event_add(int fd, short what)
735 io_event *i = io_event_get(fd);
737 if (!i) return false;
739 if ((i->what & what) == what) /* event type is already registered */
742 io_debug("io_event_add: fd, what", fd, what);
746 if (io_masterfd >= 0)
747 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
750 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
752 #ifdef IO_USE_DEVPOLL
753 return io_event_change_devpoll(fd, i->what);
756 return io_event_change_poll(fd, i->what);
759 if (fd > select_maxfd)
762 if (what & IO_WANTREAD)
763 FD_SET(fd, &readers);
764 if (what & IO_WANTWRITE)
765 FD_SET(fd, &writers);
774 io_setnonblock(int fd)
776 int flags = fcntl(fd, F_GETFL);
780 #define O_NONBLOCK O_NDELAY
784 return fcntl(fd, F_SETFL, flags) == 0;
793 i = io_event_get(fd);
795 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
796 io_event_kqueue_commit_cache();
798 /* both kqueue and epoll remove fd from all sets automatically on the last close
799 * of the descriptor. since we don't know if this is the last close we'll have
800 * to remove the set explicitly. */
802 io_event_change_kqueue(fd, i->what, EV_DELETE);
803 io_event_kqueue_commit_cache();
806 io_close_devpoll(fd);
810 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
816 return close(fd) == 0;
821 io_event_del(int fd, short what)
823 io_event *i = io_event_get(fd);
825 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
826 if (!i) return false;
828 if (!(i->what & what)) /* event is already disabled */
832 #ifdef IO_USE_DEVPOLL
833 return io_event_change_devpoll(fd, i->what);
836 return io_event_change_poll(fd, i->what);
839 if (io_masterfd >= 0)
840 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
843 return io_event_change_kqueue(fd, what, EV_DISABLE);
846 if (what & IO_WANTWRITE)
847 FD_CLR(fd, &writers);
849 if (what & IO_WANTREAD)
850 FD_CLR(fd, &readers);
858 io_dispatch(struct timeval *tv)
861 if (io_masterfd >= 0)
862 return io_dispatch_epoll(tv);
865 return io_dispatch_select(tv);
868 return io_dispatch_kqueue(tv);
870 #ifdef IO_USE_DEVPOLL
871 return io_dispatch_devpoll(tv);
874 return io_dispatch_poll(tv);
880 /* call the callback function inside the struct matching fd */
882 io_docallback(int fd, short what)
884 io_event *i = io_event_get(fd);
886 io_debug("io_docallback; fd, what", fd, what);
888 if (i->callback) { /* callback might be NULL if a previous callback function
889 called io_close on this fd */
890 i->callback(fd, (what & IO_ERROR) ? i->what : what);
892 /* if error indicator is set, we return the event(s) that were registered */