ipc/chromium/src/third_party/libevent/epoll.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/epoll.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,473 @@
     1.4 +/*
     1.5 + * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
     1.6 + * Copyright 2007-2012 Niels Provos, Nick Mathewson
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 3. The name of the author may not be used to endorse or promote products
    1.17 + *    derived from this software without specific prior written permission.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +#include "event2/event-config.h"
    1.31 +
    1.32 +#include <stdint.h>
    1.33 +#include <sys/types.h>
    1.34 +#include <sys/resource.h>
    1.35 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.36 +#include <sys/time.h>
    1.37 +#endif
    1.38 +#include <sys/queue.h>
    1.39 +#include <sys/epoll.h>
    1.40 +#include <signal.h>
    1.41 +#include <limits.h>
    1.42 +#include <stdio.h>
    1.43 +#include <stdlib.h>
    1.44 +#include <string.h>
    1.45 +#include <unistd.h>
    1.46 +#include <errno.h>
    1.47 +#ifdef _EVENT_HAVE_FCNTL_H
    1.48 +#include <fcntl.h>
    1.49 +#endif
    1.50 +
    1.51 +#include "event-internal.h"
    1.52 +#include "evsignal-internal.h"
    1.53 +#include "event2/thread.h"
    1.54 +#include "evthread-internal.h"
    1.55 +#include "log-internal.h"
    1.56 +#include "evmap-internal.h"
    1.57 +#include "changelist-internal.h"
    1.58 +
    1.59 +struct epollop {
    1.60 +	struct epoll_event *events;
    1.61 +	int nevents;
    1.62 +	int epfd;
    1.63 +};
    1.64 +
    1.65 +static void *epoll_init(struct event_base *);
    1.66 +static int epoll_dispatch(struct event_base *, struct timeval *);
    1.67 +static void epoll_dealloc(struct event_base *);
    1.68 +
    1.69 +static const struct eventop epollops_changelist = {
    1.70 +	"epoll (with changelist)",
    1.71 +	epoll_init,
    1.72 +	event_changelist_add,
    1.73 +	event_changelist_del,
    1.74 +	epoll_dispatch,
    1.75 +	epoll_dealloc,
    1.76 +	1, /* need reinit */
    1.77 +	EV_FEATURE_ET|EV_FEATURE_O1,
    1.78 +	EVENT_CHANGELIST_FDINFO_SIZE
    1.79 +};
    1.80 +
    1.81 +
    1.82 +static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
    1.83 +    short old, short events, void *p);
    1.84 +static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
    1.85 +    short old, short events, void *p);
    1.86 +
    1.87 +const struct eventop epollops = {
    1.88 +	"epoll",
    1.89 +	epoll_init,
    1.90 +	epoll_nochangelist_add,
    1.91 +	epoll_nochangelist_del,
    1.92 +	epoll_dispatch,
    1.93 +	epoll_dealloc,
    1.94 +	1, /* need reinit */
    1.95 +	EV_FEATURE_ET|EV_FEATURE_O1,
    1.96 +	0
    1.97 +};
    1.98 +
    1.99 +#define INITIAL_NEVENT 32
   1.100 +#define MAX_NEVENT 4096
   1.101 +
   1.102 +/* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
   1.103 + * values bigger than (LONG_MAX - 999ULL)/HZ.  HZ in the wild can be
   1.104 + * as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
   1.105 + * largest number of msec we can support here is 2147482.  Let's
   1.106 + * round that down by 47 seconds.
   1.107 + */
   1.108 +#define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
   1.109 +
   1.110 +static void *
   1.111 +epoll_init(struct event_base *base)
   1.112 +{
   1.113 +	int epfd;
   1.114 +	struct epollop *epollop;
   1.115 +
   1.116 +	/* Initialize the kernel queue.  (The size field is ignored since
   1.117 +	 * 2.6.8.) */
   1.118 +	if ((epfd = epoll_create(32000)) == -1) {
   1.119 +		if (errno != ENOSYS)
   1.120 +			event_warn("epoll_create");
   1.121 +		return (NULL);
   1.122 +	}
   1.123 +
   1.124 +	evutil_make_socket_closeonexec(epfd);
   1.125 +
   1.126 +	if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
   1.127 +		close(epfd);
   1.128 +		return (NULL);
   1.129 +	}
   1.130 +
   1.131 +	epollop->epfd = epfd;
   1.132 +
   1.133 +	/* Initialize fields */
   1.134 +	epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
   1.135 +	if (epollop->events == NULL) {
   1.136 +		mm_free(epollop);
   1.137 +		close(epfd);
   1.138 +		return (NULL);
   1.139 +	}
   1.140 +	epollop->nevents = INITIAL_NEVENT;
   1.141 +
   1.142 +	if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
   1.143 +	    ((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
   1.144 +		evutil_getenv("EVENT_EPOLL_USE_CHANGELIST") != NULL))
   1.145 +		base->evsel = &epollops_changelist;
   1.146 +
   1.147 +	evsig_init(base);
   1.148 +
   1.149 +	return (epollop);
   1.150 +}
   1.151 +
   1.152 +static const char *
   1.153 +change_to_string(int change)
   1.154 +{
   1.155 +	change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
   1.156 +	if (change == EV_CHANGE_ADD) {
   1.157 +		return "add";
   1.158 +	} else if (change == EV_CHANGE_DEL) {
   1.159 +		return "del";
   1.160 +	} else if (change == 0) {
   1.161 +		return "none";
   1.162 +	} else {
   1.163 +		return "???";
   1.164 +	}
   1.165 +}
   1.166 +
   1.167 +static const char *
   1.168 +epoll_op_to_string(int op)
   1.169 +{
   1.170 +	return op == EPOLL_CTL_ADD?"ADD":
   1.171 +	    op == EPOLL_CTL_DEL?"DEL":
   1.172 +	    op == EPOLL_CTL_MOD?"MOD":
   1.173 +	    "???";
   1.174 +}
   1.175 +
   1.176 +static int
   1.177 +epoll_apply_one_change(struct event_base *base,
   1.178 +    struct epollop *epollop,
   1.179 +    const struct event_change *ch)
   1.180 +{
   1.181 +	struct epoll_event epev;
   1.182 +	int op, events = 0;
   1.183 +
   1.184 +	if (1) {
   1.185 +		/* The logic here is a little tricky.  If we had no events set
   1.186 +		   on the fd before, we need to set op="ADD" and set
   1.187 +		   events=the events we want to add.  If we had any events set
   1.188 +		   on the fd before, and we want any events to remain on the
   1.189 +		   fd, we need to say op="MOD" and set events=the events we
   1.190 +		   want to remain.  But if we want to delete the last event,
   1.191 +		   we say op="DEL" and set events=the remaining events.  What
   1.192 +		   fun!
   1.193 +		*/
   1.194 +
   1.195 +		/* TODO: Turn this into a switch or a table lookup. */
   1.196 +
   1.197 +		if ((ch->read_change & EV_CHANGE_ADD) ||
   1.198 +		    (ch->write_change & EV_CHANGE_ADD)) {
   1.199 +			/* If we are adding anything at all, we'll want to do
   1.200 +			 * either an ADD or a MOD. */
   1.201 +			events = 0;
   1.202 +			op = EPOLL_CTL_ADD;
   1.203 +			if (ch->read_change & EV_CHANGE_ADD) {
   1.204 +				events |= EPOLLIN;
   1.205 +			} else if (ch->read_change & EV_CHANGE_DEL) {
   1.206 +				;
   1.207 +			} else if (ch->old_events & EV_READ) {
   1.208 +				events |= EPOLLIN;
   1.209 +			}
   1.210 +			if (ch->write_change & EV_CHANGE_ADD) {
   1.211 +				events |= EPOLLOUT;
   1.212 +			} else if (ch->write_change & EV_CHANGE_DEL) {
   1.213 +				;
   1.214 +			} else if (ch->old_events & EV_WRITE) {
   1.215 +				events |= EPOLLOUT;
   1.216 +			}
   1.217 +			if ((ch->read_change|ch->write_change) & EV_ET)
   1.218 +				events |= EPOLLET;
   1.219 +
   1.220 +			if (ch->old_events) {
   1.221 +				/* If MOD fails, we retry as an ADD, and if
   1.222 +				 * ADD fails we will retry as a MOD.  So the
   1.223 +				 * only hard part here is to guess which one
   1.224 +				 * will work.  As a heuristic, we'll try
   1.225 +				 * MOD first if we think there were old
   1.226 +				 * events and ADD if we think there were none.
   1.227 +				 *
   1.228 +				 * We can be wrong about the MOD if the file
   1.229 +				 * has in fact been closed and re-opened.
   1.230 +				 *
   1.231 +				 * We can be wrong about the ADD if the
   1.232 +				 * the fd has been re-created with a dup()
   1.233 +				 * of the same file that it was before.
   1.234 +				 */
   1.235 +				op = EPOLL_CTL_MOD;
   1.236 +			}
   1.237 +		} else if ((ch->read_change & EV_CHANGE_DEL) ||
   1.238 +		    (ch->write_change & EV_CHANGE_DEL)) {
   1.239 +			/* If we're deleting anything, we'll want to do a MOD
   1.240 +			 * or a DEL. */
   1.241 +			op = EPOLL_CTL_DEL;
   1.242 +
   1.243 +			if (ch->read_change & EV_CHANGE_DEL) {
   1.244 +				if (ch->write_change & EV_CHANGE_DEL) {
   1.245 +					events = EPOLLIN|EPOLLOUT;
   1.246 +				} else if (ch->old_events & EV_WRITE) {
   1.247 +					events = EPOLLOUT;
   1.248 +					op = EPOLL_CTL_MOD;
   1.249 +				} else {
   1.250 +					events = EPOLLIN;
   1.251 +				}
   1.252 +			} else if (ch->write_change & EV_CHANGE_DEL) {
   1.253 +				if (ch->old_events & EV_READ) {
   1.254 +					events = EPOLLIN;
   1.255 +					op = EPOLL_CTL_MOD;
   1.256 +				} else {
   1.257 +					events = EPOLLOUT;
   1.258 +				}
   1.259 +			}
   1.260 +		}
   1.261 +
   1.262 +		if (!events)
   1.263 +			return 0;
   1.264 +
   1.265 +		memset(&epev, 0, sizeof(epev));
   1.266 +		epev.data.fd = ch->fd;
   1.267 +		epev.events = events;
   1.268 +		if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == -1) {
   1.269 +			if (op == EPOLL_CTL_MOD && errno == ENOENT) {
   1.270 +				/* If a MOD operation fails with ENOENT, the
   1.271 +				 * fd was probably closed and re-opened.  We
   1.272 +				 * should retry the operation as an ADD.
   1.273 +				 */
   1.274 +				if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
   1.275 +					event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
   1.276 +					    (int)epev.events, ch->fd);
   1.277 +					return -1;
   1.278 +				} else {
   1.279 +					event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
   1.280 +						(int)epev.events,
   1.281 +						ch->fd));
   1.282 +				}
   1.283 +			} else if (op == EPOLL_CTL_ADD && errno == EEXIST) {
   1.284 +				/* If an ADD operation fails with EEXIST,
   1.285 +				 * either the operation was redundant (as with a
   1.286 +				 * precautionary add), or we ran into a fun
   1.287 +				 * kernel bug where using dup*() to duplicate the
   1.288 +				 * same file into the same fd gives you the same epitem
   1.289 +				 * rather than a fresh one.  For the second case,
   1.290 +				 * we must retry with MOD. */
   1.291 +				if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
   1.292 +					event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
   1.293 +					    (int)epev.events, ch->fd);
   1.294 +					return -1;
   1.295 +				} else {
   1.296 +					event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
   1.297 +						(int)epev.events,
   1.298 +						ch->fd));
   1.299 +				}
   1.300 +			} else if (op == EPOLL_CTL_DEL &&
   1.301 +			    (errno == ENOENT || errno == EBADF ||
   1.302 +				errno == EPERM)) {
   1.303 +				/* If a delete fails with one of these errors,
   1.304 +				 * that's fine too: we closed the fd before we
   1.305 +				 * got around to calling epoll_dispatch. */
   1.306 +				event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
   1.307 +					(int)epev.events,
   1.308 +					ch->fd,
   1.309 +					strerror(errno)));
   1.310 +			} else {
   1.311 +				event_warn("Epoll %s(%d) on fd %d failed.  Old events were %d; read change was %d (%s); write change was %d (%s)",
   1.312 +				    epoll_op_to_string(op),
   1.313 +				    (int)epev.events,
   1.314 +				    ch->fd,
   1.315 +				    ch->old_events,
   1.316 +				    ch->read_change,
   1.317 +				    change_to_string(ch->read_change),
   1.318 +				    ch->write_change,
   1.319 +				    change_to_string(ch->write_change));
   1.320 +				return -1;
   1.321 +			}
   1.322 +		} else {
   1.323 +			event_debug(("Epoll %s(%d) on fd %d okay. [old events were %d; read change was %d; write change was %d]",
   1.324 +				epoll_op_to_string(op),
   1.325 +				(int)epev.events,
   1.326 +				(int)ch->fd,
   1.327 +				ch->old_events,
   1.328 +				ch->read_change,
   1.329 +				ch->write_change));
   1.330 +		}
   1.331 +	}
   1.332 +	return 0;
   1.333 +}
   1.334 +
   1.335 +static int
   1.336 +epoll_apply_changes(struct event_base *base)
   1.337 +{
   1.338 +	struct event_changelist *changelist = &base->changelist;
   1.339 +	struct epollop *epollop = base->evbase;
   1.340 +	struct event_change *ch;
   1.341 +
   1.342 +	int r = 0;
   1.343 +	int i;
   1.344 +
   1.345 +	for (i = 0; i < changelist->n_changes; ++i) {
   1.346 +		ch = &changelist->changes[i];
   1.347 +		if (epoll_apply_one_change(base, epollop, ch) < 0)
   1.348 +			r = -1;
   1.349 +	}
   1.350 +
   1.351 +	return (r);
   1.352 +}
   1.353 +
   1.354 +static int
   1.355 +epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
   1.356 +    short old, short events, void *p)
   1.357 +{
   1.358 +	struct event_change ch;
   1.359 +	ch.fd = fd;
   1.360 +	ch.old_events = old;
   1.361 +	ch.read_change = ch.write_change = 0;
   1.362 +	if (events & EV_WRITE)
   1.363 +		ch.write_change = EV_CHANGE_ADD |
   1.364 +		    (events & EV_ET);
   1.365 +	if (events & EV_READ)
   1.366 +		ch.read_change = EV_CHANGE_ADD |
   1.367 +		    (events & EV_ET);
   1.368 +
   1.369 +	return epoll_apply_one_change(base, base->evbase, &ch);
   1.370 +}
   1.371 +
   1.372 +static int
   1.373 +epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
   1.374 +    short old, short events, void *p)
   1.375 +{
   1.376 +	struct event_change ch;
   1.377 +	ch.fd = fd;
   1.378 +	ch.old_events = old;
   1.379 +	ch.read_change = ch.write_change = 0;
   1.380 +	if (events & EV_WRITE)
   1.381 +		ch.write_change = EV_CHANGE_DEL;
   1.382 +	if (events & EV_READ)
   1.383 +		ch.read_change = EV_CHANGE_DEL;
   1.384 +
   1.385 +	return epoll_apply_one_change(base, base->evbase, &ch);
   1.386 +}
   1.387 +
   1.388 +static int
   1.389 +epoll_dispatch(struct event_base *base, struct timeval *tv)
   1.390 +{
   1.391 +	struct epollop *epollop = base->evbase;
   1.392 +	struct epoll_event *events = epollop->events;
   1.393 +	int i, res;
   1.394 +	long timeout = -1;
   1.395 +
   1.396 +	if (tv != NULL) {
   1.397 +		timeout = evutil_tv_to_msec(tv);
   1.398 +		if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
   1.399 +			/* Linux kernels can wait forever if the timeout is
   1.400 +			 * too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
   1.401 +			timeout = MAX_EPOLL_TIMEOUT_MSEC;
   1.402 +		}
   1.403 +	}
   1.404 +
   1.405 +	epoll_apply_changes(base);
   1.406 +	event_changelist_remove_all(&base->changelist, base);
   1.407 +
   1.408 +	EVBASE_RELEASE_LOCK(base, th_base_lock);
   1.409 +
   1.410 +	res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
   1.411 +
   1.412 +	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
   1.413 +
   1.414 +	if (res == -1) {
   1.415 +		if (errno != EINTR) {
   1.416 +			event_warn("epoll_wait");
   1.417 +			return (-1);
   1.418 +		}
   1.419 +
   1.420 +		return (0);
   1.421 +	}
   1.422 +
   1.423 +	event_debug(("%s: epoll_wait reports %d", __func__, res));
   1.424 +	EVUTIL_ASSERT(res <= epollop->nevents);
   1.425 +
   1.426 +	for (i = 0; i < res; i++) {
   1.427 +		int what = events[i].events;
   1.428 +		short ev = 0;
   1.429 +
   1.430 +		if (what & (EPOLLHUP|EPOLLERR)) {
   1.431 +			ev = EV_READ | EV_WRITE;
   1.432 +		} else {
   1.433 +			if (what & EPOLLIN)
   1.434 +				ev |= EV_READ;
   1.435 +			if (what & EPOLLOUT)
   1.436 +				ev |= EV_WRITE;
   1.437 +		}
   1.438 +
   1.439 +		if (!ev)
   1.440 +			continue;
   1.441 +
   1.442 +		evmap_io_active(base, events[i].data.fd, ev | EV_ET);
   1.443 +	}
   1.444 +
   1.445 +	if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
   1.446 +		/* We used all of the event space this time.  We should
   1.447 +		   be ready for more events next time. */
   1.448 +		int new_nevents = epollop->nevents * 2;
   1.449 +		struct epoll_event *new_events;
   1.450 +
   1.451 +		new_events = mm_realloc(epollop->events,
   1.452 +		    new_nevents * sizeof(struct epoll_event));
   1.453 +		if (new_events) {
   1.454 +			epollop->events = new_events;
   1.455 +			epollop->nevents = new_nevents;
   1.456 +		}
   1.457 +	}
   1.458 +
   1.459 +	return (0);
   1.460 +}
   1.461 +
   1.462 +
   1.463 +static void
   1.464 +epoll_dealloc(struct event_base *base)
   1.465 +{
   1.466 +	struct epollop *epollop = base->evbase;
   1.467 +
   1.468 +	evsig_dealloc(base);
   1.469 +	if (epollop->events)
   1.470 +		mm_free(epollop->events);
   1.471 +	if (epollop->epfd >= 0)
   1.472 +		close(epollop->epfd);
   1.473 +
   1.474 +	memset(epollop, 0, sizeof(struct epollop));
   1.475 +	mm_free(epollop);
   1.476 +}

mercurial