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)
16 * I/O abstraction interface.
23 #include <sys/types.h>
31 /* Enables extra debug messages in event add/delete/callback code. */
32 /* #define DEBUG_IO */
36 void (*callback)(int, short);
43 #define INIT_IOEVENT { NULL, -1, 0, NULL }
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
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);
95 static int poll_maxfd;
97 static bool io_event_change_poll PARAMS((int fd, short what));
100 #ifdef IO_USE_DEVPOLL
101 #include <sys/devpoll.h>
102 static int io_masterfd;
104 static bool io_event_change_devpoll(int fd, short what);
108 #include "defines.h" /* for conn.h */
109 #include "proc.h" /* for PROC_STAT (needed by conf.h) */
110 #include "conn.h" /* for CONN_ID (needed by conf.h) */
111 #include "conf.h" /* for Conf_MaxConnections */
113 static fd_set readers;
114 static fd_set writers;
116 * this is the first argument for select(), i.e.
117 * the largest fd registered, plus one.
119 static int select_maxfd;
120 static int io_dispatch_select PARAMS((struct timeval *tv));
123 #define io_masterfd -1
125 #endif /* IO_USE_SELECT */
127 static array io_events;
129 static void io_docallback PARAMS((int fd, short what));
133 io_debug(const char *s, int fd, int what)
135 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
139 io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
150 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
158 #ifdef IO_USE_DEVPOLL
160 io_dispatch_devpoll(struct timeval *tv)
163 time_t sec = tv->tv_sec * 1000;
164 int i, total, ret, timeout = tv->tv_usec + sec;
166 struct pollfd p[100];
173 dvp.dp_timeout = timeout;
176 ret = ioctl(io_masterfd, DP_POLL, &dvp);
180 for (i=0; i < ret ; i++) {
182 if (p[i].revents & (POLLIN|POLLPRI))
185 if (p[i].revents & POLLOUT)
186 what |= IO_WANTWRITE;
188 if (p[i].revents && !what) {
189 /* other flag is set, probably POLLERR */
192 io_docallback(p[i].fd, what);
194 } while (ret == 100);
201 io_event_change_devpoll(int fd, short what)
207 if (what & IO_WANTREAD)
208 p.events = POLLIN | POLLPRI;
209 if (what & IO_WANTWRITE)
213 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
217 io_close_devpoll(int fd)
220 p.events = POLLREMOVE;
222 write(io_masterfd, &p, sizeof p);
226 io_library_init_devpoll(unsigned int eventsize)
228 io_masterfd = open("/dev/poll", O_RDWR);
229 if (io_masterfd >= 0)
230 library_initialized = true;
231 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
232 eventsize, io_masterfd);
236 io_close_devpoll(int UNUSED x)
239 io_library_init_devpoll(unsigned int UNUSED ev)
246 io_dispatch_poll(struct timeval *tv)
248 time_t sec = tv->tv_sec * 1000;
249 int i, ret, timeout = tv->tv_usec + sec;
252 struct pollfd *p = array_start(&pollfds);
257 ret = poll(p, poll_maxfd + 1, timeout);
262 for (i=0; i <= poll_maxfd; i++) {
264 if (p[i].revents & (POLLIN|POLLPRI))
267 if (p[i].revents & POLLOUT)
268 what |= IO_WANTWRITE;
270 if (p[i].revents && !what) {
271 /* other flag is set, probably POLLERR */
276 io_docallback(i, what);
286 io_event_change_poll(int fd, short what)
291 if (what & IO_WANTREAD)
292 events = POLLIN | POLLPRI;
293 if (what & IO_WANTWRITE)
296 p = array_alloc(&pollfds, sizeof *p, fd);
307 io_close_poll(int fd)
310 p = array_get(&pollfds, sizeof *p, fd);
314 if (fd == poll_maxfd) {
315 while (poll_maxfd > 0) {
317 p = array_get(&pollfds, sizeof *p, poll_maxfd);
325 io_library_init_poll(unsigned int eventsize)
328 array_init(&pollfds);
330 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
332 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
335 p = array_start(&pollfds);
336 for (i = 0; i < eventsize; i++)
339 library_initialized = true;
344 io_close_poll(int UNUSED x)
347 io_library_init_poll(unsigned int UNUSED ev)
354 io_dispatch_select(struct timeval *tv)
362 readers_tmp = readers;
363 writers_tmp = writers;
365 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
371 for (i = 0; i <= select_maxfd; i++) {
373 if (FD_ISSET(i, &readers_tmp)) {
378 if (FD_ISSET(i, &writers_tmp)) {
379 what |= IO_WANTWRITE;
383 io_docallback(i, what);
392 io_library_init_select(unsigned int eventsize)
394 if (library_initialized)
396 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
401 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
403 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
404 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
406 Conf_MaxConnections = FD_SETSIZE - 1;
410 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
411 #endif /* FD_SETSIZE */
412 library_initialized = true;
416 io_close_select(int fd)
420 if (io_masterfd >= 0) /* Are we using epoll()? */
423 FD_CLR(fd, &writers);
424 FD_CLR(fd, &readers);
426 i = io_event_get(fd);
429 if (fd == select_maxfd) {
430 while (select_maxfd>0) {
431 --select_maxfd; /* find largest fd */
432 i = io_event_get(select_maxfd);
433 if (i && i->callback) break;
439 io_library_init_select(int UNUSED x)
442 io_close_select(int UNUSED x)
449 io_event_change_epoll(int fd, short what, const int action)
451 struct epoll_event ev = { 0, {0} };
454 if (what & IO_WANTREAD)
455 ev.events = EPOLLIN | EPOLLPRI;
456 if (what & IO_WANTWRITE)
457 ev.events |= EPOLLOUT;
459 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
463 io_dispatch_epoll(struct timeval *tv)
465 time_t sec = tv->tv_sec * 1000;
466 int i, total = 0, ret, timeout = tv->tv_usec + sec;
467 struct epoll_event epoll_ev[100];
474 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
479 for (i = 0; i < ret; i++) {
481 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
484 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
487 if (epoll_ev[i].events & EPOLLOUT)
488 type |= IO_WANTWRITE;
490 io_docallback(epoll_ev[i].data.fd, type);
494 } while (ret == 100);
500 io_library_init_epoll(unsigned int eventsize)
502 int ecreate_hint = (int)eventsize;
503 if (ecreate_hint <= 0)
505 io_masterfd = epoll_create(ecreate_hint);
506 if (io_masterfd >= 0) {
507 library_initialized = true;
509 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
510 ecreate_hint, eventsize, io_masterfd);
514 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
519 io_library_init_epoll(unsigned int UNUSED ev)
521 #endif /* IO_USE_EPOLL */
526 io_event_kqueue_commit_cache(void)
528 struct kevent *events;
530 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
532 if (!len) /* nothing to do */
538 array_free(&io_evcache);
542 events = array_start(&io_evcache);
544 assert(events != NULL);
546 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
548 array_trunc(&io_evcache);
553 io_event_change_kqueue(int fd, short what, const int action)
558 if (what & IO_WANTREAD) {
559 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
560 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
562 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
565 if (ret && (what & IO_WANTWRITE)) {
566 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
567 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
569 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
572 if (array_length(&io_evcache, sizeof kev) >= 100)
573 io_event_kqueue_commit_cache();
578 io_dispatch_kqueue(struct timeval *tv)
580 int i, total = 0, ret;
581 struct kevent kev[100];
582 struct kevent *newevents;
585 ts.tv_sec = tv->tv_sec;
586 ts.tv_nsec = tv->tv_usec * 1000;
589 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
590 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
591 assert(newevents_len >= 0);
593 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
594 if (newevents && ret != -1)
595 array_trunc(&io_evcache);
601 for (i = 0; i < ret; i++) {
602 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
603 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
604 if (kev[i].flags & EV_ERROR)
605 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
606 (int)kev[i].ident, strerror((int)kev[i].data));
607 io_docallback((int)kev[i].ident, IO_ERROR);
611 switch (kev[i].filter) {
613 io_docallback((int)kev[i].ident, IO_WANTREAD);
616 io_docallback((int)kev[i].ident, IO_WANTWRITE);
619 LogDebug("Unknown kev.filter number %d for fd %d",
620 kev[i].filter, kev[i].ident);
623 io_docallback((int)kev[i].ident, IO_ERROR);
629 } while (ret == 100);
635 io_library_init_kqueue(unsigned int eventsize)
637 io_masterfd = kqueue();
640 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
641 eventsize, io_masterfd);
642 if (io_masterfd >= 0)
643 library_initialized = true;
647 io_library_init_kqueue(unsigned int UNUSED ev)
653 io_library_init(unsigned int eventsize)
655 if (library_initialized)
658 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
661 io_library_init_epoll(eventsize);
662 io_library_init_kqueue(eventsize);
663 io_library_init_devpoll(eventsize);
664 io_library_init_poll(eventsize);
665 io_library_init_select(eventsize);
667 return library_initialized;
672 io_library_shutdown(void)
678 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
679 if (io_masterfd >= 0)
684 array_free(&io_evcache);
686 library_initialized = false;
691 io_event_setcb(int fd, void (*cbfunc) (int, short))
693 io_event *i = io_event_get(fd);
697 i->callback = cbfunc;
703 backend_create_ev(int fd, short what)
706 #ifdef IO_USE_DEVPOLL
707 ret = io_event_change_devpoll(fd, what);
710 ret = io_event_change_poll(fd, what);
713 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
716 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
720 ret = io_event_add(fd, what);
727 io_event_create(int fd, short what, void (*cbfunc) (int, short))
733 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
734 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
736 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
741 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
744 "array_alloc failed: could not allocate space for %d io_event structures",
749 i->callback = cbfunc;
751 ret = backend_create_ev(fd, what);
759 io_event_add(int fd, short what)
761 io_event *i = io_event_get(fd);
763 if (!i) return false;
765 if ((i->what & what) == what) /* event type is already registered */
768 io_debug("io_event_add: fd, what", fd, what);
772 if (io_masterfd >= 0)
773 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
776 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
778 #ifdef IO_USE_DEVPOLL
779 return io_event_change_devpoll(fd, i->what);
782 return io_event_change_poll(fd, i->what);
785 if (fd > select_maxfd)
788 if (what & IO_WANTREAD)
789 FD_SET(fd, &readers);
790 if (what & IO_WANTWRITE)
791 FD_SET(fd, &writers);
800 io_setnonblock(int fd)
802 int flags = fcntl(fd, F_GETFL);
806 #define O_NONBLOCK O_NDELAY
810 return fcntl(fd, F_SETFL, flags) == 0;
814 io_setcloexec(int fd)
816 int flags = fcntl(fd, F_GETFD);
823 return fcntl(fd, F_SETFD, flags) == 0;
831 i = io_event_get(fd);
833 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
834 io_event_kqueue_commit_cache();
836 /* both kqueue and epoll remove fd from all sets automatically on the last close
837 * of the descriptor. since we don't know if this is the last close we'll have
838 * to remove the set explicitly. */
840 io_event_change_kqueue(fd, i->what, EV_DELETE);
841 io_event_kqueue_commit_cache();
844 io_close_devpoll(fd);
848 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
854 return close(fd) == 0;
859 io_event_del(int fd, short what)
861 io_event *i = io_event_get(fd);
863 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
864 if (!i) return false;
866 if (!(i->what & what)) /* event is already disabled */
870 #ifdef IO_USE_DEVPOLL
871 return io_event_change_devpoll(fd, i->what);
874 return io_event_change_poll(fd, i->what);
877 if (io_masterfd >= 0)
878 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
881 return io_event_change_kqueue(fd, what, EV_DISABLE);
884 if (what & IO_WANTWRITE)
885 FD_CLR(fd, &writers);
887 if (what & IO_WANTREAD)
888 FD_CLR(fd, &readers);
896 io_dispatch(struct timeval *tv)
899 if (io_masterfd >= 0)
900 return io_dispatch_epoll(tv);
903 return io_dispatch_select(tv);
906 return io_dispatch_kqueue(tv);
908 #ifdef IO_USE_DEVPOLL
909 return io_dispatch_devpoll(tv);
912 return io_dispatch_poll(tv);
918 /* call the callback function inside the struct matching fd */
920 io_docallback(int fd, short what)
922 io_event *i = io_event_get(fd);
924 io_debug("io_docallback; fd, what", fd, what);
926 if (i->callback) { /* callback might be NULL if a previous callback function
927 called io_close on this fd */
928 i->callback(fd, (what & IO_ERROR) ? i->what : what);
930 /* if error indicator is set, we return the event(s) that were registered */