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>
|
|
|
|
|
|
|
|
|
|
|
|
int ngx_event_accept(ngx_event_t *ev)
|
|
|
|
{
|
2002-12-15 14:25:09 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_socket_t s;
|
|
|
|
ngx_event_t *rev, *wev;
|
|
|
|
ngx_connection_t *c, *ac;
|
|
|
|
|
|
|
|
ac = (ngx_connection_t *) ev->data;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
ngx_log_debug(ev->log, "ngx_event_accept: accept ready: %d" _
|
|
|
|
ev->available);
|
|
|
|
|
|
|
|
ev->ready = 0;
|
|
|
|
|
|
|
|
do {
|
2002-12-15 14:25:09 +08:00
|
|
|
if ((s = accept(ac->fd, ac->sockaddr, &ac->socklen)) == -1) {
|
2002-08-07 00:39:45 +08:00
|
|
|
err = ngx_socket_errno;
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, ev->log, err,
|
2002-08-26 23:18:19 +08:00
|
|
|
"ngx_event_accept: EAGAIN while accept %s",
|
2002-12-15 14:25:09 +08:00
|
|
|
ac->addr_text);
|
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
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, ev->log, err,
|
2002-12-15 14:25:09 +08:00
|
|
|
"ngx_event_accept: accept %s failed", ac->addr_text);
|
2002-08-26 23:18:19 +08:00
|
|
|
/* if we return NGX_ERROR listen socket would be closed */
|
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
#if !(HAVE_INHERITED_NONBLOCK)
|
|
|
|
if (ngx_nonblocking(s) == -1)
|
2002-12-16 01:10:12 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, ev->log, ngx_socket_errno,
|
2002-08-20 22:48:28 +08:00
|
|
|
ngx_nonblocking_n "failed");
|
|
|
|
#endif
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
c->sockaddr = ac->sockaddr;
|
|
|
|
c->family = ac->family;
|
|
|
|
c->socklen = ac->socklen;
|
|
|
|
c->addr = ac->addr;
|
2003-01-09 13:36:00 +08:00
|
|
|
c->addr_text_max_len = ac->addr_text_max_len;
|
2002-12-15 14:25:09 +08:00
|
|
|
c->post_accept_timeout = ac->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;
|
|
|
|
wev->ready = 1;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
wev->timer = rev->timer = 10000;
|
|
|
|
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-09 13:36:00 +08:00
|
|
|
c->ctx = ac->ctx;
|
2002-12-15 14:25:09 +08:00
|
|
|
c->servers = ac->servers;
|
|
|
|
c->log = rev->log = wev->log = ev->log;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
/* STUB: x86: SP: xadd, MT: lock xadd, MP: lock xadd, shared */
|
|
|
|
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" _
|
|
|
|
s _ c->number);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_DEFERRED_ACCEPT)
|
|
|
|
if (ev->accept_filter)
|
2002-12-15 14:25:09 +08:00
|
|
|
rev->ready = 1;
|
2002-08-07 00:39:45 +08:00
|
|
|
#endif
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
ac->handler(c);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
#if !(USE_KQUEUE)
|
|
|
|
if (ngx_event_type == NGX_KQUEUE_EVENT)
|
|
|
|
#endif
|
|
|
|
ev->available--;
|
|
|
|
#endif
|
|
|
|
} while (ev->available);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|