2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2005-2006 Florian Westphal (westphal@foo.fh-furtwangen.de)
4 * Copyright (c)2006-2014 Alexander Barton (alex@barton.de) and Contributors.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * Please read the file COPYING, README and AUTHORS for more information.
17 * I/O abstraction interface.
20 /* Extra debug messages in event add/delete/callback code: 0=off / 1=on */
25 #include <sys/types.h>
36 void (*callback)(int, short);
43 #define INIT_IOEVENT { NULL, -1, 0, NULL }
45 #define MAX_EVENTS 100
47 #ifdef HAVE_EPOLL_CREATE
48 # define IO_USE_EPOLL 1
50 # define IO_USE_SELECT 1
54 # define IO_USE_KQUEUE 1
56 # ifdef HAVE_SYS_DEVPOLL_H
57 # define IO_USE_DEVPOLL 1
59 # if defined(HAVE_POLL) && defined(HAVE_POLL_H)
60 # define IO_USE_POLL 1
63 # define IO_USE_SELECT 1
65 # error "no IO API available!?"
66 # endif /* HAVE_SELECT */
67 # endif /* HAVE_POLL */
68 # endif /* HAVE_SYS_DEVPOLL_H */
69 # endif /* HAVE_KQUEUE */
70 #endif /* HAVE_EPOLL_CREATE */
72 static bool library_initialized = false;
75 #include <sys/epoll.h>
77 static int io_masterfd = -1;
78 static bool io_event_change_epoll(int fd, short what, const int action);
79 static int io_dispatch_epoll(struct timeval *tv);
83 #include <sys/types.h>
84 #include <sys/event.h>
85 static array io_evcache;
86 static int io_masterfd;
88 static int io_dispatch_kqueue(struct timeval *tv);
89 static bool io_event_change_kqueue(int, short, const int action);
92 /* Taken from /usr/include/sys/event.h of FreeBSD 8.1 and required by all
93 * platforms that have kqueue but lack EV_SET() -- for example FreeBSD 4. */
94 #define EV_SET(kevp, a, b, c, d, e, f) do { \
95 struct kevent *__kevp__ = (kevp); \
96 __kevp__->ident = (a); \
97 __kevp__->filter = (b); \
98 __kevp__->flags = (c); \
99 __kevp__->fflags = (d); \
100 __kevp__->data = (e); \
101 __kevp__->udata = (f); \
109 static array pollfds;
110 static int poll_maxfd;
112 static bool io_event_change_poll PARAMS((int fd, short what));
115 #ifdef IO_USE_DEVPOLL
116 #include <sys/devpoll.h>
117 static int io_masterfd;
119 static bool io_event_change_devpoll(int fd, short what);
123 #include <sys/time.h>
124 #include "defines.h" /* for conn.h */
125 #include "proc.h" /* for PROC_STAT (needed by conf.h) */
126 #include "conn.h" /* for CONN_ID (needed by conf.h) */
127 #include "conf.h" /* for Conf_MaxConnections */
129 static fd_set readers;
130 static fd_set writers;
132 * this is the first argument for select(), i.e.
133 * the largest fd registered, plus one.
135 static int select_maxfd;
136 static int io_dispatch_select PARAMS((struct timeval *tv));
139 #define io_masterfd -1
141 #endif /* IO_USE_SELECT */
143 static array io_events;
145 static void io_docallback PARAMS((int fd, short what));
149 io_debug(const char *s, int fd, int what)
151 Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
155 io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
166 i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
174 #ifdef IO_USE_DEVPOLL
176 io_dispatch_devpoll(struct timeval *tv)
179 time_t sec = tv->tv_sec * 1000;
180 int i, ret, timeout = tv->tv_usec + sec;
182 struct pollfd p[MAX_EVENTS];
187 dvp.dp_timeout = timeout;
188 dvp.dp_nfds = MAX_EVENTS;
190 ret = ioctl(io_masterfd, DP_POLL, &dvp);
192 for (i=0; i < ret ; i++) {
194 if (p[i].revents & (POLLIN|POLLPRI))
197 if (p[i].revents & POLLOUT)
198 what |= IO_WANTWRITE;
200 if (p[i].revents && !what) {
201 /* other flag is set, probably POLLERR */
204 io_docallback(p[i].fd, what);
212 io_event_change_devpoll(int fd, short what)
218 if (what & IO_WANTREAD)
219 p.events = POLLIN | POLLPRI;
220 if (what & IO_WANTWRITE)
224 return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
228 io_close_devpoll(int fd)
231 p.events = POLLREMOVE;
233 write(io_masterfd, &p, sizeof p);
237 io_library_init_devpoll(unsigned int eventsize)
239 io_masterfd = open("/dev/poll", O_RDWR);
240 if (io_masterfd >= 0)
241 library_initialized = true;
242 Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
243 eventsize, io_masterfd);
247 io_close_devpoll(int UNUSED x)
250 io_library_init_devpoll(unsigned int UNUSED ev)
257 io_dispatch_poll(struct timeval *tv)
259 time_t sec = tv->tv_sec * 1000;
260 int i, ret, timeout = tv->tv_usec + sec;
263 struct pollfd *p = array_start(&pollfds);
268 ret = poll(p, poll_maxfd + 1, timeout);
273 for (i=0; i <= poll_maxfd; i++) {
275 if (p[i].revents & (POLLIN|POLLPRI))
278 if (p[i].revents & POLLOUT)
279 what |= IO_WANTWRITE;
281 if (p[i].revents && !what) {
282 /* other flag is set, probably POLLERR */
287 io_docallback(i, what);
297 io_event_change_poll(int fd, short what)
302 if (what & IO_WANTREAD)
303 events = POLLIN | POLLPRI;
304 if (what & IO_WANTWRITE)
307 p = array_alloc(&pollfds, sizeof *p, fd);
318 io_close_poll(int fd)
321 p = array_get(&pollfds, sizeof *p, fd);
325 if (fd == poll_maxfd) {
326 while (poll_maxfd > 0) {
328 p = array_get(&pollfds, sizeof *p, poll_maxfd);
336 io_library_init_poll(unsigned int eventsize)
339 array_init(&pollfds);
341 Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
343 p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
346 p = array_start(&pollfds);
347 for (i = 0; i < eventsize; i++)
350 library_initialized = true;
355 io_close_poll(int UNUSED x)
358 io_library_init_poll(unsigned int UNUSED ev)
365 io_dispatch_select(struct timeval *tv)
373 readers_tmp = readers;
374 writers_tmp = writers;
376 ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
382 for (i = 0; i <= select_maxfd; i++) {
384 if (FD_ISSET(i, &readers_tmp)) {
389 if (FD_ISSET(i, &writers_tmp)) {
390 what |= IO_WANTWRITE;
394 io_docallback(i, what);
403 io_library_init_select(unsigned int eventsize)
405 if (library_initialized)
407 Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
412 if (Conf_MaxConnections >= (int)FD_SETSIZE) {
414 "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
415 Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
417 Conf_MaxConnections = FD_SETSIZE - 1;
421 "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
422 #endif /* FD_SETSIZE */
423 library_initialized = true;
427 io_close_select(int fd)
431 if (io_masterfd >= 0) /* Are we using epoll()? */
434 FD_CLR(fd, &writers);
435 FD_CLR(fd, &readers);
437 i = io_event_get(fd);
440 if (fd == select_maxfd) {
441 while (select_maxfd>0) {
442 --select_maxfd; /* find largest fd */
443 i = io_event_get(select_maxfd);
444 if (i && i->callback) break;
450 io_library_init_select(int UNUSED x)
453 io_close_select(int UNUSED x)
460 io_event_change_epoll(int fd, short what, const int action)
462 struct epoll_event ev = { 0, {0} };
465 if (what & IO_WANTREAD)
466 ev.events = EPOLLIN | EPOLLPRI;
467 if (what & IO_WANTWRITE)
468 ev.events |= EPOLLOUT;
470 return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
474 io_dispatch_epoll(struct timeval *tv)
476 time_t sec = tv->tv_sec * 1000;
477 int i, ret, timeout = tv->tv_usec + sec;
478 struct epoll_event epoll_ev[MAX_EVENTS];
484 ret = epoll_wait(io_masterfd, epoll_ev, MAX_EVENTS, timeout);
486 for (i = 0; i < ret; i++) {
488 if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
491 if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
494 if (epoll_ev[i].events & EPOLLOUT)
495 type |= IO_WANTWRITE;
497 io_docallback(epoll_ev[i].data.fd, type);
504 io_library_init_epoll(unsigned int eventsize)
506 int ecreate_hint = (int)eventsize;
507 if (ecreate_hint <= 0)
509 io_masterfd = epoll_create(ecreate_hint);
510 if (io_masterfd >= 0) {
511 library_initialized = true;
513 "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
514 ecreate_hint, eventsize, io_masterfd);
518 Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
523 io_library_init_epoll(unsigned int UNUSED ev)
525 #endif /* IO_USE_EPOLL */
530 io_event_kqueue_commit_cache(void)
532 struct kevent *events;
534 int len = (int) array_length(&io_evcache, sizeof (struct kevent));
536 if (!len) /* nothing to do */
542 array_free(&io_evcache);
546 events = array_start(&io_evcache);
548 assert(events != NULL);
550 ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
552 array_trunc(&io_evcache);
557 io_event_change_kqueue(int fd, short what, const int action)
562 if (what & IO_WANTREAD) {
563 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
564 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
566 ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
569 if (ret && (what & IO_WANTWRITE)) {
570 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
571 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
573 ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
576 if (array_length(&io_evcache, sizeof kev) >= 100)
577 io_event_kqueue_commit_cache();
582 io_dispatch_kqueue(struct timeval *tv)
585 struct kevent kev[MAX_EVENTS];
586 struct kevent *newevents;
589 ts.tv_sec = tv->tv_sec;
590 ts.tv_nsec = tv->tv_usec * 1000;
592 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
593 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
594 assert(newevents_len >= 0);
596 ret = kevent(io_masterfd, newevents, newevents_len, kev, MAX_EVENTS, &ts);
597 if (newevents && ret != -1)
598 array_trunc(&io_evcache);
600 for (i = 0; i < ret; i++) {
601 io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
602 if (kev[i].flags & (EV_EOF|EV_ERROR)) {
603 if (kev[i].flags & EV_ERROR)
604 Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
605 (int)kev[i].ident, strerror((int)kev[i].data));
606 io_docallback((int)kev[i].ident, IO_ERROR);
610 switch (kev[i].filter) {
612 io_docallback((int)kev[i].ident, IO_WANTREAD);
615 io_docallback((int)kev[i].ident, IO_WANTWRITE);
618 LogDebug("Unknown kev.filter number %d for fd %d",
619 kev[i].filter, kev[i].ident);
622 io_docallback((int)kev[i].ident, IO_ERROR);
631 io_library_init_kqueue(unsigned int eventsize)
633 io_masterfd = kqueue();
636 "IO subsystem: kqueue (initial maxfd %u, masterfd %d).",
637 eventsize, io_masterfd);
638 if (io_masterfd >= 0)
639 library_initialized = true;
643 io_library_init_kqueue(unsigned int UNUSED ev)
649 io_library_init(unsigned int eventsize)
651 if (library_initialized)
654 if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
657 io_library_init_epoll(eventsize);
658 io_library_init_kqueue(eventsize);
659 io_library_init_devpoll(eventsize);
660 io_library_init_poll(eventsize);
661 io_library_init_select(eventsize);
663 return library_initialized;
668 io_library_shutdown(void)
674 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
675 if (io_masterfd >= 0)
680 array_free(&io_evcache);
682 library_initialized = false;
687 io_event_setcb(int fd, void (*cbfunc) (int, short))
689 io_event *i = io_event_get(fd);
693 i->callback = cbfunc;
699 backend_create_ev(int fd, short what)
702 #ifdef IO_USE_DEVPOLL
703 ret = io_event_change_devpoll(fd, what);
706 ret = io_event_change_poll(fd, what);
709 ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
712 ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
716 ret = io_event_add(fd, what);
723 io_event_create(int fd, short what, void (*cbfunc) (int, short))
729 #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
730 if (io_masterfd < 0 && fd >= FD_SETSIZE) {
732 "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
737 i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
740 "array_alloc failed: could not allocate space for %d io_event structures",
745 i->callback = cbfunc;
747 ret = backend_create_ev(fd, what);
755 io_event_add(int fd, short what)
757 io_event *i = io_event_get(fd);
759 if (!i) return false;
761 if ((i->what & what) == what) /* event type is already registered */
764 io_debug("io_event_add: fd, what", fd, what);
768 if (io_masterfd >= 0)
769 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
772 return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
774 #ifdef IO_USE_DEVPOLL
775 return io_event_change_devpoll(fd, i->what);
778 return io_event_change_poll(fd, i->what);
781 if (fd > select_maxfd)
784 if (what & IO_WANTREAD)
785 FD_SET(fd, &readers);
786 if (what & IO_WANTWRITE)
787 FD_SET(fd, &writers);
796 io_setnonblock(int fd)
798 int flags = fcntl(fd, F_GETFL);
802 #define O_NONBLOCK O_NDELAY
806 return fcntl(fd, F_SETFL, flags) == 0;
810 io_setcloexec(int fd)
812 int flags = fcntl(fd, F_GETFD);
819 return fcntl(fd, F_SETFD, flags) == 0;
827 i = io_event_get(fd);
829 if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
830 io_event_kqueue_commit_cache();
832 /* both kqueue and epoll remove fd from all sets automatically on the last close
833 * of the descriptor. since we don't know if this is the last close we'll have
834 * to remove the set explicitly. */
836 io_event_change_kqueue(fd, i->what, EV_DELETE);
837 io_event_kqueue_commit_cache();
840 io_close_devpoll(fd);
844 io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
850 return close(fd) == 0;
855 io_event_del(int fd, short what)
857 io_event *i = io_event_get(fd);
859 io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
860 if (!i) return false;
862 if (!(i->what & what)) /* event is already disabled */
866 #ifdef IO_USE_DEVPOLL
867 return io_event_change_devpoll(fd, i->what);
870 return io_event_change_poll(fd, i->what);
873 if (io_masterfd >= 0)
874 return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
877 return io_event_change_kqueue(fd, what, EV_DISABLE);
880 if (what & IO_WANTWRITE)
881 FD_CLR(fd, &writers);
883 if (what & IO_WANTREAD)
884 FD_CLR(fd, &readers);
892 io_dispatch(struct timeval *tv)
895 if (io_masterfd >= 0)
896 return io_dispatch_epoll(tv);
899 return io_dispatch_select(tv);
902 return io_dispatch_kqueue(tv);
904 #ifdef IO_USE_DEVPOLL
905 return io_dispatch_devpoll(tv);
908 return io_dispatch_poll(tv);
914 /* call the callback function inside the struct matching fd */
916 io_docallback(int fd, short what)
918 io_event *i = io_event_get(fd);
920 io_debug("io_docallback; fd, what", fd, what);
922 if (i->callback) { /* callback might be NULL if a previous callback function
923 called io_close on this fd */
924 i->callback(fd, (what & IO_ERROR) ? i->what : what);
926 /* if error indicator is set, we return the event(s) that were registered */