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
|
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
|
|
|
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
ngx_listening_t *ngx_listening_inet_stream_socket(ngx_conf_t *cf,
|
|
|
|
in_addr_t addr,
|
|
|
|
in_port_t port)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
ngx_listening_t *ls;
|
|
|
|
struct sockaddr_in *addr_in;
|
|
|
|
|
|
|
|
if (!(ls = ngx_array_push(&cf->cycle->listening))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(ls, sizeof(ngx_listening_t));
|
|
|
|
|
|
|
|
if (!(addr_in = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_in->sin_family = AF_INET;
|
|
|
|
addr_in->sin_addr.s_addr = addr;
|
|
|
|
addr_in->sin_port = htons(port);
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
ls->addr_text.data = ngx_palloc(cf->pool,
|
|
|
|
INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
|
|
|
|
if (ls->addr_text.data == NULL) {
|
2004-09-07 23:29:22 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data, INET_ADDRSTRLEN);
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
ls->addr_text.len = ngx_sprintf(ls->addr_text.data + len, ":%d", port)
|
|
|
|
- ls->addr_text.data;
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
|
|
|
|
ls->fd = (ngx_socket_t) -1;
|
|
|
|
ls->family = AF_INET;
|
|
|
|
ls->type = SOCK_STREAM;
|
|
|
|
ls->protocol = IPPROTO_IP;
|
|
|
|
ls->sockaddr = (struct sockaddr *) addr_in;
|
|
|
|
ls->socklen = sizeof(struct sockaddr_in);
|
|
|
|
ls->addr = offsetof(struct sockaddr_in, sin_addr);
|
|
|
|
ls->addr_text_max_len = INET_ADDRSTRLEN;
|
|
|
|
|
|
|
|
return ls;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2004-11-11 22:07:14 +08:00
|
|
|
size_t len;
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t i;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_listening_t *ls;
|
|
|
|
struct sockaddr_in *addr_in;
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
|
|
|
|
/* AF_INET only */
|
|
|
|
|
|
|
|
ls[i].sockaddr = ngx_palloc(cycle->pool, sizeof(struct sockaddr_in));
|
|
|
|
if (ls[i].sockaddr == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ls[i].socklen = sizeof(struct sockaddr_in);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
addr_in = (struct sockaddr_in *) ls[i].sockaddr;
|
|
|
|
|
|
|
|
if (addr_in->sin_family != AF_INET) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
|
|
|
|
"the inherited socket #%d has "
|
|
|
|
"unsupported family", ls[i].fd);
|
|
|
|
ls[i].ignore = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ls[i].addr_text_max_len = INET_ADDRSTRLEN;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
ls[i].addr_text.data = ngx_palloc(cycle->pool, INET_ADDRSTRLEN - 1
|
|
|
|
+ sizeof(":65535") - 1);
|
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
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ls[i].family = addr_in->sin_family;
|
2004-11-11 22:07:14 +08:00
|
|
|
len = ngx_sock_ntop(ls[i].family, ls[i].sockaddr,
|
|
|
|
ls[i].addr_text.data, INET_ADDRSTRLEN);
|
|
|
|
if (len == 0) {
|
2004-01-07 00:49:34 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
ls[i].addr_text.len = ngx_sprintf(ls[i].addr_text.data + len, ":%d",
|
|
|
|
ntohs(addr_in->sin_port))
|
|
|
|
- ls[i].addr_text.data;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle)
|
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t tries, failed, reuseaddr, i;
|
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;
|
|
|
|
|
|
|
|
/* TODO: tries configurable */
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
for (tries = /* STUB */ 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].fd != -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ls[i].inherited) {
|
|
|
|
|
|
|
|
/* TODO: close on exit */
|
|
|
|
/* TODO: nonblocking */
|
|
|
|
/* TODO: deferred accept */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
if (s == -1) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if (NGX_WIN32)
|
2004-01-06 04:55:48 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
ngx_socket_n " created socket %d", s);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const void *) &reuseaddr, sizeof(int)) == -1) {
|
|
|
|
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);
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (ls[i].nonblocking) {
|
|
|
|
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
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
|
|
|
|
err = ngx_socket_errno;
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, err,
|
2004-11-11 22:07:14 +08:00
|
|
|
"bind() to %V failed", &ls[i].addr_text);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
if (err != NGX_EADDRINUSE)
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1)
|
|
|
|
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);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
failed = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(s, ls[i].backlog) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
2004-11-11 22:07:14 +08:00
|
|
|
"listen() to %V failed", &ls[i].addr_text);
|
2004-01-06 04:55:48 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: deferred accept */
|
|
|
|
|
|
|
|
ls[i].fd = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!failed)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* TODO: delay configurable */
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_NOTICE, log, 0,
|
|
|
|
"try again to bind() after 500ms");
|
|
|
|
ngx_msleep(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, 0, "still can not bind()");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_close_listening_sockets(ngx_cycle_t *cycle)
|
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t i;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_socket_t fd;
|
|
|
|
ngx_listening_t *ls;
|
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-02 00:20:53 +08:00
|
|
|
ngx_accept_mutex_held = 0;
|
|
|
|
ngx_accept_mutex = NULL;
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
fd = ls[i].fd;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if (NGX_WIN32)
|
2004-01-06 04:55:48 +08:00
|
|
|
/*
|
|
|
|
* Winsock assignes a socket number divisible by 4
|
|
|
|
* so to find a connection we divide a socket number by 4.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fd /= 4;
|
|
|
|
#endif
|
|
|
|
|
2004-06-07 03:49:18 +08:00
|
|
|
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
2004-04-15 23:34:36 +08:00
|
|
|
if (cycle->connections[fd].read->active) {
|
|
|
|
ngx_del_conn(&cycle->connections[fd], NGX_CLOSE_EVENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (cycle->read_events[fd].active) {
|
|
|
|
ngx_del_event(&cycle->read_events[fd],
|
|
|
|
NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
|
|
}
|
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-11-21 03:52:20 +08:00
|
|
|
if (ngx_close_socket(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
|
|
|
}
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
cycle->connections[fd].fd = (ngx_socket_t) -1;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-09 15:46:43 +08:00
|
|
|
|
|
|
|
|
2004-09-07 23:29:22 +08:00
|
|
|
void ngx_close_connection(ngx_connection_t *c)
|
|
|
|
{
|
|
|
|
ngx_socket_t fd;
|
|
|
|
|
|
|
|
if (c->pool == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->read->timer_set) {
|
|
|
|
ngx_del_timer(c->read);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->write->timer_set) {
|
|
|
|
ngx_del_timer(c->write);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (c->single_connection) {
|
|
|
|
ngx_unlock(&c->lock);
|
|
|
|
c->read->locked = 0;
|
|
|
|
c->write->locked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
fd = c->fd;
|
|
|
|
c->fd = (ngx_socket_t) -1;
|
|
|
|
c->data = NULL;
|
|
|
|
|
|
|
|
ngx_destroy_pool(c->pool);
|
|
|
|
|
|
|
|
if (ngx_close_socket(fd) == -1) {
|
|
|
|
|
|
|
|
/* we use ngx_cycle->log because c->log was in c->pool */
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text)
|
|
|
|
{
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_uint_t level;
|
2004-02-09 15:46:43 +08:00
|
|
|
|
|
|
|
if (err == NGX_ECONNRESET
|
2004-02-11 00:23:38 +08:00
|
|
|
&& c->log_error == NGX_ERROR_IGNORE_ECONNRESET)
|
2004-02-09 15:46:43 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-04 15:04:55 +08:00
|
|
|
if (err == NGX_ECONNRESET
|
2004-11-11 22:07:14 +08:00
|
|
|
#if !(NGX_WIN32)
|
2004-03-04 15:04:55 +08:00
|
|
|
|| err == NGX_EPIPE
|
|
|
|
#endif
|
2004-03-15 04:46:25 +08:00
|
|
|
|| err == NGX_ENOTCONN
|
|
|
|
|| err == NGX_ECONNREFUSED
|
|
|
|
|| err == NGX_EHOSTUNREACH)
|
2004-03-04 15:04:55 +08:00
|
|
|
{
|
2004-02-09 15:46:43 +08:00
|
|
|
|
2004-02-11 00:23:38 +08:00
|
|
|
switch (c->log_error) {
|
2004-02-09 15:46:43 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
case NGX_ERROR_ERR:
|
|
|
|
level = NGX_LOG_ERR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
level = NGX_LOG_CRIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(level, c->log, err, text);
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|