2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-09-27 23:05:29 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-20 22:48:28 +08:00
|
|
|
#include <ngx_listen.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_connection.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
#define DEF_CONNECTIONS 1024
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
|
|
|
|
extern ngx_event_module_t ngx_select_module_ctx;
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_KQUEUE)
|
2003-05-20 00:39:14 +08:00
|
|
|
extern ngx_event_module_t ngx_kqueue_module_ctx;
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
#endif
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
#if (HAVE_DEVPOLL)
|
|
|
|
extern ngx_event_module_t ngx_devpoll_module_ctx;
|
|
|
|
#endif
|
|
|
|
|
2003-02-27 04:21:43 +08:00
|
|
|
#if (HAVE_AIO)
|
|
|
|
#include <ngx_aio_module.h>
|
|
|
|
#endif
|
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
#if (HAVE_IOCP)
|
|
|
|
#include <ngx_event_acceptex.h>
|
|
|
|
#include <ngx_iocp_module.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
|
2003-05-20 00:39:14 +08:00
|
|
|
static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
|
|
|
|
static void *ngx_event_create_conf(ngx_pool_t *pool);
|
|
|
|
static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf);
|
2003-05-16 23:27:48 +08:00
|
|
|
|
|
|
|
|
2003-02-11 15:14:40 +08:00
|
|
|
int ngx_event_flags;
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_event_actions_t ngx_event_actions;
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_connection_t *ngx_connections;
|
|
|
|
ngx_event_t *ngx_read_events, *ngx_write_events;
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
static int ngx_event_max_module;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
static int ngx_event_connections;
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_str_t events_name = ngx_string("events");
|
2003-05-20 00:39:14 +08:00
|
|
|
static ngx_str_t event_name = ngx_string("event");
|
2003-05-16 23:27:48 +08:00
|
|
|
|
|
|
|
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},
|
|
|
|
|
|
|
|
{ngx_string(""), 0, NULL, 0, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_events_module = {
|
|
|
|
&events_name, /* module context */
|
|
|
|
0, /* module index */
|
|
|
|
ngx_events_commands, /* module directives */
|
|
|
|
NGX_CORE_MODULE_TYPE, /* module type */
|
|
|
|
NULL /* init module */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_event_commands[] = {
|
|
|
|
|
|
|
|
{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},
|
|
|
|
|
|
|
|
{ngx_string("type"),
|
|
|
|
NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_event_set_type,
|
|
|
|
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
|
|
|
|
|
|
|
{ngx_string(""), 0, NULL, 0, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_event_module_t ngx_event_module_ctx = {
|
|
|
|
NGX_EVENT_MODULE,
|
|
|
|
&event_name,
|
|
|
|
ngx_event_create_conf, /* create configuration */
|
|
|
|
ngx_event_init_conf, /* init configuration */
|
|
|
|
|
|
|
|
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
ngx_module_t ngx_event_module = {
|
2003-05-20 00:39:14 +08:00
|
|
|
&ngx_event_module_ctx, /* module context */
|
2003-05-16 23:27:48 +08:00
|
|
|
0, /* module index */
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_event_commands, /* module directives */
|
2003-05-16 23:27:48 +08:00
|
|
|
NGX_EVENT_MODULE_TYPE, /* module type */
|
|
|
|
NULL /* init module */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
int ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-05-20 00:39:14 +08:00
|
|
|
int m, i, fd;
|
2002-12-23 14:29:22 +08:00
|
|
|
|
|
|
|
ngx_listen_t *s;
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_connection_t *c;
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_event_conf_t *ecf;
|
|
|
|
ngx_event_module_t *module;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ecf = ngx_event_get_conf(ngx_event_module_ctx);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_log_debug(log, "CONN: %d" _ ecf->connections);
|
|
|
|
ngx_log_debug(log, "TYPE: %d" _ ecf->type);
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
for (m = 0; ngx_modules[m]; m++) {
|
|
|
|
if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = ngx_modules[m]->ctx;
|
|
|
|
if (module->index == ecf->type) {
|
|
|
|
if (module->actions.init(log) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-01-09 13:36:00 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_test_null(ngx_connections,
|
|
|
|
ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, log),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
|
|
|
ngx_test_null(ngx_read_events,
|
|
|
|
ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
|
|
|
ngx_test_null(ngx_write_events,
|
|
|
|
ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
|
|
|
|
NGX_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
/* for each listening socket */
|
2002-08-20 22:48:28 +08:00
|
|
|
s = (ngx_listen_t *) ls->elts;
|
|
|
|
for (i = 0; i < ls->nelts; i++) {
|
|
|
|
|
|
|
|
fd = s[i].fd;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
c = &ngx_connections[fd];
|
|
|
|
ev = &ngx_read_events[fd];
|
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_memzero(c, sizeof(ngx_connection_t));
|
|
|
|
ngx_memzero(ev, sizeof(ngx_event_t));
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
c->fd = fd;
|
|
|
|
c->family = s[i].family;
|
|
|
|
c->socklen = s[i].socklen;
|
|
|
|
c->sockaddr = ngx_palloc(pool, s[i].socklen);
|
|
|
|
c->addr = s[i].addr;
|
|
|
|
c->addr_text = s[i].addr_text;
|
|
|
|
c->addr_text_max_len = s[i].addr_text_max_len;
|
|
|
|
c->post_accept_timeout = s[i].post_accept_timeout;
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
c->handler = s[i].handler;
|
|
|
|
c->ctx = s[i].ctx;
|
|
|
|
c->servers = s[i].servers;
|
|
|
|
c->log = s[i].log;
|
2003-01-30 15:28:09 +08:00
|
|
|
c->pool_size = s[i].pool_size;
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_test_null(ev->log,
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_palloc(pool, sizeof(ngx_log_t)),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t));
|
2002-12-23 14:29:22 +08:00
|
|
|
c->read = ev;
|
2003-01-09 13:36:00 +08:00
|
|
|
ev->data = c;
|
2002-12-24 15:09:57 +08:00
|
|
|
ev->index = NGX_INVALID_INDEX;
|
2003-02-07 01:21:13 +08:00
|
|
|
#if 0
|
|
|
|
ev->listening = 1;
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-09 13:36:00 +08:00
|
|
|
ev->available = 0;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#if (HAVE_DEFERRED_ACCEPT)
|
2003-01-09 13:36:00 +08:00
|
|
|
ev->deferred_accept = s[i].deferred_accept;
|
2002-08-07 00:39:45 +08:00
|
|
|
#endif
|
2003-02-07 01:21:13 +08:00
|
|
|
|
|
|
|
#if (HAVE_IOCP)
|
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
|
|
|
|
ev->event_handler = &ngx_event_acceptex;
|
|
|
|
|
2003-03-04 14:33:48 +08:00
|
|
|
/* LOOK: we call ngx_iocp_add_event() also
|
|
|
|
in ngx_event_post_acceptex() */
|
2003-02-07 01:21:13 +08:00
|
|
|
if (ngx_iocp_add_event(ev) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_event_post_acceptex(&s[i], 1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ev->event_handler = &ngx_event_accept;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ev->event_handler = &ngx_event_accept;
|
2003-01-09 13:36:00 +08:00
|
|
|
ngx_add_event(ev, NGX_READ_EVENT, 0);
|
2003-02-07 01:21:13 +08:00
|
|
|
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2003-05-20 00:39:14 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
void ngx_worker(ngx_log_t *log)
|
|
|
|
{
|
2003-01-09 13:36:00 +08:00
|
|
|
for ( ;; ) {
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_log_debug(log, "ngx_worker cycle");
|
|
|
|
|
|
|
|
ngx_process_events(log);
|
|
|
|
}
|
|
|
|
}
|
2003-05-16 23:27:48 +08:00
|
|
|
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *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++) {
|
|
|
|
if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-16 23:27:48 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
module = ngx_modules[m]->ctx;
|
|
|
|
module->index = ngx_event_max_module++;
|
|
|
|
}
|
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++) {
|
|
|
|
if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
|
|
|
|
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) {
|
|
|
|
ngx_test_null((*ctx)[module->index], module->create_conf(cf->pool),
|
|
|
|
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-16 23:27:48 +08:00
|
|
|
cf->module_type = NGX_EVENT_MODULE_TYPE;
|
|
|
|
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++) {
|
|
|
|
if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = ngx_modules[m]->ctx;
|
|
|
|
|
|
|
|
if (module->init_conf) {
|
|
|
|
rv = module->init_conf(cf->pool, (*ctx)[module->index]);
|
|
|
|
if (rv != NGX_CONF_OK) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
|
|
|
|
{
|
|
|
|
ngx_event_conf_t *ecf = (ngx_event_conf_t *) conf;
|
|
|
|
|
|
|
|
int m;
|
|
|
|
ngx_str_t *args;
|
|
|
|
ngx_event_module_t *module;
|
|
|
|
|
|
|
|
if (ecf->type != NGX_CONF_UNSET) {
|
|
|
|
return "duplicate event type" ;
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cf->args->elts;
|
|
|
|
|
|
|
|
for (m = 0; ngx_modules[m]; m++) {
|
|
|
|
if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = ngx_modules[m]->ctx;
|
|
|
|
if (module->name->len == args[1].len) {
|
|
|
|
if (ngx_strcmp(module->name->data, args[1].data) == 0) {
|
|
|
|
ecf->type = module->index;
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "invalid event type";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *ngx_event_create_conf(ngx_pool_t *pool)
|
|
|
|
{
|
|
|
|
ngx_event_conf_t *ecf;
|
|
|
|
|
|
|
|
ngx_test_null(ecf, ngx_palloc(pool, sizeof(ngx_event_conf_t)),
|
|
|
|
NGX_CONF_ERROR);
|
|
|
|
|
|
|
|
ecf->connections = NGX_CONF_UNSET;
|
2003-05-20 23:37:55 +08:00
|
|
|
ecf->timer_queues = NGX_CONF_UNSET;
|
2003-05-20 00:39:14 +08:00
|
|
|
ecf->type = NGX_CONF_UNSET;
|
|
|
|
|
|
|
|
return ecf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf)
|
|
|
|
{
|
|
|
|
ngx_event_conf_t *ecf = conf;
|
|
|
|
|
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
#if 0
|
|
|
|
if (ecf->connections != NGX_CONF_UNSET) {
|
|
|
|
ecf->connections = (ngx_max_connections < DEF_CONNECTIONS) ?
|
|
|
|
ngx_max_connections : DEF_CONNECTIONS;
|
|
|
|
|
|
|
|
} else if (ecf->connections > ngx_max_connections) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_conf_init_value(ecf->connections, DEF_CONNECTIONS);
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_conf_init_value(ecf->type, ngx_kqueue_module_ctx.index);
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
#elif (HAVE_DEVPOLL)
|
|
|
|
|
|
|
|
ngx_conf_init_value(ecf->connections, DEF_CONNECTIONS);
|
|
|
|
ngx_conf_init_value(ecf->type, ngx_devpoll_module_ctx.index);
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
#else /* HAVE_SELECT */
|
|
|
|
|
|
|
|
ngx_conf_init_value(ecf->connections,
|
2003-05-20 23:37:55 +08:00
|
|
|
FD_SETSIZE < DEF_CONNECTIONS ? FD_SETSIZE : DEF_CONNECTIONS);
|
2003-05-20 00:39:14 +08:00
|
|
|
|
|
|
|
ngx_conf_init_value(ecf->type, ngx_select_module_ctx.index);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_conf_init_value(ecf->timer_queues, 10);
|
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|