2003-03-07 23:20:36 +00:00
|
|
|
/*
|
2009-01-27 22:34:36 +00:00
|
|
|
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
|
|
|
|
* Copyright 2007-2009 Niels Provos, Nick Mathewson
|
2003-03-07 23:20:36 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-10-04 23:27:26 +00:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2003-03-07 23:20:36 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2009-01-27 22:30:46 +00:00
|
|
|
#include "event-config.h"
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
2003-04-09 18:12:11 +00:00
|
|
|
#include <sys/resource.h>
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SYS_TIME_H
|
2003-03-07 23:20:36 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_FCNTL_H
|
2004-08-10 18:29:37 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2007-03-10 06:37:53 +00:00
|
|
|
#include "event-internal.h"
|
2009-01-13 20:26:37 +00:00
|
|
|
#include "evsignal-internal.h"
|
2009-10-21 03:54:00 +00:00
|
|
|
#include "event2/thread.h"
|
|
|
|
#include "evthread-internal.h"
|
2009-01-13 20:26:37 +00:00
|
|
|
#include "log-internal.h"
|
|
|
|
#include "evmap-internal.h"
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
struct epollop {
|
|
|
|
struct epoll_event *events;
|
|
|
|
int nevents;
|
|
|
|
int epfd;
|
2005-01-03 18:58:40 +00:00
|
|
|
};
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static void *epoll_init (struct event_base *);
|
2009-01-14 20:52:32 +00:00
|
|
|
static int epoll_add(struct event_base *, int fd, short old, short events, void *);
|
|
|
|
static int epoll_del(struct event_base *, int fd, short old, short events, void *);
|
2008-12-23 16:37:01 +00:00
|
|
|
static int epoll_dispatch (struct event_base *, struct timeval *);
|
|
|
|
static void epoll_dealloc (struct event_base *);
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-13 06:11:12 +00:00
|
|
|
const struct eventop epollops = {
|
2003-03-07 23:20:36 +00:00
|
|
|
"epoll",
|
|
|
|
epoll_init,
|
|
|
|
epoll_add,
|
|
|
|
epoll_del,
|
2006-03-28 04:40:54 +00:00
|
|
|
epoll_dispatch,
|
2007-11-25 06:57:59 +00:00
|
|
|
epoll_dealloc,
|
2008-05-31 14:37:31 +00:00
|
|
|
1, /* need reinit */
|
|
|
|
EV_FEATURE_ET|EV_FEATURE_O1,
|
2009-01-14 20:52:32 +00:00
|
|
|
0
|
2003-03-07 23:20:36 +00:00
|
|
|
};
|
|
|
|
|
2009-01-27 22:30:46 +00:00
|
|
|
#ifdef _EVENT_HAVE_SETFD
|
2004-08-10 18:29:37 +00:00
|
|
|
#define FD_CLOSEONEXEC(x) do { \
|
|
|
|
if (fcntl(x, F_SETFD, 1) == -1) \
|
2005-03-29 07:03:10 +00:00
|
|
|
event_warn("fcntl(%d, F_SETFD)", x); \
|
2004-08-10 18:29:37 +00:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define FD_CLOSEONEXEC(x)
|
|
|
|
#endif
|
|
|
|
|
2009-09-11 18:47:35 +00:00
|
|
|
#define INITIAL_NEVENT 32
|
|
|
|
#define MAX_NEVENT 4096
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-04-11 20:02:50 +00:00
|
|
|
/* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
|
|
|
|
* values bigger than (LONG_MAX - 999ULL)/HZ. HZ in the wild can be
|
|
|
|
* as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
|
|
|
|
* largest number of msec we can support here is 2147482. Let's
|
|
|
|
* round that down by 47 seconds.
|
|
|
|
*/
|
|
|
|
#define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static void *
|
2007-03-10 06:37:53 +00:00
|
|
|
epoll_init(struct event_base *base)
|
2003-03-07 23:20:36 +00:00
|
|
|
{
|
2009-09-11 18:47:35 +00:00
|
|
|
int epfd;
|
2005-01-03 18:58:40 +00:00
|
|
|
struct epollop *epollop;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2009-10-16 13:19:57 +00:00
|
|
|
/* Initialize the kernel queue. (The size field is ignored since
|
2009-09-11 18:47:35 +00:00
|
|
|
* 2.6.8.) */
|
|
|
|
if ((epfd = epoll_create(32000)) == -1) {
|
2008-04-10 19:34:50 +00:00
|
|
|
if (errno != ENOSYS)
|
|
|
|
event_warn("epoll_create");
|
2003-03-07 23:20:36 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2004-08-10 18:29:37 +00:00
|
|
|
FD_CLOSEONEXEC(epfd);
|
|
|
|
|
2008-04-25 01:18:08 +00:00
|
|
|
if (!(epollop = mm_calloc(1, sizeof(struct epollop))))
|
2005-01-03 18:58:40 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
epollop->epfd = epfd;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2009-10-16 13:19:57 +00:00
|
|
|
/* Initialize fields */
|
2009-11-15 19:00:12 +00:00
|
|
|
epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
|
2005-01-03 18:58:40 +00:00
|
|
|
if (epollop->events == NULL) {
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(epollop);
|
2003-03-07 23:20:36 +00:00
|
|
|
return (NULL);
|
2005-01-03 18:58:40 +00:00
|
|
|
}
|
2009-09-11 18:47:35 +00:00
|
|
|
epollop->nevents = INITIAL_NEVENT;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-23 22:23:37 +00:00
|
|
|
evsig_init(base);
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2005-01-03 18:58:40 +00:00
|
|
|
return (epollop);
|
2003-03-07 23:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-16 19:34:09 +00:00
|
|
|
static int
|
2008-12-23 16:37:01 +00:00
|
|
|
epoll_dispatch(struct event_base *base, struct timeval *tv)
|
2003-03-07 23:20:36 +00:00
|
|
|
{
|
2008-12-23 16:37:01 +00:00
|
|
|
struct epollop *epollop = base->evbase;
|
2003-03-07 23:20:36 +00:00
|
|
|
struct epoll_event *events = epollop->events;
|
2007-07-30 22:41:00 +00:00
|
|
|
int i, res, timeout = -1;
|
|
|
|
|
|
|
|
if (tv != NULL)
|
|
|
|
timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-04-11 20:02:50 +00:00
|
|
|
if (timeout > MAX_EPOLL_TIMEOUT_MSEC) {
|
|
|
|
/* Linux kernels can wait forever if the timeout is too big;
|
|
|
|
* see comment on MAX_EPOLL_TIMEOUT_MSEC. */
|
|
|
|
timeout = MAX_EPOLL_TIMEOUT_MSEC;
|
|
|
|
}
|
|
|
|
|
2009-11-27 16:44:47 -05:00
|
|
|
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
2009-10-21 03:54:00 +00:00
|
|
|
|
2003-03-07 23:20:36 +00:00
|
|
|
res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
|
|
|
|
|
2009-11-27 16:44:47 -05:00
|
|
|
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
2009-10-21 03:54:00 +00:00
|
|
|
|
2003-03-07 23:20:36 +00:00
|
|
|
if (res == -1) {
|
|
|
|
if (errno != EINTR) {
|
2005-03-29 07:03:10 +00:00
|
|
|
event_warn("epoll_wait");
|
2003-03-07 23:20:36 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2008-12-23 22:23:37 +00:00
|
|
|
evsig_process(base);
|
2003-03-07 23:20:36 +00:00
|
|
|
return (0);
|
2008-12-23 22:23:37 +00:00
|
|
|
} else if (base->sig.evsig_caught) {
|
|
|
|
evsig_process(base);
|
2007-03-10 06:37:53 +00:00
|
|
|
}
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug(("%s: epoll_wait reports %d", __func__, res));
|
2009-10-26 20:00:43 +00:00
|
|
|
EVUTIL_ASSERT(res <= epollop->nevents);
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < res; i++) {
|
2003-10-25 21:58:33 +00:00
|
|
|
int what = events[i].events;
|
2009-09-11 18:47:35 +00:00
|
|
|
short ev = 0;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2007-11-11 03:05:03 +00:00
|
|
|
if (what & (EPOLLHUP|EPOLLERR)) {
|
2009-09-11 18:47:35 +00:00
|
|
|
ev = EV_READ | EV_WRITE;
|
2007-11-11 03:05:03 +00:00
|
|
|
} else {
|
2008-12-23 16:37:01 +00:00
|
|
|
if (what & EPOLLIN)
|
2009-09-11 18:47:35 +00:00
|
|
|
ev |= EV_READ;
|
2008-12-23 16:37:01 +00:00
|
|
|
if (what & EPOLLOUT)
|
2009-09-11 18:47:35 +00:00
|
|
|
ev |= EV_WRITE;
|
2007-11-10 05:18:11 +00:00
|
|
|
}
|
2007-11-11 03:05:03 +00:00
|
|
|
|
2009-09-11 18:47:35 +00:00
|
|
|
if (!events)
|
2007-11-11 03:05:03 +00:00
|
|
|
continue;
|
|
|
|
|
2009-09-11 18:47:35 +00:00
|
|
|
evmap_io_active(base, events[i].data.fd, ev | EV_ET);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
|
|
|
|
/* We used all of the event space this time. We should
|
|
|
|
be ready for more events next time. */
|
|
|
|
int new_nevents = epollop->nevents * 2;
|
|
|
|
struct epoll_event *new_events;
|
|
|
|
|
|
|
|
new_events = mm_realloc(epollop->events,
|
|
|
|
new_nevents * sizeof(struct epoll_event));
|
|
|
|
if (new_events) {
|
|
|
|
epollop->events = new_events;
|
|
|
|
epollop->nevents = new_nevents;
|
|
|
|
}
|
2003-03-07 23:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static int
|
2009-01-14 20:52:32 +00:00
|
|
|
epoll_add(struct event_base *base, int fd, short old, short events, void *p)
|
2003-03-07 23:20:36 +00:00
|
|
|
{
|
2008-12-23 16:37:01 +00:00
|
|
|
struct epollop *epollop = base->evbase;
|
2005-03-31 19:53:06 +00:00
|
|
|
struct epoll_event epev = {0, {0}};
|
2008-12-23 16:37:01 +00:00
|
|
|
int op, res;
|
2009-01-14 20:52:32 +00:00
|
|
|
(void)p;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
op = EPOLL_CTL_ADD;
|
2008-12-23 16:37:01 +00:00
|
|
|
res = 0;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
events |= old;
|
|
|
|
if (events & EV_READ)
|
|
|
|
res |= EPOLLIN;
|
|
|
|
if (events & EV_WRITE)
|
|
|
|
res |= EPOLLOUT;
|
|
|
|
if (events & EV_ET)
|
|
|
|
res |= EPOLLET;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
if (old != 0)
|
|
|
|
op = EPOLL_CTL_MOD;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
epev.data.fd = fd;
|
|
|
|
epev.events = res;
|
|
|
|
if (epoll_ctl(epollop->epfd, op, fd, &epev) == -1)
|
|
|
|
return (-1);
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static int
|
2009-01-14 20:52:32 +00:00
|
|
|
epoll_del(struct event_base *base, int fd, short old, short events, void *p)
|
2003-03-07 23:20:36 +00:00
|
|
|
{
|
2008-12-23 16:37:01 +00:00
|
|
|
struct epollop *epollop = base->evbase;
|
2005-03-31 19:53:06 +00:00
|
|
|
struct epoll_event epev = {0, {0}};
|
2008-12-23 16:37:01 +00:00
|
|
|
int res, op;
|
2009-01-14 20:52:32 +00:00
|
|
|
(void) p;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
|
|
|
op = EPOLL_CTL_DEL;
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
res = 0;
|
|
|
|
if (events & EV_READ)
|
|
|
|
res |= EPOLLIN;
|
|
|
|
if (events & EV_WRITE)
|
|
|
|
res |= EPOLLOUT;
|
2003-03-07 23:20:36 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
if ((res & (EPOLLIN|EPOLLOUT)) != (EPOLLIN|EPOLLOUT)) {
|
|
|
|
if ((res & EPOLLIN) && (old & EV_WRITE)) {
|
|
|
|
res = EPOLLOUT;
|
2003-03-07 23:20:36 +00:00
|
|
|
op = EPOLL_CTL_MOD;
|
2008-12-23 16:37:01 +00:00
|
|
|
} else if ((res & EPOLLOUT) && (old & EV_READ)) {
|
|
|
|
res = EPOLLIN;
|
2003-03-07 23:20:36 +00:00
|
|
|
op = EPOLL_CTL_MOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
epev.data.fd = fd;
|
|
|
|
epev.events = res;
|
2003-04-09 18:12:11 +00:00
|
|
|
|
2005-05-10 08:14:39 +00:00
|
|
|
if (epoll_ctl(epollop->epfd, op, fd, &epev) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
2003-03-07 23:20:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-03-28 04:40:54 +00:00
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static void
|
2008-12-23 16:37:01 +00:00
|
|
|
epoll_dealloc(struct event_base *base)
|
2006-03-28 04:40:54 +00:00
|
|
|
{
|
2008-12-23 16:37:01 +00:00
|
|
|
struct epollop *epollop = base->evbase;
|
2006-03-28 04:40:54 +00:00
|
|
|
|
2008-12-23 22:23:37 +00:00
|
|
|
evsig_dealloc(base);
|
2006-03-28 04:40:54 +00:00
|
|
|
if (epollop->events)
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(epollop->events);
|
2006-03-28 04:40:54 +00:00
|
|
|
if (epollop->epfd >= 0)
|
|
|
|
close(epollop->epfd);
|
|
|
|
|
|
|
|
memset(epollop, 0, sizeof(struct epollop));
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(epollop);
|
2006-03-28 04:40:54 +00:00
|
|
|
}
|