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 "conn.h" /* for CONN_ID (needed by conf.h) */
109 #include "conf.h" /* for Conf_MaxConnections */
111 static fd_set readers;
112 static fd_set writers;
114 * this is the first argument for select(), i.e.
115 * the largest fd registered, plus one.
117 static int select_maxfd;
118 static int io_dispatch_select PARAMS((struct timeval *tv));
121 #define io_masterfd -1
123 #endif /* IO_USE_SELECT */
125 static array io_events;
127 static void io_docallback PARAMS((int fd, short what));
131 io_debug(const char *s, int fd, int what)
133 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
137 io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
148 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
156 #ifdef IO_USE_DEVPOLL
158 io_dispatch_devpoll(struct timeval *tv)
161 time_t sec = tv->tv_sec * 1000;
162 int i, total, ret, timeout = tv->tv_usec + sec;
164 struct pollfd p[100];
171 dvp.dp_timeout = timeout;
174 ret = ioctl(io_masterfd, DP_POLL, &dvp);
178 for (i=0; i < ret ; i++) {
180 if (p[i].revents & (POLLIN|POLLPRI))
183 if (p[i].revents & POLLOUT)
184 what |= IO_WANTWRITE;
186 if (p[i].revents && !what) {
187 /* other flag is set, probably POLLERR */
190 io_docallback(p[i].fd, what);
192 } while (ret == 100);
199 io_event_change_devpoll(int fd, short what)
205 if (what & IO_WANTREAD)
206 p.events = POLLIN | POLLPRI;
207 if (what & IO_WANTWRITE)
211 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
215 io_close_devpoll(int fd)
218 p.events = POLLREMOVE;
220 write(io_masterfd, &p, sizeof p);
224 io_library_init_devpoll(unsigned int eventsize)
226 io_masterfd = open("/dev/poll", O_RDWR);
227 if (io_masterfd >= 0)
228 library_initialized = true;
229 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
230 eventsize, io_masterfd);
234 io_close_devpoll(int UNUSED x)
237 io_library_init_devpoll(unsigned int UNUSED ev)
244 io_dispatch_poll(struct timeval *tv)
246 time_t sec = tv->tv_sec * 1000;
247 int i, ret, timeout = tv->tv_usec + sec;
250 struct pollfd *p = array_start(&pollfds);
255 ret = poll(p, poll_maxfd + 1, timeout);
260 for (i=0; i <= poll_maxfd; i++) {
262 if (p[i].revents & (POLLIN|POLLPRI))
265 if (p[i].revents & POLLOUT)
266 what |= IO_WANTWRITE;
268 if (p[i].revents && !what) {
269 /* other flag is set, probably POLLERR */
274 io_docallback(i, what);
284 io_event_change_poll(int fd, short what)
289 if (what & IO_WANTREAD)
290 events = POLLIN | POLLPRI;
291 if (what & IO_WANTWRITE)
294 p = array_alloc(&pollfds, sizeof *p, fd);
305 io_close_poll(int fd)
308 p = array_get(&pollfds, sizeof *p, fd);
312 if (fd == poll_maxfd) {
313 while (poll_maxfd > 0) {
315 p = array_get(&pollfds, sizeof *p, poll_maxfd);
323 io_library_init_poll(unsigned int eventsize)
326 array_init(&pollfds);
328 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
330 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
333 p = array_start(&pollfds);
334 for (i = 0; i < eventsize; i++)
337 library_initialized = true;
342 io_close_poll(int UNUSED x)
345 io_library_init_poll(unsigned int UNUSED ev)
352 io_dispatch_select(struct timeval *tv)
360 readers_tmp = readers;
361 writers_tmp = writers;
363 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
369 for (i = 0; i <= select_maxfd; i++) {
371 if (FD_ISSET(i, &readers_tmp)) {
376 if (FD_ISSET(i, &writers_tmp)) {
377 what |= IO_WANTWRITE;
381 io_docallback(i, what);
390 io_library_init_select(unsigned int eventsize)
392 if (library_initialized)
394 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
399 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
401 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
402 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
404 Conf_MaxConnections = FD_SETSIZE - 1;
408 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
409 #endif /* FD_SETSIZE */
410 library_initialized = true;
414 io_close_select(int fd)
418 if (io_masterfd >= 0) /* Are we using epoll()? */
421 FD_CLR(fd, &writers);
422 FD_CLR(fd, &readers);
424 i = io_event_get(fd);
427 if (fd == select_maxfd) {
428 while (select_maxfd>0) {
429 --select_maxfd; /* find largest fd */
430 i = io_event_get(select_maxfd);
431 if (i && i->callback) break;
437 io_library_init_select(int UNUSED x)
440 io_close_select(int UNUSED x)
447 io_event_change_epoll(int fd, short what, const int action)
449 struct epoll_event ev = { 0, {0} };
452 if (what & IO_WANTREAD)
453 ev.events = EPOLLIN | EPOLLPRI;
454 if (what & IO_WANTWRITE)
455 ev.events |= EPOLLOUT;
457 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
461 io_dispatch_epoll(struct timeval *tv)
463 time_t sec = tv->tv_sec * 1000;
464 int i, total = 0, ret, timeout = tv->tv_usec + sec;
465 struct epoll_event epoll_ev[100];
472 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
477 for (i = 0; i < ret; i++) {
479 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
482 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
485 if (epoll_ev[i].events & EPOLLOUT)
486 type |= IO_WANTWRITE;
488 io_docallback(epoll_ev[i].data.fd, type);
492 } while (ret == 100);
498 io_library_init_epoll(unsigned int eventsize)
500 int ecreate_hint = (int)eventsize;
501 if (ecreate_hint <= 0)
503 io_masterfd = epoll_create(ecreate_hint);
504 if (io_masterfd >= 0) {
505 library_initialized = true;
507 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
508 ecreate_hint, eventsize, io_masterfd);
512 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
517 io_library_init_epoll(unsigned int UNUSED ev)
519 #endif /* IO_USE_EPOLL */
524 io_event_kqueue_commit_cache(void)
526 struct kevent *events;
528 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
530 if (!len) /* nothing to do */
536 array_free(&io_evcache);
540 events = array_start(&io_evcache);
542 assert(events != NULL);
544 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
546 array_trunc(&io_evcache);
551 io_event_change_kqueue(int fd, short what, const int action)
556 if (what & IO_WANTREAD) {
557 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
558 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
560 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
563 if (ret && (what & IO_WANTWRITE)) {
564 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
565 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
567 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
570 if (array_length(&io_evcache, sizeof kev) >= 100)
571 io_event_kqueue_commit_cache();
576 io_dispatch_kqueue(struct timeval *tv)
578 int i, total = 0, ret;
579 struct kevent kev[100];
580 struct kevent *newevents;
583 ts.tv_sec = tv->tv_sec;
584 ts.tv_nsec = tv->tv_usec * 1000;
587 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
588 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
589 assert(newevents_len >= 0);
591 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
592 if (newevents && ret != -1)
593 array_trunc(&io_evcache);
599 for (i = 0; i < ret; i++) {
600 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
601 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
602 if (kev[i].flags & EV_ERROR)
603 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
604 (int)kev[i].ident, strerror((int)kev[i].data));
605 io_docallback((int)kev[i].ident, IO_ERROR);
609 switch (kev[i].filter) {
611 io_docallback((int)kev[i].ident, IO_WANTREAD);
614 io_docallback((int)kev[i].ident, IO_WANTWRITE);
617 LogDebug("Unknown kev.filter number %d for fd %d",
618 kev[i].filter, kev[i].ident);
621 io_docallback((int)kev[i].ident, IO_ERROR);
627 } while (ret == 100);
633 io_library_init_kqueue(unsigned int eventsize)
635 io_masterfd = kqueue();
638 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
639 eventsize, io_masterfd);
640 if (io_masterfd >= 0)
641 library_initialized = true;
645 io_library_init_kqueue(unsigned int UNUSED ev)
651 io_library_init(unsigned int eventsize)
653 if (library_initialized)
656 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
659 io_library_init_epoll(eventsize);
660 io_library_init_kqueue(eventsize);
661 io_library_init_devpoll(eventsize);
662 io_library_init_poll(eventsize);
663 io_library_init_select(eventsize);
665 return library_initialized;
670 io_library_shutdown(void)
676 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
677 if (io_masterfd >= 0)
682 array_free(&io_evcache);
684 library_initialized = false;
689 io_event_setcb(int fd, void (*cbfunc) (int, short))
691 io_event *i = io_event_get(fd);
695 i->callback = cbfunc;
701 backend_create_ev(int fd, short what)
704 #ifdef IO_USE_DEVPOLL
705 ret = io_event_change_devpoll(fd, what);
708 ret = io_event_change_poll(fd, what);
711 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
714 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
718 ret = io_event_add(fd, what);
725 io_event_create(int fd, short what, void (*cbfunc) (int, short))
731 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
732 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
734 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
739 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
742 "array_alloc failed: could not allocate space for %d io_event structures",
747 i->callback = cbfunc;
749 ret = backend_create_ev(fd, what);
757 io_event_add(int fd, short what)
759 io_event *i = io_event_get(fd);
761 if (!i) return false;
763 if ((i->what & what) == what) /* event type is already registered */
766 io_debug("io_event_add: fd, what", fd, what);
770 if (io_masterfd >= 0)
771 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
774 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
776 #ifdef IO_USE_DEVPOLL
777 return io_event_change_devpoll(fd, i->what);
780 return io_event_change_poll(fd, i->what);
783 if (fd > select_maxfd)
786 if (what & IO_WANTREAD)
787 FD_SET(fd, &readers);
788 if (what & IO_WANTWRITE)
789 FD_SET(fd, &writers);
798 io_setnonblock(int fd)
800 int flags = fcntl(fd, F_GETFL);
804 #define O_NONBLOCK O_NDELAY
808 return fcntl(fd, F_SETFL, flags) == 0;
812 io_setcloexec(int fd)
814 int flags = fcntl(fd, F_GETFD);
821 return fcntl(fd, F_SETFD, flags) == 0;
829 i = io_event_get(fd);
831 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
832 io_event_kqueue_commit_cache();
834 /* both kqueue and epoll remove fd from all sets automatically on the last close
835 * of the descriptor. since we don't know if this is the last close we'll have
836 * to remove the set explicitly. */
838 io_event_change_kqueue(fd, i->what, EV_DELETE);
839 io_event_kqueue_commit_cache();
842 io_close_devpoll(fd);
846 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
852 return close(fd) == 0;
857 io_event_del(int fd, short what)
859 io_event *i = io_event_get(fd);
861 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
862 if (!i) return false;
864 if (!(i->what & what)) /* event is already disabled */
868 #ifdef IO_USE_DEVPOLL
869 return io_event_change_devpoll(fd, i->what);
872 return io_event_change_poll(fd, i->what);
875 if (io_masterfd >= 0)
876 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
879 return io_event_change_kqueue(fd, what, EV_DISABLE);
882 if (what & IO_WANTWRITE)
883 FD_CLR(fd, &writers);
885 if (what & IO_WANTREAD)
886 FD_CLR(fd, &readers);
894 io_dispatch(struct timeval *tv)
897 if (io_masterfd >= 0)
898 return io_dispatch_epoll(tv);
901 return io_dispatch_select(tv);
904 return io_dispatch_kqueue(tv);
906 #ifdef IO_USE_DEVPOLL
907 return io_dispatch_devpoll(tv);
910 return io_dispatch_poll(tv);
916 /* call the callback function inside the struct matching fd */
918 io_docallback(int fd, short what)
920 io_event *i = io_event_get(fd);
922 io_debug("io_docallback; fd, what", fd, what);
924 if (i->callback) { /* callback might be NULL if a previous callback function
925 called io_close on this fd */
926 i->callback(fd, (what & IO_ERROR) ? i->what : what);
928 /* if error indicator is set, we return the event(s) that were registered */