nginx/src/event/ngx_event.c

415 lines
10 KiB
C
Raw Normal View History

#include <ngx_config.h>
2002-09-27 23:05:29 +08:00
#include <ngx_core.h>
#include <ngx_event.h>
2003-02-07 01:21:13 +08:00
2003-07-04 23:10:33 +08:00
#define DEFAULT_CONNECTIONS 512
2003-02-07 01:21:13 +08:00
2003-05-20 23:37:55 +08:00
2003-05-29 21:02:09 +08:00
extern ngx_module_t ngx_select_module;
2003-02-07 01:21:13 +08:00
#if (HAVE_KQUEUE)
#include <ngx_kqueue_module.h>
#endif
2003-05-20 23:37:55 +08:00
#if (HAVE_DEVPOLL)
2003-05-27 20:18:54 +08:00
extern ngx_module_t ngx_devpoll_module;
2003-05-20 23:37:55 +08:00
#endif
2003-02-27 04:21:43 +08:00
#if (HAVE_AIO)
#include <ngx_aio_module.h>
#endif
2003-07-04 23:10:33 +08:00
static int ngx_event_init(ngx_cycle_t *cycle);
2003-05-27 20:18:54 +08:00
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
2003-07-07 14:11:50 +08:00
static void *ngx_event_create_conf(ngx_cycle_t *cycle);
static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
2003-05-16 23:27:48 +08:00
2003-02-11 15:14:40 +08:00
int ngx_event_flags;
ngx_event_actions_t ngx_event_actions;
2003-07-04 23:10:33 +08:00
static int ngx_event_max_module;
2003-05-16 23:27:48 +08:00
static ngx_str_t events_name = ngx_string("events");
static ngx_command_t ngx_events_commands[] = {
{ngx_string("events"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_events_block,
0,
0,
NULL},
2003-05-27 20:18:54 +08:00
ngx_null_command
2003-05-16 23:27:48 +08:00
};
ngx_module_t ngx_events_module = {
2003-05-27 20:18:54 +08:00
NGX_MODULE,
2003-05-16 23:27:48 +08:00
&events_name, /* module context */
ngx_events_commands, /* module directives */
2003-05-27 20:18:54 +08:00
NGX_CORE_MODULE, /* module type */
2003-07-04 23:10:33 +08:00
NULL, /* init module */
NULL /* init child */
2003-05-16 23:27:48 +08:00
};
2003-07-01 23:00:03 +08:00
static ngx_str_t event_core_name = ngx_string("event_core");
2003-05-16 23:27:48 +08:00
2003-07-01 23:00:03 +08:00
static ngx_command_t ngx_event_core_commands[] = {
2003-05-16 23:27:48 +08:00
{ngx_string("connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
2003-05-20 00:39:14 +08:00
offsetof(ngx_event_conf_t, connections),
2003-05-16 23:27:48 +08:00
NULL},
2003-05-21 21:28:21 +08:00
{ngx_string("use"),
2003-05-16 23:27:48 +08:00
NGX_EVENT_CONF|NGX_CONF_TAKE1,
2003-05-21 21:28:21 +08:00
ngx_event_use,
2003-05-16 23:27:48 +08:00
0,
0,
NULL},
2003-05-20 00:39:14 +08:00
{ngx_string("timer_queues"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_event_conf_t, timer_queues),
NULL},
2003-05-16 23:27:48 +08:00
2003-05-27 20:18:54 +08:00
ngx_null_command
2003-05-16 23:27:48 +08:00
};
2003-07-01 23:00:03 +08:00
ngx_event_module_t ngx_event_core_module_ctx = {
&event_core_name,
2003-05-20 00:39:14 +08:00
ngx_event_create_conf, /* create configuration */
ngx_event_init_conf, /* init configuration */
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
2003-07-01 23:00:03 +08:00
ngx_module_t ngx_event_core_module = {
2003-05-27 20:18:54 +08:00
NGX_MODULE,
2003-07-01 23:00:03 +08:00
&ngx_event_core_module_ctx, /* module context */
ngx_event_core_commands, /* module directives */
2003-05-27 20:18:54 +08:00
NGX_EVENT_MODULE, /* module type */
2003-07-07 14:11:50 +08:00
NULL, /* init module */
ngx_event_init /* init child */
2003-05-16 23:27:48 +08:00
};
2003-07-04 23:10:33 +08:00
static int ngx_event_init(ngx_cycle_t *cycle)
{
2003-05-29 21:02:09 +08:00
int m, i, fd;
2003-06-11 23:28:34 +08:00
ngx_event_t *rev, *wev;
ngx_listening_t *s;
2003-05-29 21:02:09 +08:00
ngx_connection_t *c;
ngx_event_conf_t *ecf;
2003-05-20 00:39:14 +08:00
ngx_event_module_t *module;
2003-06-11 23:28:34 +08:00
#if (WIN32)
ngx_iocp_conf_t *iocpcf;
#endif
2003-07-04 14:03:52 +08:00
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
2003-07-04 23:10:33 +08:00
ngx_log_debug(cycle->log, "CONN: %d" _ ecf->connections);
ngx_log_debug(cycle->log, "TYPE: %d" _ ecf->use);
2003-02-07 01:21:13 +08:00
2003-07-07 14:11:50 +08:00
cycle->connection_n = ecf->connections;
2003-05-20 00:39:14 +08:00
for (m = 0; ngx_modules[m]; m++) {
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
2003-05-20 00:39:14 +08:00
continue;
}
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->ctx_index == ecf->use) {
module = ngx_modules[m]->ctx;
2003-07-04 23:10:33 +08:00
if (module->actions.init(cycle) == NGX_ERROR) {
2003-05-20 00:39:14 +08:00
return NGX_ERROR;
}
break;
}
2003-01-09 13:36:00 +08:00
}
2003-07-07 14:11:50 +08:00
if (cycle->old_cycle && cycle->old_cycle->connection_n < ecf->connections) {
2003-07-04 23:10:33 +08:00
/* TODO: push into delayed array and temporary pool */
2003-07-07 14:11:50 +08:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "NOT READY: connections");
2003-07-04 23:10:33 +08:00
exit(1);
2003-07-04 14:03:52 +08:00
}
2003-07-07 14:11:50 +08:00
ngx_test_null(cycle->connections,
2003-07-04 23:10:33 +08:00
ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
cycle->log),
2003-05-20 00:39:14 +08:00
NGX_ERROR);
2003-07-07 14:11:50 +08:00
c = cycle->connections;
for (i = 0; i < cycle->connection_n; i++) {
c[i].fd = -1;
}
ngx_test_null(cycle->read_events,
2003-07-04 23:10:33 +08:00
ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log),
2003-05-20 00:39:14 +08:00
NGX_ERROR);
2003-07-07 14:11:50 +08:00
ngx_test_null(cycle->write_events,
2003-07-04 23:10:33 +08:00
ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log),
2003-05-20 00:39:14 +08:00
NGX_ERROR);
/* for each listening socket */
2003-06-11 23:28:34 +08:00
2003-07-07 14:11:50 +08:00
s = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
2002-08-20 22:48:28 +08:00
fd = s[i].fd;
2003-06-11 23:28:34 +08:00
#if (WIN32)
/*
* Winsock assignes a socket number divisible by 4
* so to find a connection we divide a socket number by 4.
*/
2003-07-07 14:11:50 +08:00
fd /= 4;
2003-06-11 23:28:34 +08:00
#endif
2002-12-23 14:29:22 +08:00
2003-07-07 14:11:50 +08:00
c = &cycle->connections[fd];
rev = &cycle->read_events[fd];
wev = &cycle->write_events[fd];
2003-01-09 13:36:00 +08:00
ngx_memzero(c, sizeof(ngx_connection_t));
2003-06-11 23:28:34 +08:00
ngx_memzero(rev, sizeof(ngx_event_t));
2003-07-07 14:11:50 +08:00
c->fd = s[i].fd;
2003-06-11 23:28:34 +08:00
c->listening = &s[i];
2003-01-09 13:36:00 +08:00
c->ctx = s[i].ctx;
c->servers = s[i].servers;
c->log = s[i].log;
2003-07-07 14:11:50 +08:00
c->read = rev;
2002-08-26 23:18:19 +08:00
2003-07-07 14:11:50 +08:00
/* required by iocp in "c->write->active = 1" */
c->write = wev;
#if 0
2003-07-04 23:10:33 +08:00
ngx_test_null(rev->log, ngx_palloc(cycle->pool, sizeof(ngx_log_t)),
NGX_ERROR);
2003-05-20 00:39:14 +08:00
2003-06-11 23:28:34 +08:00
ngx_memcpy(rev->log, c->log, sizeof(ngx_log_t));
2003-07-07 14:11:50 +08:00
#endif
rev->log = c->log;
2003-06-11 23:28:34 +08:00
rev->data = c;
rev->index = NGX_INVALID_INDEX;
2003-07-07 14:11:50 +08:00
2003-02-07 01:21:13 +08:00
#if 0
2003-06-11 23:28:34 +08:00
rev->listening = 1;
2003-02-07 01:21:13 +08:00
#endif
2003-06-11 23:28:34 +08:00
rev->available = 0;
#if (HAVE_DEFERRED_ACCEPT)
2003-06-11 23:28:34 +08:00
rev->deferred_accept = s[i].deferred_accept;
#endif
2003-02-07 01:21:13 +08:00
2003-07-07 14:11:50 +08:00
/* required by poll */
wev->index = NGX_INVALID_INDEX;
if ((ngx_event_flags & NGX_HAVE_IOCP_EVENT) == 0) {
if (s[i].remain) {
if (ngx_del_event(&cycle->old_cycle->read_events[fd],
NGX_READ_EVENT, 0) == NGX_ERROR) {
return NGX_ERROR;
}
cycle->old_cycle->connections[fd].fd = -1;
}
}
2003-06-11 23:28:34 +08:00
#if (WIN32)
2003-02-07 01:21:13 +08:00
if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
2003-06-11 23:28:34 +08:00
rev->event_handler = &ngx_event_acceptex;
2003-02-07 01:21:13 +08:00
2003-06-11 23:28:34 +08:00
if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
2003-02-07 01:21:13 +08:00
return NGX_ERROR;
}
2003-07-07 14:11:50 +08:00
iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
2003-06-11 23:28:34 +08:00
if (ngx_event_post_acceptex(&s[i], iocpcf->acceptex) == NGX_ERROR) {
return NGX_ERROR;
}
2003-02-07 01:21:13 +08:00
} else {
2003-06-11 23:28:34 +08:00
rev->event_handler = &ngx_event_accept;
ngx_add_event(rev, NGX_READ_EVENT, 0);
2003-02-07 01:21:13 +08:00
}
#else
2003-06-11 23:28:34 +08:00
rev->event_handler = &ngx_event_accept;
ngx_add_event(rev, NGX_READ_EVENT, 0);
2003-02-07 01:21:13 +08:00
#endif
}
2003-05-20 00:39:14 +08:00
return NGX_OK;
2002-08-20 22:48:28 +08:00
}
2003-05-16 23:27:48 +08:00
2003-05-27 20:18:54 +08:00
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2003-05-16 23:27:48 +08:00
{
2003-05-20 00:39:14 +08:00
int m;
char *rv;
void ***ctx;
ngx_conf_t pcf;
ngx_event_module_t *module;
/* count the number of the event modules and set up their indices */
ngx_event_max_module = 0;
for (m = 0; ngx_modules[m]; m++) {
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
2003-05-20 00:39:14 +08:00
continue;
}
2003-05-16 23:27:48 +08:00
2003-05-27 20:18:54 +08:00
ngx_modules[m]->ctx_index = ngx_event_max_module++;
2003-05-20 00:39:14 +08:00
}
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR);
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
ngx_test_null(*ctx,
ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)),
NGX_CONF_ERROR);
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
*(void **) conf = ctx;
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
for (m = 0; ngx_modules[m]; m++) {
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
2003-05-20 00:39:14 +08:00
continue;
}
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
module = ngx_modules[m]->ctx;
2003-05-16 23:27:48 +08:00
2003-05-20 00:39:14 +08:00
if (module->create_conf) {
2003-05-27 20:18:54 +08:00
ngx_test_null((*ctx)[ngx_modules[m]->ctx_index],
2003-07-07 14:11:50 +08:00
module->create_conf(cf->cycle),
2003-05-20 00:39:14 +08:00
NGX_CONF_ERROR);
}
}
2003-05-16 23:27:48 +08:00
pcf = *cf;
2003-05-20 00:39:14 +08:00
cf->ctx = ctx;
2003-05-27 20:18:54 +08:00
cf->module_type = NGX_EVENT_MODULE;
2003-05-16 23:27:48 +08:00
cf->cmd_type = NGX_EVENT_CONF;
rv = ngx_conf_parse(cf, NULL);
*cf = pcf;
if (rv != NGX_CONF_OK)
return rv;
2003-05-20 00:39:14 +08:00
for (m = 0; ngx_modules[m]; m++) {
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
2003-05-20 00:39:14 +08:00
continue;
}
module = ngx_modules[m]->ctx;
if (module->init_conf) {
2003-07-07 14:11:50 +08:00
rv = module->init_conf(cf->cycle,
(*ctx)[ngx_modules[m]->ctx_index]);
2003-05-20 00:39:14 +08:00
if (rv != NGX_CONF_OK) {
return rv;
}
}
}
return NGX_CONF_OK;
}
2003-05-27 20:18:54 +08:00
static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2003-05-20 00:39:14 +08:00
{
2003-05-27 20:18:54 +08:00
ngx_event_conf_t *ecf = conf;
2003-05-20 00:39:14 +08:00
int m;
ngx_str_t *args;
ngx_event_module_t *module;
2003-05-21 21:28:21 +08:00
if (ecf->use != NGX_CONF_UNSET) {
return "is duplicate" ;
2003-05-20 00:39:14 +08:00
}
args = cf->args->elts;
for (m = 0; ngx_modules[m]; m++) {
2003-05-27 20:18:54 +08:00
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
2003-05-20 00:39:14 +08:00
continue;
}
module = ngx_modules[m]->ctx;
if (module->name->len == args[1].len) {
if (ngx_strcmp(module->name->data, args[1].data) == 0) {
2003-05-27 20:18:54 +08:00
ecf->use = ngx_modules[m]->ctx_index;
2003-05-20 00:39:14 +08:00
return NGX_CONF_OK;
}
}
}
return "invalid event type";
}
2003-07-07 14:11:50 +08:00
static void *ngx_event_create_conf(ngx_cycle_t *cycle)
2003-05-20 00:39:14 +08:00
{
ngx_event_conf_t *ecf;
2003-07-07 14:11:50 +08:00
ngx_test_null(ecf, ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t)),
2003-05-20 00:39:14 +08:00
NGX_CONF_ERROR);
ecf->connections = NGX_CONF_UNSET;
2003-05-20 23:37:55 +08:00
ecf->timer_queues = NGX_CONF_UNSET;
2003-05-21 21:28:21 +08:00
ecf->use = NGX_CONF_UNSET;
2003-05-20 00:39:14 +08:00
return ecf;
}
2003-07-07 14:11:50 +08:00
static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
2003-05-20 00:39:14 +08:00
{
ngx_event_conf_t *ecf = conf;
#if (HAVE_KQUEUE)
2003-07-04 23:10:33 +08:00
ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS);
2003-05-27 20:18:54 +08:00
ngx_conf_init_value(ecf->use, ngx_kqueue_module.ctx_index);
2003-05-20 00:39:14 +08:00
2003-05-20 23:37:55 +08:00
#elif (HAVE_DEVPOLL)
2003-07-04 23:10:33 +08:00
ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS);
2003-05-27 20:18:54 +08:00
ngx_conf_init_value(ecf->use, ngx_devpoll_module.ctx_index);
2003-05-20 23:37:55 +08:00
2003-05-20 00:39:14 +08:00
#else /* HAVE_SELECT */
ngx_conf_init_value(ecf->connections,
2003-07-04 23:10:33 +08:00
FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS);
2003-05-20 00:39:14 +08:00
2003-05-27 20:18:54 +08:00
ngx_conf_init_value(ecf->use, ngx_select_module.ctx_index);
2003-05-20 00:39:14 +08:00
#endif
ngx_conf_init_value(ecf->timer_queues, 10);
2003-05-16 23:27:48 +08:00
return NGX_CONF_OK;
}