2002-08-06 11:39:45 -05:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-09-27 10:05:29 -05:00
|
|
|
#include <ngx_core.h>
|
2002-08-06 11:39:45 -05:00
|
|
|
#include <ngx_types.h>
|
2002-09-02 09:48:24 -05:00
|
|
|
#include <ngx_string.h>
|
2002-08-06 11:39:45 -05:00
|
|
|
#include <ngx_log.h>
|
|
|
|
#include <ngx_alloc.h>
|
2002-12-15 15:08:04 -06:00
|
|
|
#include <ngx_array.h>
|
2002-08-20 09:48:28 -05:00
|
|
|
#include <ngx_listen.h>
|
2002-08-06 11:39:45 -05:00
|
|
|
#include <ngx_connection.h>
|
|
|
|
#include <ngx_event.h>
|
2003-05-16 10:27:48 -05:00
|
|
|
#include <ngx_conf_file.h>
|
2002-08-06 11:39:45 -05:00
|
|
|
|
|
|
|
#include <ngx_select_module.h>
|
2003-02-06 11:21:13 -06:00
|
|
|
|
2002-12-23 00:29:22 -06:00
|
|
|
#if (HAVE_POLL)
|
|
|
|
#include <ngx_poll_module.h>
|
|
|
|
#endif
|
2003-02-06 11:21:13 -06:00
|
|
|
|
2002-12-24 11:30:59 -06:00
|
|
|
#if (HAVE_DEVPOLL)
|
|
|
|
#include <ngx_devpoll_module.h>
|
|
|
|
#endif
|
2003-02-06 11:21:13 -06:00
|
|
|
|
2002-08-06 11:39:45 -05:00
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
#endif
|
|
|
|
|
2003-02-26 14:21:43 -06:00
|
|
|
#if (HAVE_AIO)
|
|
|
|
#include <ngx_aio_module.h>
|
|
|
|
#endif
|
|
|
|
|
2003-02-06 11:21:13 -06:00
|
|
|
#if (HAVE_IOCP)
|
|
|
|
#include <ngx_event_acceptex.h>
|
|
|
|
#include <ngx_iocp_module.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2003-05-16 10:27:48 -05:00
|
|
|
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
|
|
|
|
|
|
|
|
|
2002-08-06 11:39:45 -05:00
|
|
|
ngx_connection_t *ngx_connections;
|
|
|
|
ngx_event_t *ngx_read_events, *ngx_write_events;
|
|
|
|
|
|
|
|
#if !(USE_KQUEUE)
|
|
|
|
|
2003-02-11 01:14:40 -06:00
|
|
|
ngx_event_type_e ngx_event_type;
|
2002-12-23 12:22:18 -06:00
|
|
|
|
2003-02-11 01:14:40 -06:00
|
|
|
int ngx_event_flags;
|
2003-01-26 15:08:14 -06:00
|
|
|
|
2002-08-06 11:39:45 -05:00
|
|
|
ngx_event_actions_t ngx_event_actions;
|
|
|
|
|
|
|
|
/* ngx_event_type_e order */
|
2002-09-27 10:05:29 -05:00
|
|
|
static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
|
2002-08-06 11:39:45 -05:00
|
|
|
ngx_select_init,
|
|
|
|
#if (HAVE_POLL)
|
|
|
|
ngx_poll_init,
|
|
|
|
#endif
|
2002-12-24 11:30:59 -06:00
|
|
|
#if (HAVE_DEVPOLL)
|
|
|
|
ngx_devpoll_init,
|
|
|
|
#endif
|
2002-08-06 11:39:45 -05:00
|
|
|
#if (HAVE_KQUEUE)
|
2003-02-06 11:21:13 -06:00
|
|
|
ngx_kqueue_init,
|
|
|
|
#endif
|
2003-02-26 14:21:43 -06:00
|
|
|
#if (HAVE_AIO)
|
|
|
|
ngx_aio_init,
|
|
|
|
#endif
|
2003-02-06 11:21:13 -06:00
|
|
|
#if (HAVE_IOCP)
|
|
|
|
ngx_iocp_init
|
2002-08-06 11:39:45 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
|
|
|
|
|
2003-05-16 10:27:48 -05:00
|
|
|
static int ngx_event_connections;
|
|
|
|
|
|
|
|
|
|
|
|
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},
|
|
|
|
|
|
|
|
{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,
|
|
|
|
addressof(ngx_event_connections),
|
|
|
|
NULL},
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
{ngx_string("type"),
|
|
|
|
NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_event_set_type,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{ngx_string(""), 0, NULL, 0, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_event_module = {
|
|
|
|
NULL, /* module context */
|
|
|
|
0, /* module index */
|
|
|
|
ngx_events_commands, /* module directives */
|
|
|
|
NGX_EVENT_MODULE_TYPE, /* module type */
|
|
|
|
NULL /* init module */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-20 09:48:28 -05:00
|
|
|
void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
|
2002-08-06 11:39:45 -05:00
|
|
|
{
|
|
|
|
int i, fd;
|
2002-12-23 00:29:22 -06:00
|
|
|
|
|
|
|
ngx_listen_t *s;
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_connection_t *c;
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2002-08-26 10:18:19 -05:00
|
|
|
/* STUB */
|
2002-08-06 11:39:45 -05:00
|
|
|
int max_connections = 512;
|
|
|
|
|
2003-02-11 01:14:40 -06:00
|
|
|
#if 0
|
|
|
|
ngx_event_type = NGX_POLL_EVENT_N;
|
|
|
|
#endif
|
|
|
|
#if 1
|
|
|
|
ngx_event_type = NGX_KQUEUE_EVENT_N;
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
ngx_event_type = NGX_DEVPOLL_EVENT_N;
|
|
|
|
#endif
|
2003-02-26 14:21:43 -06:00
|
|
|
#if 0
|
|
|
|
ngx_event_type = NGX_AIO_EVENT_N;
|
|
|
|
#endif
|
2003-02-11 01:14:40 -06:00
|
|
|
#if 0
|
|
|
|
ngx_event_type = NGX_IOCP_EVENT_N;
|
2003-02-06 11:21:13 -06:00
|
|
|
#endif
|
|
|
|
|
2003-01-08 23:36:00 -06:00
|
|
|
if (ngx_init_events(max_connections, log) == NGX_ERROR) {
|
2002-09-27 10:05:29 -05:00
|
|
|
exit(1);
|
2003-01-08 23:36:00 -06:00
|
|
|
}
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2002-08-26 10:18:19 -05:00
|
|
|
ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
|
|
|
|
* max_connections, log);
|
2002-08-06 11:39:45 -05:00
|
|
|
ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
|
|
|
|
ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
|
|
|
|
|
|
|
|
/* for each listening socket */
|
2002-08-20 09:48:28 -05:00
|
|
|
s = (ngx_listen_t *) ls->elts;
|
|
|
|
for (i = 0; i < ls->nelts; i++) {
|
|
|
|
|
|
|
|
fd = s[i].fd;
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2002-12-23 00:29:22 -06:00
|
|
|
c = &ngx_connections[fd];
|
|
|
|
ev = &ngx_read_events[fd];
|
|
|
|
|
2003-01-08 23:36:00 -06:00
|
|
|
ngx_memzero(c, sizeof(ngx_connection_t));
|
|
|
|
ngx_memzero(ev, sizeof(ngx_event_t));
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2003-01-08 23:36:00 -06: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 10:18:19 -05:00
|
|
|
|
2003-01-08 23:36:00 -06:00
|
|
|
c->handler = s[i].handler;
|
|
|
|
c->ctx = s[i].ctx;
|
|
|
|
c->servers = s[i].servers;
|
|
|
|
c->log = s[i].log;
|
2003-01-30 01:28:09 -06:00
|
|
|
c->pool_size = s[i].pool_size;
|
2002-08-26 10:18:19 -05:00
|
|
|
|
2003-01-08 23:36:00 -06:00
|
|
|
ngx_test_null(ev->log,
|
2002-09-02 09:48:24 -05:00
|
|
|
ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
|
2003-01-08 23:36:00 -06:00
|
|
|
ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t));
|
2002-12-23 00:29:22 -06:00
|
|
|
c->read = ev;
|
2003-01-08 23:36:00 -06:00
|
|
|
ev->data = c;
|
2002-12-24 01:09:57 -06:00
|
|
|
ev->index = NGX_INVALID_INDEX;
|
2003-02-06 11:21:13 -06:00
|
|
|
#if 0
|
|
|
|
ev->listening = 1;
|
|
|
|
#endif
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2003-01-08 23:36:00 -06:00
|
|
|
ev->available = 0;
|
2002-08-06 11:39:45 -05:00
|
|
|
|
|
|
|
#if (HAVE_DEFERRED_ACCEPT)
|
2003-01-08 23:36:00 -06:00
|
|
|
ev->deferred_accept = s[i].deferred_accept;
|
2002-08-06 11:39:45 -05:00
|
|
|
#endif
|
2003-02-06 11:21:13 -06:00
|
|
|
|
|
|
|
#if (HAVE_IOCP)
|
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
|
|
|
|
ev->event_handler = &ngx_event_acceptex;
|
|
|
|
|
2003-03-04 00:33:48 -06:00
|
|
|
/* LOOK: we call ngx_iocp_add_event() also
|
|
|
|
in ngx_event_post_acceptex() */
|
2003-02-06 11:21:13 -06: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-08 23:36:00 -06:00
|
|
|
ngx_add_event(ev, NGX_READ_EVENT, 0);
|
2003-02-06 11:21:13 -06:00
|
|
|
|
|
|
|
#endif
|
2002-08-06 11:39:45 -05:00
|
|
|
}
|
2002-08-20 09:48:28 -05:00
|
|
|
}
|
2002-08-06 11:39:45 -05:00
|
|
|
|
2003-05-16 10:27:48 -05:00
|
|
|
|
2002-08-20 09:48:28 -05:00
|
|
|
void ngx_worker(ngx_log_t *log)
|
|
|
|
{
|
2003-01-08 23:36:00 -06:00
|
|
|
for ( ;; ) {
|
2002-08-06 11:39:45 -05:00
|
|
|
ngx_log_debug(log, "ngx_worker cycle");
|
|
|
|
|
|
|
|
ngx_process_events(log);
|
|
|
|
}
|
|
|
|
}
|
2003-05-16 10:27:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_events_init(ngx_pool_t *pool)
|
|
|
|
{
|
|
|
|
ngx_event_connections = -1;
|
|
|
|
ngx_event_type = -1;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_events_postconf(ngx_pool_t *pool)
|
|
|
|
{
|
|
|
|
if (ngx_event_connections == -1) {
|
|
|
|
ngx_event_connections = 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
|
|
|
|
{
|
|
|
|
char *rv;
|
|
|
|
ngx_conf_t pcf;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
*(ngx_events_conf_ctx_t **) conf = ctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pcf = *cf;
|
|
|
|
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;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|