nginx/src/event/ngx_event_accept.c

412 lines
11 KiB
C
Raw Normal View History

#include <ngx_config.h>
2002-08-26 23:18:19 +08:00
#include <ngx_core.h>
#include <ngx_event.h>
2003-06-02 23:24:30 +08:00
#include <nginx.h>
2004-02-12 01:08:49 +08:00
typedef struct {
2004-03-16 15:10:12 +08:00
int flag;
u_char *name;
2004-02-12 01:08:49 +08:00
} ngx_accept_log_ctx_t;
2003-11-20 00:26:41 +08:00
static size_t ngx_accept_log_error(void *data, char *buf, size_t len);
2003-05-14 00:02:32 +08:00
void ngx_event_accept(ngx_event_t *ev)
{
2004-03-31 23:26:46 +08:00
ngx_uint_t instance, rinstance, winstance, accepted;
2003-11-20 00:26:41 +08:00
socklen_t len;
struct sockaddr *sa;
ngx_err_t err;
ngx_log_t *log;
ngx_pool_t *pool;
ngx_socket_t s;
ngx_event_t *rev, *wev;
ngx_connection_t *c, *ls;
ngx_event_conf_t *ecf;
2004-02-12 01:08:49 +08:00
ngx_accept_log_ctx_t *ctx;
2003-05-29 21:02:09 +08:00
2003-07-07 14:11:50 +08:00
ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
2003-01-30 15:28:09 +08:00
2004-02-03 05:19:52 +08:00
if (ngx_event_flags & (NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT)) {
ev->available = 1;
} else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) {
ev->available = ecf->multi_accept;
}
2003-05-29 21:02:09 +08:00
ls = ev->data;
2002-12-15 14:25:09 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"accept on %s, ready: %d",
ls->listening->addr_text.data, ev->available);
2003-01-30 15:28:09 +08:00
ev->ready = 0;
2003-11-20 00:26:41 +08:00
accepted = 0;
2004-02-03 05:19:52 +08:00
pool = NULL;
2003-01-30 15:28:09 +08:00
do {
2003-05-20 23:37:55 +08:00
2004-02-03 05:19:52 +08:00
if (pool == NULL) {
2003-05-20 23:37:55 +08:00
2004-02-03 05:19:52 +08:00
/*
* Create the pool before accept() to avoid copy the sockaddr.
* Although accept() can fail it's an uncommon case
* and besides the pool can be got from the free pool list
*/
if (!(pool = ngx_create_pool(ls->listening->pool_size, ev->log))) {
return;
}
2003-05-14 00:02:32 +08:00
}
2003-11-20 00:26:41 +08:00
if (!(sa = ngx_palloc(pool, ls->listening->socklen))) {
2004-02-03 05:19:52 +08:00
ngx_destroy_pool(pool);
2003-05-14 00:02:32 +08:00
return;
}
2003-01-30 15:28:09 +08:00
2003-11-20 00:26:41 +08:00
if (!(log = ngx_palloc(pool, sizeof(ngx_log_t)))) {
2004-02-03 05:19:52 +08:00
ngx_destroy_pool(pool);
2003-11-20 00:26:41 +08:00
return;
}
2004-02-03 05:19:52 +08:00
2003-11-20 00:26:41 +08:00
ngx_memcpy(log, ls->log, sizeof(ngx_log_t));
pool->log = log;
2003-06-11 23:28:34 +08:00
2004-02-12 01:08:49 +08:00
if (!(ctx = ngx_palloc(pool, sizeof(ngx_accept_log_ctx_t)))) {
ngx_destroy_pool(pool);
return;
}
/* -1 disable logging the connection number */
ctx->flag = -1;
ctx->name = ls->listening->addr_text.data;
log->data = ctx;
2003-11-20 00:26:41 +08:00
log->handler = ngx_accept_log_error;
len = ls->listening->socklen;
2003-01-30 15:28:09 +08:00
s = accept(ls->fd, sa, &len);
if (s == -1) {
err = ngx_socket_errno;
2003-01-30 15:28:09 +08:00
if (err == NGX_EAGAIN) {
2004-02-18 01:53:12 +08:00
if (!(ngx_event_flags
& (NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT)))
{
ngx_log_error(NGX_LOG_NOTICE, log, err,
"EAGAIN after %d accepted connection(s)",
accepted);
}
2004-02-03 05:19:52 +08:00
ngx_destroy_pool(pool);
2003-05-14 00:02:32 +08:00
return;
}
2002-08-26 23:18:19 +08:00
2003-01-30 15:28:09 +08:00
ngx_log_error(NGX_LOG_ALERT, ev->log, err,
2003-11-20 00:26:41 +08:00
"accept() on %s failed",
ls->listening->addr_text.data);
2003-05-29 21:02:09 +08:00
2004-02-03 05:19:52 +08:00
if (err == NGX_ECONNABORTED) {
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
ev->available--;
}
if (ev->available) {
/* reuse the previously allocated pool */
continue;
}
}
2003-05-29 21:02:09 +08:00
ngx_destroy_pool(pool);
return;
}
2003-07-23 03:53:10 +08:00
/* disable warning: Win32 SOCKET is u_int while UNIX socket is int */
2003-07-21 05:15:59 +08:00
2003-05-30 22:27:59 +08:00
if ((unsigned) s >= (unsigned) ecf->connections) {
2003-05-29 21:02:09 +08:00
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
2003-11-20 00:26:41 +08:00
"accept() on %s returned socket #%d while "
2003-05-29 21:02:09 +08:00
"only %d connections was configured, "
"closing the connection",
2003-06-11 23:28:34 +08:00
ls->listening->addr_text.data, s, ecf->connections);
2003-05-29 21:02:09 +08:00
if (ngx_close_socket(s) == -1) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
ngx_close_socket_n "failed");
2003-05-29 21:02:09 +08:00
}
/* TODO: disable temporary accept() event */
2003-05-29 21:02:09 +08:00
ngx_destroy_pool(pool);
2003-05-14 00:02:32 +08:00
return;
}
2002-08-26 23:18:19 +08:00
2004-02-13 04:57:10 +08:00
/* set a blocking mode for aio and non-blocking mode for the others */
2003-05-21 21:28:21 +08:00
if (ngx_inherited_nonblocking) {
if ((ngx_event_flags & NGX_USE_AIO_EVENT)) {
if (ngx_blocking(s) == -1) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
ngx_blocking_n " failed");
2003-05-29 21:02:09 +08:00
if (ngx_close_socket(s) == -1) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
ngx_close_socket_n " failed");
2003-05-29 21:02:09 +08:00
}
ngx_destroy_pool(pool);
2003-05-21 21:28:21 +08:00
return;
}
2003-02-07 01:21:13 +08:00
}
2003-05-21 21:28:21 +08:00
} else {
2004-02-09 15:46:43 +08:00
if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) {
2003-05-21 21:28:21 +08:00
if (ngx_nonblocking(s) == -1) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
ngx_nonblocking_n " failed");
2003-05-29 21:02:09 +08:00
if (ngx_close_socket(s) == -1) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
ngx_close_socket_n " failed");
2003-05-29 21:02:09 +08:00
}
ngx_destroy_pool(pool);
2003-05-21 21:28:21 +08:00
return;
}
2003-02-07 01:21:13 +08:00
}
}
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.
*/
if (s % 4) {
2003-11-20 00:26:41 +08:00
ngx_log_error(NGX_LOG_EMERG, ev->log, 0,
"accept() on %s returned socket #%d, "
"not divisible by 4",
ls->listening->addr_text.data, s);
2003-06-11 23:28:34 +08:00
exit(1);
}
2003-07-07 14:11:50 +08:00
c = &ngx_cycle->connections[s / 4];
rev = &ngx_cycle->read_events[s / 4];
wev = &ngx_cycle->write_events[s / 4];
2003-06-11 23:28:34 +08:00
#else
2003-07-07 14:11:50 +08:00
c = &ngx_cycle->connections[s];
rev = &ngx_cycle->read_events[s];
wev = &ngx_cycle->write_events[s];
2003-06-11 23:28:34 +08:00
#endif
2002-12-15 14:25:09 +08:00
2003-05-12 23:52:24 +08:00
instance = rev->instance;
2004-03-31 23:26:46 +08:00
rinstance = rev->returned_instance;
winstance = wev->returned_instance;
2003-05-12 23:52:24 +08:00
2002-12-15 14:25:09 +08:00
ngx_memzero(rev, sizeof(ngx_event_t));
ngx_memzero(wev, sizeof(ngx_event_t));
ngx_memzero(c, sizeof(ngx_connection_t));
2003-01-30 15:28:09 +08:00
c->pool = pool;
2003-06-11 23:28:34 +08:00
c->listening = ls->listening;
2003-01-30 15:28:09 +08:00
c->sockaddr = sa;
c->socklen = len;
2004-04-05 04:32:09 +08:00
if (ngx_event_flags & NGX_HAVE_INSTANCE_EVENT) {
rev->use_instance = 1;
rev->instance = (u_char) !instance;
rev->returned_instance = (u_char) rinstance;
wev->use_instance = 1;
wev->instance = (u_char) !instance;
wev->returned_instance = (u_char) winstance;
}
2003-05-12 23:52:24 +08:00
2003-11-20 00:26:41 +08:00
rev->index = NGX_INVALID_INDEX;
wev->index = NGX_INVALID_INDEX;
rev->data = c;
wev->data = c;
2002-08-26 23:18:19 +08:00
2002-12-15 14:25:09 +08:00
c->read = rev;
c->write = wev;
2002-12-15 14:25:09 +08:00
c->fd = s;
c->unexpected_eof = 1;
2003-11-20 00:26:41 +08:00
2002-12-15 14:25:09 +08:00
wev->write = 1;
2003-10-29 16:30:44 +08:00
wev->ready = 1;
2004-02-03 05:19:52 +08:00
if (ngx_event_flags
& (NGX_USE_AIO_EVENT|NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT))
{
/* aio, iocp, sigio, epoll */
2003-10-29 16:30:44 +08:00
rev->ready = 1;
2003-02-07 01:21:13 +08:00
}
2004-01-30 05:45:01 +08:00
if (ev->deferred_accept) {
rev->ready = 1;
}
2003-01-30 15:28:09 +08:00
c->ctx = ls->ctx;
c->servers = ls->servers;
2003-11-20 00:26:41 +08:00
c->log = log;
rev->log = log;
wev->log = log;
2003-12-09 04:48:12 +08:00
/*
2004-02-12 01:08:49 +08:00
* TODO: MT: - atomic increment (x86: lock xadd)
2004-02-24 04:57:12 +08:00
* or protection by critical section or light mutex
2003-12-09 04:48:12 +08:00
*
* TODO: MP: - allocated in a shared memory
* - atomic increment (x86: lock xadd)
2004-02-24 04:57:12 +08:00
* or protection by critical section or light mutex
2003-12-09 04:48:12 +08:00
*/
2003-12-19 16:15:11 +08:00
2004-02-24 04:57:12 +08:00
c->number = ngx_atomic_inc(&ngx_connection_counter);
2004-01-22 14:47:28 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
2004-02-24 04:57:12 +08:00
"accept: fd:%d c:%d", s, c->number);
2002-08-26 23:18:19 +08:00
2003-05-29 21:02:09 +08:00
if (ngx_add_conn) {
if (ngx_add_conn(c) == NGX_ERROR) {
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
2003-11-20 00:26:41 +08:00
ngx_close_socket_n " failed");
2003-05-29 21:02:09 +08:00
}
2003-03-04 14:33:48 +08:00
2003-05-29 21:02:09 +08:00
ngx_destroy_pool(pool);
2003-05-14 00:02:32 +08:00
return;
2003-03-04 14:33:48 +08:00
}
}
2004-02-03 05:19:52 +08:00
pool = NULL;
2003-11-20 00:26:41 +08:00
log->data = NULL;
log->handler = NULL;
2003-06-11 23:28:34 +08:00
ls->listening->handler(c);
2003-05-20 00:39:14 +08:00
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
ev->available--;
2003-01-30 15:28:09 +08:00
}
2003-11-20 00:26:41 +08:00
accepted++;
2003-01-30 15:28:09 +08:00
} while (ev->available);
}
2003-11-20 00:26:41 +08:00
2004-03-31 23:26:46 +08:00
ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle)
{
2004-04-05 04:32:09 +08:00
if (*ngx_accept_mutex == 0
&& ngx_atomic_cmp_set(ngx_accept_mutex, 0, ngx_pid))
{
2004-03-31 23:26:46 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"accept mutex locked");
2004-04-01 14:21:13 +08:00
if (!ngx_accept_mutex_held) {
2004-03-31 23:26:46 +08:00
if (ngx_enable_accept_events(cycle) == NGX_ERROR) {
2004-04-01 14:21:13 +08:00
*ngx_accept_mutex = 0;
2004-03-31 23:26:46 +08:00
return NGX_ERROR;
}
2004-04-01 14:21:13 +08:00
ngx_accept_mutex_held = 1;
2004-03-31 23:26:46 +08:00
}
return NGX_OK;
}
2004-04-01 14:21:13 +08:00
if (ngx_accept_mutex_held) {
2004-03-31 23:26:46 +08:00
if (ngx_disable_accept_events(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
2004-04-01 14:21:13 +08:00
ngx_accept_mutex_held = 0;
2004-03-31 23:26:46 +08:00
}
return NGX_OK;
}
ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_listening_t *s;
s = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
/*
* we do not need to handle the Winsock sockets here (divde a socket
* number by 4) because this function would never called
* in the Winsock environment
*/
if (ngx_event_flags & NGX_USE_SIGIO_EVENT) {
if (ngx_add_conn(&cycle->connections[s[i].fd]) == NGX_ERROR) {
return NGX_ERROR;
}
} else {
if (ngx_add_event(&cycle->read_events[s[i].fd], NGX_READ_EVENT, 0)
== NGX_ERROR)
{
return NGX_ERROR;
}
}
}
return NGX_OK;
}
ngx_int_t ngx_disable_accept_events(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_listening_t *s;
s = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
/*
* we do not need to handle the Winsock sockets here (divde a socket
* number by 4) because this function would never called
* in the Winsock environment
*/
if (ngx_event_flags & NGX_USE_SIGIO_EVENT) {
if (ngx_del_conn(&cycle->connections[s[i].fd], NGX_DISABLE_EVENT)
== NGX_ERROR)
{
return NGX_ERROR;
}
} else {
if (ngx_del_event(&cycle->read_events[s[i].fd], NGX_READ_EVENT,
NGX_DISABLE_EVENT) == NGX_ERROR)
{
return NGX_ERROR;
}
}
}
return NGX_OK;
}
2003-11-20 00:26:41 +08:00
static size_t ngx_accept_log_error(void *data, char *buf, size_t len)
{
2004-02-12 01:08:49 +08:00
ngx_accept_log_ctx_t *ctx = data;
2003-11-20 00:26:41 +08:00
2004-02-12 01:08:49 +08:00
return ngx_snprintf(buf, len, " while accept() on %s", ctx->name);
2003-11-20 00:26:41 +08:00
}