0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
|
12 ngx_os_io_t ngx_io;
|
|
13
|
|
14
|
70
|
15 ngx_listening_t *
|
|
16 ngx_listening_inet_stream_socket(ngx_conf_t *cf, in_addr_t addr, in_port_t port)
|
0
|
17 {
|
|
18 size_t len;
|
|
19 ngx_listening_t *ls;
|
28
|
20 struct sockaddr_in *sin;
|
0
|
21
|
50
|
22 ls = ngx_array_push(&cf->cycle->listening);
|
|
23 if (ls == NULL) {
|
0
|
24 return NULL;
|
|
25 }
|
|
26
|
|
27 ngx_memzero(ls, sizeof(ngx_listening_t));
|
|
28
|
50
|
29 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
|
|
30 if (sin == NULL) {
|
0
|
31 return NULL;
|
|
32 }
|
|
33
|
28
|
34 sin->sin_family = AF_INET;
|
|
35 sin->sin_addr.s_addr = addr;
|
|
36 sin->sin_port = htons(port);
|
0
|
37
|
10
|
38
|
|
39 ls->addr_text.data = ngx_palloc(cf->pool,
|
|
40 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
|
|
41 if (ls->addr_text.data == NULL) {
|
0
|
42 return NULL;
|
|
43 }
|
|
44
|
|
45 len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data, INET_ADDRSTRLEN);
|
10
|
46
|
|
47 ls->addr_text.len = ngx_sprintf(ls->addr_text.data + len, ":%d", port)
|
|
48 - ls->addr_text.data;
|
|
49
|
0
|
50 ls->fd = (ngx_socket_t) -1;
|
|
51 ls->family = AF_INET;
|
|
52 ls->type = SOCK_STREAM;
|
28
|
53 ls->sockaddr = (struct sockaddr *) sin;
|
0
|
54 ls->socklen = sizeof(struct sockaddr_in);
|
|
55 ls->addr = offsetof(struct sockaddr_in, sin_addr);
|
|
56 ls->addr_text_max_len = INET_ADDRSTRLEN;
|
|
57
|
|
58 return ls;
|
|
59 }
|
|
60
|
|
61
|
70
|
62 ngx_int_t
|
|
63 ngx_set_inherited_sockets(ngx_cycle_t *cycle)
|
0
|
64 {
|
72
|
65 size_t len;
|
|
66 ngx_uint_t i;
|
|
67 ngx_listening_t *ls;
|
|
68 struct sockaddr_in *sin;
|
112
|
69 socklen_t olen;
|
72
|
70 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
90
|
71 ngx_err_t err;
|
72
|
72 struct accept_filter_arg af;
|
|
73 #endif
|
|
74 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
75 int timeout;
|
|
76 #endif
|
0
|
77
|
|
78 ls = cycle->listening.elts;
|
|
79 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
80
|
|
81 /* AF_INET only */
|
|
82
|
|
83 ls[i].sockaddr = ngx_palloc(cycle->pool, sizeof(struct sockaddr_in));
|
|
84 if (ls[i].sockaddr == NULL) {
|
|
85 return NGX_ERROR;
|
|
86 }
|
|
87
|
|
88 ls[i].socklen = sizeof(struct sockaddr_in);
|
|
89 if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) {
|
|
90 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
|
|
91 "getsockname() of the inherited "
|
|
92 "socket #%d failed", ls[i].fd);
|
|
93 ls[i].ignore = 1;
|
|
94 continue;
|
|
95 }
|
|
96
|
28
|
97 sin = (struct sockaddr_in *) ls[i].sockaddr;
|
0
|
98
|
28
|
99 if (sin->sin_family != AF_INET) {
|
0
|
100 ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
|
|
101 "the inherited socket #%d has "
|
|
102 "unsupported family", ls[i].fd);
|
|
103 ls[i].ignore = 1;
|
|
104 continue;
|
|
105 }
|
10
|
106
|
0
|
107 ls[i].addr_text_max_len = INET_ADDRSTRLEN;
|
|
108
|
10
|
109 ls[i].addr_text.data = ngx_palloc(cycle->pool, INET_ADDRSTRLEN - 1
|
|
110 + sizeof(":65535") - 1);
|
0
|
111 if (ls[i].addr_text.data == NULL) {
|
|
112 return NGX_ERROR;
|
|
113 }
|
|
114
|
28
|
115 ls[i].family = sin->sin_family;
|
10
|
116 len = ngx_sock_ntop(ls[i].family, ls[i].sockaddr,
|
|
117 ls[i].addr_text.data, INET_ADDRSTRLEN);
|
|
118 if (len == 0) {
|
0
|
119 return NGX_ERROR;
|
|
120 }
|
10
|
121
|
|
122 ls[i].addr_text.len = ngx_sprintf(ls[i].addr_text.data + len, ":%d",
|
28
|
123 ntohs(sin->sin_port))
|
10
|
124 - ls[i].addr_text.data;
|
72
|
125
|
86
|
126 ls[i].backlog = -1;
|
|
127
|
112
|
128 olen = sizeof(int);
|
|
129
|
|
130 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF, (void *) &ls[i].rcvbuf,
|
|
131 &olen)
|
|
132 == -1)
|
|
133 {
|
|
134 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
135 "getsockopt(SO_RCVBUF) %V failed, ignored",
|
|
136 &ls[i].addr_text);
|
|
137
|
|
138 ls[i].rcvbuf = -1;
|
|
139 }
|
|
140
|
|
141 olen = sizeof(int);
|
|
142
|
|
143 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF, (void *) &ls[i].sndbuf,
|
|
144 &olen)
|
|
145 == -1)
|
|
146 {
|
|
147 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
148 "getsockopt(SO_SNDBUF) %V failed, ignored",
|
|
149 &ls[i].addr_text);
|
|
150
|
|
151 ls[i].sndbuf = -1;
|
|
152 }
|
|
153
|
72
|
154 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
155
|
|
156 ngx_memzero(&af, sizeof(struct accept_filter_arg));
|
112
|
157 olen = sizeof(struct accept_filter_arg);
|
72
|
158
|
112
|
159 if (getsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, &af, &olen)
|
72
|
160 == -1)
|
|
161 {
|
90
|
162 err = ngx_errno;
|
|
163
|
|
164 if (err == NGX_EINVAL) {
|
|
165 continue;
|
|
166 }
|
|
167
|
|
168 ngx_log_error(NGX_LOG_NOTICE, cycle->log, err,
|
72
|
169 "getsockopt(SO_ACCEPTFILTER) for %V failed, ignored",
|
|
170 &ls[i].addr_text);
|
|
171 continue;
|
|
172 }
|
|
173
|
112
|
174 if (olen < sizeof(struct accept_filter_arg) || af.af_name[0] == '\0') {
|
72
|
175 continue;
|
|
176 }
|
|
177
|
|
178 ls[i].accept_filter = ngx_palloc(cycle->pool, 16);
|
|
179 if (ls[i].accept_filter == NULL) {
|
|
180 return NGX_ERROR;
|
|
181 }
|
|
182
|
|
183 (void) ngx_cpystrn((u_char *) ls[i].accept_filter,
|
|
184 (u_char *) af.af_name, 16);
|
|
185 #endif
|
|
186
|
|
187 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
188
|
|
189 timeout = 0;
|
112
|
190 olen = sizeof(int);
|
72
|
191
|
112
|
192 if (getsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, &olen)
|
72
|
193 == -1)
|
|
194 {
|
|
195 ngx_log_error(NGX_LOG_NOTICE, cycle->log, ngx_errno,
|
|
196 "getsockopt(TCP_DEFER_ACCEPT) for %V failed, ignored",
|
|
197 &ls[i].addr_text);
|
|
198 continue;
|
|
199 }
|
|
200
|
114
|
201 if (olen < sizeof(int) || timeout == 0) {
|
72
|
202 continue;
|
|
203 }
|
|
204
|
|
205 ls[i].deferred_accept = 1;
|
|
206 #endif
|
0
|
207 }
|
|
208
|
|
209 return NGX_OK;
|
|
210 }
|
|
211
|
|
212
|
70
|
213 ngx_int_t
|
|
214 ngx_open_listening_sockets(ngx_cycle_t *cycle)
|
0
|
215 {
|
112
|
216 int reuseaddr;
|
|
217 ngx_uint_t i, tries, failed;
|
0
|
218 ngx_err_t err;
|
|
219 ngx_log_t *log;
|
|
220 ngx_socket_t s;
|
|
221 ngx_listening_t *ls;
|
|
222
|
|
223 reuseaddr = 1;
|
|
224 #if (NGX_SUPPRESS_WARN)
|
|
225 failed = 0;
|
|
226 #endif
|
|
227
|
|
228 log = cycle->log;
|
|
229
|
112
|
230 /* TODO: configurable try number */
|
0
|
231
|
112
|
232 for (tries = 5 ; tries; tries--) {
|
0
|
233 failed = 0;
|
|
234
|
|
235 /* for each listening socket */
|
|
236
|
|
237 ls = cycle->listening.elts;
|
|
238 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
239
|
|
240 if (ls[i].ignore) {
|
|
241 continue;
|
|
242 }
|
|
243
|
|
244 if (ls[i].fd != -1) {
|
|
245 continue;
|
|
246 }
|
|
247
|
|
248 if (ls[i].inherited) {
|
|
249
|
|
250 /* TODO: close on exit */
|
|
251 /* TODO: nonblocking */
|
|
252 /* TODO: deferred accept */
|
|
253
|
|
254 continue;
|
|
255 }
|
|
256
|
28
|
257 s = ngx_socket(ls[i].family, ls[i].type, 0);
|
0
|
258
|
|
259 if (s == -1) {
|
|
260 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
10
|
261 ngx_socket_n " %V failed", &ls[i].addr_text);
|
0
|
262 return NGX_ERROR;
|
|
263 }
|
|
264
|
|
265 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
|
112
|
266 (const void *) &reuseaddr, sizeof(int))
|
|
267 == -1)
|
|
268 {
|
0
|
269 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
10
|
270 "setsockopt(SO_REUSEADDR) %V failed",
|
|
271 &ls[i].addr_text);
|
112
|
272
|
|
273 if (ngx_close_socket(s) == -1)
|
|
274 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
275 ngx_close_socket_n " %V failed",
|
|
276 &ls[i].addr_text);
|
|
277
|
0
|
278 return NGX_ERROR;
|
|
279 }
|
|
280
|
|
281 /* TODO: close on exit */
|
|
282
|
|
283 if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) {
|
|
284 if (ngx_nonblocking(s) == -1) {
|
|
285 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
10
|
286 ngx_nonblocking_n " %V failed",
|
|
287 &ls[i].addr_text);
|
112
|
288
|
|
289 if (ngx_close_socket(s) == -1)
|
|
290 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
291 ngx_close_socket_n " %V failed",
|
|
292 &ls[i].addr_text);
|
|
293
|
0
|
294 return NGX_ERROR;
|
|
295 }
|
|
296 }
|
|
297
|
142
|
298 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
299 "bind() %V #%d ", &ls[i].addr_text, s);
|
|
300
|
0
|
301 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
|
|
302 err = ngx_socket_errno;
|
112
|
303
|
|
304 if (err == NGX_EADDRINUSE && ngx_test_config) {
|
|
305 continue;
|
|
306 }
|
|
307
|
0
|
308 ngx_log_error(NGX_LOG_EMERG, log, err,
|
10
|
309 "bind() to %V failed", &ls[i].addr_text);
|
0
|
310
|
|
311 if (ngx_close_socket(s) == -1)
|
|
312 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
10
|
313 ngx_close_socket_n " %V failed",
|
|
314 &ls[i].addr_text);
|
0
|
315
|
112
|
316 if (err != NGX_EADDRINUSE) {
|
|
317 return NGX_ERROR;
|
|
318 }
|
|
319
|
0
|
320 failed = 1;
|
112
|
321
|
0
|
322 continue;
|
|
323 }
|
|
324
|
112
|
325 ls[i].listen = 1;
|
0
|
326
|
|
327 ls[i].fd = s;
|
|
328 }
|
|
329
|
112
|
330 if (!failed) {
|
0
|
331 break;
|
112
|
332 }
|
0
|
333
|
|
334 /* TODO: delay configurable */
|
|
335
|
|
336 ngx_log_error(NGX_LOG_NOTICE, log, 0,
|
|
337 "try again to bind() after 500ms");
|
112
|
338
|
0
|
339 ngx_msleep(500);
|
|
340 }
|
|
341
|
|
342 if (failed) {
|
26
|
343 ngx_log_error(NGX_LOG_EMERG, log, 0, "still could not bind()");
|
0
|
344 return NGX_ERROR;
|
|
345 }
|
|
346
|
|
347 return NGX_OK;
|
|
348 }
|
|
349
|
|
350
|
70
|
351 void
|
112
|
352 ngx_configure_listening_socket(ngx_cycle_t *cycle)
|
|
353 {
|
|
354 ngx_uint_t i;
|
|
355 ngx_listening_t *ls;
|
|
356
|
|
357 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
358 struct accept_filter_arg af;
|
|
359 #endif
|
|
360 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
361 int timeout;
|
|
362 #endif
|
|
363
|
|
364 ls = cycle->listening.elts;
|
|
365 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
366
|
|
367 if (ls[i].rcvbuf != -1) {
|
|
368 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_RCVBUF,
|
|
369 (const void *) &ls[i].rcvbuf, sizeof(int))
|
|
370 == -1)
|
|
371 {
|
|
372 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
118
|
373 "setsockopt(SO_RCVBUF, %d) %V failed, ignored",
|
|
374 ls[i].rcvbuf, &ls[i].addr_text);
|
112
|
375 }
|
|
376 }
|
|
377
|
|
378 if (ls[i].sndbuf != -1) {
|
|
379 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_SNDBUF,
|
|
380 (const void *) &ls[i].sndbuf, sizeof(int))
|
|
381 == -1)
|
|
382 {
|
|
383 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
118
|
384 "setsockopt(SO_SNDBUF, %d) %V failed, ignored",
|
|
385 ls[i].sndbuf, &ls[i].addr_text);
|
112
|
386 }
|
|
387 }
|
|
388
|
|
389 if (ls[i].listen) {
|
|
390 if (listen(ls[i].fd, ls[i].backlog) == -1) {
|
|
391 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
|
|
392 "changing the listen() backlog to %d "
|
|
393 "for %V failed, ignored",
|
|
394 &ls[i].addr_text, ls[i].backlog);
|
|
395 }
|
|
396 }
|
|
397
|
|
398 /*
|
|
399 * setting deferred mode should be last operation on socket,
|
|
400 * because code may prematurely continue cycle on failure
|
|
401 */
|
|
402
|
|
403 #if (NGX_HAVE_DEFERRED_ACCEPT)
|
|
404
|
|
405 #ifdef SO_ACCEPTFILTER
|
|
406
|
176
|
407 if (ls[i].delete_deferred) {
|
|
408 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0)
|
|
409 == -1)
|
112
|
410 {
|
|
411 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
412 "setsockopt(SO_ACCEPTFILTER, NULL) "
|
|
413 "for %V failed, ignored",
|
176
|
414 &ls[i].addr_text);
|
112
|
415
|
176
|
416 if (ls[i].accept_filter) {
|
112
|
417 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
418 "could not change the accept filter "
|
|
419 "to \"%s\" for %V, ignored",
|
176
|
420 ls[i].accept_filter, &ls[i].addr_text);
|
112
|
421 }
|
|
422
|
|
423 continue;
|
|
424 }
|
|
425
|
176
|
426 ls[i].deferred_accept = 0;
|
112
|
427 }
|
|
428
|
176
|
429 if (ls[i].add_deferred) {
|
112
|
430 ngx_memzero(&af, sizeof(struct accept_filter_arg));
|
|
431 (void) ngx_cpystrn((u_char *) af.af_name,
|
176
|
432 (u_char *) ls[i].accept_filter, 16);
|
112
|
433
|
176
|
434 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_ACCEPTFILTER,
|
112
|
435 &af, sizeof(struct accept_filter_arg))
|
|
436 == -1)
|
|
437 {
|
|
438 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
439 "setsockopt(SO_ACCEPTFILTER, \"%s\") "
|
|
440 " for %V failed, ignored",
|
176
|
441 ls[i].accept_filter, &ls[i].addr_text);
|
112
|
442 continue;
|
|
443 }
|
|
444
|
176
|
445 ls[i].deferred_accept = 1;
|
112
|
446 }
|
|
447
|
|
448 #endif
|
|
449
|
|
450 #ifdef TCP_DEFER_ACCEPT
|
|
451
|
176
|
452 if (ls[i].add_deferred || ls[i].delete_deferred) {
|
112
|
453
|
176
|
454 if (ls[i].add_deferred) {
|
|
455 timeout = (int) (ls[i].post_accept_timeout / 1000);
|
112
|
456
|
|
457 } else {
|
|
458 timeout = 0;
|
|
459 }
|
|
460
|
176
|
461 if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_DEFER_ACCEPT,
|
112
|
462 &timeout, sizeof(int))
|
|
463 == -1)
|
|
464 {
|
|
465 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
466 "setsockopt(TCP_DEFER_ACCEPT, %d) for %V failed, "
|
|
467 "ignored",
|
176
|
468 timeout, &ls[i].addr_text);
|
112
|
469
|
|
470 continue;
|
|
471 }
|
|
472 }
|
|
473
|
176
|
474 if (ls[i].add_deferred) {
|
|
475 ls[i].deferred_accept = 1;
|
112
|
476 }
|
|
477
|
|
478 #endif
|
|
479
|
|
480 #endif /* NGX_HAVE_DEFERRED_ACCEPT */
|
|
481 }
|
|
482
|
|
483 return;
|
|
484 }
|
|
485
|
|
486
|
|
487 void
|
70
|
488 ngx_close_listening_sockets(ngx_cycle_t *cycle)
|
0
|
489 {
|
92
|
490 ngx_uint_t i;
|
|
491 ngx_listening_t *ls;
|
|
492 ngx_connection_t *c;
|
0
|
493
|
|
494 if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
|
|
495 return;
|
|
496 }
|
|
497
|
|
498 ngx_accept_mutex_held = 0;
|
160
|
499 ngx_use_accept_mutex = 0;
|
0
|
500
|
|
501 ls = cycle->listening.elts;
|
|
502 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
503
|
92
|
504 c = ls[i].connection;
|
0
|
505
|
|
506 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
92
|
507 if (c->read->active) {
|
|
508 ngx_del_conn(c, NGX_CLOSE_EVENT);
|
0
|
509 }
|
|
510
|
|
511 } else {
|
92
|
512 if (c->read->active) {
|
|
513 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
0
|
514 }
|
|
515 }
|
|
516
|
92
|
517 ngx_free_connection(c);
|
|
518
|
|
519 c->fd = (ngx_socket_t) -1;
|
|
520
|
112
|
521 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
522 "close listening %V #%d ", &ls[i].addr_text, ls[i].fd);
|
|
523
|
92
|
524 if (ngx_close_socket(ls[i].fd) == -1) {
|
0
|
525 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
|
10
|
526 ngx_close_socket_n " %V failed", &ls[i].addr_text);
|
0
|
527 }
|
92
|
528 }
|
|
529 }
|
0
|
530
|
92
|
531
|
|
532 ngx_connection_t *
|
|
533 ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
|
|
534 {
|
110
|
535 ngx_uint_t instance;
|
|
536 ngx_event_t *rev, *wev;
|
|
537 ngx_connection_t *c;
|
92
|
538
|
|
539 /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */
|
|
540
|
|
541 if (ngx_cycle->files && (ngx_uint_t) s >= ngx_cycle->files_n) {
|
|
542 ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
543 "the new socket has number %d, "
|
|
544 "but only %ui files are available",
|
|
545 s, ngx_cycle->files_n);
|
|
546 return NULL;
|
|
547 }
|
|
548
|
|
549 /* ngx_mutex_lock */
|
|
550
|
|
551 c = ngx_cycle->free_connections;
|
|
552
|
|
553 if (c == NULL) {
|
|
554 ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
555 "%ui worker_connections is not enough",
|
|
556 ngx_cycle->connection_n);
|
|
557
|
|
558 /* ngx_mutex_unlock */
|
|
559
|
|
560 return NULL;
|
|
561 }
|
|
562
|
|
563 ngx_cycle->free_connections = c->data;
|
|
564 ngx_cycle->free_connection_n--;
|
|
565
|
|
566 /* ngx_mutex_unlock */
|
|
567
|
|
568 if (ngx_cycle->files) {
|
|
569 ngx_cycle->files[s] = c;
|
|
570 }
|
|
571
|
110
|
572 rev = c->read;
|
|
573 wev = c->write;
|
|
574
|
|
575 ngx_memzero(c, sizeof(ngx_connection_t));
|
|
576
|
|
577 c->read = rev;
|
|
578 c->write = wev;
|
|
579 c->fd = s;
|
|
580 c->log = log;
|
|
581
|
|
582 instance = rev->instance;
|
|
583
|
|
584 ngx_memzero(rev, sizeof(ngx_event_t));
|
|
585 ngx_memzero(wev, sizeof(ngx_event_t));
|
|
586
|
|
587 rev->instance = !instance;
|
|
588 wev->instance = !instance;
|
|
589
|
|
590 rev->index = NGX_INVALID_INDEX;
|
|
591 wev->index = NGX_INVALID_INDEX;
|
|
592
|
|
593 rev->data = c;
|
|
594 wev->data = c;
|
|
595
|
|
596 wev->write = 1;
|
|
597
|
92
|
598 return c;
|
|
599 }
|
|
600
|
|
601
|
|
602 void
|
|
603 ngx_free_connection(ngx_connection_t *c)
|
|
604 {
|
|
605 /* ngx_mutex_lock */
|
|
606
|
|
607 c->data = ngx_cycle->free_connections;
|
|
608 ngx_cycle->free_connections = c;
|
|
609 ngx_cycle->free_connection_n++;
|
|
610
|
|
611 /* ngx_mutex_unlock */
|
|
612
|
|
613 if (ngx_cycle->files) {
|
|
614 ngx_cycle->files[c->fd] = NULL;
|
0
|
615 }
|
|
616 }
|
|
617
|
|
618
|
70
|
619 void
|
|
620 ngx_close_connection(ngx_connection_t *c)
|
0
|
621 {
|
|
622 ngx_socket_t fd;
|
|
623
|
28
|
624 if (c->fd == -1) {
|
0
|
625 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
|
|
626 return;
|
|
627 }
|
|
628
|
|
629 if (c->read->timer_set) {
|
|
630 ngx_del_timer(c->read);
|
|
631 }
|
126
|
632
|
0
|
633 if (c->write->timer_set) {
|
|
634 ngx_del_timer(c->write);
|
|
635 }
|
126
|
636
|
0
|
637 if (ngx_del_conn) {
|
|
638 ngx_del_conn(c, NGX_CLOSE_EVENT);
|
|
639
|
|
640 } else {
|
|
641 if (c->read->active || c->read->disabled) {
|
|
642 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
643 }
|
|
644
|
|
645 if (c->write->active || c->write->disabled) {
|
|
646 ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
|
|
647 }
|
|
648 }
|
|
649
|
|
650 #if (NGX_THREADS)
|
|
651
|
|
652 /*
|
|
653 * we have to clean the connection information before the closing
|
|
654 * because another thread may reopen the same file descriptor
|
|
655 * before we clean the connection
|
|
656 */
|
|
657
|
112
|
658 ngx_mutex_lock(ngx_posted_events_mutex);
|
0
|
659
|
112
|
660 if (c->read->prev) {
|
|
661 ngx_delete_posted_event(c->read);
|
|
662 }
|
0
|
663
|
112
|
664 if (c->write->prev) {
|
|
665 ngx_delete_posted_event(c->write);
|
|
666 }
|
0
|
667
|
112
|
668 c->read->closed = 1;
|
|
669 c->write->closed = 1;
|
0
|
670
|
112
|
671 if (c->single_connection) {
|
|
672 ngx_unlock(&c->lock);
|
|
673 c->read->locked = 0;
|
|
674 c->write->locked = 0;
|
|
675 }
|
0
|
676
|
112
|
677 ngx_mutex_unlock(ngx_posted_events_mutex);
|
0
|
678
|
|
679 #else
|
|
680
|
|
681 if (c->read->prev) {
|
|
682 ngx_delete_posted_event(c->read);
|
|
683 }
|
|
684
|
|
685 if (c->write->prev) {
|
|
686 ngx_delete_posted_event(c->write);
|
|
687 }
|
|
688
|
|
689 c->read->closed = 1;
|
|
690 c->write->closed = 1;
|
|
691
|
|
692 #endif
|
|
693
|
92
|
694 ngx_free_connection(c);
|
|
695
|
0
|
696 fd = c->fd;
|
|
697 c->fd = (ngx_socket_t) -1;
|
|
698
|
|
699 if (ngx_close_socket(fd) == -1) {
|
|
700
|
|
701 /* we use ngx_cycle->log because c->log was in c->pool */
|
|
702
|
|
703 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno,
|
|
704 ngx_close_socket_n " failed");
|
|
705 }
|
|
706 }
|
|
707
|
|
708
|
70
|
709 ngx_int_t
|
|
710 ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text)
|
0
|
711 {
|
|
712 ngx_uint_t level;
|
|
713
|
|
714 if (err == NGX_ECONNRESET
|
|
715 && c->log_error == NGX_ERROR_IGNORE_ECONNRESET)
|
|
716 {
|
|
717 return 0;
|
|
718 }
|
|
719
|
86
|
720 if (err == 0
|
|
721 || err == NGX_ECONNRESET
|
10
|
722 #if !(NGX_WIN32)
|
0
|
723 || err == NGX_EPIPE
|
|
724 #endif
|
|
725 || err == NGX_ENOTCONN
|
136
|
726 || err == NGX_ETIMEDOUT
|
0
|
727 || err == NGX_ECONNREFUSED
|
|
728 || err == NGX_EHOSTUNREACH)
|
|
729 {
|
|
730 switch (c->log_error) {
|
|
731
|
|
732 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
733 case NGX_ERROR_INFO:
|
|
734 level = NGX_LOG_INFO;
|
|
735 break;
|
|
736
|
|
737 case NGX_ERROR_ERR:
|
|
738 level = NGX_LOG_ERR;
|
|
739 break;
|
|
740
|
|
741 default:
|
|
742 level = NGX_LOG_CRIT;
|
|
743 }
|
|
744
|
|
745 } else {
|
|
746 level = NGX_LOG_CRIT;
|
|
747 }
|
|
748
|
|
749 ngx_log_error(level, c->log, err, text);
|
|
750
|
|
751 return NGX_ERROR;
|
|
752 }
|