2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
#include <nginx.h>
|
|
|
|
|
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_types.h>
|
|
|
|
#include <ngx_log.h>
|
|
|
|
#include <ngx_connection.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
#include <ngx_event_close.h>
|
|
|
|
#include <ngx_event_accept.h>
|
|
|
|
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
/* This function should always return NGX_OK even there are some failures
|
|
|
|
because if we return NGX_ERROR then listening socket would be closed */
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
int ngx_event_accept(ngx_event_t *ev)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
ls = (ngx_connection_t *) 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
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
#if 0
|
2003-01-30 15:28:09 +08:00
|
|
|
/* DEBUG */ ev->available++;
|
2003-02-07 01:21:13 +08:00
|
|
|
#endif
|
2003-01-30 15:28:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
do {
|
2003-01-30 15:28:09 +08:00
|
|
|
ngx_test_null(pool, ngx_create_pool(ls->pool_size, ev->log), NGX_OK);
|
|
|
|
|
|
|
|
ngx_test_null(sa, ngx_palloc(pool, ls->socklen), NGX_OK);
|
|
|
|
len = ls->socklen;
|
|
|
|
|
|
|
|
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
|
|
|
ngx_destroy_pool(pool);
|
|
|
|
|
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,
|
|
|
|
"EAGAIN while accept %s", ls->addr_text.data);
|
2002-08-26 23:18:19 +08:00
|
|
|
return NGX_OK;
|
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,
|
|
|
|
"accept %s failed", ls->addr_text.data);
|
2002-08-26 23:18:19 +08:00
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
|
|
|
|
#if (HAVE_INHERITED_NONBLOCK)
|
|
|
|
|
|
|
|
#if (HAVE_AIO_EVENT)
|
|
|
|
if ((ngx_event_flags & NGX_HAVE_AIO_EVENT)) {
|
|
|
|
if (ngx_blocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_blocking_n " %s failed", ls->addr_text.data);
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else /* !HAVE_INHERITED_NONBLOCK */
|
|
|
|
|
|
|
|
#if (HAVE_AIO_EVENT)
|
|
|
|
if (!(ngx_event_flags & NGX_HAVE_AIO_EVENT)) {
|
|
|
|
if (ngx_nonblocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_nonblocking_n " %s failed", ls->addr_text.data);
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2003-01-30 15:28:09 +08:00
|
|
|
if (ngx_nonblocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
|
|
|
|
ngx_nonblocking_n " %s failed", ls->addr_text.data);
|
2003-02-07 01:21:13 +08:00
|
|
|
return NGX_OK;
|
2003-01-30 15:28:09 +08:00
|
|
|
}
|
2002-08-20 22:48:28 +08:00
|
|
|
#endif
|
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
#endif /* HAVE_INHERITED_NONBLOCK */
|
|
|
|
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
rev = &ngx_read_events[s];
|
|
|
|
wev = &ngx_write_events[s];
|
|
|
|
c = &ngx_connections[s];
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
c->sockaddr = sa;
|
|
|
|
c->family = ls->family;
|
|
|
|
c->socklen = len;
|
|
|
|
c->addr = ls->addr;
|
|
|
|
c->addr_text_max_len = ls->addr_text_max_len;
|
|
|
|
c->post_accept_timeout = ls->post_accept_timeout;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
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-03-21 00:09:44 +08:00
|
|
|
rev->first = wev->first = 1;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-04-15 01:04:58 +08:00
|
|
|
#if (USE_KQUEUE)
|
|
|
|
wev->ready = 1;
|
|
|
|
#else
|
|
|
|
if ((ngx_event_flags & NGX_USE_AIO_EVENT) == 0) {
|
2003-02-07 01:21:13 +08:00
|
|
|
wev->ready = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* STUB ? */ wev->timer = rev->timer = 10000;
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
wev->timer_handler = rev->timer_handler = ngx_event_close_connection;
|
|
|
|
wev->close_handler = rev->close_handler = ngx_event_close_connection;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
c->ctx = ls->ctx;
|
|
|
|
c->servers = ls->servers;
|
|
|
|
|
|
|
|
ngx_test_null(c->log, ngx_palloc(c->pool, sizeof(ngx_log_t)), NGX_OK);
|
|
|
|
ngx_memcpy(c->log, ev->log, sizeof(ngx_log_t));
|
|
|
|
rev->log = wev->log = c->log;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
/* STUB: x86: SP: xadd ?, 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
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_log_debug(ev->log, "ngx_event_accept: accept: %d, %d" _
|
2003-01-30 15:28:09 +08:00
|
|
|
s _ c->number);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_DEFERRED_ACCEPT)
|
2003-01-30 15:28:09 +08:00
|
|
|
if (ev->accept_filter) {
|
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
|
|
|
#endif
|
|
|
|
|
2003-03-04 14:33:48 +08:00
|
|
|
#if (HAVE_EDGE_EVENT) /* epoll */
|
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_HAVE_EDGE_EVENT) {
|
|
|
|
if (ngx_edge_add_event(ev) == NGX_ERROR) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
ls->handler(c);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
#if (USE_KQUEUE)
|
|
|
|
|
|
|
|
ev->available--;
|
|
|
|
|
|
|
|
#elif (HAVE_KQUEUE)
|
|
|
|
|
2003-02-27 04:21:43 +08:00
|
|
|
/*
|
|
|
|
if (ngx_event_type == NGX_HAVE_AIO_EVENT or NGX_HAVE_KQUEUE_EVENT) {
|
|
|
|
*/
|
2003-02-11 15:14:40 +08:00
|
|
|
if (ngx_event_type == 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
|
|
|
#endif
|
|
|
|
} while (ev->available);
|
|
|
|
|
2003-01-30 15:28:09 +08:00
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|