Blame libevent/devpoll.c

Packit e9ba0d
/*
Packit e9ba0d
 * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
Packit e9ba0d
 * Copyright 2009-2012 Niels Provos and Nick Mathewson
Packit e9ba0d
 *
Packit e9ba0d
 * Redistribution and use in source and binary forms, with or without
Packit e9ba0d
 * modification, are permitted provided that the following conditions
Packit e9ba0d
 * are met:
Packit e9ba0d
 * 1. Redistributions of source code must retain the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer.
Packit e9ba0d
 * 2. Redistributions in binary form must reproduce the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer in the
Packit e9ba0d
 *    documentation and/or other materials provided with the distribution.
Packit e9ba0d
 * 3. The name of the author may not be used to endorse or promote products
Packit e9ba0d
 *    derived from this software without specific prior written permission.
Packit e9ba0d
 *
Packit e9ba0d
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit e9ba0d
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit e9ba0d
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit e9ba0d
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit e9ba0d
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit e9ba0d
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit e9ba0d
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit e9ba0d
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit e9ba0d
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit e9ba0d
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit e9ba0d
 */
Packit e9ba0d
#include "event2/event-config.h"
Packit e9ba0d
Packit e9ba0d
#include <sys/types.h>
Packit e9ba0d
#include <sys/resource.h>
Packit e9ba0d
#ifdef _EVENT_HAVE_SYS_TIME_H
Packit e9ba0d
#include <sys/time.h>
Packit e9ba0d
#endif
Packit e9ba0d
#include <sys/queue.h>
Packit e9ba0d
#include <sys/devpoll.h>
Packit e9ba0d
#include <signal.h>
Packit e9ba0d
#include <stdio.h>
Packit e9ba0d
#include <stdlib.h>
Packit e9ba0d
#include <string.h>
Packit e9ba0d
#include <unistd.h>
Packit e9ba0d
#include <fcntl.h>
Packit e9ba0d
#include <errno.h>
Packit e9ba0d
Packit e9ba0d
#include "event2/event.h"
Packit e9ba0d
#include "event2/event_struct.h"
Packit e9ba0d
#include "event2/thread.h"
Packit e9ba0d
#include "event-internal.h"
Packit e9ba0d
#include "evsignal-internal.h"
Packit e9ba0d
#include "log-internal.h"
Packit e9ba0d
#include "evmap-internal.h"
Packit e9ba0d
#include "evthread-internal.h"
Packit e9ba0d
Packit e9ba0d
struct devpollop {
Packit e9ba0d
	struct pollfd *events;
Packit e9ba0d
	int nevents;
Packit e9ba0d
	int dpfd;
Packit e9ba0d
	struct pollfd *changes;
Packit e9ba0d
	int nchanges;
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
static void *devpoll_init(struct event_base *);
Packit e9ba0d
static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
Packit e9ba0d
static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
Packit e9ba0d
static int devpoll_dispatch(struct event_base *, struct timeval *);
Packit e9ba0d
static void devpoll_dealloc(struct event_base *);
Packit e9ba0d
Packit e9ba0d
const struct eventop devpollops = {
Packit e9ba0d
	"devpoll",
Packit e9ba0d
	devpoll_init,
Packit e9ba0d
	devpoll_add,
Packit e9ba0d
	devpoll_del,
Packit e9ba0d
	devpoll_dispatch,
Packit e9ba0d
	devpoll_dealloc,
Packit e9ba0d
	1, /* need reinit */
Packit e9ba0d
	EV_FEATURE_FDS|EV_FEATURE_O1,
Packit e9ba0d
	0
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
#define NEVENT	32000
Packit e9ba0d
Packit e9ba0d
static int
Packit e9ba0d
devpoll_commit(struct devpollop *devpollop)
Packit e9ba0d
{
Packit e9ba0d
	/*
Packit e9ba0d
	 * Due to a bug in Solaris, we have to use pwrite with an offset of 0.
Packit e9ba0d
	 * Write is limited to 2GB of data, until it will fail.
Packit e9ba0d
	 */
Packit e9ba0d
	if (pwrite(devpollop->dpfd, devpollop->changes,
Packit e9ba0d
		sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
Packit e9ba0d
		return (-1);
Packit e9ba0d
Packit e9ba0d
	devpollop->nchanges = 0;
Packit e9ba0d
	return (0);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
static int
Packit e9ba0d
devpoll_queue(struct devpollop *devpollop, int fd, int events) {
Packit e9ba0d
	struct pollfd *pfd;
Packit e9ba0d
Packit e9ba0d
	if (devpollop->nchanges >= devpollop->nevents) {
Packit e9ba0d
		/*
Packit e9ba0d
		 * Change buffer is full, must commit it to /dev/poll before
Packit e9ba0d
		 * adding more
Packit e9ba0d
		 */
Packit e9ba0d
		if (devpoll_commit(devpollop) != 0)
Packit e9ba0d
			return (-1);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	pfd = &devpollop->changes[devpollop->nchanges++];
Packit e9ba0d
	pfd->fd = fd;
Packit e9ba0d
	pfd->events = events;
Packit e9ba0d
	pfd->revents = 0;
Packit e9ba0d
Packit e9ba0d
	return (0);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
static void *
Packit e9ba0d
devpoll_init(struct event_base *base)
Packit e9ba0d
{
Packit e9ba0d
	int dpfd, nfiles = NEVENT;
Packit e9ba0d
	struct rlimit rl;
Packit e9ba0d
	struct devpollop *devpollop;
Packit e9ba0d
Packit e9ba0d
	if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
Packit e9ba0d
		return (NULL);
Packit e9ba0d
Packit e9ba0d
	if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
Packit e9ba0d
	    rl.rlim_cur != RLIM_INFINITY)
Packit e9ba0d
		nfiles = rl.rlim_cur;
Packit e9ba0d
Packit e9ba0d
	/* Initialize the kernel queue */
Packit e9ba0d
	if ((dpfd = evutil_open_closeonexec("/dev/poll", O_RDWR, 0)) == -1) {
Packit e9ba0d
		event_warn("open: /dev/poll");
Packit e9ba0d
		mm_free(devpollop);
Packit e9ba0d
		return (NULL);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	devpollop->dpfd = dpfd;
Packit e9ba0d
Packit e9ba0d
	/* Initialize fields */
Packit e9ba0d
	/* FIXME: allocating 'nfiles' worth of space here can be
Packit e9ba0d
	 * expensive and unnecessary.  See how epoll.c does it instead. */
Packit e9ba0d
	devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
Packit e9ba0d
	if (devpollop->events == NULL) {
Packit e9ba0d
		mm_free(devpollop);
Packit e9ba0d
		close(dpfd);
Packit e9ba0d
		return (NULL);
Packit e9ba0d
	}
Packit e9ba0d
	devpollop->nevents = nfiles;
Packit e9ba0d
Packit e9ba0d
	devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
Packit e9ba0d
	if (devpollop->changes == NULL) {
Packit e9ba0d
		mm_free(devpollop->events);
Packit e9ba0d
		mm_free(devpollop);
Packit e9ba0d
		close(dpfd);
Packit e9ba0d
		return (NULL);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	evsig_init(base);
Packit e9ba0d
Packit e9ba0d
	return (devpollop);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
static int
Packit e9ba0d
devpoll_dispatch(struct event_base *base, struct timeval *tv)
Packit e9ba0d
{
Packit e9ba0d
	struct devpollop *devpollop = base->evbase;
Packit e9ba0d
	struct pollfd *events = devpollop->events;
Packit e9ba0d
	struct dvpoll dvp;
Packit e9ba0d
	int i, res, timeout = -1;
Packit e9ba0d
Packit e9ba0d
	if (devpollop->nchanges)
Packit e9ba0d
		devpoll_commit(devpollop);
Packit e9ba0d
Packit e9ba0d
	if (tv != NULL)
Packit e9ba0d
		timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
Packit e9ba0d
Packit e9ba0d
	dvp.dp_fds = devpollop->events;
Packit e9ba0d
	dvp.dp_nfds = devpollop->nevents;
Packit e9ba0d
	dvp.dp_timeout = timeout;
Packit e9ba0d
Packit e9ba0d
	EVBASE_RELEASE_LOCK(base, th_base_lock);
Packit e9ba0d
Packit e9ba0d
	res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
Packit e9ba0d
Packit e9ba0d
	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Packit e9ba0d
Packit e9ba0d
	if (res == -1) {
Packit e9ba0d
		if (errno != EINTR) {
Packit e9ba0d
			event_warn("ioctl: DP_POLL");
Packit e9ba0d
			return (-1);
Packit e9ba0d
		}
Packit e9ba0d
Packit e9ba0d
		return (0);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	event_debug(("%s: devpoll_wait reports %d", __func__, res));
Packit e9ba0d
Packit e9ba0d
	for (i = 0; i < res; i++) {
Packit e9ba0d
		int which = 0;
Packit e9ba0d
		int what = events[i].revents;
Packit e9ba0d
Packit e9ba0d
		if (what & POLLHUP)
Packit e9ba0d
			what |= POLLIN | POLLOUT;
Packit e9ba0d
		else if (what & POLLERR)
Packit e9ba0d
			what |= POLLIN | POLLOUT;
Packit e9ba0d
Packit e9ba0d
		if (what & POLLIN)
Packit e9ba0d
			which |= EV_READ;
Packit e9ba0d
		if (what & POLLOUT)
Packit e9ba0d
			which |= EV_WRITE;
Packit e9ba0d
Packit e9ba0d
		if (!which)
Packit e9ba0d
			continue;
Packit e9ba0d
Packit e9ba0d
		/* XXX(niels): not sure if this works for devpoll */
Packit e9ba0d
		evmap_io_active(base, events[i].fd, which);
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	return (0);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
static int
Packit e9ba0d
devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
Packit e9ba0d
{
Packit e9ba0d
	struct devpollop *devpollop = base->evbase;
Packit e9ba0d
	int res;
Packit e9ba0d
	(void)p;
Packit e9ba0d
Packit e9ba0d
	/*
Packit e9ba0d
	 * It's not necessary to OR the existing read/write events that we
Packit e9ba0d
	 * are currently interested in with the new event we are adding.
Packit e9ba0d
	 * The /dev/poll driver ORs any new events with the existing events
Packit e9ba0d
	 * that it has cached for the fd.
Packit e9ba0d
	 */
Packit e9ba0d
Packit e9ba0d
	res = 0;
Packit e9ba0d
	if (events & EV_READ)
Packit e9ba0d
		res |= POLLIN;
Packit e9ba0d
	if (events & EV_WRITE)
Packit e9ba0d
		res |= POLLOUT;
Packit e9ba0d
Packit e9ba0d
	if (devpoll_queue(devpollop, fd, res) != 0)
Packit e9ba0d
		return (-1);
Packit e9ba0d
Packit e9ba0d
	return (0);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
static int
Packit e9ba0d
devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
Packit e9ba0d
{
Packit e9ba0d
	struct devpollop *devpollop = base->evbase;
Packit e9ba0d
	int res;
Packit e9ba0d
	(void)p;
Packit e9ba0d
Packit e9ba0d
	res = 0;
Packit e9ba0d
	if (events & EV_READ)
Packit e9ba0d
		res |= POLLIN;
Packit e9ba0d
	if (events & EV_WRITE)
Packit e9ba0d
		res |= POLLOUT;
Packit e9ba0d
Packit e9ba0d
	/*
Packit e9ba0d
	 * The only way to remove an fd from the /dev/poll monitored set is
Packit e9ba0d
	 * to use POLLREMOVE by itself.  This removes ALL events for the fd
Packit e9ba0d
	 * provided so if we care about two events and are only removing one
Packit e9ba0d
	 * we must re-add the other event after POLLREMOVE.
Packit e9ba0d
	 */
Packit e9ba0d
Packit e9ba0d
	if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
Packit e9ba0d
		return (-1);
Packit e9ba0d
Packit e9ba0d
	if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
Packit e9ba0d
		/*
Packit e9ba0d
		 * We're not deleting all events, so we must resubmit the
Packit e9ba0d
		 * event that we are still interested in if one exists.
Packit e9ba0d
		 */
Packit e9ba0d
Packit e9ba0d
		if ((res & POLLIN) && (old & EV_WRITE)) {
Packit e9ba0d
			/* Deleting read, still care about write */
Packit e9ba0d
			devpoll_queue(devpollop, fd, POLLOUT);
Packit e9ba0d
		} else if ((res & POLLOUT) && (old & EV_READ)) {
Packit e9ba0d
			/* Deleting write, still care about read */
Packit e9ba0d
			devpoll_queue(devpollop, fd, POLLIN);
Packit e9ba0d
		}
Packit e9ba0d
	}
Packit e9ba0d
Packit e9ba0d
	return (0);
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
static void
Packit e9ba0d
devpoll_dealloc(struct event_base *base)
Packit e9ba0d
{
Packit e9ba0d
	struct devpollop *devpollop = base->evbase;
Packit e9ba0d
Packit e9ba0d
	evsig_dealloc(base);
Packit e9ba0d
	if (devpollop->events)
Packit e9ba0d
		mm_free(devpollop->events);
Packit e9ba0d
	if (devpollop->changes)
Packit e9ba0d
		mm_free(devpollop->changes);
Packit e9ba0d
	if (devpollop->dpfd >= 0)
Packit e9ba0d
		close(devpollop->dpfd);
Packit e9ba0d
Packit e9ba0d
	memset(devpollop, 0, sizeof(struct devpollop));
Packit e9ba0d
	mm_free(devpollop);
Packit e9ba0d
}