nginx/src/event/ngx_event.c

839 lines
22 KiB
C
Raw Normal View History

/*
* Copyright (C) Igor Sysoev
*/
#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-12-26 04:26:58 +08:00
extern ngx_event_module_t ngx_select_module_ctx;
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;
2004-02-04 00:43:54 +08:00
extern ngx_event_module_t ngx_devpoll_module_ctx;
2003-05-20 23:37:55 +08:00
#endif
2004-02-12 01:08:49 +08:00
#if (HAVE_EPOLL)
extern ngx_module_t ngx_epoll_module;
extern ngx_event_module_t ngx_epoll_module_ctx;
#endif
2004-06-11 02:36:57 +08:00
#if (HAVE_RTSIG)
extern ngx_module_t ngx_rtsig_module;
extern ngx_event_module_t ngx_rtsig_module_ctx;
#endif
2003-02-27 04:21:43 +08:00
#if (HAVE_AIO)
#include <ngx_aio_module.h>
#endif
2004-04-02 00:20:53 +08:00
static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);
static ngx_int_t ngx_event_process_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);
2003-12-09 23:08:11 +08:00
static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
2003-05-27 20:18:54 +08:00
static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
2004-04-08 23:58:25 +08:00
static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
2003-12-09 23:08:11 +08:00
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);
static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data);
2003-05-16 23:27:48 +08:00
2004-04-02 00:20:53 +08:00
static ngx_uint_t ngx_event_max_module;
2004-04-02 00:20:53 +08:00
ngx_uint_t ngx_event_flags;
ngx_event_actions_t ngx_event_actions;
2004-04-10 00:03:04 +08:00
ngx_atomic_t connection_counter;
ngx_atomic_t *ngx_connection_counter = &connection_counter;
2004-04-02 00:20:53 +08:00
ngx_atomic_t *ngx_accept_mutex_ptr;
ngx_atomic_t *ngx_accept_mutex;
ngx_uint_t ngx_accept_mutex_held;
2004-04-05 04:32:09 +08:00
ngx_msec_t ngx_accept_mutex_delay;
2004-04-22 02:54:33 +08:00
ngx_int_t ngx_accept_disabled;
#if (NGX_STAT_STUB)
ngx_atomic_t ngx_stat_accepted0;
ngx_atomic_t *ngx_stat_accepted = &ngx_stat_accepted0;
ngx_atomic_t ngx_stat_requests0;
ngx_atomic_t *ngx_stat_requests = &ngx_stat_requests0;
ngx_atomic_t ngx_stat_active0;
ngx_atomic_t *ngx_stat_active = &ngx_stat_active0;
ngx_atomic_t ngx_stat_reading0;
ngx_atomic_t *ngx_stat_reading = &ngx_stat_reading0;
ngx_atomic_t ngx_stat_writing0;
ngx_atomic_t *ngx_stat_writing = &ngx_stat_reading0;
#endif
2003-05-16 23:27:48 +08:00
static ngx_command_t ngx_events_commands[] = {
2003-12-09 23:08:11 +08:00
{ ngx_string("events"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_events_block,
0,
0,
NULL },
2003-05-16 23:27:48 +08:00
2003-12-09 23:08:11 +08:00
ngx_null_command
2003-05-16 23:27:48 +08:00
};
2004-04-13 00:38:09 +08:00
static ngx_core_module_t ngx_events_module_ctx = {
ngx_string("events"),
NULL,
NULL
};
2003-05-16 23:27:48 +08:00
ngx_module_t ngx_events_module = {
2003-05-27 20:18:54 +08:00
NGX_MODULE,
2004-04-13 00:38:09 +08:00
&ngx_events_module_ctx, /* module context */
2003-05-16 23:27:48 +08:00
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 */
2004-07-05 14:55:54 +08:00
NULL /* init process */
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
static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ;
2003-07-01 23:00:03 +08:00
static ngx_command_t ngx_event_core_commands[] = {
2003-05-16 23:27:48 +08:00
2003-12-09 23:08:11 +08:00
{ ngx_string("connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_connections,
0,
0,
NULL },
{ ngx_string("use"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_use,
0,
0,
NULL },
2004-02-03 05:19:52 +08:00
{ ngx_string("multi_accept"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_event_conf_t, multi_accept),
NULL },
2004-04-02 00:20:53 +08:00
{ ngx_string("accept_mutex"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_event_conf_t, accept_mutex),
&ngx_accept_mutex_post },
2004-04-02 00:20:53 +08:00
2004-04-05 04:32:09 +08:00
{ ngx_string("accept_mutex_delay"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
0,
offsetof(ngx_event_conf_t, accept_mutex_delay),
NULL },
2004-04-08 23:58:25 +08:00
{ ngx_string("debug_connection"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_debug_connection,
0,
0,
NULL },
2003-12-09 23:08:11 +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 */
2004-07-07 14:15:04 +08:00
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
2003-05-20 00:39:14 +08:00
};
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 */
2004-04-02 00:20:53 +08:00
ngx_event_module_init, /* init module */
ngx_event_process_init /* init process */
2003-05-16 23:27:48 +08:00
};
2004-04-02 00:20:53 +08:00
static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle)
{
2004-04-02 13:14:40 +08:00
#if !(WIN32)
2004-04-10 00:03:04 +08:00
size_t size;
char *shared;
2004-04-02 00:20:53 +08:00
ngx_core_conf_t *ccf;
ngx_event_conf_t *ecf;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
2004-04-24 00:50:51 +08:00
if (ccf->master == 0 || ngx_accept_mutex_ptr) {
2004-04-02 00:20:53 +08:00
return NGX_OK;
}
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
2004-04-10 00:03:04 +08:00
/* TODO: 128 is cache line size */
size = 128 /* ngx_accept_mutex */
+ 128; /* ngx_connection_counter */
2004-04-02 00:20:53 +08:00
#if (NGX_STAT_STUB)
size += 128 /* ngx_stat_accepted */
+ 128 /* ngx_stat_requests */
+ 128 /* ngx_stat_active */
+ 128 /* ngx_stat_reading */
+ 128; /* ngx_stat_writing */
#endif
2004-06-18 01:18:53 +08:00
if (!(shared = ngx_create_shared_memory(size, cycle->log))) {
return NGX_ERROR;
}
2004-04-10 00:03:04 +08:00
2004-04-30 00:34:26 +08:00
ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
2004-04-10 00:03:04 +08:00
ngx_connection_counter = (ngx_atomic_t *) (shared + 128);
#if (NGX_STAT_STUB)
ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * 128);
ngx_stat_requests = (ngx_atomic_t *) (shared + 3 * 128);
ngx_stat_active = (ngx_atomic_t *) (shared + 4 * 128);
ngx_stat_reading = (ngx_atomic_t *) (shared + 5 * 128);
ngx_stat_writing = (ngx_atomic_t *) (shared + 6 * 128);
#endif
2004-04-24 00:50:51 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"counter: " PTR_FMT ", %d",
ngx_connection_counter, *ngx_connection_counter);
2004-04-02 13:14:40 +08:00
#endif
2004-04-02 00:20:53 +08:00
return NGX_OK;
}
static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle)
{
2004-03-16 15:10:12 +08:00
ngx_uint_t m, i;
ngx_socket_t 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;
2004-04-02 00:20:53 +08:00
ngx_core_conf_t *ccf;
2003-05-29 21:02:09 +08:00
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
2004-04-02 00:20:53 +08:00
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
2004-04-05 04:32:09 +08:00
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
2004-04-02 00:20:53 +08:00
2004-05-05 01:56:58 +08:00
if (ngx_accept_mutex_ptr && ccf->worker_processes > 1 && ecf->accept_mutex)
{
2004-04-02 00:20:53 +08:00
ngx_accept_mutex = ngx_accept_mutex_ptr;
2004-06-04 22:57:33 +08:00
ngx_accept_mutex_held = 0;
2004-04-05 04:32:09 +08:00
ngx_accept_mutex_delay = ecf->accept_mutex_delay;
2004-04-02 00:20:53 +08:00
}
2004-03-03 05:14:37 +08:00
#if (NGX_THREADS)
if (!(ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0))) {
return NGX_ERROR;
}
#endif
2004-02-26 04:16:15 +08:00
if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
return NGX_ERROR;
}
2003-12-09 23:08:11 +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) {
2004-01-30 05:45:01 +08:00
/* fatal */
exit(2);
2003-05-20 00:39:14 +08:00
}
break;
}
2003-01-09 13:36:00 +08:00
}
2003-12-09 23:08:11 +08:00
cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
cycle->log);
if (cycle->connections == NULL) {
return NGX_ERROR;
2003-07-04 14:03:52 +08:00
}
2003-07-07 14:11:50 +08:00
c = cycle->connections;
for (i = 0; i < cycle->connection_n; i++) {
2004-03-16 15:10:12 +08:00
c[i].fd = (ngx_socket_t) -1;
2004-06-24 15:53:37 +08:00
c[i].data = NULL;
2004-06-28 02:01:57 +08:00
#if (NGX_THREADS)
c[i].lock = 0;
#endif
2003-07-07 14:11:50 +08:00
}
2003-12-09 23:08:11 +08:00
cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
cycle->log);
if (cycle->read_events == NULL) {
return NGX_ERROR;
}
2003-05-20 00:39:14 +08:00
2004-07-02 23:54:34 +08:00
rev = cycle->read_events;
for (i = 0; i < cycle->connection_n; i++) {
rev[i].closed = 1;
2004-07-05 23:08:23 +08:00
#if (NGX_THREADS)
rev[i].lock = &c[i].lock;
2004-07-07 14:15:04 +08:00
rev[i].own_lock = &c[i].lock;
2004-07-05 23:08:23 +08:00
#endif
2004-07-02 23:54:34 +08:00
}
2003-12-09 23:08:11 +08:00
cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
cycle->log);
if (cycle->write_events == NULL) {
return NGX_ERROR;
}
2004-07-02 23:54:34 +08:00
wev = cycle->write_events;
for (i = 0; i < cycle->connection_n; i++) {
wev[i].closed = 1;
2004-07-05 23:08:23 +08:00
#if (NGX_THREADS)
wev[i].lock = &c[i].lock;
2004-07-07 14:15:04 +08:00
wev[i].own_lock = &c[i].lock;
2004-07-05 23:08:23 +08:00
#endif
2004-07-02 23:54:34 +08:00
}
/* 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;
2003-12-09 23:08:11 +08:00
/* required by poll */
wev->index = NGX_INVALID_INDEX;
2003-07-07 14:11:50 +08:00
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-06-11 23:28:34 +08:00
rev->available = 0;
2004-03-31 23:26:46 +08:00
rev->accept = 1;
#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-12-09 23:08:11 +08:00
if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
2003-07-07 14:11:50 +08:00
if (s[i].remain) {
2003-12-09 23:08:11 +08:00
/*
* delete the old accept events that were bound to
* the old cycle read events array
*/
2003-07-07 14:11:50 +08:00
if (ngx_del_event(&cycle->old_cycle->read_events[fd],
2004-01-06 04:55:48 +08:00
NGX_READ_EVENT, NGX_CLOSE_EVENT) == NGX_ERROR)
{
2003-07-07 14:11:50 +08:00
return NGX_ERROR;
}
2004-03-16 15:10:12 +08:00
cycle->old_cycle->connections[fd].fd = (ngx_socket_t) -1;
2003-07-07 14:11:50 +08:00
}
}
2003-06-11 23:28:34 +08:00
#if (WIN32)
2003-02-07 01:21:13 +08:00
2003-10-13 00:49:16 +08:00
if (ngx_event_flags & NGX_USE_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);
2004-03-15 04:46:25 +08:00
if (ngx_event_post_acceptex(&s[i], iocpcf->post_acceptex)
== NGX_ERROR)
{
2003-06-11 23:28:34 +08:00
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;
2004-03-31 23:26:46 +08:00
if (ngx_add_event(rev, NGX_READ_EVENT, 0) == 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;
2004-02-18 01:53:12 +08:00
2004-06-04 22:57:33 +08:00
if (ngx_accept_mutex) {
continue;
}
2004-06-01 14:04:46 +08:00
2004-06-07 03:49:18 +08:00
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
2004-06-04 22:57:33 +08:00
if (ngx_add_conn(c) == NGX_ERROR) {
return NGX_ERROR;
2004-03-31 23:26:46 +08:00
}
2004-06-01 14:04:46 +08:00
2004-02-18 01:53:12 +08:00
} else {
2004-03-31 23:26:46 +08:00
if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
return NGX_ERROR;
}
2004-02-18 01:53:12 +08:00
}
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
ngx_int_t ngx_send_lowat(ngx_connection_t *c, size_t lowat)
{
int sndlowat;
#if (NGX_HAVE_LOWAT_EVENT)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
c->write->available = lowat;
return NGX_OK;
}
#endif
if (lowat == 0 || c->sndlowat) {
return NGX_OK;
}
sndlowat = (int) lowat;
if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
(const void *) &sndlowat, sizeof(int)) == -1)
{
ngx_connection_error(c, ngx_socket_errno,
"setsockopt(SO_SNDLOWAT) failed");
return NGX_ERROR;
}
c->sndlowat = 1;
return NGX_OK;
}
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-12-09 23:08:11 +08:00
static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ngx_event_conf_t *ecf = conf;
ngx_str_t *value;
2004-04-22 02:54:33 +08:00
if (ecf->connections != NGX_CONF_UNSET_UINT) {
2003-12-09 23:08:11 +08:00
return "is duplicate" ;
}
value = cf->args->elts;
ecf->connections = ngx_atoi(value[1].data, value[1].len);
2004-04-22 02:54:33 +08:00
if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
2003-12-09 23:08:11 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid number \"%s\"", value[1].data);
return NGX_CONF_ERROR;
}
cf->cycle->connection_n = ecf->connections;
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
2004-02-10 04:47:18 +08:00
ngx_int_t m;
2003-12-09 23:08:11 +08:00
ngx_str_t *value;
ngx_event_conf_t *old_ecf;
2003-05-20 00:39:14 +08:00
ngx_event_module_t *module;
2004-06-16 23:32:11 +08:00
if (ecf->use != NGX_CONF_UNSET_UINT) {
2003-05-21 21:28:21 +08:00
return "is duplicate" ;
2003-05-20 00:39:14 +08:00
}
2003-12-09 23:08:11 +08:00
value = cf->args->elts;
2004-01-15 02:19:42 +08:00
if (cf->cycle->old_cycle->conf_ctx) {
2003-12-09 23:08:11 +08:00
old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
ngx_event_core_module);
} else {
old_ecf = NULL;
}
2003-05-20 00:39:14 +08:00
2004-02-04 00:43:54 +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;
}
module = ngx_modules[m]->ctx;
2003-12-09 23:08:11 +08:00
if (module->name->len == value[1].len) {
if (ngx_strcmp(module->name->data, value[1].data) == 0) {
2003-05-27 20:18:54 +08:00
ecf->use = ngx_modules[m]->ctx_index;
2003-12-09 23:08:11 +08:00
ecf->name = module->name->data;
2004-02-04 00:43:54 +08:00
if (ngx_process == NGX_PROCESS_SINGLE
&& old_ecf
&& old_ecf->use != ecf->use)
{
2003-12-09 23:08:11 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2004-02-04 00:43:54 +08:00
"when the server runs without a master process "
2003-12-09 23:08:11 +08:00
"the \"%s\" event type must be the same as "
"in previous configuration - \"%s\" "
"and it can not be changed on the fly, "
"to change it you need to stop server "
"and start it again",
value[1].data, old_ecf->name);
2004-02-04 00:43:54 +08:00
2003-12-09 23:08:11 +08:00
return NGX_CONF_ERROR;
}
2003-05-20 00:39:14 +08:00
return NGX_CONF_OK;
}
}
}
2003-12-09 23:08:11 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid event type \"%s\"", value[1].data);
return NGX_CONF_ERROR;
2003-05-20 00:39:14 +08:00
}
2004-04-08 23:58:25 +08:00
static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
#if (NGX_DEBUG)
ngx_event_conf_t *ecf = conf;
in_addr_t *addr;
ngx_str_t *value;
struct hostent *h;
value = cf->args->elts;
/* AF_INET only */
if (!(addr = ngx_push_array(&ecf->debug_connection))) {
return NGX_CONF_ERROR;
}
*addr = inet_addr((char *) value[1].data);
if (*addr != INADDR_NONE) {
return NGX_OK;
}
h = gethostbyname((char *) value[1].data);
if (h == NULL || h->h_addr_list[0] == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"host %s not found", value[1].data);
return NGX_CONF_ERROR;
}
*addr = *(in_addr_t *)(h->h_addr_list[0]);
#else
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"\"debug_connection\" is ignored, you need to rebuild "
"nginx using --with-debug option to enable it");
#endif
return NGX_OK;
}
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);
2004-04-22 02:54:33 +08:00
ecf->connections = NGX_CONF_UNSET_UINT;
2004-06-18 01:18:53 +08:00
ecf->use = NGX_CONF_UNSET_UINT;
2004-02-03 05:19:52 +08:00
ecf->multi_accept = NGX_CONF_UNSET;
2004-04-02 00:20:53 +08:00
ecf->accept_mutex = NGX_CONF_UNSET;
2004-04-05 04:32:09 +08:00
ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
2003-12-09 23:08:11 +08:00
ecf->name = (void *) NGX_CONF_UNSET;
2003-05-20 00:39:14 +08:00
2004-04-08 23:58:25 +08:00
#if (NGX_DEBUG)
ngx_init_array(ecf->debug_connection, cycle->pool, 5, sizeof(in_addr_t),
NGX_CONF_ERROR);
#endif
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
{
2004-06-15 15:55:11 +08:00
ngx_event_conf_t *ecf = conf;
2004-06-19 00:22:16 +08:00
#if (HAVE_RTSIG)
ngx_core_conf_t *ccf;
#endif
2003-05-20 00:39:14 +08:00
#if (HAVE_KQUEUE)
2004-04-22 02:54:33 +08:00
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
2004-06-16 23:32:11 +08:00
ngx_conf_init_unsigned_value(ecf->use, ngx_kqueue_module.ctx_index);
2003-12-09 23:08:11 +08:00
ngx_conf_init_ptr_value(ecf->name, ngx_kqueue_module_ctx.name->data);
2003-05-20 00:39:14 +08:00
2003-05-20 23:37:55 +08:00
#elif (HAVE_DEVPOLL)
2004-04-22 02:54:33 +08:00
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
2004-06-16 23:32:11 +08:00
ngx_conf_init_unsigned_value(ecf->use, ngx_devpoll_module.ctx_index);
2003-12-09 23:08:11 +08:00
ngx_conf_init_ptr_value(ecf->name, ngx_devpoll_module_ctx.name->data);
2003-05-20 23:37:55 +08:00
2004-02-09 15:46:43 +08:00
#elif (HAVE_EPOLL)
2004-04-22 02:54:33 +08:00
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
2004-06-16 23:32:11 +08:00
ngx_conf_init_unsigned_value(ecf->use, ngx_epoll_module.ctx_index);
2004-02-09 15:46:43 +08:00
ngx_conf_init_ptr_value(ecf->name, ngx_epoll_module_ctx.name->data);
2004-06-11 02:36:57 +08:00
#elif (HAVE_RTSIG)
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
2004-06-16 23:32:11 +08:00
ngx_conf_init_unsigned_value(ecf->use, ngx_rtsig_module.ctx_index);
2004-06-11 02:36:57 +08:00
ngx_conf_init_ptr_value(ecf->name, ngx_rtsig_module_ctx.name->data);
2004-02-10 04:47:18 +08:00
#elif (HAVE_SELECT)
2003-05-20 00:39:14 +08:00
2004-06-16 01:47:16 +08:00
#if (WIN32)
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
#else
2004-04-22 02:54:33 +08:00
ngx_conf_init_unsigned_value(ecf->connections,
2003-07-04 23:10:33 +08:00
FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS);
2004-06-16 01:47:16 +08:00
#endif
2003-05-20 00:39:14 +08:00
2004-06-16 23:32:11 +08:00
ngx_conf_init_unsigned_value(ecf->use, ngx_select_module.ctx_index);
2003-12-09 23:08:11 +08:00
ngx_conf_init_ptr_value(ecf->name, ngx_select_module_ctx.name->data);
2003-05-20 00:39:14 +08:00
2004-02-10 04:47:18 +08:00
#else
ngx_int_t i, m;
ngx_event_module_t *module;
m = -1;
module = NULL;
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->type == NGX_EVENT_MODULE) {
module = ngx_modules[i]->ctx;
if (ngx_strcmp(module->name->data, event_core_name.data) == 0) {
continue;
}
m = ngx_modules[i]->ctx_index;
break;
}
}
if (m == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
return NGX_CONF_ERROR;
}
2004-06-18 01:18:53 +08:00
ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS);
2004-02-10 04:47:18 +08:00
2004-06-18 01:18:53 +08:00
ngx_conf_init_unsigned_value(ecf->use, m);
2004-02-10 04:47:18 +08:00
ngx_conf_init_ptr_value(ecf->name, module->name->data);
2003-05-20 00:39:14 +08:00
#endif
2003-12-09 23:08:11 +08:00
cycle->connection_n = ecf->connections;
2004-02-04 04:27:11 +08:00
ngx_conf_init_value(ecf->multi_accept, 0);
2004-04-02 00:20:53 +08:00
ngx_conf_init_value(ecf->accept_mutex, 1);
2004-04-05 04:32:09 +08:00
ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
2003-05-20 00:39:14 +08:00
2004-06-15 15:55:11 +08:00
#if (HAVE_RTSIG)
if (ecf->use == ngx_rtsig_module.ctx_index && ecf->accept_mutex == 0) {
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_core_module);
if (ccf->worker_processes) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"the \"rtsig\" method requires "
"\"accept_mutex\" to be on");
return NGX_CONF_ERROR;
}
}
#endif
2003-05-16 23:27:48 +08:00
return NGX_CONF_OK;
}
static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data)
{
#if !(NGX_HAVE_ATOMIC_OPS)
ngx_flag_t *fp = data;
*fp = 0;
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"\"accept_mutex\" is not supported, ignored");
#endif
return NGX_CONF_OK;
}