+ struct dvpoll dvp;
+ time_t sec = tv->tv_sec * 1000;
+ int i, ret, timeout = tv->tv_usec + sec;
+ short what;
+ struct pollfd p[MAX_EVENTS];
+
+ if (timeout < 0)
+ timeout = 1000;
+
+ dvp.dp_timeout = timeout;
+ dvp.dp_nfds = MAX_EVENTS;
+ dvp.dp_fds = p;
+ ret = ioctl(io_masterfd, DP_POLL, &dvp);
+
+ for (i=0; i < ret ; i++) {
+ what = 0;
+ if (p[i].revents & (POLLIN|POLLPRI))
+ what = IO_WANTREAD;
+
+ if (p[i].revents & POLLOUT)
+ what |= IO_WANTWRITE;
+
+ if (p[i].revents && !what) {
+ /* other flag is set, probably POLLERR */
+ what = IO_ERROR;
+ }
+ io_docallback(p[i].fd, what);
+ }
+
+ return ret;
+}
+
+
+static bool
+io_event_change_devpoll(int fd, short what)
+{
+ struct pollfd p;
+
+ p.events = 0;
+
+ if (what & IO_WANTREAD)
+ p.events = POLLIN | POLLPRI;
+ if (what & IO_WANTWRITE)
+ p.events |= POLLOUT;
+
+ p.fd = fd;
+ return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
+}
+
+static void
+io_close_devpoll(int fd)
+{
+ struct pollfd p;
+ p.events = POLLREMOVE;
+ p.fd = fd;
+ write(io_masterfd, &p, sizeof p);
+}
+
+static void
+io_library_init_devpoll(unsigned int eventsize)
+{
+ io_masterfd = open("/dev/poll", O_RDWR);
+ if (io_masterfd >= 0)
+ library_initialized = true;
+ Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
+ eventsize, io_masterfd);
+}
+#else
+static inline void
+io_close_devpoll(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_library_init_devpoll(unsigned int UNUSED ev)
+{ /* NOTHING */ }
+#endif
+
+
+#ifdef IO_USE_POLL
+static int
+io_dispatch_poll(struct timeval *tv)
+{
+ time_t sec = tv->tv_sec * 1000;
+ int i, ret, timeout = tv->tv_usec + sec;
+ int fds_ready;
+ short what;
+ struct pollfd *p = array_start(&pollfds);
+
+ if (timeout < 0)
+ timeout = 1000;
+
+ ret = poll(p, poll_maxfd + 1, timeout);
+ if (ret <= 0)
+ return ret;
+
+ fds_ready = ret;
+ for (i=0; i <= poll_maxfd; i++) {
+ what = 0;
+ if (p[i].revents & (POLLIN|POLLPRI))
+ what = IO_WANTREAD;
+
+ if (p[i].revents & POLLOUT)
+ what |= IO_WANTWRITE;
+
+ if (p[i].revents && !what) {
+ /* other flag is set, probably POLLERR */
+ what = IO_ERROR;
+ }
+ if (what) {
+ fds_ready--;
+ io_docallback(i, what);
+ }
+ if (fds_ready <= 0)
+ break;
+ }
+
+ return ret;
+}
+
+static bool
+io_event_change_poll(int fd, short what)
+{
+ struct pollfd *p;
+ short events = 0;
+
+ if (what & IO_WANTREAD)
+ events = POLLIN | POLLPRI;
+ if (what & IO_WANTWRITE)
+ events |= POLLOUT;
+
+ p = array_alloc(&pollfds, sizeof *p, fd);
+ if (p) {
+ p->events = events;
+ p->fd = fd;
+ if (fd > poll_maxfd)
+ poll_maxfd = fd;
+ }
+ return p != NULL;
+}
+
+static void
+io_close_poll(int fd)
+{
+ struct pollfd *p;
+ p = array_get(&pollfds, sizeof *p, fd);
+ if (!p) return;
+
+ p->fd = -1;
+ if (fd == poll_maxfd) {
+ while (poll_maxfd > 0) {
+ --poll_maxfd;
+ p = array_get(&pollfds, sizeof *p, poll_maxfd);
+ if (p && p->fd >= 0)
+ break;
+ }
+ }
+}
+
+static void
+io_library_init_poll(unsigned int eventsize)
+{
+ struct pollfd *p;
+ array_init(&pollfds);
+ poll_maxfd = 0;
+ Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
+ eventsize);
+ p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
+ if (p) {
+ unsigned i;
+ p = array_start(&pollfds);
+ for (i = 0; i < eventsize; i++)
+ p[i].fd = -1;
+
+ library_initialized = true;
+ }
+}
+#else
+static inline void
+io_close_poll(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_library_init_poll(unsigned int UNUSED ev)
+{ /* NOTHING */ }
+#endif
+
+
+#ifdef IO_USE_SELECT
+static int
+io_dispatch_select(struct timeval *tv)
+{
+ fd_set readers_tmp;
+ fd_set writers_tmp;
+ short what;
+ int ret, i;
+ int fds_ready;
+
+ readers_tmp = readers;
+ writers_tmp = writers;
+
+ ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
+ if (ret <= 0)
+ return ret;
+
+ fds_ready = ret;
+
+ for (i = 0; i <= select_maxfd; i++) {
+ what = 0;
+ if (FD_ISSET(i, &readers_tmp)) {
+ what = IO_WANTREAD;
+ fds_ready--;
+ }
+
+ if (FD_ISSET(i, &writers_tmp)) {
+ what |= IO_WANTWRITE;
+ fds_ready--;
+ }
+ if (what)
+ io_docallback(i, what);
+ if (fds_ready <= 0)
+ break;
+ }
+
+ return ret;
+}
+
+static void
+io_library_init_select(unsigned int eventsize)
+{
+ if (library_initialized)
+ return;
+ Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
+ eventsize);
+ FD_ZERO(&readers);
+ FD_ZERO(&writers);
+#ifdef FD_SETSIZE
+ if (Conf_MaxConnections >= (int)FD_SETSIZE) {
+ Log(LOG_WARNING,
+ "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
+ Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
+
+ Conf_MaxConnections = FD_SETSIZE - 1;
+ }
+#else
+ Log(LOG_WARNING,
+ "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
+#endif /* FD_SETSIZE */
+ library_initialized = true;
+}
+
+static void
+io_close_select(int fd)
+{
+ io_event *i;
+
+ if (io_masterfd >= 0) /* Are we using epoll()? */
+ return;
+
+ FD_CLR(fd, &writers);
+ FD_CLR(fd, &readers);
+
+ i = io_event_get(fd);
+ if (!i) return;
+
+ if (fd == select_maxfd) {
+ while (select_maxfd>0) {
+ --select_maxfd; /* find largest fd */
+ i = io_event_get(select_maxfd);
+ if (i && i->callback) break;
+ }
+ }
+}
+#else
+static inline void
+io_library_init_select(int UNUSED x)
+{ /* NOTHING */ }
+static inline void
+io_close_select(int UNUSED x)
+{ /* NOTHING */ }
+#endif /* SELECT */
+
+