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 * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
15 * I/O abstraction interface.
22 #include <sys/types.h>
30 /* Enables extra debug messages in event add/delete/callback code. */
31 /* #define DEBUG_IO */
35 void (*callback)(int, short);
42 #define INIT_IOEVENT { NULL, -1, 0, NULL }
44 #define MAX_EVENTS 100
46 #ifdef HAVE_EPOLL_CREATE
47 # define IO_USE_EPOLL 1
49 # define IO_USE_SELECT 1
53 # define IO_USE_KQUEUE 1
55 # ifdef HAVE_SYS_DEVPOLL_H
56 # define IO_USE_DEVPOLL 1
58 # if defined(HAVE_POLL) && defined(HAVE_POLL_H)
59 # define IO_USE_POLL 1
62 # define IO_USE_SELECT 1
64 # error "no IO API available!?"
65 # endif /* HAVE_SELECT */
66 # endif /* HAVE_POLL */
67 # endif /* HAVE_SYS_DEVPOLL_H */
68 # endif /* HAVE_KQUEUE */
69 #endif /* HAVE_EPOLL_CREATE */
71 static bool library_initialized = false;
74 #include <sys/epoll.h>
76 static int io_masterfd = -1;
77 static bool io_event_change_epoll(int fd, short what, const int action);
78 static int io_dispatch_epoll(struct timeval *tv);
82 #include <sys/types.h>
83 #include <sys/event.h>
84 static array io_evcache;
85 static int io_masterfd;
87 static int io_dispatch_kqueue(struct timeval *tv);
88 static bool io_event_change_kqueue(int, short, const int action);
91 /* Taken from /usr/include/sys/event.h of FreeBSD 8.1 and required by all
92 * platforms that have kqueue but lack EV_SET() -- for example FreeBSD 4. */
93 #define EV_SET(kevp, a, b, c, d, e, f) do { \
94 struct kevent *__kevp__ = (kevp); \
95 __kevp__->ident = (a); \
96 __kevp__->filter = (b); \
97 __kevp__->flags = (c); \
98 __kevp__->fflags = (d); \
99 __kevp__->data = (e); \
100 __kevp__->udata = (f); \
108 static array pollfds;
109 static int poll_maxfd;
111 static bool io_event_change_poll PARAMS((int fd, short what));
114 #ifdef IO_USE_DEVPOLL
115 #include <sys/devpoll.h>
116 static int io_masterfd;
118 static bool io_event_change_devpoll(int fd, short what);
122 #include "defines.h" /* for conn.h */
123 #include "proc.h" /* for PROC_STAT (needed by conf.h) */
124 #include "conn.h" /* for CONN_ID (needed by conf.h) */
125 #include "conf.h" /* for Conf_MaxConnections */
127 static fd_set readers;
128 static fd_set writers;
130 * this is the first argument for select(), i.e.
131 * the largest fd registered, plus one.
133 static int select_maxfd;
134 static int io_dispatch_select PARAMS((struct timeval *tv));
137 #define io_masterfd -1
139 #endif /* IO_USE_SELECT */
141 static array io_events;
143 static void io_docallback PARAMS((int fd, short what));
147 io_debug(const char *s, int fd, int what)
149 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
153 io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
164 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
172 #ifdef IO_USE_DEVPOLL
174 io_dispatch_devpoll(struct timeval *tv)
177 time_t sec = tv->tv_sec * 1000;
178 int i, ret, timeout = tv->tv_usec + sec;
180 struct pollfd p[MAX_EVENTS];
185 dvp.dp_timeout = timeout;
186 dvp.dp_nfds = MAX_EVENTS;
188 ret = ioctl(io_masterfd, DP_POLL, &dvp);
190 for (i=0; i < ret ; i++) {
192 if (p[i].revents & (POLLIN|POLLPRI))
195 if (p[i].revents & POLLOUT)
196 what |= IO_WANTWRITE;
198 if (p[i].revents && !what) {
199 /* other flag is set, probably POLLERR */
202 io_docallback(p[i].fd, what);
210 io_event_change_devpoll(int fd, short what)
216 if (what & IO_WANTREAD)
217 p.events = POLLIN | POLLPRI;
218 if (what & IO_WANTWRITE)
222 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
226 io_close_devpoll(int fd)
229 p.events = POLLREMOVE;
231 write(io_masterfd, &p, sizeof p);
235 io_library_init_devpoll(unsigned int eventsize)
237 io_masterfd = open("/dev/poll", O_RDWR);
238 if (io_masterfd >= 0)
239 library_initialized = true;
240 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
241 eventsize, io_masterfd);
245 io_close_devpoll(int UNUSED x)
248 io_library_init_devpoll(unsigned int UNUSED ev)
255 io_dispatch_poll(struct timeval *tv)
257 time_t sec = tv->tv_sec * 1000;
258 int i, ret, timeout = tv->tv_usec + sec;
261 struct pollfd *p = array_start(&pollfds);
266 ret = poll(p, poll_maxfd + 1, timeout);
271 for (i=0; i <= poll_maxfd; i++) {
273 if (p[i].revents & (POLLIN|POLLPRI))
276 if (p[i].revents & POLLOUT)
277 what |= IO_WANTWRITE;
279 if (p[i].revents && !what) {
280 /* other flag is set, probably POLLERR */
285 io_docallback(i, what);
295 io_event_change_poll(int fd, short what)
300 if (what & IO_WANTREAD)
301 events = POLLIN | POLLPRI;
302 if (what & IO_WANTWRITE)
305 p = array_alloc(&pollfds, sizeof *p, fd);
316 io_close_poll(int fd)
319 p = array_get(&pollfds, sizeof *p, fd);
323 if (fd == poll_maxfd) {
324 while (poll_maxfd > 0) {
326 p = array_get(&pollfds, sizeof *p, poll_maxfd);
334 io_library_init_poll(unsigned int eventsize)
337 array_init(&pollfds);
339 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
341 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
344 p = array_start(&pollfds);
345 for (i = 0; i < eventsize; i++)
348 library_initialized = true;
353 io_close_poll(int UNUSED x)
356 io_library_init_poll(unsigned int UNUSED ev)
363 io_dispatch_select(struct timeval *tv)
371 readers_tmp = readers;
372 writers_tmp = writers;
374 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
380 for (i = 0; i <= select_maxfd; i++) {
382 if (FD_ISSET(i, &readers_tmp)) {
387 if (FD_ISSET(i, &writers_tmp)) {
388 what |= IO_WANTWRITE;
392 io_docallback(i, what);
401 io_library_init_select(unsigned int eventsize)
403 if (library_initialized)
405 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
410 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
412 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
413 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
415 Conf_MaxConnections = FD_SETSIZE - 1;
419 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
420 #endif /* FD_SETSIZE */
421 library_initialized = true;
425 io_close_select(int fd)
429 if (io_masterfd >= 0) /* Are we using epoll()? */
432 FD_CLR(fd, &writers);
433 FD_CLR(fd, &readers);
435 i = io_event_get(fd);
438 if (fd == select_maxfd) {
439 while (select_maxfd>0) {
440 --select_maxfd; /* find largest fd */
441 i = io_event_get(select_maxfd);
442 if (i && i->callback) break;
448 io_library_init_select(int UNUSED x)
451 io_close_select(int UNUSED x)
458 io_event_change_epoll(int fd, short what, const int action)
460 struct epoll_event ev = { 0, {0} };
463 if (what & IO_WANTREAD)
464 ev.events = EPOLLIN | EPOLLPRI;
465 if (what & IO_WANTWRITE)
466 ev.events |= EPOLLOUT;
468 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
472 io_dispatch_epoll(struct timeval *tv)
474 time_t sec = tv->tv_sec * 1000;
475 int i, ret, timeout = tv->tv_usec + sec;
476 struct epoll_event epoll_ev[MAX_EVENTS];
482 ret = epoll_wait(io_masterfd, epoll_ev, MAX_EVENTS, timeout);
484 for (i = 0; i < ret; i++) {
486 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
489 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
492 if (epoll_ev[i].events & EPOLLOUT)
493 type |= IO_WANTWRITE;
495 io_docallback(epoll_ev[i].data.fd, type);
502 io_library_init_epoll(unsigned int eventsize)
504 int ecreate_hint = (int)eventsize;
505 if (ecreate_hint <= 0)
507 io_masterfd = epoll_create(ecreate_hint);
508 if (io_masterfd >= 0) {
509 library_initialized = true;
511 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
512 ecreate_hint, eventsize, io_masterfd);
516 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
521 io_library_init_epoll(unsigned int UNUSED ev)
523 #endif /* IO_USE_EPOLL */
528 io_event_kqueue_commit_cache(void)
530 struct kevent *events;
532 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
534 if (!len) /* nothing to do */
540 array_free(&io_evcache);
544 events = array_start(&io_evcache);
546 assert(events != NULL);
548 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
550 array_trunc(&io_evcache);
555 io_event_change_kqueue(int fd, short what, const int action)
560 if (what & IO_WANTREAD) {
561 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
562 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
564 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
567 if (ret && (what & IO_WANTWRITE)) {
568 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
569 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
571 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
574 if (array_length(&io_evcache, sizeof kev) >= 100)
575 io_event_kqueue_commit_cache();
580 io_dispatch_kqueue(struct timeval *tv)
583 struct kevent kev[MAX_EVENTS];
584 struct kevent *newevents;
587 ts.tv_sec = tv->tv_sec;
588 ts.tv_nsec = tv->tv_usec * 1000;
590 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
591 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
592 assert(newevents_len >= 0);
594 ret = kevent(io_masterfd, newevents, newevents_len, kev, MAX_EVENTS, &ts);
595 if (newevents && ret != -1)
596 array_trunc(&io_evcache);
598 for (i = 0; i < ret; i++) {
599 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
600 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
601 if (kev[i].flags & EV_ERROR)
602 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
603 (int)kev[i].ident, strerror((int)kev[i].data));
604 io_docallback((int)kev[i].ident, IO_ERROR);
608 switch (kev[i].filter) {
610 io_docallback((int)kev[i].ident, IO_WANTREAD);
613 io_docallback((int)kev[i].ident, IO_WANTWRITE);
616 LogDebug("Unknown kev.filter number %d for fd %d",
617 kev[i].filter, kev[i].ident);
620 io_docallback((int)kev[i].ident, IO_ERROR);
629 io_library_init_kqueue(unsigned int eventsize)
631 io_masterfd = kqueue();
634 "IO subsystem: kqueue (initial maxfd %u, masterfd %d).",
635 eventsize, io_masterfd);
636 if (io_masterfd >= 0)
637 library_initialized = true;
641 io_library_init_kqueue(unsigned int UNUSED ev)
647 io_library_init(unsigned int eventsize)
649 if (library_initialized)
652 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
655 io_library_init_epoll(eventsize);
656 io_library_init_kqueue(eventsize);
657 io_library_init_devpoll(eventsize);
658 io_library_init_poll(eventsize);
659 io_library_init_select(eventsize);
661 return library_initialized;
666 io_library_shutdown(void)
672 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
673 if (io_masterfd >= 0)
678 array_free(&io_evcache);
680 library_initialized = false;
685 io_event_setcb(int fd, void (*cbfunc) (int, short))
687 io_event *i = io_event_get(fd);
691 i->callback = cbfunc;
697 backend_create_ev(int fd, short what)
700 #ifdef IO_USE_DEVPOLL
701 ret = io_event_change_devpoll(fd, what);
704 ret = io_event_change_poll(fd, what);
707 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
710 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
714 ret = io_event_add(fd, what);
721 io_event_create(int fd, short what, void (*cbfunc) (int, short))
727 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
728 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
730 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
735 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
738 "array_alloc failed: could not allocate space for %d io_event structures",
743 i->callback = cbfunc;
745 ret = backend_create_ev(fd, what);
753 io_event_add(int fd, short what)
755 io_event *i = io_event_get(fd);
757 if (!i) return false;
759 if ((i->what & what) == what) /* event type is already registered */
762 io_debug("io_event_add: fd, what", fd, what);
766 if (io_masterfd >= 0)
767 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
770 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
772 #ifdef IO_USE_DEVPOLL
773 return io_event_change_devpoll(fd, i->what);
776 return io_event_change_poll(fd, i->what);
779 if (fd > select_maxfd)
782 if (what & IO_WANTREAD)
783 FD_SET(fd, &readers);
784 if (what & IO_WANTWRITE)
785 FD_SET(fd, &writers);
794 io_setnonblock(int fd)
796 int flags = fcntl(fd, F_GETFL);
800 #define O_NONBLOCK O_NDELAY
804 return fcntl(fd, F_SETFL, flags) == 0;
808 io_setcloexec(int fd)
810 int flags = fcntl(fd, F_GETFD);
817 return fcntl(fd, F_SETFD, flags) == 0;
825 i = io_event_get(fd);
827 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
828 io_event_kqueue_commit_cache();
830 /* both kqueue and epoll remove fd from all sets automatically on the last close
831 * of the descriptor. since we don't know if this is the last close we'll have
832 * to remove the set explicitly. */
834 io_event_change_kqueue(fd, i->what, EV_DELETE);
835 io_event_kqueue_commit_cache();
838 io_close_devpoll(fd);
842 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
848 return close(fd) == 0;
853 io_event_del(int fd, short what)
855 io_event *i = io_event_get(fd);
857 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
858 if (!i) return false;
860 if (!(i->what & what)) /* event is already disabled */
864 #ifdef IO_USE_DEVPOLL
865 return io_event_change_devpoll(fd, i->what);
868 return io_event_change_poll(fd, i->what);
871 if (io_masterfd >= 0)
872 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
875 return io_event_change_kqueue(fd, what, EV_DISABLE);
878 if (what & IO_WANTWRITE)
879 FD_CLR(fd, &writers);
881 if (what & IO_WANTREAD)
882 FD_CLR(fd, &readers);
890 io_dispatch(struct timeval *tv)
893 if (io_masterfd >= 0)
894 return io_dispatch_epoll(tv);
897 return io_dispatch_select(tv);
900 return io_dispatch_kqueue(tv);
902 #ifdef IO_USE_DEVPOLL
903 return io_dispatch_devpoll(tv);
906 return io_dispatch_poll(tv);
912 /* call the callback function inside the struct matching fd */
914 io_docallback(int fd, short what)
916 io_event *i = io_event_get(fd);
918 io_debug("io_docallback; fd, what", fd, what);
920 if (i->callback) { /* callback might be NULL if a previous callback function
921 called io_close on this fd */
922 i->callback(fd, (what & IO_ERROR) ? i->what : what);
924 /* if error indicator is set, we return the event(s) that were registered */