nginx-quic/src/event/modules/ngx_epoll_module.c

838 lines
20 KiB
C
Raw Normal View History

2004-01-29 18:45:01 -03:00
/*
* Copyright (C) Igor Sysoev
2012-01-18 12:07:43 -03:00
* Copyright (C) Nginx, Inc.
2004-01-29 18:45:01 -03:00
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#if (NGX_TEST_BUILD_EPOLL)
2004-01-29 18:45:01 -03:00
/* epoll declarations */
2004-02-01 05:10:52 -03:00
#define EPOLLIN 0x001
#define EPOLLPRI 0x002
#define EPOLLOUT 0x004
#define EPOLLRDNORM 0x040
#define EPOLLRDBAND 0x080
#define EPOLLWRNORM 0x100
#define EPOLLWRBAND 0x200
#define EPOLLMSG 0x400
#define EPOLLERR 0x008
#define EPOLLHUP 0x010
#define EPOLLET 0x80000000
#define EPOLLONESHOT 0x40000000
2004-01-29 18:45:01 -03:00
#define EPOLL_CTL_ADD 1
#define EPOLL_CTL_DEL 2
#define EPOLL_CTL_MOD 3
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event {
uint32_t events;
epoll_data_t data;
};
int epoll_create(int size);
2004-01-29 18:45:01 -03:00
int epoll_create(int size)
{
return -1;
}
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
2004-01-29 18:45:01 -03:00
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
return -1;
}
int epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout);
2004-01-29 18:45:01 -03:00
int epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout)
{
return -1;
}
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
#define SYS_io_setup 245
#define SYS_io_destroy 246
#define SYS_io_getevents 247
#define SYS_eventfd 323
typedef u_int aio_context_t;
struct io_event {
uint64_t data; /* the data field from the iocb */
uint64_t obj; /* what iocb this event came from */
int64_t res; /* result code for this event */
int64_t res2; /* secondary result */
};
#endif
2004-01-29 18:45:01 -03:00
#endif
typedef struct {
ngx_uint_t events;
ngx_uint_t aio_requests;
2004-01-29 18:45:01 -03:00
} ngx_epoll_conf_t;
static ngx_int_t ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
2004-01-29 18:45:01 -03:00
static void ngx_epoll_done(ngx_cycle_t *cycle);
static ngx_int_t ngx_epoll_add_event(ngx_event_t *ev, ngx_int_t event,
ngx_uint_t flags);
static ngx_int_t ngx_epoll_del_event(ngx_event_t *ev, ngx_int_t event,
ngx_uint_t flags);
static ngx_int_t ngx_epoll_add_connection(ngx_connection_t *c);
static ngx_int_t ngx_epoll_del_connection(ngx_connection_t *c,
ngx_uint_t flags);
static ngx_int_t ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
ngx_uint_t flags);
2004-01-29 18:45:01 -03:00
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
static void ngx_epoll_eventfd_handler(ngx_event_t *ev);
#endif
2004-01-29 18:45:01 -03:00
static void *ngx_epoll_create_conf(ngx_cycle_t *cycle);
static char *ngx_epoll_init_conf(ngx_cycle_t *cycle, void *conf);
static int ep = -1;
static struct epoll_event *event_list;
static ngx_uint_t nevents;
2004-01-29 18:45:01 -03:00
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
int ngx_eventfd = -1;
aio_context_t ngx_aio_ctx = 0;
static ngx_event_t ngx_eventfd_event;
static ngx_connection_t ngx_eventfd_conn;
#endif
2004-01-29 18:45:01 -03:00
static ngx_str_t epoll_name = ngx_string("epoll");
static ngx_command_t ngx_epoll_commands[] = {
{ ngx_string("epoll_events"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_epoll_conf_t, events),
NULL },
2004-01-29 18:45:01 -03:00
{ ngx_string("worker_aio_requests"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_epoll_conf_t, aio_requests),
NULL },
ngx_null_command
2004-01-29 18:45:01 -03:00
};
ngx_event_module_t ngx_epoll_module_ctx = {
&epoll_name,
ngx_epoll_create_conf, /* create configuration */
ngx_epoll_init_conf, /* init configuration */
{
2004-01-30 14:39:00 -03:00
ngx_epoll_add_event, /* add an event */
ngx_epoll_del_event, /* delete an event */
ngx_epoll_add_event, /* enable an event */
ngx_epoll_del_event, /* disable an event */
2004-07-07 11:01:00 -04:00
ngx_epoll_add_connection, /* add an connection */
ngx_epoll_del_connection, /* delete an connection */
2004-07-07 02:15:04 -04:00
NULL, /* process the changes */
2004-01-29 18:45:01 -03:00
ngx_epoll_process_events, /* process the events */
ngx_epoll_init, /* init the events */
ngx_epoll_done, /* done the events */
}
};
ngx_module_t ngx_epoll_module = {
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 10:58:06 -04:00
NGX_MODULE_V1,
2004-01-29 18:45:01 -03:00
&ngx_epoll_module_ctx, /* module context */
ngx_epoll_commands, /* module directives */
2004-07-07 11:01:00 -04:00
NGX_EVENT_MODULE, /* module type */
NULL, /* init master */
2004-07-07 11:01:00 -04:00
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
2004-01-29 18:45:01 -03:00
};
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
/*
* We call io_setup(), io_destroy() io_submit(), and io_getevents() directly
* as syscalls instead of libaio usage, because the library header file
* supports eventfd() since 0.3.107 version only.
*
* Also we do not use eventfd() in glibc, because glibc supports it
* since 2.8 version and glibc maps two syscalls eventfd() and eventfd2()
* into single eventfd() function with different number of parameters.
*/
static int
2009-08-28 04:12:35 -04:00
io_setup(u_int nr_reqs, aio_context_t *ctx)
{
return syscall(SYS_io_setup, nr_reqs, ctx);
}
static int
io_destroy(aio_context_t ctx)
{
return syscall(SYS_io_destroy, ctx);
}
static int
2009-08-28 04:12:35 -04:00
io_getevents(aio_context_t ctx, long min_nr, long nr, struct io_event *events,
struct timespec *tmo)
{
return syscall(SYS_io_getevents, ctx, min_nr, nr, events, tmo);
}
static void
ngx_epoll_aio_init(ngx_cycle_t *cycle, ngx_epoll_conf_t *epcf)
{
int n;
struct epoll_event ee;
ngx_eventfd = syscall(SYS_eventfd, 0);
if (ngx_eventfd == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"eventfd() failed");
ngx_file_aio = 0;
return;
}
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"eventfd: %d", ngx_eventfd);
n = 1;
if (ioctl(ngx_eventfd, FIONBIO, &n) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"ioctl(eventfd, FIONBIO) failed");
goto failed;
}
if (io_setup(epcf->aio_requests, &ngx_aio_ctx) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"io_setup() failed");
goto failed;
}
ngx_eventfd_event.data = &ngx_eventfd_conn;
ngx_eventfd_event.handler = ngx_epoll_eventfd_handler;
ngx_eventfd_event.log = cycle->log;
ngx_eventfd_event.active = 1;
ngx_eventfd_conn.fd = ngx_eventfd;
ngx_eventfd_conn.read = &ngx_eventfd_event;
ngx_eventfd_conn.log = cycle->log;
ee.events = EPOLLIN|EPOLLET;
ee.data.ptr = &ngx_eventfd_conn;
if (epoll_ctl(ep, EPOLL_CTL_ADD, ngx_eventfd, &ee) != -1) {
return;
}
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"epoll_ctl(EPOLL_CTL_ADD, eventfd) failed");
if (io_destroy(ngx_aio_ctx) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"io_destroy() failed");
}
failed:
if (close(ngx_eventfd) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"eventfd close() failed");
}
ngx_eventfd = -1;
ngx_aio_ctx = 0;
ngx_file_aio = 0;
}
2009-08-28 04:12:35 -04:00
#endif
static ngx_int_t
ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
2004-01-29 18:45:01 -03:00
{
ngx_epoll_conf_t *epcf;
epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module);
if (ep == -1) {
ep = epoll_create(cycle->connection_n / 2);
2004-01-29 18:45:01 -03:00
if (ep == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"epoll_create() failed");
return NGX_ERROR;
}
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
ngx_epoll_aio_init(cycle, epcf);
2009-08-28 04:12:35 -04:00
#endif
2004-01-29 18:45:01 -03:00
}
if (nevents < epcf->events) {
if (event_list) {
ngx_free(event_list);
}
event_list = ngx_alloc(sizeof(struct epoll_event) * epcf->events,
cycle->log);
if (event_list == NULL) {
return NGX_ERROR;
}
}
nevents = epcf->events;
ngx_io = ngx_os_io;
ngx_event_actions = ngx_epoll_module_ctx.actions;
#if (NGX_HAVE_CLEAR_EVENT)
2004-04-04 16:32:09 -04:00
ngx_event_flags = NGX_USE_CLEAR_EVENT
2004-02-12 17:57:10 -03:00
#else
2004-04-04 16:32:09 -04:00
ngx_event_flags = NGX_USE_LEVEL_EVENT
2004-02-12 17:57:10 -03:00
#endif
|NGX_USE_GREEDY_EVENT
2004-07-07 11:01:00 -04:00
|NGX_USE_EPOLL_EVENT;
2004-01-29 18:45:01 -03:00
return NGX_OK;
}
static void
ngx_epoll_done(ngx_cycle_t *cycle)
2004-01-29 18:45:01 -03:00
{
if (close(ep) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"epoll close() failed");
}
ep = -1;
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
if (ngx_eventfd != -1) {
if (io_destroy(ngx_aio_ctx) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"io_destroy() failed");
}
if (close(ngx_eventfd) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"eventfd close() failed");
}
ngx_eventfd = -1;
2009-08-28 04:12:35 -04:00
}
ngx_aio_ctx = 0;
#endif
2004-01-29 18:45:01 -03:00
ngx_free(event_list);
event_list = NULL;
nevents = 0;
}
static ngx_int_t
ngx_epoll_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
2004-01-30 14:39:00 -03:00
{
int op;
uint32_t events, prev;
2004-02-12 17:57:10 -03:00
ngx_event_t *e;
2004-02-11 04:19:26 -03:00
ngx_connection_t *c;
2004-02-12 17:57:10 -03:00
struct epoll_event ee;
2004-01-30 14:39:00 -03:00
c = ev->data;
events = (uint32_t) event;
2004-01-30 14:39:00 -03:00
if (event == NGX_READ_EVENT) {
2004-02-12 17:57:10 -03:00
e = c->write;
prev = EPOLLOUT;
#if (NGX_READ_EVENT != EPOLLIN)
events = EPOLLIN;
2004-02-12 17:57:10 -03:00
#endif
2004-01-30 14:39:00 -03:00
} else {
2004-02-12 17:57:10 -03:00
e = c->read;
prev = EPOLLIN;
#if (NGX_WRITE_EVENT != EPOLLOUT)
events = EPOLLOUT;
2004-02-11 04:19:26 -03:00
#endif
2004-02-12 17:57:10 -03:00
}
2004-01-30 14:39:00 -03:00
2004-02-12 17:57:10 -03:00
if (e->active) {
op = EPOLL_CTL_MOD;
events |= prev;
2004-02-11 14:08:49 -03:00
2004-02-12 17:57:10 -03:00
} else {
op = EPOLL_CTL_ADD;
}
2004-02-11 14:08:49 -03:00
ee.events = events | (uint32_t) flags;
ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
2004-02-12 17:57:10 -03:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"epoll add event: fd:%d op:%d ev:%08XD",
2004-02-12 17:57:10 -03:00
c->fd, op, ee.events);
if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"epoll_ctl(%d, %d) failed", op, c->fd);
2004-01-30 14:39:00 -03:00
return NGX_ERROR;
}
2004-02-01 05:10:52 -03:00
ev->active = 1;
2004-02-12 17:57:10 -03:00
#if 0
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
#endif
2004-02-01 05:10:52 -03:00
2004-01-30 14:39:00 -03:00
return NGX_OK;
}
static ngx_int_t
ngx_epoll_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
2004-01-30 14:39:00 -03:00
{
int op;
uint32_t prev;
2004-02-12 17:57:10 -03:00
ngx_event_t *e;
2004-02-11 04:19:26 -03:00
ngx_connection_t *c;
2004-02-12 17:57:10 -03:00
struct epoll_event ee;
/*
* when the file descriptor is closed, the epoll automatically deletes
* it from its queue, so we do not need to delete explicitly the event
2004-04-04 16:32:09 -04:00
* before the closing the file descriptor
2004-02-12 17:57:10 -03:00
*/
if (flags & NGX_CLOSE_EVENT) {
ev->active = 0;
return NGX_OK;
}
2004-01-30 14:39:00 -03:00
c = ev->data;
2004-02-12 17:57:10 -03:00
if (event == NGX_READ_EVENT) {
e = c->write;
prev = EPOLLOUT;
2004-01-30 14:39:00 -03:00
2004-02-12 17:57:10 -03:00
} else {
e = c->read;
prev = EPOLLIN;
}
2004-01-30 14:39:00 -03:00
2004-02-12 17:57:10 -03:00
if (e->active) {
op = EPOLL_CTL_MOD;
ee.events = prev | (uint32_t) flags;
ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
2004-02-12 17:57:10 -03:00
} else {
op = EPOLL_CTL_DEL;
ee.events = 0;
ee.data.ptr = NULL;
}
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"epoll del event: fd:%d op:%d ev:%08XD",
2004-02-12 17:57:10 -03:00
c->fd, op, ee.events);
if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"epoll_ctl(%d, %d) failed", op, c->fd);
2004-01-30 14:39:00 -03:00
return NGX_ERROR;
}
2004-02-01 05:10:52 -03:00
ev->active = 0;
2004-01-30 14:39:00 -03:00
return NGX_OK;
}
static ngx_int_t
ngx_epoll_add_connection(ngx_connection_t *c)
2004-01-29 18:45:01 -03:00
{
2004-02-01 05:10:52 -03:00
struct epoll_event ee;
2004-01-29 18:45:01 -03:00
2004-02-09 04:46:43 -03:00
ee.events = EPOLLIN|EPOLLOUT|EPOLLET;
ee.data.ptr = (void *) ((uintptr_t) c | c->read->instance);
2004-01-29 18:45:01 -03:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
"epoll add connection: fd:%d ev:%08XD", c->fd, ee.events);
2004-01-29 18:45:01 -03:00
2004-02-01 05:10:52 -03:00
if (epoll_ctl(ep, EPOLL_CTL_ADD, c->fd, &ee) == -1) {
2004-01-29 18:45:01 -03:00
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
2004-01-30 14:39:00 -03:00
"epoll_ctl(EPOLL_CTL_ADD, %d) failed", c->fd);
2004-01-29 18:45:01 -03:00
return NGX_ERROR;
}
2004-01-30 14:39:00 -03:00
c->read->active = 1;
c->write->active = 1;
2004-01-29 18:45:01 -03:00
return NGX_OK;
}
static ngx_int_t
ngx_epoll_del_connection(ngx_connection_t *c, ngx_uint_t flags)
2004-01-29 18:45:01 -03:00
{
int op;
struct epoll_event ee;
2004-07-07 11:01:00 -04:00
/*
* when the file descriptor is closed the epoll automatically deletes
* it from its queue so we do not need to delete explicitly the event
2004-07-07 11:01:00 -04:00
* before the closing the file descriptor
*/
if (flags & NGX_CLOSE_EVENT) {
c->read->active = 0;
c->write->active = 0;
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"epoll del connection: fd:%d", c->fd);
op = EPOLL_CTL_DEL;
ee.events = 0;
ee.data.ptr = NULL;
2004-07-07 11:01:00 -04:00
if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"epoll_ctl(%d, %d) failed", op, c->fd);
return NGX_ERROR;
}
2004-01-29 18:45:01 -03:00
c->read->active = 0;
c->write->active = 0;
return NGX_OK;
}
static ngx_int_t
ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
2004-01-29 18:45:01 -03:00
{
2004-04-04 16:32:09 -04:00
int events;
uint32_t revents;
2004-04-04 16:32:09 -04:00
ngx_int_t instance, i;
ngx_uint_t level;
2004-04-04 16:32:09 -04:00
ngx_err_t err;
ngx_event_t *rev, *wev, **queue;
2004-04-04 16:32:09 -04:00
ngx_connection_t *c;
2004-01-29 18:45:01 -03:00
2004-04-14 16:34:05 -04:00
/* NGX_TIMER_INFINITE == INFTIM */
2004-04-02 11:13:20 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"epoll timer: %M", timer);
2004-01-29 18:45:01 -03:00
events = epoll_wait(ep, event_list, (int) nevents, timer);
2004-01-29 18:45:01 -03:00
2009-08-25 05:06:21 -04:00
err = (events == -1) ? ngx_errno : 0;
2004-01-29 18:45:01 -03:00
if (flags & NGX_UPDATE_TIME || ngx_event_timer_alarm) {
ngx_time_update();
}
2004-01-29 18:45:01 -03:00
if (err) {
if (err == NGX_EINTR) {
2004-01-29 18:45:01 -03:00
if (ngx_event_timer_alarm) {
ngx_event_timer_alarm = 0;
return NGX_OK;
}
level = NGX_LOG_INFO;
} else {
level = NGX_LOG_ALERT;
}
2004-07-07 11:01:00 -04:00
ngx_log_error(level, cycle->log, err, "epoll_wait() failed");
return NGX_ERROR;
}
2004-07-07 11:01:00 -04:00
if (events == 0) {
if (timer != NGX_TIMER_INFINITE) {
return NGX_OK;
}
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"epoll_wait() returned no events without timeout");
return NGX_ERROR;
2004-04-04 16:32:09 -04:00
}
ngx_mutex_lock(ngx_posted_events_mutex);
2004-01-29 18:45:01 -03:00
for (i = 0; i < events; i++) {
c = event_list[i].data.ptr;
instance = (uintptr_t) c & 1;
c = (ngx_connection_t *) ((uintptr_t) c & (uintptr_t) ~1);
2004-07-07 11:01:00 -04:00
rev = c->read;
2004-04-02 11:13:20 -04:00
2004-07-07 11:01:00 -04:00
if (c->fd == -1 || rev->instance != instance) {
2004-01-29 18:45:01 -03:00
/*
2004-02-01 05:10:52 -03:00
* the stale event from a file descriptor
2004-01-29 18:45:01 -03:00
* that was just closed in this iteration
*/
2004-04-13 01:27:03 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"epoll: stale event %p", c);
2004-01-29 18:45:01 -03:00
continue;
}
revents = event_list[i].events;
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"epoll: fd:%d ev:%04XD d:%p",
c->fd, revents, event_list[i].data.ptr);
2004-04-12 12:38:09 -04:00
if (revents & (EPOLLERR|EPOLLHUP)) {
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"epoll_wait() error on fd:%d ev:%04XD",
c->fd, revents);
2004-01-29 18:45:01 -03:00
}
2004-01-30 14:39:00 -03:00
#if 0
if (revents & ~(EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"strange epoll_wait() events fd:%d ev:%04XD",
c->fd, revents);
}
#endif
if ((revents & (EPOLLERR|EPOLLHUP))
&& (revents & (EPOLLIN|EPOLLOUT)) == 0)
{
/*
* if the error events were returned without EPOLLIN or EPOLLOUT,
* then add these flags to handle the events at least in one
* active handler
*/
revents |= EPOLLIN|EPOLLOUT;
2004-01-30 14:39:00 -03:00
}
if ((revents & EPOLLIN) && rev->active) {
if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
2004-07-07 11:01:00 -04:00
rev->posted_ready = 1;
} else {
rev->ready = 1;
2004-07-07 11:01:00 -04:00
}
if (flags & NGX_POST_EVENTS) {
queue = (ngx_event_t **) (rev->accept ?
&ngx_posted_accept_events : &ngx_posted_events);
2004-04-04 16:32:09 -04:00
ngx_locked_post_event(rev, queue);
2004-04-04 16:32:09 -04:00
} else {
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 10:58:06 -04:00
rev->handler(rev);
2004-04-04 16:32:09 -04:00
}
2004-02-01 05:10:52 -03:00
}
2004-01-29 18:45:01 -03:00
wev = c->write;
2004-04-21 14:54:33 -04:00
if ((revents & EPOLLOUT) && wev->active) {
if (c->fd == -1 || wev->instance != instance) {
/*
* the stale event from a file descriptor
* that was just closed in this iteration
*/
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"epoll: stale event %p", c);
continue;
}
2004-04-04 16:32:09 -04:00
if (flags & NGX_POST_THREAD_EVENTS) {
wev->posted_ready = 1;
} else {
wev->ready = 1;
}
2004-01-29 18:45:01 -03:00
if (flags & NGX_POST_EVENTS) {
ngx_locked_post_event(wev, &ngx_posted_events);
2004-07-07 11:01:00 -04:00
} else {
wev->handler(wev);
}
2004-07-07 11:01:00 -04:00
}
2004-04-04 16:32:09 -04:00
}
ngx_mutex_unlock(ngx_posted_events_mutex);
2004-01-29 18:45:01 -03:00
return NGX_OK;
}
2009-08-28 04:12:35 -04:00
#if (NGX_HAVE_FILE_AIO)
static void
ngx_epoll_eventfd_handler(ngx_event_t *ev)
{
int n, events;
long i;
2009-08-28 04:12:35 -04:00
uint64_t ready;
ngx_err_t err;
ngx_event_t *e;
ngx_event_aio_t *aio;
struct io_event event[64];
struct timespec ts;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "eventfd handler");
n = read(ngx_eventfd, &ready, 8);
err = ngx_errno;
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, "eventfd: %d", n);
if (n != 8) {
if (n == -1) {
if (err == NGX_EAGAIN) {
return;
}
ngx_log_error(NGX_LOG_ALERT, ev->log, err, "read(eventfd) failed");
return;
}
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"read(eventfd) returned only %d bytes", n);
return;
}
ts.tv_sec = 0;
ts.tv_nsec = 0;
while (ready) {
events = io_getevents(ngx_aio_ctx, 1, 64, event, &ts);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"io_getevents: %l", events);
if (events > 0) {
ready -= events;
for (i = 0; i < events; i++) {
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"io_event: %uXL %uXL %L %L",
event[i].data, event[i].obj,
event[i].res, event[i].res2);
e = (ngx_event_t *) (uintptr_t) event[i].data;
e->complete = 1;
e->active = 0;
e->ready = 1;
aio = e->data;
aio->res = event[i].res;
ngx_post_event(e, &ngx_posted_events);
}
continue;
}
if (events == 0) {
return;
}
/* events == -1 */
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"io_getevents() failed");
2009-08-28 04:12:35 -04:00
return;
}
}
#endif
static void *
ngx_epoll_create_conf(ngx_cycle_t *cycle)
2004-01-29 18:45:01 -03:00
{
ngx_epoll_conf_t *epcf;
epcf = ngx_palloc(cycle->pool, sizeof(ngx_epoll_conf_t));
if (epcf == NULL) {
return NULL;
}
2004-01-29 18:45:01 -03:00
epcf->events = NGX_CONF_UNSET;
epcf->aio_requests = NGX_CONF_UNSET;
2004-01-29 18:45:01 -03:00
return epcf;
}
static char *
ngx_epoll_init_conf(ngx_cycle_t *cycle, void *conf)
2004-01-29 18:45:01 -03:00
{
ngx_epoll_conf_t *epcf = conf;
ngx_conf_init_uint_value(epcf->events, 512);
ngx_conf_init_uint_value(epcf->aio_requests, 32);
2004-01-29 18:45:01 -03:00
return NGX_CONF_OK;
}