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
|
|
|
|
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
void ngx_event_accept(ngx_event_t *ev)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-05-12 23:52:24 +08:00
|
|
|
int instance;
|
2003-01-30 15:28:09 +08:00
|
|
|
socklen_t len;
|
|
|
|
struct sockaddr *sa;
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_err_t err;
|
2003-01-30 15:28:09 +08:00
|
|
|
ngx_pool_t *pool;
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_socket_t s;
|
|
|
|
ngx_event_t *rev, *wev;
|
2003-01-30 15:28:09 +08:00
|
|
|
ngx_connection_t *c, *ls;
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_event_conf_t *ecf;
|
|
|
|
|
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
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
ls = ev->data;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_log_debug(ev->log, "ngx_event_accept: accept ready: %d" _
|
|
|
|
ev->available);
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
ev->ready = 0;
|
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
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
/*
|
|
|
|
* Create the pool before accept() to avoid copy the sockaddr.
|
2003-06-11 23:28:34 +08:00
|
|
|
* Although accept() can fail it's an uncommon case
|
2003-06-02 23:24:30 +08:00
|
|
|
* and the pool can be got from the free pool list
|
|
|
|
*/
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2003-06-11 23:28:34 +08:00
|
|
|
pool = ngx_create_pool(ls->listening->pool_size, ev->log);
|
2003-05-14 00:02:32 +08:00
|
|
|
if (pool == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-06-11 23:28:34 +08:00
|
|
|
sa = ngx_palloc(pool, ls->listening->socklen);
|
2003-05-14 00:02:32 +08:00
|
|
|
if (sa == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2003-06-11 23:28:34 +08:00
|
|
|
len = ls->listening->socklen;
|
|
|
|
|
|
|
|
ngx_log_debug(ev->log, "ADDR %s" _ ls->listening->addr_text.data);
|
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) {
|
2003-01-30 15:28:09 +08:00
|
|
|
ngx_log_error(NGX_LOG_NOTICE, ev->log, err,
|
2003-06-11 23:28:34 +08:00
|
|
|
"EAGAIN while accept() %s",
|
|
|
|
ls->listening->addr_text.data);
|
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-06-11 23:28:34 +08:00
|
|
|
"accept() %s failed", ls->listening->addr_text.data);
|
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,
|
|
|
|
"accept() %s returned socket #%d while "
|
|
|
|
"only %d connections was configured, "
|
|
|
|
"sleeping for 1 second",
|
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) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_msleep(1000);
|
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
|
|
|
|
2003-05-21 21:28:21 +08:00
|
|
|
/* set a blocking mode for aio and non-blocking mode for others */
|
|
|
|
|
|
|
|
if (ngx_inherited_nonblocking) {
|
|
|
|
if ((ngx_event_flags & NGX_USE_AIO_EVENT)) {
|
|
|
|
if (ngx_blocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_blocking_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
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 {
|
|
|
|
if ((ngx_event_flags & NGX_USE_AIO_EVENT) == 0) {
|
|
|
|
if (ngx_nonblocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_nonblocking_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
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) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, ls->log, 0,
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_socket_n
|
|
|
|
" created socket %d, not divisible by 4", 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;
|
|
|
|
|
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
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
rev->instance = wev->instance = !instance;
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
rev->index = wev->index = NGX_INVALID_INDEX;
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
rev->data = wev->data = c;
|
|
|
|
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;
|
|
|
|
wev->write = 1;
|
2003-10-29 16:30:44 +08:00
|
|
|
wev->ready = 1;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_EDGE_EVENT)) {
|
|
|
|
/* aio, iocp, epoll */
|
|
|
|
rev->ready = 1;
|
2003-02-07 01:21:13 +08:00
|
|
|
}
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
c->ctx = ls->ctx;
|
|
|
|
c->servers = ls->servers;
|
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
c->log = ngx_palloc(c->pool, sizeof(ngx_log_t));
|
|
|
|
if (c->log == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2003-07-21 05:15:59 +08:00
|
|
|
ngx_memcpy(c->log, ls->log, sizeof(ngx_log_t));
|
2003-07-11 23:17:50 +08:00
|
|
|
rev->log = wev->log = c->log;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-06-02 23:24:30 +08:00
|
|
|
/* TODO: x86: MT: lock xadd, MP: lock xadd, shared */
|
2002-12-15 14:25:09 +08:00
|
|
|
c->number = ngx_connection_counter++;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_log_debug(ev->log, "accept: %d, %d" _ s _ c->number);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
if (ev->deferred_accept) {
|
2002-12-15 14:25:09 +08:00
|
|
|
rev->ready = 1;
|
2003-01-30 15:28:09 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +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,
|
|
|
|
ngx_close_socket_n " %s failed",
|
2003-06-11 23:28:34 +08:00
|
|
|
ls->listening->addr_text.data);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
} while (ev->available);
|
2003-05-27 20:18:54 +08:00
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
return;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|