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 }
45 #ifdef HAVE_EPOLL_CREATE
46 # define IO_USE_EPOLL 1
48 # define IO_USE_SELECT 1
52 # define IO_USE_KQUEUE 1
54 # ifdef HAVE_SYS_DEVPOLL_H
55 # define IO_USE_DEVPOLL 1
57 # if defined(HAVE_POLL) && defined(HAVE_POLL_H)
58 # define IO_USE_POLL 1
61 # define IO_USE_SELECT 1
63 # error "no IO API available!?"
64 # endif /* HAVE_SELECT */
65 # endif /* HAVE_POLL */
66 # endif /* HAVE_SYS_DEVPOLL_H */
67 # endif /* HAVE_KQUEUE */
68 #endif /* HAVE_EPOLL_CREATE */
70 static bool library_initialized = false;
73 #include <sys/epoll.h>
75 static int io_masterfd = -1;
76 static bool io_event_change_epoll(int fd, short what, const int action);
77 static int io_dispatch_epoll(struct timeval *tv);
81 #include <sys/types.h>
82 #include <sys/event.h>
83 static array io_evcache;
84 static int io_masterfd;
86 static int io_dispatch_kqueue(struct timeval *tv);
87 static bool io_event_change_kqueue(int, short, const int action);
94 static int poll_maxfd;
96 static bool io_event_change_poll PARAMS((int fd, short what));
100 #include <sys/devpoll.h>
101 static int io_masterfd;
103 static bool io_event_change_devpoll(int fd, short what);
107 #include "defines.h" /* for conn.h */
108 #include "proc.h" /* for PROC_STAT (needed by conf.h) */
109 #include "conn.h" /* for CONN_ID (needed by conf.h) */
110 #include "conf.h" /* for Conf_MaxConnections */
112 static fd_set readers;
113 static fd_set writers;
115 * this is the first argument for select(), i.e.
116 * the largest fd registered, plus one.
118 static int select_maxfd;
119 static int io_dispatch_select PARAMS((struct timeval *tv));
122 #define io_masterfd -1
124 #endif /* IO_USE_SELECT */
126 static array io_events;
128 static void io_docallback PARAMS((int fd, short what));
132 io_debug(const char *s, int fd, int what)
134 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
138 io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
149 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
157 #ifdef IO_USE_DEVPOLL
159 io_dispatch_devpoll(struct timeval *tv)
162 time_t sec = tv->tv_sec * 1000;
163 int i, ret, timeout = tv->tv_usec + sec;
165 struct pollfd p[100];
170 dvp.dp_timeout = timeout;
173 ret = ioctl(io_masterfd, DP_POLL, &dvp);
175 for (i=0; i < ret ; i++) {
177 if (p[i].revents & (POLLIN|POLLPRI))
180 if (p[i].revents & POLLOUT)
181 what |= IO_WANTWRITE;
183 if (p[i].revents && !what) {
184 /* other flag is set, probably POLLERR */
187 io_docallback(p[i].fd, what);
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);
230 io_close_devpoll(int UNUSED x)
233 io_library_init_devpoll(unsigned int UNUSED ev)
240 io_dispatch_poll(struct timeval *tv)
242 time_t sec = tv->tv_sec * 1000;
243 int i, ret, timeout = tv->tv_usec + sec;
246 struct pollfd *p = array_start(&pollfds);
251 ret = poll(p, poll_maxfd + 1, timeout);
256 for (i=0; i <= poll_maxfd; i++) {
258 if (p[i].revents & (POLLIN|POLLPRI))
261 if (p[i].revents & POLLOUT)
262 what |= IO_WANTWRITE;
264 if (p[i].revents && !what) {
265 /* other flag is set, probably POLLERR */
270 io_docallback(i, what);
280 io_event_change_poll(int fd, short what)
285 if (what & IO_WANTREAD)
286 events = POLLIN | POLLPRI;
287 if (what & IO_WANTWRITE)
290 p = array_alloc(&pollfds, sizeof *p, fd);
301 io_close_poll(int fd)
304 p = array_get(&pollfds, sizeof *p, fd);
308 if (fd == poll_maxfd) {
309 while (poll_maxfd > 0) {
311 p = array_get(&pollfds, sizeof *p, poll_maxfd);
319 io_library_init_poll(unsigned int eventsize)
322 array_init(&pollfds);
324 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
326 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
329 p = array_start(&pollfds);
330 for (i = 0; i < eventsize; i++)
333 library_initialized = true;
338 io_close_poll(int UNUSED x)
341 io_library_init_poll(unsigned int UNUSED ev)
348 io_dispatch_select(struct timeval *tv)
356 readers_tmp = readers;
357 writers_tmp = writers;
359 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
365 for (i = 0; i <= select_maxfd; i++) {
367 if (FD_ISSET(i, &readers_tmp)) {
372 if (FD_ISSET(i, &writers_tmp)) {
373 what |= IO_WANTWRITE;
377 io_docallback(i, what);
386 io_library_init_select(unsigned int eventsize)
388 if (library_initialized)
390 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
395 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
397 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
398 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
400 Conf_MaxConnections = FD_SETSIZE - 1;
404 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
405 #endif /* FD_SETSIZE */
406 library_initialized = true;
410 io_close_select(int fd)
414 if (io_masterfd >= 0) /* Are we using epoll()? */
417 FD_CLR(fd, &writers);
418 FD_CLR(fd, &readers);
420 i = io_event_get(fd);
423 if (fd == select_maxfd) {
424 while (select_maxfd>0) {
425 --select_maxfd; /* find largest fd */
426 i = io_event_get(select_maxfd);
427 if (i && i->callback) break;
433 io_library_init_select(int UNUSED x)
436 io_close_select(int UNUSED x)
443 io_event_change_epoll(int fd, short what, const int action)
445 struct epoll_event ev = { 0, {0} };
448 if (what & IO_WANTREAD)
449 ev.events = EPOLLIN | EPOLLPRI;
450 if (what & IO_WANTWRITE)
451 ev.events |= EPOLLOUT;
453 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
457 io_dispatch_epoll(struct timeval *tv)
459 time_t sec = tv->tv_sec * 1000;
460 int i, ret, timeout = tv->tv_usec + sec;
461 struct epoll_event epoll_ev[100];
467 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
469 for (i = 0; i < ret; i++) {
471 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
474 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
477 if (epoll_ev[i].events & EPOLLOUT)
478 type |= IO_WANTWRITE;
480 io_docallback(epoll_ev[i].data.fd, type);
487 io_library_init_epoll(unsigned int eventsize)
489 int ecreate_hint = (int)eventsize;
490 if (ecreate_hint <= 0)
492 io_masterfd = epoll_create(ecreate_hint);
493 if (io_masterfd >= 0) {
494 library_initialized = true;
496 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
497 ecreate_hint, eventsize, io_masterfd);
501 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
506 io_library_init_epoll(unsigned int UNUSED ev)
508 #endif /* IO_USE_EPOLL */
513 io_event_kqueue_commit_cache(void)
515 struct kevent *events;
517 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
519 if (!len) /* nothing to do */
525 array_free(&io_evcache);
529 events = array_start(&io_evcache);
531 assert(events != NULL);
533 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
535 array_trunc(&io_evcache);
540 io_event_change_kqueue(int fd, short what, const int action)
545 if (what & IO_WANTREAD) {
546 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
547 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
549 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
552 if (ret && (what & IO_WANTWRITE)) {
553 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
554 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
556 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
559 if (array_length(&io_evcache, sizeof kev) >= 100)
560 io_event_kqueue_commit_cache();
565 io_dispatch_kqueue(struct timeval *tv)
568 struct kevent kev[100];
569 struct kevent *newevents;
572 ts.tv_sec = tv->tv_sec;
573 ts.tv_nsec = tv->tv_usec * 1000;
575 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
576 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
577 assert(newevents_len >= 0);
579 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
580 if (newevents && ret != -1)
581 array_trunc(&io_evcache);
583 for (i = 0; i < ret; i++) {
584 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
585 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
586 if (kev[i].flags & EV_ERROR)
587 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
588 (int)kev[i].ident, strerror((int)kev[i].data));
589 io_docallback((int)kev[i].ident, IO_ERROR);
593 switch (kev[i].filter) {
595 io_docallback((int)kev[i].ident, IO_WANTREAD);
598 io_docallback((int)kev[i].ident, IO_WANTWRITE);
601 LogDebug("Unknown kev.filter number %d for fd %d",
602 kev[i].filter, kev[i].ident);
605 io_docallback((int)kev[i].ident, IO_ERROR);
614 io_library_init_kqueue(unsigned int eventsize)
616 io_masterfd = kqueue();
619 "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
620 eventsize, io_masterfd);
621 if (io_masterfd >= 0)
622 library_initialized = true;
626 io_library_init_kqueue(unsigned int UNUSED ev)
632 io_library_init(unsigned int eventsize)
634 if (library_initialized)
637 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
640 io_library_init_epoll(eventsize);
641 io_library_init_kqueue(eventsize);
642 io_library_init_devpoll(eventsize);
643 io_library_init_poll(eventsize);
644 io_library_init_select(eventsize);
646 return library_initialized;
651 io_library_shutdown(void)
657 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
658 if (io_masterfd >= 0)
663 array_free(&io_evcache);
665 library_initialized = false;
670 io_event_setcb(int fd, void (*cbfunc) (int, short))
672 io_event *i = io_event_get(fd);
676 i->callback = cbfunc;
682 backend_create_ev(int fd, short what)
685 #ifdef IO_USE_DEVPOLL
686 ret = io_event_change_devpoll(fd, what);
689 ret = io_event_change_poll(fd, what);
692 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
695 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
699 ret = io_event_add(fd, what);
706 io_event_create(int fd, short what, void (*cbfunc) (int, short))
712 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
713 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
715 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
720 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
723 "array_alloc failed: could not allocate space for %d io_event structures",
728 i->callback = cbfunc;
730 ret = backend_create_ev(fd, what);
738 io_event_add(int fd, short what)
740 io_event *i = io_event_get(fd);
742 if (!i) return false;
744 if ((i->what & what) == what) /* event type is already registered */
747 io_debug("io_event_add: fd, what", fd, what);
751 if (io_masterfd >= 0)
752 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
755 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
757 #ifdef IO_USE_DEVPOLL
758 return io_event_change_devpoll(fd, i->what);
761 return io_event_change_poll(fd, i->what);
764 if (fd > select_maxfd)
767 if (what & IO_WANTREAD)
768 FD_SET(fd, &readers);
769 if (what & IO_WANTWRITE)
770 FD_SET(fd, &writers);
779 io_setnonblock(int fd)
781 int flags = fcntl(fd, F_GETFL);
785 #define O_NONBLOCK O_NDELAY
789 return fcntl(fd, F_SETFL, flags) == 0;
793 io_setcloexec(int fd)
795 int flags = fcntl(fd, F_GETFD);
802 return fcntl(fd, F_SETFD, flags) == 0;
810 i = io_event_get(fd);
812 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
813 io_event_kqueue_commit_cache();
815 /* both kqueue and epoll remove fd from all sets automatically on the last close
816 * of the descriptor. since we don't know if this is the last close we'll have
817 * to remove the set explicitly. */
819 io_event_change_kqueue(fd, i->what, EV_DELETE);
820 io_event_kqueue_commit_cache();
823 io_close_devpoll(fd);
827 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
833 return close(fd) == 0;
838 io_event_del(int fd, short what)
840 io_event *i = io_event_get(fd);
842 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
843 if (!i) return false;
845 if (!(i->what & what)) /* event is already disabled */
849 #ifdef IO_USE_DEVPOLL
850 return io_event_change_devpoll(fd, i->what);
853 return io_event_change_poll(fd, i->what);
856 if (io_masterfd >= 0)
857 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
860 return io_event_change_kqueue(fd, what, EV_DISABLE);
863 if (what & IO_WANTWRITE)
864 FD_CLR(fd, &writers);
866 if (what & IO_WANTREAD)
867 FD_CLR(fd, &readers);
875 io_dispatch(struct timeval *tv)
878 if (io_masterfd >= 0)
879 return io_dispatch_epoll(tv);
882 return io_dispatch_select(tv);
885 return io_dispatch_kqueue(tv);
887 #ifdef IO_USE_DEVPOLL
888 return io_dispatch_devpoll(tv);
891 return io_dispatch_poll(tv);
897 /* call the callback function inside the struct matching fd */
899 io_docallback(int fd, short what)
901 io_event *i = io_event_get(fd);
903 io_debug("io_docallback; fd, what", fd, what);
905 if (i->callback) { /* callback might be NULL if a previous callback function
906 called io_close on this fd */
907 i->callback(fd, (what & IO_ERROR) ? i->what : what);
909 /* if error indicator is set, we return the event(s) that were registered */