Mercurial > hg > nginx
annotate src/core/ngx_connection.c @ 4104:84f34973684c
Version bump.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 14 Sep 2011 15:20:01 +0000 |
parents | b044d6553d52 |
children | c4513d4dd024 4919fb357a5d |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
5 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
236
86e473b5641e
nginx-0.0.1-2004-01-22-09:47:28 import
Igor Sysoev <igor@sysoev.ru>
parents:
219
diff
changeset
|
12 ngx_os_io_t ngx_io; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
13 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
14 |
3880
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
15 static void ngx_drain_connections(void); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
16 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
17 |
2806 | 18 ngx_listening_t * |
19 ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen) | |
20 { | |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
21 size_t len; |
2806 | 22 ngx_listening_t *ls; |
23 struct sockaddr *sa; | |
24 u_char text[NGX_SOCKADDR_STRLEN]; | |
25 | |
26 ls = ngx_array_push(&cf->cycle->listening); | |
27 if (ls == NULL) { | |
28 return NULL; | |
29 } | |
30 | |
31 ngx_memzero(ls, sizeof(ngx_listening_t)); | |
32 | |
33 sa = ngx_palloc(cf->pool, socklen); | |
34 if (sa == NULL) { | |
35 return NULL; | |
36 } | |
37 | |
38 ngx_memcpy(sa, sockaddr, socklen); | |
39 | |
40 ls->sockaddr = sa; | |
41 ls->socklen = socklen; | |
42 | |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
43 len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1); |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
44 ls->addr_text.len = len; |
2806 | 45 |
46 switch (ls->sockaddr->sa_family) { | |
47 #if (NGX_HAVE_INET6) | |
48 case AF_INET6: | |
49 ls->addr_text_max_len = NGX_INET6_ADDRSTRLEN; | |
50 break; | |
51 #endif | |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
52 #if (NGX_HAVE_UNIX_DOMAIN) |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
53 case AF_UNIX: |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
54 ls->addr_text_max_len = NGX_UNIX_ADDRSTRLEN; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
55 len++; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
56 break; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
57 #endif |
2806 | 58 case AF_INET: |
59 ls->addr_text_max_len = NGX_INET_ADDRSTRLEN; | |
60 break; | |
61 default: | |
62 ls->addr_text_max_len = NGX_SOCKADDR_STRLEN; | |
63 break; | |
64 } | |
65 | |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
66 ls->addr_text.data = ngx_pnalloc(cf->pool, len); |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
67 if (ls->addr_text.data == NULL) { |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
68 return NULL; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
69 } |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
70 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
71 ngx_memcpy(ls->addr_text.data, text, len); |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
72 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
73 ls->fd = (ngx_socket_t) -1; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
74 ls->type = SOCK_STREAM; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
75 |
2806 | 76 ls->backlog = NGX_LISTEN_BACKLOG; |
77 ls->rcvbuf = -1; | |
78 ls->sndbuf = -1; | |
79 | |
3676 | 80 #if (NGX_HAVE_SETFIB) |
81 ls->setfib = -1; | |
82 #endif | |
83 | |
2806 | 84 return ls; |
85 } | |
86 | |
87 | |
521 | 88 ngx_int_t |
89 ngx_set_inherited_sockets(ngx_cycle_t *cycle) | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 { |
523 | 91 size_t len; |
92 ngx_uint_t i; | |
93 ngx_listening_t *ls; | |
563 | 94 socklen_t olen; |
523 | 95 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
541 | 96 ngx_err_t err; |
523 | 97 struct accept_filter_arg af; |
98 #endif | |
99 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
100 int timeout; | |
101 #endif | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
102 |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
103 ls = cycle->listening.elts; |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
104 for (i = 0; i < cycle->listening.nelts; i++) { |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
105 |
3537
d2848db4d98e
fix IPv6 and Unix domain sockets inheritance while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3371
diff
changeset
|
106 ls[i].sockaddr = ngx_palloc(cycle->pool, NGX_SOCKADDRLEN); |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
107 if (ls[i].sockaddr == NULL) { |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
108 return NGX_ERROR; |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
109 } |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
110 |
3537
d2848db4d98e
fix IPv6 and Unix domain sockets inheritance while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3371
diff
changeset
|
111 ls[i].socklen = NGX_SOCKADDRLEN; |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
112 if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) { |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
113 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
114 "getsockname() of the inherited " |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
115 "socket #%d failed", ls[i].fd); |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
116 ls[i].ignore = 1; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
117 continue; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
118 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
119 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
120 switch (ls[i].sockaddr->sa_family) { |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
121 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
122 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
123 case AF_INET6: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
124 ls[i].addr_text_max_len = NGX_INET6_ADDRSTRLEN; |
3292
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
125 len = NGX_INET6_ADDRSTRLEN + sizeof(":65535") - 1; |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
126 break; |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
127 #endif |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
128 |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
129 #if (NGX_HAVE_UNIX_DOMAIN) |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
130 case AF_UNIX: |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
131 ls[i].addr_text_max_len = NGX_UNIX_ADDRSTRLEN; |
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
132 len = NGX_UNIX_ADDRSTRLEN; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
133 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
134 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
135 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
136 case AF_INET: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
137 ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN; |
3292
6ed04ab2d062
allow to inherit unix domain sockets while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3263
diff
changeset
|
138 len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
139 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
140 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
141 default: |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
142 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
143 "the inherited socket #%d has " |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
144 "an unsupported protocol family", ls[i].fd); |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
145 ls[i].ignore = 1; |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
146 continue; |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
147 } |
461 | 148 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
149 ls[i].addr_text.data = ngx_pnalloc(cycle->pool, len); |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
150 if (ls[i].addr_text.data == NULL) { |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
151 return NGX_ERROR; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
152 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
153 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
154 len = ngx_sock_ntop(ls[i].sockaddr, ls[i].addr_text.data, len, 1); |
461 | 155 if (len == 0) { |
219
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
156 return NGX_ERROR; |
f57597ec5249
nginx-0.0.1-2004-01-06-19:49:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
218
diff
changeset
|
157 } |
461 | 158 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2254
diff
changeset
|
159 ls[i].addr_text.len = len; |
523 | 160 |
1377
0d57c150115b
set default listen() backlog to 511 on all platforms except FreeBSD
Igor Sysoev <igor@sysoev.ru>
parents:
1225
diff
changeset
|
161 ls[i].backlog = NGX_LISTEN_BACKLOG; |
537 | 162 |
563 | 163 olen = sizeof(int); |
164 | |
165 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF, (void *) &ls[i].rcvbuf, | |
166 &olen) | |
167 == -1) | |
168 { | |
169 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
170 "getsockopt(SO_RCVBUF) %V failed, ignored", | |
171 &ls[i].addr_text); | |
172 | |
173 ls[i].rcvbuf = -1; | |
174 } | |
175 | |
176 olen = sizeof(int); | |
177 | |
178 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF, (void *) &ls[i].sndbuf, | |
179 &olen) | |
180 == -1) | |
181 { | |
182 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
183 "getsockopt(SO_SNDBUF) %V failed, ignored", | |
184 &ls[i].addr_text); | |
185 | |
186 ls[i].sndbuf = -1; | |
187 } | |
188 | |
3676 | 189 #if 0 |
190 /* SO_SETFIB is currently a set only option */ | |
191 | |
192 #if (NGX_HAVE_SETFIB) | |
193 | |
194 if (getsockopt(ls[i].setfib, SOL_SOCKET, SO_SETFIB, | |
195 (void *) &ls[i].setfib, &olen) | |
196 == -1) | |
197 { | |
198 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
199 "getsockopt(SO_SETFIB) %V failed, ignored", | |
200 &ls[i].addr_text); | |
201 | |
202 ls[i].setfib = -1; | |
203 } | |
204 | |
205 #endif | |
206 #endif | |
207 | |
523 | 208 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
209 | |
210 ngx_memzero(&af, sizeof(struct accept_filter_arg)); | |
563 | 211 olen = sizeof(struct accept_filter_arg); |
523 | 212 |
563 | 213 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, &af, &olen) |
523 | 214 == -1) |
215 { | |
541 | 216 err = ngx_errno; |
217 | |
218 if (err == NGX_EINVAL) { | |
219 continue; | |
220 } | |
221 | |
222 ngx_log_error(NGX_LOG_NOTICE, cycle->log, err, | |
523 | 223 "getsockopt(SO_ACCEPTFILTER) for %V failed, ignored", |
224 &ls[i].addr_text); | |
225 continue; | |
226 } | |
227 | |
563 | 228 if (olen < sizeof(struct accept_filter_arg) || af.af_name[0] == '\0') { |
523 | 229 continue; |
230 } | |
231 | |
232 ls[i].accept_filter = ngx_palloc(cycle->pool, 16); | |
233 if (ls[i].accept_filter == NULL) { | |
234 return NGX_ERROR; | |
235 } | |
236 | |
237 (void) ngx_cpystrn((u_char *) ls[i].accept_filter, | |
238 (u_char *) af.af_name, 16); | |
239 #endif | |
240 | |
241 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
242 | |
243 timeout = 0; | |
563 | 244 olen = sizeof(int); |
523 | 245 |
563 | 246 if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, &olen) |
523 | 247 == -1) |
248 { | |
249 ngx_log_error(NGX_LOG_NOTICE, cycle->log, ngx_errno, | |
250 "getsockopt(TCP_DEFER_ACCEPT) for %V failed, ignored", | |
251 &ls[i].addr_text); | |
252 continue; | |
253 } | |
254 | |
565 | 255 if (olen < sizeof(int) || timeout == 0) { |
523 | 256 continue; |
257 } | |
258 | |
259 ls[i].deferred_accept = 1; | |
260 #endif | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
261 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
262 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
263 return NGX_OK; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
264 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
265 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
266 |
521 | 267 ngx_int_t |
268 ngx_open_listening_sockets(ngx_cycle_t *cycle) | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
269 { |
563 | 270 int reuseaddr; |
271 ngx_uint_t i, tries, failed; | |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
272 ngx_err_t err; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
273 ngx_log_t *log; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
274 ngx_socket_t s; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
275 ngx_listening_t *ls; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
276 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
277 reuseaddr = 1; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
278 #if (NGX_SUPPRESS_WARN) |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
279 failed = 0; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
280 #endif |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
281 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
282 log = cycle->log; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
283 |
563 | 284 /* TODO: configurable try number */ |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
285 |
2024 | 286 for (tries = 5; tries; tries--) { |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
287 failed = 0; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
288 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
289 /* for each listening socket */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
290 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
291 ls = cycle->listening.elts; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
292 for (i = 0; i < cycle->listening.nelts; i++) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
293 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
294 if (ls[i].ignore) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
295 continue; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
296 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
297 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
298 if (ls[i].fd != -1) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
299 continue; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
300 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
301 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
302 if (ls[i].inherited) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
303 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
304 /* TODO: close on exit */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
305 /* TODO: nonblocking */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
306 /* TODO: deferred accept */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
307 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
308 continue; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
309 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
310 |
2198
5975975eedc0
ngx_sock_ntop() takes family from sockaddr, remove duplicate field
Igor Sysoev <igor@sysoev.ru>
parents:
2197
diff
changeset
|
311 s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0); |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
312 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
313 if (s == -1) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
314 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
461 | 315 ngx_socket_n " %V failed", &ls[i].addr_text); |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
316 return NGX_ERROR; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
317 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
318 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
319 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
563 | 320 (const void *) &reuseaddr, sizeof(int)) |
321 == -1) | |
322 { | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
323 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
461 | 324 "setsockopt(SO_REUSEADDR) %V failed", |
325 &ls[i].addr_text); | |
563 | 326 |
1223 | 327 if (ngx_close_socket(s) == -1) { |
563 | 328 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
329 ngx_close_socket_n " %V failed", | |
330 &ls[i].addr_text); | |
1223 | 331 } |
563 | 332 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
333 return NGX_ERROR; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
334 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
335 |
2562 | 336 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) |
337 | |
338 if (ls[i].sockaddr->sa_family == AF_INET6 && ls[i].ipv6only) { | |
339 int ipv6only; | |
340 | |
341 ipv6only = (ls[i].ipv6only == 1); | |
342 | |
343 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, | |
344 (const void *) &ipv6only, sizeof(int)) | |
345 == -1) | |
346 { | |
2801 | 347 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
2562 | 348 "setsockopt(IPV6_V6ONLY) %V failed, ignored", |
349 &ls[i].addr_text); | |
350 } | |
351 } | |
352 #endif | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
353 /* TODO: close on exit */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
354 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
355 if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
356 if (ngx_nonblocking(s) == -1) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
357 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
461 | 358 ngx_nonblocking_n " %V failed", |
359 &ls[i].addr_text); | |
563 | 360 |
1223 | 361 if (ngx_close_socket(s) == -1) { |
563 | 362 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
363 ngx_close_socket_n " %V failed", | |
364 &ls[i].addr_text); | |
1223 | 365 } |
563 | 366 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
367 return NGX_ERROR; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
368 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
369 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
370 |
2801 | 371 ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0, |
593 | 372 "bind() %V #%d ", &ls[i].addr_text, s); |
373 | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
374 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
375 err = ngx_socket_errno; |
563 | 376 |
377 if (err == NGX_EADDRINUSE && ngx_test_config) { | |
378 continue; | |
379 } | |
380 | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
381 ngx_log_error(NGX_LOG_EMERG, log, err, |
461 | 382 "bind() to %V failed", &ls[i].addr_text); |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
383 |
1223 | 384 if (ngx_close_socket(s) == -1) { |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
385 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
461 | 386 ngx_close_socket_n " %V failed", |
387 &ls[i].addr_text); | |
1223 | 388 } |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
389 |
563 | 390 if (err != NGX_EADDRINUSE) { |
391 return NGX_ERROR; | |
392 } | |
393 | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
394 failed = 1; |
563 | 395 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
396 continue; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
397 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
398 |
3260
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
399 #if (NGX_HAVE_UNIX_DOMAIN) |
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
400 |
3371
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
401 if (ls[i].sockaddr->sa_family == AF_UNIX) { |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
402 mode_t mode; |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
403 u_char *name; |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
404 |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
405 name = ls[i].addr_text.data + sizeof("unix:") - 1; |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
406 mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); |
3260
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
407 |
3371
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
408 if (chmod((char *) name, mode) == -1) { |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
409 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
410 "chmod() \"%s\" failed", name); |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
411 } |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
412 |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
413 if (ngx_test_config) { |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
414 if (ngx_delete_file(name) == -1) { |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
415 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
416 ngx_delete_file_n " %s failed", name); |
102fdc22de4d
chmod unix listen domain socket to 0666
Igor Sysoev <igor@sysoev.ru>
parents:
3358
diff
changeset
|
417 } |
3260
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
418 } |
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
419 } |
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
420 #endif |
a6df0624203c
unlink unix domain socket file after testing
Igor Sysoev <igor@sysoev.ru>
parents:
3230
diff
changeset
|
421 |
1224
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
422 if (listen(s, ls[i].backlog) == -1) { |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
423 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
424 "listen() to %V, backlog %d failed", |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
425 &ls[i].addr_text, ls[i].backlog); |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
426 |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
427 if (ngx_close_socket(s) == -1) { |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
428 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
429 ngx_close_socket_n " %V failed", |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
430 &ls[i].addr_text); |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
431 } |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
432 |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
433 return NGX_ERROR; |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
434 } |
53e3dd035140
do listen() just after bind() to exit early
Igor Sysoev <igor@sysoev.ru>
parents:
1223
diff
changeset
|
435 |
563 | 436 ls[i].listen = 1; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
437 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
438 ls[i].fd = s; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
439 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
440 |
563 | 441 if (!failed) { |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
442 break; |
563 | 443 } |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
444 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
445 /* TODO: delay configurable */ |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
446 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
447 ngx_log_error(NGX_LOG_NOTICE, log, 0, |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
448 "try again to bind() after 500ms"); |
563 | 449 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
450 ngx_msleep(500); |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
451 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
452 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
453 if (failed) { |
477 | 454 ngx_log_error(NGX_LOG_EMERG, log, 0, "still could not bind()"); |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
455 return NGX_ERROR; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
456 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
457 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
458 return NGX_OK; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
459 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
460 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
461 |
521 | 462 void |
2802 | 463 ngx_configure_listening_sockets(ngx_cycle_t *cycle) |
563 | 464 { |
465 ngx_uint_t i; | |
466 ngx_listening_t *ls; | |
467 | |
468 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) | |
469 struct accept_filter_arg af; | |
470 #endif | |
471 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
472 int timeout; | |
473 #endif | |
474 | |
475 ls = cycle->listening.elts; | |
476 for (i = 0; i < cycle->listening.nelts; i++) { | |
477 | |
2803
60758ab4e5f4
move listen log copying from ngx_open_listening_sockets()
Igor Sysoev <igor@sysoev.ru>
parents:
2802
diff
changeset
|
478 ls[i].log = *ls[i].logp; |
60758ab4e5f4
move listen log copying from ngx_open_listening_sockets()
Igor Sysoev <igor@sysoev.ru>
parents:
2802
diff
changeset
|
479 |
563 | 480 if (ls[i].rcvbuf != -1) { |
481 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF, | |
482 (const void *) &ls[i].rcvbuf, sizeof(int)) | |
483 == -1) | |
484 { | |
485 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
569 | 486 "setsockopt(SO_RCVBUF, %d) %V failed, ignored", |
487 ls[i].rcvbuf, &ls[i].addr_text); | |
563 | 488 } |
489 } | |
490 | |
491 if (ls[i].sndbuf != -1) { | |
492 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF, | |
493 (const void *) &ls[i].sndbuf, sizeof(int)) | |
494 == -1) | |
495 { | |
496 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
569 | 497 "setsockopt(SO_SNDBUF, %d) %V failed, ignored", |
498 ls[i].sndbuf, &ls[i].addr_text); | |
563 | 499 } |
500 } | |
501 | |
3676 | 502 #if (NGX_HAVE_SETFIB) |
503 if (ls[i].setfib != -1) { | |
504 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_SETFIB, | |
505 (const void *) &ls[i].setfib, sizeof(int)) | |
506 == -1) | |
507 { | |
508 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
509 "setsockopt(SO_SETFIB, %d) %V failed, ignored", | |
510 ls[i].setfib, &ls[i].addr_text); | |
511 } | |
512 } | |
513 #endif | |
514 | |
635 | 515 #if 0 |
516 if (1) { | |
517 int tcp_nodelay = 1; | |
518 | |
519 if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_NODELAY, | |
520 (const void *) &tcp_nodelay, sizeof(int)) | |
521 == -1) | |
522 { | |
523 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
524 "setsockopt(TCP_NODELAY) %V failed, ignored", | |
525 &ls[i].addr_text); | |
526 } | |
527 } | |
528 #endif | |
529 | |
563 | 530 if (ls[i].listen) { |
1225 | 531 |
532 /* change backlog via listen() */ | |
533 | |
563 | 534 if (listen(ls[i].fd, ls[i].backlog) == -1) { |
535 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, | |
1222 | 536 "listen() to %V, backlog %d failed, ignored", |
537 &ls[i].addr_text, ls[i].backlog); | |
563 | 538 } |
539 } | |
540 | |
541 /* | |
542 * setting deferred mode should be last operation on socket, | |
543 * because code may prematurely continue cycle on failure | |
544 */ | |
545 | |
546 #if (NGX_HAVE_DEFERRED_ACCEPT) | |
547 | |
548 #ifdef SO_ACCEPTFILTER | |
549 | |
627 | 550 if (ls[i].delete_deferred) { |
551 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0) | |
552 == -1) | |
563 | 553 { |
554 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
555 "setsockopt(SO_ACCEPTFILTER, NULL) " | |
556 "for %V failed, ignored", | |
627 | 557 &ls[i].addr_text); |
563 | 558 |
627 | 559 if (ls[i].accept_filter) { |
563 | 560 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, |
561 "could not change the accept filter " | |
562 "to \"%s\" for %V, ignored", | |
627 | 563 ls[i].accept_filter, &ls[i].addr_text); |
563 | 564 } |
565 | |
566 continue; | |
567 } | |
568 | |
627 | 569 ls[i].deferred_accept = 0; |
563 | 570 } |
571 | |
627 | 572 if (ls[i].add_deferred) { |
563 | 573 ngx_memzero(&af, sizeof(struct accept_filter_arg)); |
574 (void) ngx_cpystrn((u_char *) af.af_name, | |
627 | 575 (u_char *) ls[i].accept_filter, 16); |
563 | 576 |
627 | 577 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, |
563 | 578 &af, sizeof(struct accept_filter_arg)) |
579 == -1) | |
580 { | |
581 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
582 "setsockopt(SO_ACCEPTFILTER, \"%s\") " | |
4016
b044d6553d52
Move SO_ACCEPTFILTER and TCP_DEFER_ACCEPT checks into configure.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3880
diff
changeset
|
583 "for %V failed, ignored", |
627 | 584 ls[i].accept_filter, &ls[i].addr_text); |
563 | 585 continue; |
586 } | |
587 | |
627 | 588 ls[i].deferred_accept = 1; |
563 | 589 } |
590 | |
591 #endif | |
592 | |
593 #ifdef TCP_DEFER_ACCEPT | |
594 | |
627 | 595 if (ls[i].add_deferred || ls[i].delete_deferred) { |
563 | 596 |
627 | 597 if (ls[i].add_deferred) { |
598 timeout = (int) (ls[i].post_accept_timeout / 1000); | |
563 | 599 |
600 } else { | |
601 timeout = 0; | |
602 } | |
603 | |
627 | 604 if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, |
563 | 605 &timeout, sizeof(int)) |
606 == -1) | |
607 { | |
608 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
609 "setsockopt(TCP_DEFER_ACCEPT, %d) for %V failed, " | |
610 "ignored", | |
627 | 611 timeout, &ls[i].addr_text); |
563 | 612 |
613 continue; | |
614 } | |
615 } | |
616 | |
627 | 617 if (ls[i].add_deferred) { |
618 ls[i].deferred_accept = 1; | |
563 | 619 } |
620 | |
621 #endif | |
622 | |
623 #endif /* NGX_HAVE_DEFERRED_ACCEPT */ | |
624 } | |
625 | |
626 return; | |
627 } | |
628 | |
629 | |
630 void | |
521 | 631 ngx_close_listening_sockets(ngx_cycle_t *cycle) |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
632 { |
543 | 633 ngx_uint_t i; |
634 ngx_listening_t *ls; | |
635 ngx_connection_t *c; | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
636 |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
637 if (ngx_event_flags & NGX_USE_IOCP_EVENT) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
638 return; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
639 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
640 |
303
00c5660d2707
nginx-0.0.3-2004-04-01-20:20:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
641 ngx_accept_mutex_held = 0; |
611 | 642 ngx_use_accept_mutex = 0; |
303
00c5660d2707
nginx-0.0.3-2004-04-01-20:20:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
643 |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
644 ls = cycle->listening.elts; |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
645 for (i = 0; i < cycle->listening.nelts; i++) { |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
646 |
543 | 647 c = ls[i].connection; |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
648 |
2725 | 649 if (c) { |
650 if (c->read->active) { | |
651 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) { | |
652 ngx_del_conn(c, NGX_CLOSE_EVENT); | |
1833
86464f8eddd6
workaround for Linux 2.6 OpenVZ
Igor Sysoev <igor@sysoev.ru>
parents:
1435
diff
changeset
|
653 |
2725 | 654 } else if (ngx_event_flags & NGX_USE_EPOLL_EVENT) { |
317
1308b98496a2
nginx-0.0.3-2004-04-15-19:34:36 import
Igor Sysoev <igor@sysoev.ru>
parents:
303
diff
changeset
|
655 |
2725 | 656 /* |
657 * it seems that Linux-2.6.x OpenVZ sends events | |
658 * for closed shared listening sockets unless | |
659 * the events was explicity deleted | |
660 */ | |
1833
86464f8eddd6
workaround for Linux 2.6 OpenVZ
Igor Sysoev <igor@sysoev.ru>
parents:
1435
diff
changeset
|
661 |
2725 | 662 ngx_del_event(c->read, NGX_READ_EVENT, 0); |
1833
86464f8eddd6
workaround for Linux 2.6 OpenVZ
Igor Sysoev <igor@sysoev.ru>
parents:
1435
diff
changeset
|
663 |
2725 | 664 } else { |
665 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT); | |
666 } | |
317
1308b98496a2
nginx-0.0.3-2004-04-15-19:34:36 import
Igor Sysoev <igor@sysoev.ru>
parents:
303
diff
changeset
|
667 } |
2725 | 668 |
669 ngx_free_connection(c); | |
670 | |
671 c->fd = (ngx_socket_t) -1; | |
317
1308b98496a2
nginx-0.0.3-2004-04-15-19:34:36 import
Igor Sysoev <igor@sysoev.ru>
parents:
303
diff
changeset
|
672 } |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
673 |
563 | 674 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0, |
675 "close listening %V #%d ", &ls[i].addr_text, ls[i].fd); | |
676 | |
543 | 677 if (ngx_close_socket(ls[i].fd) == -1) { |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
678 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno, |
461 | 679 ngx_close_socket_n " %V failed", &ls[i].addr_text); |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
680 } |
2928 | 681 |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
682 #if (NGX_HAVE_UNIX_DOMAIN) |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
683 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
684 if (ls[i].sockaddr->sa_family == AF_UNIX |
3358
f94e8ff65aa2
add NGX_PROCESS_HELPER process status
Igor Sysoev <igor@sysoev.ru>
parents:
3292
diff
changeset
|
685 && ngx_process <= NGX_PROCESS_MASTER |
3261
35077c22326c
do not unlink unix domain socket file while online upgrade
Igor Sysoev <igor@sysoev.ru>
parents:
3260
diff
changeset
|
686 && ngx_new_binary == 0) |
3230
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
687 { |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
688 u_char *name = ls[i].addr_text.data + sizeof("unix:") - 1; |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
689 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
690 if (ngx_delete_file(name) == -1) { |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
691 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno, |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
692 ngx_delete_file_n " %s failed", name); |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
693 } |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
694 } |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
695 |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
696 #endif |
a7491af45540
http listen unix domain sockets
Igor Sysoev <igor@sysoev.ru>
parents:
3151
diff
changeset
|
697 |
2928 | 698 ls[i].fd = (ngx_socket_t) -1; |
543 | 699 } |
700 } | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
701 |
543 | 702 |
703 ngx_connection_t * | |
704 ngx_get_connection(ngx_socket_t s, ngx_log_t *log) | |
705 { | |
561 | 706 ngx_uint_t instance; |
707 ngx_event_t *rev, *wev; | |
708 ngx_connection_t *c; | |
543 | 709 |
710 /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */ | |
711 | |
712 if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) { | |
713 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
714 "the new socket has number %d, " | |
715 "but only %ui files are available", | |
716 s, ngx_cycle->files_n); | |
717 return NULL; | |
718 } | |
719 | |
720 /* ngx_mutex_lock */ | |
721 | |
722 c = ngx_cycle->free_connections; | |
723 | |
724 if (c == NULL) { | |
3880
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
725 ngx_drain_connections(); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
726 c = ngx_cycle->free_connections; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
727 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
728 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
729 if (c == NULL) { |
543 | 730 ngx_log_error(NGX_LOG_ALERT, log, 0, |
2254 | 731 "%ui worker_connections are not enough", |
543 | 732 ngx_cycle->connection_n); |
733 | |
734 /* ngx_mutex_unlock */ | |
735 | |
736 return NULL; | |
737 } | |
738 | |
739 ngx_cycle->free_connections = c->data; | |
740 ngx_cycle->free_connection_n--; | |
741 | |
742 /* ngx_mutex_unlock */ | |
743 | |
744 if (ngx_cycle->files) { | |
745 ngx_cycle->files[s] = c; | |
746 } | |
747 | |
561 | 748 rev = c->read; |
749 wev = c->write; | |
750 | |
751 ngx_memzero(c, sizeof(ngx_connection_t)); | |
752 | |
753 c->read = rev; | |
754 c->write = wev; | |
755 c->fd = s; | |
756 c->log = log; | |
757 | |
758 instance = rev->instance; | |
759 | |
760 ngx_memzero(rev, sizeof(ngx_event_t)); | |
761 ngx_memzero(wev, sizeof(ngx_event_t)); | |
762 | |
763 rev->instance = !instance; | |
764 wev->instance = !instance; | |
765 | |
766 rev->index = NGX_INVALID_INDEX; | |
767 wev->index = NGX_INVALID_INDEX; | |
768 | |
769 rev->data = c; | |
770 wev->data = c; | |
771 | |
772 wev->write = 1; | |
773 | |
543 | 774 return c; |
775 } | |
776 | |
777 | |
778 void | |
779 ngx_free_connection(ngx_connection_t *c) | |
780 { | |
781 /* ngx_mutex_lock */ | |
782 | |
783 c->data = ngx_cycle->free_connections; | |
784 ngx_cycle->free_connections = c; | |
785 ngx_cycle->free_connection_n++; | |
786 | |
787 /* ngx_mutex_unlock */ | |
788 | |
789 if (ngx_cycle->files) { | |
790 ngx_cycle->files[c->fd] = NULL; | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
791 } |
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
792 } |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
793 |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
794 |
521 | 795 void |
796 ngx_close_connection(ngx_connection_t *c) | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
797 { |
1917
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
798 ngx_err_t err; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
799 ngx_uint_t log_error, level; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
800 ngx_socket_t fd; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
801 |
479 | 802 if (c->fd == -1) { |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
803 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed"); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
804 return; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
805 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
806 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
807 if (c->read->timer_set) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
808 ngx_del_timer(c->read); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
809 } |
577 | 810 |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
811 if (c->write->timer_set) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
812 ngx_del_timer(c->write); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
813 } |
577 | 814 |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
815 if (ngx_del_conn) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
816 ngx_del_conn(c, NGX_CLOSE_EVENT); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
817 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
818 } else { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
819 if (c->read->active || c->read->disabled) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
820 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
821 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
822 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
823 if (c->write->active || c->write->disabled) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
824 ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
825 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
826 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
827 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
828 #if (NGX_THREADS) |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
829 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
830 /* |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
831 * we have to clean the connection information before the closing |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
832 * because another thread may reopen the same file descriptor |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
833 * before we clean the connection |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
834 */ |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
835 |
563 | 836 ngx_mutex_lock(ngx_posted_events_mutex); |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
837 |
563 | 838 if (c->read->prev) { |
839 ngx_delete_posted_event(c->read); | |
840 } | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
841 |
563 | 842 if (c->write->prev) { |
843 ngx_delete_posted_event(c->write); | |
844 } | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
845 |
563 | 846 c->read->closed = 1; |
847 c->write->closed = 1; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
848 |
563 | 849 if (c->single_connection) { |
850 ngx_unlock(&c->lock); | |
851 c->read->locked = 0; | |
852 c->write->locked = 0; | |
853 } | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
854 |
563 | 855 ngx_mutex_unlock(ngx_posted_events_mutex); |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
856 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
857 #else |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
858 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
859 if (c->read->prev) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
860 ngx_delete_posted_event(c->read); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
861 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
862 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
863 if (c->write->prev) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
864 ngx_delete_posted_event(c->write); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
865 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
866 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
867 c->read->closed = 1; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
868 c->write->closed = 1; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
869 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
870 #endif |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
871 |
3880
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
872 ngx_reusable_connection(c, 0); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
873 |
1917
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
874 log_error = c->log_error; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
875 |
543 | 876 ngx_free_connection(c); |
877 | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
878 fd = c->fd; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
879 c->fd = (ngx_socket_t) -1; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
880 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
881 if (ngx_close_socket(fd) == -1) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
882 |
1917
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
883 err = ngx_socket_errno; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
884 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
885 if (err == NGX_ECONNRESET || err == NGX_ENOTCONN) { |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
886 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
887 switch (log_error) { |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
888 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
889 case NGX_ERROR_INFO: |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
890 level = NGX_LOG_INFO; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
891 break; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
892 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
893 case NGX_ERROR_ERR: |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
894 level = NGX_LOG_ERR; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
895 break; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
896 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
897 default: |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
898 level = NGX_LOG_CRIT; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
899 } |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
900 |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
901 } else { |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
902 level = NGX_LOG_CRIT; |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
903 } |
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
904 |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
905 /* we use ngx_cycle->log because c->log was in c->pool */ |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
906 |
1917
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
907 ngx_log_error(level, ngx_cycle->log, err, |
1435 | 908 ngx_close_socket_n " %d failed", fd); |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
909 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
910 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
911 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
346
diff
changeset
|
912 |
3880
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
913 void |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
914 ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable) |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
915 { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
916 ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
917 "reusable connection: %ui", reusable); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
918 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
919 if (c->reusable) { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
920 ngx_queue_remove(&c->queue); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
921 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
922 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
923 c->reusable = reusable; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
924 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
925 if (reusable) { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
926 /* need cast as ngx_cycle is volatile */ |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
927 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
928 ngx_queue_insert_head( |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
929 (ngx_queue_t *) &ngx_cycle->reusable_connections_queue, &c->queue); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
930 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
931 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
932 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
933 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
934 static void |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
935 ngx_drain_connections(void) |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
936 { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
937 ngx_int_t i; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
938 ngx_queue_t *q; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
939 ngx_connection_t *c; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
940 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
941 for (i = 0; i < 32; i++) { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
942 if (ngx_queue_empty(&ngx_cycle->reusable_connections_queue)) { |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
943 break; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
944 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
945 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
946 q = ngx_queue_last(&ngx_cycle->reusable_connections_queue); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
947 c = ngx_queue_data(q, ngx_connection_t, queue); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
948 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
949 ngx_log_debug0(NGX_LOG_DEBUG_CORE, c->log, 0, |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
950 "reusing connection"); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
951 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
952 c->close = 1; |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
953 c->read->handler(c->read); |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
954 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
955 } |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
956 |
e3cb8e27e413
reuse keepalive connections if there are no free worker connections
Igor Sysoev <igor@sysoev.ru>
parents:
3676
diff
changeset
|
957 |
521 | 958 ngx_int_t |
2854
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
959 ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s, |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
960 ngx_uint_t port) |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
961 { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
962 socklen_t len; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
963 ngx_uint_t addr; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
964 u_char sa[NGX_SOCKADDRLEN]; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
965 struct sockaddr_in *sin; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
966 #if (NGX_HAVE_INET6) |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
967 ngx_uint_t i; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
968 struct sockaddr_in6 *sin6; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
969 #endif |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
970 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
971 switch (c->local_sockaddr->sa_family) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
972 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
973 #if (NGX_HAVE_INET6) |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
974 case AF_INET6: |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
975 sin6 = (struct sockaddr_in6 *) c->local_sockaddr; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
976 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
977 for (addr = 0, i = 0; addr == 0 && i < 16; i++) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
978 addr |= sin6->sin6_addr.s6_addr[i]; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
979 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
980 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
981 break; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
982 #endif |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
983 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
984 default: /* AF_INET */ |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
985 sin = (struct sockaddr_in *) c->local_sockaddr; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
986 addr = sin->sin_addr.s_addr; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
987 break; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
988 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
989 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
990 if (addr == 0) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
991 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
992 len = NGX_SOCKADDRLEN; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
993 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
994 if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
995 ngx_connection_error(c, ngx_socket_errno, "getsockname() failed"); |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
996 return NGX_ERROR; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
997 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
998 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
999 c->local_sockaddr = ngx_palloc(c->pool, len); |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1000 if (c->local_sockaddr == NULL) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1001 return NGX_ERROR; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1002 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1003 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1004 ngx_memcpy(c->local_sockaddr, &sa, len); |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1005 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1006 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1007 if (s == NULL) { |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1008 return NGX_OK; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1009 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1010 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1011 s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, port); |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1012 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1013 return NGX_OK; |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1014 } |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1015 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1016 |
d788477f5a67
ngx_connection_local_sockaddr()
Igor Sysoev <igor@sysoev.ru>
parents:
2806
diff
changeset
|
1017 ngx_int_t |
521 | 1018 ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text) |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1019 { |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
256
diff
changeset
|
1020 ngx_uint_t level; |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1021 |
2775 | 1022 /* Winsock may return NGX_ECONNABORTED instead of NGX_ECONNRESET */ |
2774
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1023 |
2775 | 1024 if ((err == NGX_ECONNRESET |
1025 #if (NGX_WIN32) | |
1026 || err == NGX_ECONNABORTED | |
2774
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1027 #endif |
2775 | 1028 ) && c->log_error == NGX_ERROR_IGNORE_ECONNRESET) |
2774
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1029 { |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1030 return 0; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1031 } |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1032 |
2560
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1033 #if (NGX_SOLARIS) |
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1034 if (err == NGX_EINVAL && c->log_error == NGX_ERROR_IGNORE_EINVAL) { |
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1035 return 0; |
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1036 } |
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1037 #endif |
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1038 |
537 | 1039 if (err == 0 |
1040 || err == NGX_ECONNRESET | |
2774
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1041 #if (NGX_WIN32) |
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1042 || err == NGX_ECONNABORTED |
baad3036086e
Winsock uses ECONNABORTED instead of ECONNRESET
Igor Sysoev <igor@sysoev.ru>
parents:
2725
diff
changeset
|
1043 #else |
278
0ba4821f4460
nginx-0.0.2-2004-03-04-10:04:55 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
1044 || err == NGX_EPIPE |
0ba4821f4460
nginx-0.0.2-2004-03-04-10:04:55 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
1045 #endif |
288
f81d075ad172
nginx-0.0.2-2004-03-14-23:46:25 import
Igor Sysoev <igor@sysoev.ru>
parents:
282
diff
changeset
|
1046 || err == NGX_ENOTCONN |
587 | 1047 || err == NGX_ETIMEDOUT |
288
f81d075ad172
nginx-0.0.2-2004-03-14-23:46:25 import
Igor Sysoev <igor@sysoev.ru>
parents:
282
diff
changeset
|
1048 || err == NGX_ECONNREFUSED |
1869
192443881e51
add NGX_ENETDOWN, NGX_ENETUNREACH, and NGX_EHOSTDOWN
Igor Sysoev <igor@sysoev.ru>
parents:
1833
diff
changeset
|
1049 || err == NGX_ENETDOWN |
192443881e51
add NGX_ENETDOWN, NGX_ENETUNREACH, and NGX_EHOSTDOWN
Igor Sysoev <igor@sysoev.ru>
parents:
1833
diff
changeset
|
1050 || err == NGX_ENETUNREACH |
192443881e51
add NGX_ENETDOWN, NGX_ENETUNREACH, and NGX_EHOSTDOWN
Igor Sysoev <igor@sysoev.ru>
parents:
1833
diff
changeset
|
1051 || err == NGX_EHOSTDOWN |
288
f81d075ad172
nginx-0.0.2-2004-03-14-23:46:25 import
Igor Sysoev <igor@sysoev.ru>
parents:
282
diff
changeset
|
1052 || err == NGX_EHOSTUNREACH) |
278
0ba4821f4460
nginx-0.0.2-2004-03-04-10:04:55 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
1053 { |
256
8e39cab6abd5
nginx-0.0.2-2004-02-10-19:23:38 import
Igor Sysoev <igor@sysoev.ru>
parents:
253
diff
changeset
|
1054 switch (c->log_error) { |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1055 |
2560
a7443674e429
ignore EINVAL from setsockopt() on Solaris
Igor Sysoev <igor@sysoev.ru>
parents:
2512
diff
changeset
|
1056 case NGX_ERROR_IGNORE_EINVAL: |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
256
diff
changeset
|
1057 case NGX_ERROR_IGNORE_ECONNRESET: |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1058 case NGX_ERROR_INFO: |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1059 level = NGX_LOG_INFO; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1060 break; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1061 |
3151
ad3ddfd06e7d
low default connection errors level from alert to error
Igor Sysoev <igor@sysoev.ru>
parents:
2928
diff
changeset
|
1062 default: |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1063 level = NGX_LOG_ERR; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1064 } |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1065 |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1066 } else { |
1917
1b2e83c4886d
decrease log level of connection errors while closing socket
Igor Sysoev <igor@sysoev.ru>
parents:
1869
diff
changeset
|
1067 level = NGX_LOG_ALERT; |
253
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1068 } |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1069 |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1070 ngx_log_error(level, c->log, err, text); |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1071 |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1072 return NGX_ERROR; |
b6793bc5034b
nginx-0.0.2-2004-02-09-10:46:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
242
diff
changeset
|
1073 } |