2002-08-07 00:39:45 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_config.h>
|
2002-08-26 23:18:19 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_event.h>
|
2003-06-02 23:24:30 +08:00
|
|
|
#include <nginx.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
static void ngx_close_accepted_socket(ngx_socket_t s, ngx_log_t *log);
|
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)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2004-07-07 23:01:00 +08:00
|
|
|
ngx_uint_t instance, 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-07-07 23:01:00 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
2004-02-03 05:19:52 +08:00
|
|
|
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
|
|
|
|
2002-08-07 00:39:45 +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
|
|
|
|
2002-08-07 00:39:45 +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
|
|
|
/*
|
2004-06-07 03:49:18 +08:00
|
|
|
* Create the pool before accept() to avoid the copying of
|
|
|
|
* the sockaddr. Although accept() can fail it is uncommon
|
|
|
|
* case and besides the pool can be got from the free pool list
|
2004-02-03 05:19:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-06-07 03:49:18 +08:00
|
|
|
/* -1 disables the connection number logging */
|
2004-02-12 01:08:49 +08:00
|
|
|
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) {
|
2002-08-07 00:39:45 +08:00
|
|
|
err = ngx_socket_errno;
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
if (err == NGX_EAGAIN) {
|
2004-09-14 23:55:24 +08:00
|
|
|
#if 0
|
2004-07-07 23:01:00 +08:00
|
|
|
if (!(ngx_event_flags & NGX_USE_RTSIG_EVENT))
|
2004-02-18 01:53:12 +08:00
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, log, err,
|
|
|
|
"EAGAIN after %d accepted connection(s)",
|
|
|
|
accepted);
|
|
|
|
}
|
2004-09-14 23:55:24 +08:00
|
|
|
#endif
|
2004-02-03 05:19:52 +08:00
|
|
|
|
|
|
|
ngx_destroy_pool(pool);
|
2003-05-14 00:02:32 +08:00
|
|
|
return;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-15 03:39:54 +08:00
|
|
|
#if (NGX_STAT_STUB)
|
|
|
|
(*ngx_stat_accepted)++;
|
|
|
|
#endif
|
2004-04-22 02:54:33 +08:00
|
|
|
|
|
|
|
ngx_accept_disabled = (ngx_uint_t) s + NGX_ACCEPT_THRESHOLD
|
|
|
|
- ecf->connections;
|
|
|
|
|
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
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
if ((ngx_uint_t) s >= 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, "
|
2003-11-20 15:05:50 +08:00
|
|
|
"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
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
ngx_close_accepted_socket(s, log);
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_destroy_pool(pool);
|
2003-05-14 00:02:32 +08:00
|
|
|
return;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-09-20 23:17:02 +08:00
|
|
|
#if (NGX_STAT_STUB)
|
|
|
|
(*ngx_stat_active)++;
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
ngx_close_accepted_socket(s, log);
|
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-06-07 03:49:18 +08:00
|
|
|
if (!(ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_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
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
ngx_close_accepted_socket(s, log);
|
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-07-05 23:08:23 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-07-07 14:15:04 +08:00
|
|
|
|
|
|
|
if (*(&c->lock)) {
|
2004-07-07 00:12:16 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
2004-07-07 14:15:04 +08:00
|
|
|
"spinlock in accept, fd:%", s);
|
|
|
|
ngx_spinlock(&c->lock, 1000);
|
|
|
|
ngx_unlock(&c->lock);
|
2004-07-05 23:08:23 +08:00
|
|
|
}
|
2004-07-07 14:15:04 +08:00
|
|
|
|
2004-07-05 23:08:23 +08:00
|
|
|
#endif
|
|
|
|
|
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;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-07-07 23:01:00 +08:00
|
|
|
rev->instance = !instance;
|
|
|
|
wev->instance = !instance;
|
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-08-07 00:39:45 +08:00
|
|
|
|
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;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-07-07 23:01:00 +08:00
|
|
|
if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_EVENT)) {
|
2004-06-07 03:49:18 +08:00
|
|
|
/* epoll, rtsig, aio, iocp */
|
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;
|
|
|
|
|
2004-07-19 03:11:20 +08:00
|
|
|
c->recv = ngx_recv;
|
|
|
|
c->send_chain = ngx_send_chain;
|
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
c->log = log;
|
|
|
|
rev->log = log;
|
|
|
|
wev->log = log;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
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-04-10 00:03:04 +08:00
|
|
|
c->number = ngx_atomic_inc(ngx_connection_counter);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-06-28 02:01:57 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
rev->lock = &c->lock;
|
|
|
|
wev->lock = &c->lock;
|
2004-07-07 14:15:04 +08:00
|
|
|
rev->own_lock = &c->lock;
|
|
|
|
wev->own_lock = &c->lock;
|
2004-06-28 02:01:57 +08:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
if (c->listening->addr_ntop) {
|
|
|
|
c->addr_text.data = ngx_palloc(c->pool,
|
|
|
|
c->listening->addr_text_max_len);
|
|
|
|
if (c->addr_text.data == NULL) {
|
|
|
|
ngx_close_accepted_socket(s, log);
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->addr_text.len = ngx_sock_ntop(c->listening->family, c->sockaddr,
|
|
|
|
c->addr_text.data,
|
|
|
|
c->listening->addr_text_max_len);
|
|
|
|
if (c->addr_text.len == 0) {
|
|
|
|
ngx_close_accepted_socket(s, log);
|
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-08 23:58:25 +08:00
|
|
|
#if (NGX_DEBUG)
|
|
|
|
{
|
|
|
|
|
|
|
|
uint32_t *addr;
|
|
|
|
in_addr_t i;
|
|
|
|
struct sockaddr_in *addr_in;
|
|
|
|
|
|
|
|
addr_in = (struct sockaddr_in *) sa;
|
|
|
|
addr = ecf->debug_connection.elts;
|
|
|
|
for (i = 0; i < ecf->debug_connection.nelts; i++) {
|
|
|
|
if (addr[i] == addr_in->sin_addr.s_addr) {
|
|
|
|
log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-07 23:01:00 +08:00
|
|
|
if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
|
2003-05-29 21:02:09 +08:00
|
|
|
if (ngx_add_conn(c) == NGX_ERROR) {
|
2004-09-09 23:40:48 +08:00
|
|
|
ngx_close_accepted_socket(s, log);
|
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);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
|
2002-08-07 00:39:45 +08:00
|
|
|
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
|
|
|
|
2002-08-07 00:39:45 +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++) {
|
|
|
|
|
|
|
|
/*
|
2004-06-07 03:49:18 +08:00
|
|
|
* we do not need to handle the Winsock sockets here (divide a socket
|
2004-03-31 23:26:46 +08:00
|
|
|
* number by 4) because this function would never called
|
|
|
|
* in the Winsock environment
|
|
|
|
*/
|
|
|
|
|
2004-06-07 03:49:18 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
2004-03-31 23:26:46 +08:00
|
|
|
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++) {
|
|
|
|
|
|
|
|
/*
|
2004-06-07 03:49:18 +08:00
|
|
|
* we do not need to handle the Winsock sockets here (divide a socket
|
2004-03-31 23:26:46 +08:00
|
|
|
* number by 4) because this function would never called
|
|
|
|
* in the Winsock environment
|
|
|
|
*/
|
|
|
|
|
2004-06-07 03:49:18 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
2004-04-15 01:44:28 +08:00
|
|
|
if (!cycle->connections[s[i].fd].read->active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
if (ngx_del_conn(&cycle->connections[s[i].fd], NGX_DISABLE_EVENT)
|
|
|
|
== NGX_ERROR)
|
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2004-04-15 01:44:28 +08:00
|
|
|
if (!cycle->read_events[s[i].fd].active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
if (ngx_del_event(&cycle->read_events[s[i].fd], NGX_READ_EVENT,
|
|
|
|
NGX_DISABLE_EVENT) == NGX_ERROR)
|
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
static void ngx_close_accepted_socket(ngx_socket_t s, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
if (ngx_close_socket(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|