2004-01-06 04:55:48 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
ngx_os_io_t ngx_io;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
|
2011-04-04 20:26:53 +08:00
|
|
|
static void ngx_drain_connections(void);
|
|
|
|
|
|
|
|
|
2009-05-06 01:33:26 +08:00
|
|
|
ngx_listening_t *
|
|
|
|
ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
|
|
|
|
{
|
2009-10-26 19:43:32 +08:00
|
|
|
size_t len;
|
2009-05-06 01:33:26 +08:00
|
|
|
ngx_listening_t *ls;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
u_char text[NGX_SOCKADDR_STRLEN];
|
|
|
|
|
|
|
|
ls = ngx_array_push(&cf->cycle->listening);
|
|
|
|
if (ls == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(ls, sizeof(ngx_listening_t));
|
|
|
|
|
|
|
|
sa = ngx_palloc(cf->pool, socklen);
|
|
|
|
if (sa == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(sa, sockaddr, socklen);
|
|
|
|
|
|
|
|
ls->sockaddr = sa;
|
|
|
|
ls->socklen = socklen;
|
|
|
|
|
2013-07-11 20:07:25 +08:00
|
|
|
len = ngx_sock_ntop(sa, socklen, text, NGX_SOCKADDR_STRLEN, 1);
|
2009-10-26 19:43:32 +08:00
|
|
|
ls->addr_text.len = len;
|
2009-05-06 01:33:26 +08:00
|
|
|
|
|
|
|
switch (ls->sockaddr->sa_family) {
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
case AF_INET6:
|
|
|
|
ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN;
|
|
|
|
break;
|
2009-10-26 19:43:32 +08:00
|
|
|
#endif
|
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
|
|
|
case AF_UNIX:
|
|
|
|
ls->addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
|
|
|
|
len++;
|
|
|
|
break;
|
2009-05-06 01:33:26 +08:00
|
|
|
#endif
|
|
|
|
case AF_INET:
|
|
|
|
ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ls->addr_text_max_len = NGX_SOCKADDR_STRLEN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-26 19:43:32 +08:00
|
|
|
ls->addr_text.data = ngx_pnalloc(cf->pool, len);
|
|
|
|
if (ls->addr_text.data == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(ls->addr_text.data, text, len);
|
|
|
|
|
|
|
|
ls->fd = (ngx_socket_t) -1;
|
|
|
|
ls->type = SOCK_STREAM;
|
|
|
|
|
2009-05-06 01:33:26 +08:00
|
|
|
ls->backlog = NGX_LISTEN_BACKLOG;
|
|
|
|
ls->rcvbuf = -1;
|
|
|
|
ls->sndbuf = -1;
|
|
|
|
|
2010-07-05 21:49:16 +08:00
|
|
|
#if (NGX_HAVE_SETFIB)
|
|
|
|
ls->setfib = -1;
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 02:07:03 +08:00
|
|
|
#if (NGX_HAVE_TCP_FASTOPEN)
|
|
|
|
ls->fastopen = -1;
|
|
|
|
#endif
|
|
|
|
|
2009-05-06 01:33:26 +08:00
|
|
|
return ls;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_set_inherited_sockets(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2005-06-16 02:33:41 +08:00
|
|
|
size_t len;
|
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_listening_t *ls;
|
2005-10-19 20:33:58 +08:00
|
|
|
socklen_t olen;
|
2014-01-31 06:58:21 +08:00
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT || NGX_HAVE_TCP_FASTOPEN)
|
2005-09-08 22:36:09 +08:00
|
|
|
ngx_err_t err;
|
2013-10-31 08:00:37 +08:00
|
|
|
#endif
|
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
2005-06-16 02:33:41 +08:00
|
|
|
struct accept_filter_arg af;
|
|
|
|
#endif
|
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
|
|
int timeout;
|
|
|
|
#endif
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
|
2010-05-31 22:49:11 +08:00
|
|
|
ls[i].sockaddr = ngx_palloc(cycle->pool, NGX_SOCKADDRLEN);
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ls[i].sockaddr == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-05-31 22:49:11 +08:00
|
|
|
ls[i].socklen = NGX_SOCKADDRLEN;
|
2004-01-07 00:49:34 +08:00
|
|
|
if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
|
|
|
|
"getsockname() of the inherited "
|
|
|
|
"socket #%d failed", ls[i].fd);
|
|
|
|
ls[i].ignore = 1;
|
2004-01-06 04:55:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-21 15:02:02 +08:00
|
|
|
switch (ls[i].sockaddr->sa_family) {
|
2004-01-07 00:49:34 +08:00
|
|
|
|
2009-02-21 15:02:02 +08:00
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
case AF_INET6:
|
|
|
|
ls[i].addr_text_max_len = NGX_INET6_ADDRSTRLEN;
|
2014-02-22 16:08:31 +08:00
|
|
|
len = NGX_INET6_ADDRSTRLEN + sizeof("[]:65535") - 1;
|
2009-11-05 02:36:43 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
|
|
|
case AF_UNIX:
|
|
|
|
ls[i].addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
|
|
|
|
len = NGX_UNIX_ADDRSTRLEN;
|
2009-02-21 15:02:02 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN;
|
2009-11-05 02:36:43 +08:00
|
|
|
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
|
2009-02-21 15:02:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
|
|
|
|
"the inherited socket #%d has "
|
2009-02-21 15:02:02 +08:00
|
|
|
"an unsupported protocol family", ls[i].fd);
|
2004-01-07 00:49:34 +08:00
|
|
|
ls[i].ignore = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2009-02-21 15:02:02 +08:00
|
|
|
ls[i].addr_text.data = ngx_pnalloc(cycle->pool, len);
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ls[i].addr_text.data == NULL) {
|
|
|
|
return NGX_ERROR;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-11 20:07:25 +08:00
|
|
|
len = ngx_sock_ntop(ls[i].sockaddr, ls[i].socklen,
|
|
|
|
ls[i].addr_text.data, len, 1);
|
2004-11-11 22:07:14 +08:00
|
|
|
if (len == 0) {
|
2004-01-07 00:49:34 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2009-02-21 15:02:02 +08:00
|
|
|
ls[i].addr_text.len = len;
|
2005-06-16 02:33:41 +08:00
|
|
|
|
2007-08-09 21:32:21 +08:00
|
|
|
ls[i].backlog = NGX_LISTEN_BACKLOG;
|
2005-08-30 18:55:07 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
olen = sizeof(int);
|
|
|
|
|
|
|
|
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF, (void *) &ls[i].rcvbuf,
|
|
|
|
&olen)
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"getsockopt(SO_RCVBUF) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
|
|
|
|
ls[i].rcvbuf = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
olen = sizeof(int);
|
|
|
|
|
|
|
|
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF, (void *) &ls[i].sndbuf,
|
|
|
|
&olen)
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"getsockopt(SO_SNDBUF) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
|
|
|
|
ls[i].sndbuf = -1;
|
|
|
|
}
|
|
|
|
|
2010-07-05 21:49:16 +08:00
|
|
|
#if 0
|
|
|
|
/* SO_SETFIB is currently a set only option */
|
|
|
|
|
|
|
|
#if (NGX_HAVE_SETFIB)
|
|
|
|
|
2013-12-19 17:43:18 +08:00
|
|
|
olen = sizeof(int);
|
|
|
|
|
|
|
|
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_SETFIB,
|
2010-07-05 21:49:16 +08:00
|
|
|
(void *) &ls[i].setfib, &olen)
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"getsockopt(SO_SETFIB) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
|
|
|
|
ls[i].setfib = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 02:07:03 +08:00
|
|
|
#if (NGX_HAVE_TCP_FASTOPEN)
|
|
|
|
|
2013-12-19 17:43:18 +08:00
|
|
|
olen = sizeof(int);
|
|
|
|
|
|
|
|
if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_FASTOPEN,
|
2013-12-04 02:07:03 +08:00
|
|
|
(void *) &ls[i].fastopen, &olen)
|
|
|
|
== -1)
|
|
|
|
{
|
2014-01-31 06:58:21 +08:00
|
|
|
err = ngx_socket_errno;
|
|
|
|
|
|
|
|
if (err != NGX_EOPNOTSUPP && err != NGX_ENOPROTOOPT) {
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
|
|
|
|
"getsockopt(TCP_FASTOPEN) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
}
|
2013-12-04 02:07:03 +08:00
|
|
|
|
|
|
|
ls[i].fastopen = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-06-16 02:33:41 +08:00
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
|
|
|
|
|
|
ngx_memzero(&af, sizeof(struct accept_filter_arg));
|
2005-10-19 20:33:58 +08:00
|
|
|
olen = sizeof(struct accept_filter_arg);
|
2005-06-16 02:33:41 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, &af, &olen)
|
2005-06-16 02:33:41 +08:00
|
|
|
== -1)
|
|
|
|
{
|
2014-02-04 06:17:17 +08:00
|
|
|
err = ngx_socket_errno;
|
2005-09-08 22:36:09 +08:00
|
|
|
|
|
|
|
if (err == NGX_EINVAL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
|
2005-06-16 02:33:41 +08:00
|
|
|
"getsockopt(SO_ACCEPTFILTER) for %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (olen < sizeof(struct accept_filter_arg) || af.af_name[0] == '\0') {
|
2005-06-16 02:33:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ls[i].accept_filter = ngx_palloc(cycle->pool, 16);
|
|
|
|
if (ls[i].accept_filter == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) ngx_cpystrn((u_char *) ls[i].accept_filter,
|
|
|
|
(u_char *) af.af_name, 16);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
|
|
|
|
|
|
timeout = 0;
|
2005-10-19 20:33:58 +08:00
|
|
|
olen = sizeof(int);
|
2005-06-16 02:33:41 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, &olen)
|
2005-06-16 02:33:41 +08:00
|
|
|
== -1)
|
|
|
|
{
|
2014-02-04 06:17:17 +08:00
|
|
|
err = ngx_socket_errno;
|
2013-10-31 08:00:37 +08:00
|
|
|
|
|
|
|
if (err == NGX_EOPNOTSUPP) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
|
2005-06-16 02:33:41 +08:00
|
|
|
"getsockopt(TCP_DEFER_ACCEPT) for %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-19 21:34:28 +08:00
|
|
|
if (olen < sizeof(int) || timeout == 0) {
|
2005-06-16 02:33:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ls[i].deferred_accept = 1;
|
|
|
|
#endif
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_open_listening_sockets(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2005-10-19 20:33:58 +08:00
|
|
|
int reuseaddr;
|
|
|
|
ngx_uint_t i, tries, failed;
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_log_t *log;
|
|
|
|
ngx_socket_t s;
|
|
|
|
ngx_listening_t *ls;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
reuseaddr = 1;
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
|
|
|
failed = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
log = cycle->log;
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
/* TODO: configurable try number */
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2008-05-22 20:09:41 +08:00
|
|
|
for (tries = 5; tries; tries--) {
|
2004-01-06 04:55:48 +08:00
|
|
|
failed = 0;
|
|
|
|
|
|
|
|
/* for each listening socket */
|
|
|
|
|
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
|
|
|
|
if (ls[i].ignore) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-05 00:48:28 +08:00
|
|
|
if (ls[i].fd != (ngx_socket_t) -1) {
|
2004-01-06 04:55:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].inherited) {
|
|
|
|
|
|
|
|
/* TODO: close on exit */
|
|
|
|
/* TODO: nonblocking */
|
|
|
|
/* TODO: deferred accept */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-22 03:24:07 +08:00
|
|
|
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2013-09-05 00:48:28 +08:00
|
|
|
if (s == (ngx_socket_t) -1) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_socket_n " %V failed", &ls[i].addr_text);
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
|
2005-10-19 20:33:58 +08:00
|
|
|
(const void *) &reuseaddr, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
"setsockopt(SO_REUSEADDR) %V failed",
|
|
|
|
&ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2007-05-30 20:28:37 +08:00
|
|
|
if (ngx_close_socket(s) == -1) {
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %V failed",
|
|
|
|
&ls[i].addr_text);
|
2007-05-30 20:28:37 +08:00
|
|
|
}
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:20:34 +08:00
|
|
|
#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
|
|
|
|
|
2012-07-30 20:27:06 +08:00
|
|
|
if (ls[i].sockaddr->sa_family == AF_INET6) {
|
2009-03-13 22:20:34 +08:00
|
|
|
int ipv6only;
|
|
|
|
|
2012-07-30 20:27:06 +08:00
|
|
|
ipv6only = ls[i].ipv6only;
|
2009-03-13 22:20:34 +08:00
|
|
|
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
|
|
|
|
(const void *) &ipv6only, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
2009-05-05 21:05:42 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2009-03-13 22:20:34 +08:00
|
|
|
"setsockopt(IPV6_V6ONLY) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-06 04:55:48 +08:00
|
|
|
/* TODO: close on exit */
|
|
|
|
|
|
|
|
if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) {
|
|
|
|
if (ngx_nonblocking(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_nonblocking_n " %V failed",
|
|
|
|
&ls[i].addr_text);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2007-05-30 20:28:37 +08:00
|
|
|
if (ngx_close_socket(s) == -1) {
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %V failed",
|
|
|
|
&ls[i].addr_text);
|
2007-05-30 20:28:37 +08:00
|
|
|
}
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 21:05:42 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0,
|
2005-12-27 01:07:48 +08:00
|
|
|
"bind() %V #%d ", &ls[i].addr_text, s);
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
|
|
|
|
err = ngx_socket_errno;
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2014-06-26 07:34:05 +08:00
|
|
|
if (err != NGX_EADDRINUSE || !ngx_test_config) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, err,
|
|
|
|
"bind() to %V failed", &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
2007-05-30 20:28:37 +08:00
|
|
|
if (ngx_close_socket(s) == -1) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_close_socket_n " %V failed",
|
|
|
|
&ls[i].addr_text);
|
2007-05-30 20:28:37 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (err != NGX_EADDRINUSE) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-06-26 07:34:05 +08:00
|
|
|
if (!ngx_test_config) {
|
|
|
|
failed = 1;
|
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
continue;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2009-10-31 03:16:50 +08:00
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
|
|
|
|
2009-11-30 19:26:24 +08:00
|
|
|
if (ls[i].sockaddr->sa_family == AF_UNIX) {
|
|
|
|
mode_t mode;
|
|
|
|
u_char *name;
|
2009-10-31 03:16:50 +08:00
|
|
|
|
2009-11-30 19:26:24 +08:00
|
|
|
name = ls[i].addr_text.data + sizeof("unix:") - 1;
|
|
|
|
mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
|
|
|
|
|
|
|
|
if (chmod((char *) name, mode) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
|
|
"chmod() \"%s\" failed", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_test_config) {
|
2013-03-25 23:49:11 +08:00
|
|
|
if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
2009-11-30 19:26:24 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
|
|
ngx_delete_file_n " %s failed", name);
|
|
|
|
}
|
2009-10-31 03:16:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-30 21:23:48 +08:00
|
|
|
if (listen(s, ls[i].backlog) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
|
|
"listen() to %V, backlog %d failed",
|
|
|
|
&ls[i].addr_text, ls[i].backlog);
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " %V failed",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ls[i].listen = 1;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
ls[i].fd = s;
|
|
|
|
}
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (!failed) {
|
2004-01-06 04:55:48 +08:00
|
|
|
break;
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
/* TODO: delay configurable */
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, log, 0,
|
|
|
|
"try again to bind() after 500ms");
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_msleep(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
2004-12-21 20:30:30 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, 0, "still could not bind()");
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
void
|
2009-05-05 21:12:59 +08:00
|
|
|
ngx_configure_listening_sockets(ngx_cycle_t *cycle)
|
2005-10-19 20:33:58 +08:00
|
|
|
{
|
2013-07-25 16:46:02 +08:00
|
|
|
int value;
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_listening_t *ls;
|
|
|
|
|
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
|
|
struct accept_filter_arg af;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
|
2009-05-05 21:16:29 +08:00
|
|
|
ls[i].log = *ls[i].logp;
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (ls[i].rcvbuf != -1) {
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
(const void *) &ls[i].rcvbuf, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2005-10-24 23:09:41 +08:00
|
|
|
"setsockopt(SO_RCVBUF, %d) %V failed, ignored",
|
|
|
|
ls[i].rcvbuf, &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].sndbuf != -1) {
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF,
|
|
|
|
(const void *) &ls[i].sndbuf, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2005-10-24 23:09:41 +08:00
|
|
|
"setsockopt(SO_SNDBUF, %d) %V failed, ignored",
|
|
|
|
ls[i].sndbuf, &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 16:06:15 +08:00
|
|
|
if (ls[i].keepalive) {
|
2013-07-25 16:46:02 +08:00
|
|
|
value = (ls[i].keepalive == 1) ? 1 : 0;
|
2011-12-05 16:06:15 +08:00
|
|
|
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_KEEPALIVE,
|
2013-07-25 16:46:02 +08:00
|
|
|
(const void *) &value, sizeof(int))
|
2011-12-05 16:06:15 +08:00
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(SO_KEEPALIVE, %d) %V failed, ignored",
|
2013-07-25 16:46:02 +08:00
|
|
|
value, &ls[i].addr_text);
|
2011-12-05 16:06:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 02:09:06 +08:00
|
|
|
#if (NGX_HAVE_KEEPALIVE_TUNABLE)
|
2011-12-05 16:06:15 +08:00
|
|
|
|
|
|
|
if (ls[i].keepidle) {
|
2013-07-25 16:46:03 +08:00
|
|
|
value = ls[i].keepidle;
|
|
|
|
|
|
|
|
#if (NGX_KEEPALIVE_FACTOR)
|
|
|
|
value *= NGX_KEEPALIVE_FACTOR;
|
|
|
|
#endif
|
|
|
|
|
2011-12-05 16:06:15 +08:00
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPIDLE,
|
2013-07-25 16:46:03 +08:00
|
|
|
(const void *) &value, sizeof(int))
|
2011-12-05 16:06:15 +08:00
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_KEEPIDLE, %d) %V failed, ignored",
|
2013-07-25 16:46:03 +08:00
|
|
|
value, &ls[i].addr_text);
|
2011-12-05 16:06:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].keepintvl) {
|
2013-07-25 16:46:03 +08:00
|
|
|
value = ls[i].keepintvl;
|
|
|
|
|
|
|
|
#if (NGX_KEEPALIVE_FACTOR)
|
|
|
|
value *= NGX_KEEPALIVE_FACTOR;
|
|
|
|
#endif
|
|
|
|
|
2011-12-05 16:06:15 +08:00
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPINTVL,
|
2013-07-25 16:46:03 +08:00
|
|
|
(const void *) &value, sizeof(int))
|
2011-12-05 16:06:15 +08:00
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_KEEPINTVL, %d) %V failed, ignored",
|
2013-07-25 16:46:03 +08:00
|
|
|
value, &ls[i].addr_text);
|
2011-12-05 16:06:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].keepcnt) {
|
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPCNT,
|
|
|
|
(const void *) &ls[i].keepcnt, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_KEEPCNT, %d) %V failed, ignored",
|
|
|
|
ls[i].keepcnt, &ls[i].addr_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-07-05 21:49:16 +08:00
|
|
|
#if (NGX_HAVE_SETFIB)
|
|
|
|
if (ls[i].setfib != -1) {
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_SETFIB,
|
|
|
|
(const void *) &ls[i].setfib, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(SO_SETFIB, %d) %V failed, ignored",
|
|
|
|
ls[i].setfib, &ls[i].addr_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 02:07:03 +08:00
|
|
|
#if (NGX_HAVE_TCP_FASTOPEN)
|
|
|
|
if (ls[i].fastopen != -1) {
|
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_FASTOPEN,
|
|
|
|
(const void *) &ls[i].fastopen, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_FASTOPEN, %d) %V failed, ignored",
|
|
|
|
ls[i].fastopen, &ls[i].addr_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-18 03:55:41 +08:00
|
|
|
#if 0
|
|
|
|
if (1) {
|
|
|
|
int tcp_nodelay = 1;
|
|
|
|
|
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(const void *) &tcp_nodelay, sizeof(int))
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_NODELAY) %V failed, ignored",
|
|
|
|
&ls[i].addr_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (ls[i].listen) {
|
2007-05-30 21:24:50 +08:00
|
|
|
|
|
|
|
/* change backlog via listen() */
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (listen(ls[i].fd, ls[i].backlog) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2007-05-30 03:43:18 +08:00
|
|
|
"listen() to %V, backlog %d failed, ignored",
|
|
|
|
&ls[i].addr_text, ls[i].backlog);
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setting deferred mode should be last operation on socket,
|
|
|
|
* because code may prematurely continue cycle on failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if (NGX_HAVE_DEFERRED_ACCEPT)
|
|
|
|
|
|
|
|
#ifdef SO_ACCEPTFILTER
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].delete_deferred) {
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0)
|
|
|
|
== -1)
|
2005-10-19 20:33:58 +08:00
|
|
|
{
|
2014-02-04 06:17:17 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2005-10-19 20:33:58 +08:00
|
|
|
"setsockopt(SO_ACCEPTFILTER, NULL) "
|
|
|
|
"for %V failed, ignored",
|
2006-03-28 20:24:47 +08:00
|
|
|
&ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].accept_filter) {
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
|
|
"could not change the accept filter "
|
|
|
|
"to \"%s\" for %V, ignored",
|
2006-03-28 20:24:47 +08:00
|
|
|
ls[i].accept_filter, &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
ls[i].deferred_accept = 0;
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].add_deferred) {
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_memzero(&af, sizeof(struct accept_filter_arg));
|
|
|
|
(void) ngx_cpystrn((u_char *) af.af_name,
|
2006-03-28 20:24:47 +08:00
|
|
|
(u_char *) ls[i].accept_filter, 16);
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER,
|
2005-10-19 20:33:58 +08:00
|
|
|
&af, sizeof(struct accept_filter_arg))
|
|
|
|
== -1)
|
|
|
|
{
|
2014-02-04 06:17:17 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2005-10-19 20:33:58 +08:00
|
|
|
"setsockopt(SO_ACCEPTFILTER, \"%s\") "
|
2011-08-21 19:37:37 +08:00
|
|
|
"for %V failed, ignored",
|
2006-03-28 20:24:47 +08:00
|
|
|
ls[i].accept_filter, &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
ls[i].deferred_accept = 1;
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_DEFER_ACCEPT
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].add_deferred || ls[i].delete_deferred) {
|
2005-10-19 20:33:58 +08:00
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].add_deferred) {
|
2014-01-28 19:40:46 +08:00
|
|
|
/*
|
|
|
|
* There is no way to find out how long a connection was
|
|
|
|
* in queue (and a connection may bypass deferred queue at all
|
|
|
|
* if syncookies were used), hence we use 1 second timeout
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
value = 1;
|
2005-10-19 20:33:58 +08:00
|
|
|
|
|
|
|
} else {
|
2013-07-25 16:46:02 +08:00
|
|
|
value = 0;
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT,
|
2013-07-25 16:46:02 +08:00
|
|
|
&value, sizeof(int))
|
2005-10-19 20:33:58 +08:00
|
|
|
== -1)
|
|
|
|
{
|
2014-02-04 06:17:17 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
2005-10-19 20:33:58 +08:00
|
|
|
"setsockopt(TCP_DEFER_ACCEPT, %d) for %V failed, "
|
|
|
|
"ignored",
|
2013-07-25 16:46:02 +08:00
|
|
|
value, &ls[i].addr_text);
|
2005-10-19 20:33:58 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-28 20:24:47 +08:00
|
|
|
if (ls[i].add_deferred) {
|
|
|
|
ls[i].deferred_accept = 1;
|
2005-10-19 20:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* NGX_HAVE_DEFERRED_ACCEPT */
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
void
|
|
|
|
ngx_close_listening_sockets(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2005-09-23 19:02:22 +08:00
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_listening_t *ls;
|
|
|
|
ngx_connection_t *c;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-02 00:20:53 +08:00
|
|
|
ngx_accept_mutex_held = 0;
|
2006-02-08 23:33:12 +08:00
|
|
|
ngx_use_accept_mutex = 0;
|
2004-04-02 00:20:53 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
c = ls[i].connection;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
if (c) {
|
|
|
|
if (c->read->active) {
|
|
|
|
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
|
|
|
ngx_del_conn(c, NGX_CLOSE_EVENT);
|
2004-04-15 23:34:36 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
} else if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
|
2008-01-08 19:31:50 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
/*
|
|
|
|
* it seems that Linux-2.6.x OpenVZ sends events
|
|
|
|
* for closed shared listening sockets unless
|
2012-04-03 15:37:31 +08:00
|
|
|
* the events was explicitly deleted
|
2009-04-20 14:08:47 +08:00
|
|
|
*/
|
2008-01-08 19:31:50 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
ngx_del_event(c->read, NGX_READ_EVENT, 0);
|
2008-01-08 19:31:50 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
} else {
|
|
|
|
ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
|
|
}
|
2004-04-15 23:34:36 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
ngx_free_connection(c);
|
2005-09-23 19:02:22 +08:00
|
|
|
|
2009-04-20 14:08:47 +08:00
|
|
|
c->fd = (ngx_socket_t) -1;
|
|
|
|
}
|
2005-09-23 19:02:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
|
|
"close listening %V #%d ", &ls[i].addr_text, ls[i].fd);
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
if (ngx_close_socket(ls[i].fd) == -1) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_close_socket_n " %V failed", &ls[i].addr_text);
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
2009-06-08 02:22:58 +08:00
|
|
|
|
2009-10-26 19:43:32 +08:00
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
|
|
|
|
|
|
|
if (ls[i].sockaddr->sa_family == AF_UNIX
|
2009-11-23 23:46:21 +08:00
|
|
|
&& ngx_process <= NGX_PROCESS_MASTER
|
2009-10-31 03:18:21 +08:00
|
|
|
&& ngx_new_binary == 0)
|
2009-10-26 19:43:32 +08:00
|
|
|
{
|
|
|
|
u_char *name = ls[i].addr_text.data + sizeof("unix:") - 1;
|
|
|
|
|
2013-03-25 23:49:11 +08:00
|
|
|
if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
2009-10-26 19:43:32 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
|
|
|
|
ngx_delete_file_n " %s failed", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-06-08 02:22:58 +08:00
|
|
|
ls[i].fd = (ngx_socket_t) -1;
|
2005-09-23 19:02:22 +08:00
|
|
|
}
|
2012-11-20 21:37:55 +08:00
|
|
|
|
|
|
|
cycle->listening.nelts = 0;
|
2005-09-23 19:02:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_connection_t *
|
|
|
|
ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
|
|
|
|
{
|
2005-10-12 21:50:36 +08:00
|
|
|
ngx_uint_t instance;
|
|
|
|
ngx_event_t *rev, *wev;
|
|
|
|
ngx_connection_t *c;
|
2005-09-23 19:02:22 +08:00
|
|
|
|
|
|
|
/* disable warning: Win32 SOCKET is u_int while UNIX socket is int */
|
|
|
|
|
|
|
|
if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
|
|
"the new socket has number %d, "
|
|
|
|
"but only %ui files are available",
|
|
|
|
s, ngx_cycle->files_n);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ngx_mutex_lock */
|
|
|
|
|
|
|
|
c = ngx_cycle->free_connections;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2011-04-04 20:26:53 +08:00
|
|
|
if (c == NULL) {
|
|
|
|
ngx_drain_connections();
|
|
|
|
c = ngx_cycle->free_connections;
|
|
|
|
}
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
if (c == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
2008-09-17 16:03:58 +08:00
|
|
|
"%ui worker_connections are not enough",
|
2005-09-23 19:02:22 +08:00
|
|
|
ngx_cycle->connection_n);
|
|
|
|
|
|
|
|
/* ngx_mutex_unlock */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_cycle->free_connections = c->data;
|
|
|
|
ngx_cycle->free_connection_n--;
|
|
|
|
|
|
|
|
/* ngx_mutex_unlock */
|
|
|
|
|
|
|
|
if (ngx_cycle->files) {
|
|
|
|
ngx_cycle->files[s] = c;
|
|
|
|
}
|
|
|
|
|
2005-10-12 21:50:36 +08:00
|
|
|
rev = c->read;
|
|
|
|
wev = c->write;
|
|
|
|
|
|
|
|
ngx_memzero(c, sizeof(ngx_connection_t));
|
|
|
|
|
|
|
|
c->read = rev;
|
|
|
|
c->write = wev;
|
|
|
|
c->fd = s;
|
|
|
|
c->log = log;
|
|
|
|
|
|
|
|
instance = rev->instance;
|
|
|
|
|
|
|
|
ngx_memzero(rev, sizeof(ngx_event_t));
|
|
|
|
ngx_memzero(wev, sizeof(ngx_event_t));
|
|
|
|
|
|
|
|
rev->instance = !instance;
|
|
|
|
wev->instance = !instance;
|
|
|
|
|
|
|
|
rev->index = NGX_INVALID_INDEX;
|
|
|
|
wev->index = NGX_INVALID_INDEX;
|
|
|
|
|
|
|
|
rev->data = c;
|
|
|
|
wev->data = c;
|
|
|
|
|
|
|
|
wev->write = 1;
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_free_connection(ngx_connection_t *c)
|
|
|
|
{
|
|
|
|
/* ngx_mutex_lock */
|
|
|
|
|
|
|
|
c->data = ngx_cycle->free_connections;
|
|
|
|
ngx_cycle->free_connections = c;
|
|
|
|
ngx_cycle->free_connection_n++;
|
|
|
|
|
|
|
|
/* ngx_mutex_unlock */
|
|
|
|
|
|
|
|
if (ngx_cycle->files) {
|
|
|
|
ngx_cycle->files[c->fd] = NULL;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-09 15:46:43 +08:00
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
void
|
|
|
|
ngx_close_connection(ngx_connection_t *c)
|
2004-09-07 23:29:22 +08:00
|
|
|
{
|
2008-03-06 05:21:26 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_uint_t log_error, level;
|
2004-09-07 23:29:22 +08:00
|
|
|
ngx_socket_t fd;
|
|
|
|
|
2013-09-05 00:48:28 +08:00
|
|
|
if (c->fd == (ngx_socket_t) -1) {
|
2004-09-07 23:29:22 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->read->timer_set) {
|
|
|
|
ngx_del_timer(c->read);
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
if (c->write->timer_set) {
|
|
|
|
ngx_del_timer(c->write);
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
if (ngx_del_conn) {
|
|
|
|
ngx_del_conn(c, NGX_CLOSE_EVENT);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (c->read->active || c->read->disabled) {
|
|
|
|
ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->write->active || c->write->disabled) {
|
|
|
|
ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (NGX_THREADS)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we have to clean the connection information before the closing
|
|
|
|
* because another thread may reopen the same file descriptor
|
|
|
|
* before we clean the connection
|
|
|
|
*/
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_mutex_lock(ngx_posted_events_mutex);
|
2004-09-07 23:29:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (c->read->prev) {
|
|
|
|
ngx_delete_posted_event(c->read);
|
|
|
|
}
|
2004-09-07 23:29:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (c->write->prev) {
|
|
|
|
ngx_delete_posted_event(c->write);
|
|
|
|
}
|
2004-09-07 23:29:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
c->read->closed = 1;
|
|
|
|
c->write->closed = 1;
|
2004-09-07 23:29:22 +08:00
|
|
|
|
2013-03-08 02:07:16 +08:00
|
|
|
ngx_unlock(&c->lock);
|
|
|
|
c->read->locked = 0;
|
|
|
|
c->write->locked = 0;
|
2004-09-07 23:29:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
if (c->read->prev) {
|
|
|
|
ngx_delete_posted_event(c->read);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->write->prev) {
|
|
|
|
ngx_delete_posted_event(c->write);
|
|
|
|
}
|
|
|
|
|
|
|
|
c->read->closed = 1;
|
|
|
|
c->write->closed = 1;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-04-04 20:26:53 +08:00
|
|
|
ngx_reusable_connection(c, 0);
|
|
|
|
|
2008-03-06 05:21:26 +08:00
|
|
|
log_error = c->log_error;
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
ngx_free_connection(c);
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
fd = c->fd;
|
|
|
|
c->fd = (ngx_socket_t) -1;
|
|
|
|
|
|
|
|
if (ngx_close_socket(fd) == -1) {
|
|
|
|
|
2008-03-06 05:21:26 +08:00
|
|
|
err = ngx_socket_errno;
|
|
|
|
|
|
|
|
if (err == NGX_ECONNRESET || err == NGX_ENOTCONN) {
|
|
|
|
|
|
|
|
switch (log_error) {
|
|
|
|
|
|
|
|
case NGX_ERROR_INFO:
|
|
|
|
level = NGX_LOG_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGX_ERROR_ERR:
|
|
|
|
level = NGX_LOG_ERR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
}
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
/* we use ngx_cycle->log because c->log was in c->pool */
|
|
|
|
|
2008-03-06 05:21:26 +08:00
|
|
|
ngx_log_error(level, ngx_cycle->log, err,
|
2007-08-29 23:33:59 +08:00
|
|
|
ngx_close_socket_n " %d failed", fd);
|
2004-09-07 23:29:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-04 20:26:53 +08:00
|
|
|
void
|
|
|
|
ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable)
|
|
|
|
{
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
|
|
|
|
"reusable connection: %ui", reusable);
|
|
|
|
|
|
|
|
if (c->reusable) {
|
|
|
|
ngx_queue_remove(&c->queue);
|
2013-03-16 04:00:49 +08:00
|
|
|
|
|
|
|
#if (NGX_STAT_STUB)
|
|
|
|
(void) ngx_atomic_fetch_add(ngx_stat_waiting, -1);
|
|
|
|
#endif
|
2011-04-04 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c->reusable = reusable;
|
|
|
|
|
|
|
|
if (reusable) {
|
|
|
|
/* need cast as ngx_cycle is volatile */
|
|
|
|
|
|
|
|
ngx_queue_insert_head(
|
|
|
|
(ngx_queue_t *) &ngx_cycle->reusable_connections_queue, &c->queue);
|
2013-03-16 04:00:49 +08:00
|
|
|
|
|
|
|
#if (NGX_STAT_STUB)
|
|
|
|
(void) ngx_atomic_fetch_add(ngx_stat_waiting, 1);
|
|
|
|
#endif
|
2011-04-04 20:26:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_drain_connections(void)
|
|
|
|
{
|
|
|
|
ngx_int_t i;
|
|
|
|
ngx_queue_t *q;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
if (ngx_queue_empty(&ngx_cycle->reusable_connections_queue)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = ngx_queue_last(&ngx_cycle->reusable_connections_queue);
|
|
|
|
c = ngx_queue_data(q, ngx_connection_t, queue);
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, c->log, 0,
|
|
|
|
"reusing connection");
|
|
|
|
|
|
|
|
c->close = 1;
|
|
|
|
c->read->handler(c->read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-18 20:12:06 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
|
|
|
|
ngx_uint_t port)
|
|
|
|
{
|
|
|
|
socklen_t len;
|
|
|
|
ngx_uint_t addr;
|
|
|
|
u_char sa[NGX_SOCKADDRLEN];
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
ngx_uint_t i;
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
#endif
|
|
|
|
|
2013-12-09 14:14:51 +08:00
|
|
|
if (c->local_socklen == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-05-18 20:12:06 +08:00
|
|
|
switch (c->local_sockaddr->sa_family) {
|
|
|
|
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
case AF_INET6:
|
|
|
|
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
|
|
|
|
|
|
|
|
for (addr = 0, i = 0; addr == 0 && i < 16; i++) {
|
|
|
|
addr |= sin6->sin6_addr.s6_addr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2013-12-09 14:16:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (NGX_HAVE_UNIX_DOMAIN)
|
|
|
|
case AF_UNIX:
|
|
|
|
addr = 1;
|
|
|
|
break;
|
2009-05-18 20:12:06 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
default: /* AF_INET */
|
|
|
|
sin = (struct sockaddr_in *) c->local_sockaddr;
|
|
|
|
addr = sin->sin_addr.s_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr == 0) {
|
|
|
|
|
|
|
|
len = NGX_SOCKADDRLEN;
|
|
|
|
|
|
|
|
if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) {
|
|
|
|
ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->local_sockaddr = ngx_palloc(c->pool, len);
|
|
|
|
if (c->local_sockaddr == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(c->local_sockaddr, &sa, len);
|
2013-12-09 14:14:51 +08:00
|
|
|
|
|
|
|
c->local_socklen = len;
|
2009-05-18 20:12:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s == NULL) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-09 14:14:51 +08:00
|
|
|
s->len = ngx_sock_ntop(c->local_sockaddr, c->local_socklen,
|
|
|
|
s->data, s->len, port);
|
2009-05-18 20:12:06 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text)
|
2004-02-09 15:46:43 +08:00
|
|
|
{
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_uint_t level;
|
2004-02-09 15:46:43 +08:00
|
|
|
|
2009-04-29 21:07:16 +08:00
|
|
|
/* Winsock may return NGX_ECONNABORTED instead of NGX_ECONNRESET */
|
2009-04-29 19:34:58 +08:00
|
|
|
|
2009-04-29 21:07:16 +08:00
|
|
|
if ((err == NGX_ECONNRESET
|
|
|
|
#if (NGX_WIN32)
|
|
|
|
|| err == NGX_ECONNABORTED
|
2009-04-29 19:34:58 +08:00
|
|
|
#endif
|
2009-04-29 21:07:16 +08:00
|
|
|
) && c->log_error == NGX_ERROR_IGNORE_ECONNRESET)
|
2009-04-29 19:34:58 +08:00
|
|
|
{
|
2004-02-09 15:46:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-12 15:16:15 +08:00
|
|
|
#if (NGX_SOLARIS)
|
|
|
|
if (err == NGX_EINVAL && c->log_error == NGX_ERROR_IGNORE_EINVAL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-30 18:55:07 +08:00
|
|
|
if (err == 0
|
|
|
|
|| err == NGX_ECONNRESET
|
2009-04-29 19:34:58 +08:00
|
|
|
#if (NGX_WIN32)
|
|
|
|
|| err == NGX_ECONNABORTED
|
|
|
|
#else
|
2004-03-04 15:04:55 +08:00
|
|
|
|| err == NGX_EPIPE
|
|
|
|
#endif
|
2004-03-15 04:46:25 +08:00
|
|
|
|| err == NGX_ENOTCONN
|
2005-12-07 22:51:31 +08:00
|
|
|
|| err == NGX_ETIMEDOUT
|
2004-03-15 04:46:25 +08:00
|
|
|
|| err == NGX_ECONNREFUSED
|
2008-01-25 22:57:35 +08:00
|
|
|
|| err == NGX_ENETDOWN
|
|
|
|
|| err == NGX_ENETUNREACH
|
|
|
|
|| err == NGX_EHOSTDOWN
|
2004-03-15 04:46:25 +08:00
|
|
|
|| err == NGX_EHOSTUNREACH)
|
2004-03-04 15:04:55 +08:00
|
|
|
{
|
2004-02-11 00:23:38 +08:00
|
|
|
switch (c->log_error) {
|
2004-02-09 15:46:43 +08:00
|
|
|
|
2009-03-12 15:16:15 +08:00
|
|
|
case NGX_ERROR_IGNORE_EINVAL:
|
2004-02-12 01:08:49 +08:00
|
|
|
case NGX_ERROR_IGNORE_ECONNRESET:
|
2004-02-09 15:46:43 +08:00
|
|
|
case NGX_ERROR_INFO:
|
|
|
|
level = NGX_LOG_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-09-23 23:28:33 +08:00
|
|
|
level = NGX_LOG_ERR;
|
2004-02-09 15:46:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2008-03-06 05:21:26 +08:00
|
|
|
level = NGX_LOG_ALERT;
|
2004-02-09 15:46:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(level, c->log, err, text);
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|