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 #define DEFAULT_CONNECTIONS 512
|
|
13
|
|
14
|
16
|
15 extern ngx_module_t ngx_kqueue_module;
|
|
16 extern ngx_module_t ngx_devpoll_module;
|
|
17 extern ngx_module_t ngx_epoll_module;
|
|
18 extern ngx_module_t ngx_rtsig_module;
|
0
|
19 extern ngx_module_t ngx_select_module;
|
|
20
|
|
21
|
|
22 static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);
|
|
23 static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle);
|
|
24 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
25
|
|
26 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
|
58
|
27 void *conf);
|
0
|
28 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
29 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
|
58
|
30 void *conf);
|
0
|
31
|
|
32 static void *ngx_event_create_conf(ngx_cycle_t *cycle);
|
|
33 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
|
|
34 static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data);
|
|
35
|
|
36
|
44
|
37 static ngx_uint_t ngx_event_max_module;
|
0
|
38
|
44
|
39 ngx_uint_t ngx_event_flags;
|
|
40 ngx_event_actions_t ngx_event_actions;
|
0
|
41
|
|
42
|
110
|
43 ngx_atomic_t connection_counter = 1;
|
42
|
44 ngx_atomic_t *ngx_connection_counter = &connection_counter;
|
0
|
45
|
|
46
|
42
|
47 ngx_atomic_t *ngx_accept_mutex_ptr;
|
|
48 ngx_atomic_t *ngx_accept_mutex;
|
44
|
49 ngx_uint_t ngx_accept_mutex_held;
|
|
50 ngx_msec_t ngx_accept_mutex_delay;
|
|
51 ngx_int_t ngx_accept_disabled;
|
110
|
52 ngx_file_t ngx_accept_mutex_lock_file;
|
0
|
53
|
|
54
|
|
55 #if (NGX_STAT_STUB)
|
|
56
|
|
57 ngx_atomic_t ngx_stat_accepted0;
|
|
58 ngx_atomic_t *ngx_stat_accepted = &ngx_stat_accepted0;
|
44
|
59 ngx_atomic_t ngx_stat_handled0;
|
|
60 ngx_atomic_t *ngx_stat_handled = &ngx_stat_handled0;
|
0
|
61 ngx_atomic_t ngx_stat_requests0;
|
|
62 ngx_atomic_t *ngx_stat_requests = &ngx_stat_requests0;
|
|
63 ngx_atomic_t ngx_stat_active0;
|
|
64 ngx_atomic_t *ngx_stat_active = &ngx_stat_active0;
|
|
65 ngx_atomic_t ngx_stat_reading0;
|
|
66 ngx_atomic_t *ngx_stat_reading = &ngx_stat_reading0;
|
|
67 ngx_atomic_t ngx_stat_writing0;
|
44
|
68 ngx_atomic_t *ngx_stat_writing = &ngx_stat_writing0;
|
0
|
69
|
|
70 #endif
|
|
71
|
|
72
|
|
73
|
|
74 static ngx_command_t ngx_events_commands[] = {
|
|
75
|
|
76 { ngx_string("events"),
|
|
77 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
|
78 ngx_events_block,
|
|
79 0,
|
|
80 0,
|
|
81 NULL },
|
|
82
|
|
83 ngx_null_command
|
|
84 };
|
|
85
|
|
86
|
|
87 static ngx_core_module_t ngx_events_module_ctx = {
|
|
88 ngx_string("events"),
|
|
89 NULL,
|
|
90 NULL
|
|
91 };
|
|
92
|
|
93
|
|
94 ngx_module_t ngx_events_module = {
|
58
|
95 NGX_MODULE_V1,
|
0
|
96 &ngx_events_module_ctx, /* module context */
|
|
97 ngx_events_commands, /* module directives */
|
|
98 NGX_CORE_MODULE, /* module type */
|
90
|
99 NULL, /* init master */
|
0
|
100 NULL, /* init module */
|
90
|
101 NULL, /* init process */
|
|
102 NULL, /* init thread */
|
|
103 NULL, /* exit thread */
|
|
104 NULL, /* exit process */
|
|
105 NULL, /* exit master */
|
|
106 NGX_MODULE_V1_PADDING
|
0
|
107 };
|
|
108
|
|
109
|
|
110 static ngx_str_t event_core_name = ngx_string("event_core");
|
|
111
|
|
112 static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ;
|
|
113
|
|
114
|
|
115 static ngx_command_t ngx_event_core_commands[] = {
|
|
116
|
92
|
117 { ngx_string("worker_connections"),
|
|
118 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
119 ngx_event_connections,
|
|
120 0,
|
|
121 0,
|
|
122 NULL },
|
|
123
|
0
|
124 { ngx_string("connections"),
|
|
125 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
126 ngx_event_connections,
|
|
127 0,
|
|
128 0,
|
|
129 NULL },
|
|
130
|
|
131 { ngx_string("use"),
|
|
132 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
133 ngx_event_use,
|
|
134 0,
|
|
135 0,
|
|
136 NULL },
|
|
137
|
|
138 { ngx_string("multi_accept"),
|
|
139 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
140 ngx_conf_set_flag_slot,
|
|
141 0,
|
|
142 offsetof(ngx_event_conf_t, multi_accept),
|
|
143 NULL },
|
|
144
|
|
145 { ngx_string("accept_mutex"),
|
|
146 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
147 ngx_conf_set_flag_slot,
|
|
148 0,
|
|
149 offsetof(ngx_event_conf_t, accept_mutex),
|
|
150 &ngx_accept_mutex_post },
|
|
151
|
|
152 { ngx_string("accept_mutex_delay"),
|
|
153 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
154 ngx_conf_set_msec_slot,
|
|
155 0,
|
|
156 offsetof(ngx_event_conf_t, accept_mutex_delay),
|
|
157 NULL },
|
|
158
|
|
159 { ngx_string("debug_connection"),
|
|
160 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
161 ngx_event_debug_connection,
|
|
162 0,
|
|
163 0,
|
|
164 NULL },
|
|
165
|
|
166 ngx_null_command
|
|
167 };
|
|
168
|
|
169
|
|
170 ngx_event_module_t ngx_event_core_module_ctx = {
|
|
171 &event_core_name,
|
|
172 ngx_event_create_conf, /* create configuration */
|
|
173 ngx_event_init_conf, /* init configuration */
|
|
174
|
|
175 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
|
176 };
|
|
177
|
|
178
|
|
179 ngx_module_t ngx_event_core_module = {
|
58
|
180 NGX_MODULE_V1,
|
0
|
181 &ngx_event_core_module_ctx, /* module context */
|
|
182 ngx_event_core_commands, /* module directives */
|
|
183 NGX_EVENT_MODULE, /* module type */
|
90
|
184 NULL, /* init master */
|
0
|
185 ngx_event_module_init, /* init module */
|
90
|
186 ngx_event_process_init, /* init process */
|
|
187 NULL, /* init thread */
|
|
188 NULL, /* exit thread */
|
|
189 NULL, /* exit process */
|
|
190 NULL, /* exit master */
|
|
191 NGX_MODULE_V1_PADDING
|
0
|
192 };
|
|
193
|
|
194
|
58
|
195 ngx_int_t
|
|
196 ngx_handle_read_event(ngx_event_t *rev, u_int flags)
|
|
197 {
|
|
198 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
|
|
199
|
|
200 /* kqueue, epoll */
|
|
201
|
|
202 if (!rev->active && !rev->ready) {
|
|
203 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)
|
|
204 == NGX_ERROR)
|
|
205 {
|
|
206 return NGX_ERROR;
|
|
207 }
|
|
208 }
|
|
209
|
|
210 return NGX_OK;
|
|
211
|
|
212 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
|
|
213
|
|
214 /* select, poll, /dev/poll */
|
|
215
|
|
216 if (!rev->active && !rev->ready) {
|
|
217 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT)
|
|
218 == NGX_ERROR)
|
|
219 {
|
|
220 return NGX_ERROR;
|
|
221 }
|
|
222
|
|
223 return NGX_OK;
|
|
224 }
|
|
225
|
|
226 if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {
|
|
227 if (ngx_del_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT | flags)
|
|
228 == NGX_ERROR)
|
|
229 {
|
|
230 return NGX_ERROR;
|
|
231 }
|
|
232
|
|
233 return NGX_OK;
|
|
234 }
|
|
235
|
|
236 } else if (ngx_event_flags & NGX_USE_ONESHOT_EVENT) {
|
|
237
|
|
238 /* event ports */
|
|
239
|
|
240 if (!rev->active) {
|
|
241 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_ONESHOT_EVENT)
|
|
242 == NGX_ERROR)
|
|
243 {
|
|
244 return NGX_ERROR;
|
|
245 }
|
|
246 }
|
|
247
|
|
248 return NGX_OK;
|
|
249 }
|
|
250
|
|
251 /* aio, iocp, rtsig */
|
|
252
|
|
253 return NGX_OK;
|
|
254 }
|
|
255
|
|
256
|
|
257 ngx_int_t
|
|
258 ngx_handle_write_event(ngx_event_t *wev, size_t lowat)
|
|
259 {
|
|
260 ngx_connection_t *c;
|
|
261
|
|
262 if (lowat) {
|
|
263 c = (ngx_connection_t *) wev->data;
|
|
264
|
|
265 if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
|
|
266 return NGX_ERROR;
|
|
267 }
|
|
268 }
|
|
269
|
|
270 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
|
|
271
|
|
272 /* kqueue, epoll */
|
|
273
|
|
274 if (!wev->active && !wev->ready) {
|
|
275 if (ngx_add_event(wev, NGX_WRITE_EVENT,
|
|
276 NGX_CLEAR_EVENT | (lowat ? NGX_LOWAT_EVENT : 0))
|
|
277 == NGX_ERROR)
|
|
278 {
|
|
279 return NGX_ERROR;
|
|
280 }
|
|
281 }
|
|
282
|
|
283 return NGX_OK;
|
|
284
|
|
285 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
|
|
286
|
|
287 /* select, poll, /dev/poll */
|
|
288
|
|
289 if (!wev->active && !wev->ready) {
|
|
290 if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
|
|
291 == NGX_ERROR)
|
|
292 {
|
|
293 return NGX_ERROR;
|
|
294 }
|
|
295
|
|
296 return NGX_OK;
|
|
297 }
|
|
298
|
|
299 if (wev->active && wev->ready) {
|
|
300 if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
|
|
301 == NGX_ERROR)
|
|
302 {
|
|
303 return NGX_ERROR;
|
|
304 }
|
|
305
|
|
306 return NGX_OK;
|
|
307 }
|
|
308
|
|
309 } else if (ngx_event_flags & NGX_USE_ONESHOT_EVENT) {
|
|
310
|
|
311 /* event ports */
|
|
312
|
|
313 if (!wev->active) {
|
|
314 if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_ONESHOT_EVENT)
|
|
315 == NGX_ERROR)
|
|
316 {
|
|
317 return NGX_ERROR;
|
|
318 }
|
|
319 }
|
|
320
|
|
321 return NGX_OK;
|
|
322 }
|
|
323
|
|
324 /* aio, iocp, rtsig */
|
|
325
|
|
326 return NGX_OK;
|
|
327 }
|
|
328
|
|
329
|
|
330 static ngx_int_t
|
|
331 ngx_event_module_init(ngx_cycle_t *cycle)
|
0
|
332 {
|
92
|
333 void ***cf;
|
|
334 ngx_event_conf_t *ecf;
|
10
|
335 #if !(NGX_WIN32)
|
92
|
336 char *shared;
|
22
|
337 size_t size;
|
92
|
338 ngx_int_t limit;
|
|
339 struct rlimit rlmt;
|
22
|
340 ngx_core_conf_t *ccf;
|
92
|
341 #endif
|
22
|
342
|
|
343 cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
|
|
344
|
|
345 if (cf == NULL) {
|
|
346 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
347 "no \"events\" section in configuration");
|
|
348 return NGX_ERROR;
|
|
349 }
|
|
350
|
|
351 ecf = (*cf)[ngx_event_core_module.ctx_index];
|
|
352
|
110
|
353 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
22
|
354 "using the \"%s\" event method", ecf->name);
|
|
355
|
92
|
356 #if !(NGX_WIN32)
|
0
|
357
|
|
358 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
359
|
92
|
360 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
|
|
361 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
362 "getrlimit(RLIMIT_NOFILE) failed, ignored");
|
|
363
|
|
364 } else {
|
|
365 if (ecf->connections > (ngx_uint_t) rlmt.rlim_cur
|
|
366 && (ccf->rlimit_nofile == NGX_CONF_UNSET
|
|
367 || ecf->connections > (ngx_uint_t) ccf->rlimit_nofile))
|
|
368 {
|
|
369 limit = (ccf->rlimit_nofile == NGX_CONF_UNSET) ?
|
|
370 (ngx_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
|
|
371
|
|
372 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
|
|
373 "%ui worker_connections are more than "
|
|
374 "open file resource limit: %i",
|
|
375 ecf->connections, limit);
|
|
376 }
|
|
377 }
|
|
378
|
|
379
|
0
|
380 if (ccf->master == 0 || ngx_accept_mutex_ptr) {
|
|
381 return NGX_OK;
|
|
382 }
|
|
383
|
|
384
|
32
|
385 /* TODO: adjust cache line size, 128 is P4 cache line size */
|
0
|
386
|
|
387 size = 128 /* ngx_accept_mutex */
|
|
388 + 128; /* ngx_connection_counter */
|
|
389
|
|
390 #if (NGX_STAT_STUB)
|
|
391
|
|
392 size += 128 /* ngx_stat_accepted */
|
44
|
393 + 128 /* ngx_stat_handled */
|
0
|
394 + 128 /* ngx_stat_requests */
|
|
395 + 128 /* ngx_stat_active */
|
|
396 + 128 /* ngx_stat_reading */
|
|
397 + 128; /* ngx_stat_writing */
|
|
398
|
|
399 #endif
|
|
400
|
50
|
401 shared = ngx_create_shared_memory(size, cycle->log);
|
|
402 if (shared == NULL) {
|
0
|
403 return NGX_ERROR;
|
|
404 }
|
|
405
|
|
406 ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
|
|
407 ngx_connection_counter = (ngx_atomic_t *) (shared + 128);
|
|
408
|
|
409 #if (NGX_STAT_STUB)
|
|
410
|
|
411 ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * 128);
|
44
|
412 ngx_stat_handled = (ngx_atomic_t *) (shared + 3 * 128);
|
|
413 ngx_stat_requests = (ngx_atomic_t *) (shared + 4 * 128);
|
|
414 ngx_stat_active = (ngx_atomic_t *) (shared + 5 * 128);
|
|
415 ngx_stat_reading = (ngx_atomic_t *) (shared + 6 * 128);
|
|
416 ngx_stat_writing = (ngx_atomic_t *) (shared + 7 * 128);
|
0
|
417
|
|
418 #endif
|
|
419
|
|
420 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
421 "counter: %p, %d",
|
0
|
422 ngx_connection_counter, *ngx_connection_counter);
|
|
423
|
92
|
424 #endif /* !(NGX_WIN32) */
|
0
|
425
|
|
426 return NGX_OK;
|
|
427 }
|
|
428
|
|
429
|
58
|
430 static ngx_int_t
|
|
431 ngx_event_process_init(ngx_cycle_t *cycle)
|
0
|
432 {
|
|
433 ngx_uint_t m, i;
|
|
434 ngx_event_t *rev, *wev;
|
92
|
435 ngx_listening_t *ls;
|
|
436 ngx_connection_t *c, *next, *old;
|
0
|
437 ngx_core_conf_t *ccf;
|
|
438 ngx_event_conf_t *ecf;
|
|
439 ngx_event_module_t *module;
|
10
|
440 #if (NGX_WIN32)
|
0
|
441 ngx_iocp_conf_t *iocpcf;
|
92
|
442 #else
|
|
443 struct rlimit rlmt;
|
0
|
444 #endif
|
|
445
|
|
446 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
447 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
|
|
448
|
|
449 if (ngx_accept_mutex_ptr && ccf->worker_processes > 1 && ecf->accept_mutex)
|
|
450 {
|
|
451 ngx_accept_mutex = ngx_accept_mutex_ptr;
|
|
452 ngx_accept_mutex_held = 0;
|
|
453 ngx_accept_mutex_delay = ecf->accept_mutex_delay;
|
|
454 }
|
|
455
|
|
456 #if (NGX_THREADS)
|
50
|
457 ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0);
|
|
458 if (ngx_posted_events_mutex == NULL) {
|
0
|
459 return NGX_ERROR;
|
|
460 }
|
|
461 #endif
|
|
462
|
|
463 if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
|
|
464 return NGX_ERROR;
|
|
465 }
|
|
466
|
|
467 cycle->connection_n = ecf->connections;
|
|
468
|
|
469 for (m = 0; ngx_modules[m]; m++) {
|
|
470 if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
|
|
471 continue;
|
|
472 }
|
|
473
|
|
474 if (ngx_modules[m]->ctx_index == ecf->use) {
|
|
475 module = ngx_modules[m]->ctx;
|
|
476 if (module->actions.init(cycle) == NGX_ERROR) {
|
|
477 /* fatal */
|
|
478 exit(2);
|
|
479 }
|
|
480 break;
|
|
481 }
|
|
482 }
|
|
483
|
92
|
484 #if !(NGX_WIN32)
|
|
485
|
|
486 if (ngx_event_flags & NGX_USE_FD_EVENT) {
|
|
487
|
|
488 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
|
|
489 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
490 "getrlimit(RLIMIT_NOFILE) failed");
|
|
491 return NGX_ERROR;
|
|
492 }
|
|
493
|
|
494 cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
|
|
495
|
|
496 cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
|
|
497 cycle->log);
|
|
498 if (cycle->files == NULL) {
|
|
499 return NGX_ERROR;
|
|
500 }
|
|
501 }
|
|
502
|
|
503 #endif
|
|
504
|
110
|
505 cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
|
0
|
506 cycle->log);
|
110
|
507 if (cycle->connections == NULL) {
|
0
|
508 return NGX_ERROR;
|
|
509 }
|
|
510
|
110
|
511 c = cycle->connections;
|
0
|
512
|
110
|
513 cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
|
0
|
514 cycle->log);
|
110
|
515 if (cycle->read_events == NULL) {
|
0
|
516 return NGX_ERROR;
|
|
517 }
|
|
518
|
110
|
519 rev = cycle->read_events;
|
0
|
520 for (i = 0; i < cycle->connection_n; i++) {
|
|
521 rev[i].closed = 1;
|
30
|
522 rev[i].instance = 1;
|
0
|
523 #if (NGX_THREADS)
|
|
524 rev[i].lock = &c[i].lock;
|
|
525 rev[i].own_lock = &c[i].lock;
|
|
526 #endif
|
|
527 }
|
|
528
|
110
|
529 cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
|
30
|
530 cycle->log);
|
110
|
531 if (cycle->write_events == NULL) {
|
0
|
532 return NGX_ERROR;
|
|
533 }
|
|
534
|
110
|
535 wev = cycle->write_events;
|
0
|
536 for (i = 0; i < cycle->connection_n; i++) {
|
|
537 wev[i].closed = 1;
|
|
538 #if (NGX_THREADS)
|
|
539 wev[i].lock = &c[i].lock;
|
|
540 wev[i].own_lock = &c[i].lock;
|
|
541 #endif
|
|
542 }
|
|
543
|
92
|
544 i = cycle->connection_n;
|
|
545 next = NULL;
|
|
546
|
|
547 do {
|
|
548 i--;
|
|
549
|
|
550 c[i].data = next;
|
110
|
551 c[i].read = &cycle->read_events[i];
|
|
552 c[i].write = &cycle->write_events[i];
|
92
|
553 c[i].fd = (ngx_socket_t) -1;
|
|
554
|
|
555 next = &c[i];
|
|
556
|
|
557 #if (NGX_THREADS)
|
|
558 c[i].lock = 0;
|
|
559 #endif
|
|
560 } while (i);
|
|
561
|
|
562 cycle->free_connections = next;
|
|
563 cycle->free_connection_n = ecf->connections;
|
|
564
|
0
|
565 /* for each listening socket */
|
|
566
|
92
|
567 ls = cycle->listening.elts;
|
0
|
568 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
569
|
92
|
570 c = ngx_get_connection(ls[i].fd, cycle->log);
|
0
|
571
|
92
|
572 if (c == NULL) {
|
|
573 return NGX_ERROR;
|
|
574 }
|
0
|
575
|
92
|
576 c->log = &ls[i].log;
|
|
577
|
|
578 c->listening = &ls[i];
|
|
579 ls[i].connection = c;
|
|
580
|
|
581 c->ctx = ls[i].ctx;
|
|
582 c->servers = ls[i].servers;
|
|
583
|
110
|
584 rev = c->read;
|
0
|
585
|
|
586 rev->log = c->log;
|
|
587 rev->accept = 1;
|
|
588
|
18
|
589 #if (NGX_HAVE_DEFERRED_ACCEPT)
|
92
|
590 rev->deferred_accept = ls[i].deferred_accept;
|
0
|
591 #endif
|
|
592
|
|
593 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
|
92
|
594 if (ls[i].previous) {
|
0
|
595
|
|
596 /*
|
|
597 * delete the old accept events that were bound to
|
|
598 * the old cycle read events array
|
|
599 */
|
|
600
|
92
|
601 old = ls[i].previous->connection;
|
|
602
|
|
603 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
|
|
604 == NGX_ERROR)
|
0
|
605 {
|
|
606 return NGX_ERROR;
|
|
607 }
|
|
608
|
92
|
609 old->fd = (ngx_socket_t) -1;
|
0
|
610 }
|
|
611 }
|
|
612
|
10
|
613 #if (NGX_WIN32)
|
0
|
614
|
|
615 if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
|
58
|
616 rev->handler = ngx_event_acceptex;
|
0
|
617
|
|
618 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
|
|
619 return NGX_ERROR;
|
|
620 }
|
|
621
|
92
|
622 ls[i].log.handler = ngx_acceptex_log_error;
|
|
623
|
0
|
624 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
|
92
|
625 if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
|
|
626 == NGX_ERROR)
|
0
|
627 {
|
|
628 return NGX_ERROR;
|
|
629 }
|
|
630
|
|
631 } else {
|
58
|
632 rev->handler = ngx_event_accept;
|
10
|
633
|
0
|
634 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
635 return NGX_ERROR;
|
|
636 }
|
|
637 }
|
|
638
|
|
639 #else
|
|
640
|
58
|
641 rev->handler = ngx_event_accept;
|
0
|
642
|
|
643 if (ngx_accept_mutex) {
|
|
644 continue;
|
|
645 }
|
|
646
|
|
647 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
|
|
648 if (ngx_add_conn(c) == NGX_ERROR) {
|
|
649 return NGX_ERROR;
|
|
650 }
|
|
651
|
|
652 } else {
|
|
653 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
654 return NGX_ERROR;
|
|
655 }
|
|
656 }
|
|
657
|
|
658 #endif
|
92
|
659
|
0
|
660 }
|
|
661
|
|
662 return NGX_OK;
|
|
663 }
|
|
664
|
|
665
|
58
|
666 ngx_int_t
|
|
667 ngx_send_lowat(ngx_connection_t *c, size_t lowat)
|
2
|
668 {
|
|
669 int sndlowat;
|
|
670
|
4
|
671 #if (NGX_HAVE_LOWAT_EVENT)
|
2
|
672
|
4
|
673 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
2
|
674 c->write->available = lowat;
|
|
675 return NGX_OK;
|
|
676 }
|
|
677
|
|
678 #endif
|
|
679
|
|
680 if (lowat == 0 || c->sndlowat) {
|
|
681 return NGX_OK;
|
|
682 }
|
|
683
|
|
684 sndlowat = (int) lowat;
|
|
685
|
|
686 if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
|
|
687 (const void *) &sndlowat, sizeof(int)) == -1)
|
|
688 {
|
|
689 ngx_connection_error(c, ngx_socket_errno,
|
|
690 "setsockopt(SO_SNDLOWAT) failed");
|
|
691 return NGX_ERROR;
|
|
692 }
|
|
693
|
|
694 c->sndlowat = 1;
|
|
695
|
|
696 return NGX_OK;
|
|
697 }
|
|
698
|
|
699
|
58
|
700 static char *
|
|
701 ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
702 {
|
50
|
703 char *rv;
|
0
|
704 void ***ctx;
|
50
|
705 ngx_uint_t i;
|
0
|
706 ngx_conf_t pcf;
|
50
|
707 ngx_event_module_t *m;
|
0
|
708
|
|
709 /* count the number of the event modules and set up their indices */
|
|
710
|
|
711 ngx_event_max_module = 0;
|
50
|
712 for (i = 0; ngx_modules[i]; i++) {
|
|
713 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
|
0
|
714 continue;
|
|
715 }
|
|
716
|
50
|
717 ngx_modules[i]->ctx_index = ngx_event_max_module++;
|
0
|
718 }
|
|
719
|
50
|
720 ctx = ngx_pcalloc(cf->pool, sizeof(void *));
|
|
721 if (ctx == NULL) {
|
|
722 return NGX_CONF_ERROR;
|
|
723 }
|
0
|
724
|
50
|
725 *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
|
|
726 if (*ctx == NULL) {
|
|
727 return NGX_CONF_ERROR;
|
|
728 }
|
0
|
729
|
|
730 *(void **) conf = ctx;
|
|
731
|
50
|
732 for (i = 0; ngx_modules[i]; i++) {
|
|
733 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
|
0
|
734 continue;
|
|
735 }
|
|
736
|
50
|
737 m = ngx_modules[i]->ctx;
|
0
|
738
|
50
|
739 if (m->create_conf) {
|
|
740 (*ctx)[ngx_modules[i]->ctx_index] = m->create_conf(cf->cycle);
|
|
741 if ((*ctx)[ngx_modules[i]->ctx_index] == NULL) {
|
|
742 return NGX_CONF_ERROR;
|
|
743 }
|
0
|
744 }
|
|
745 }
|
|
746
|
|
747 pcf = *cf;
|
|
748 cf->ctx = ctx;
|
|
749 cf->module_type = NGX_EVENT_MODULE;
|
|
750 cf->cmd_type = NGX_EVENT_CONF;
|
50
|
751
|
0
|
752 rv = ngx_conf_parse(cf, NULL);
|
50
|
753
|
0
|
754 *cf = pcf;
|
|
755
|
|
756 if (rv != NGX_CONF_OK)
|
|
757 return rv;
|
|
758
|
50
|
759 for (i = 0; ngx_modules[i]; i++) {
|
|
760 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
|
0
|
761 continue;
|
|
762 }
|
|
763
|
50
|
764 m = ngx_modules[i]->ctx;
|
0
|
765
|
50
|
766 if (m->init_conf) {
|
|
767 rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);
|
0
|
768 if (rv != NGX_CONF_OK) {
|
|
769 return rv;
|
|
770 }
|
|
771 }
|
|
772 }
|
|
773
|
|
774 return NGX_CONF_OK;
|
|
775 }
|
|
776
|
|
777
|
58
|
778 static char *
|
|
779 ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
780 {
|
|
781 ngx_event_conf_t *ecf = conf;
|
|
782
|
|
783 ngx_str_t *value;
|
|
784
|
|
785 if (ecf->connections != NGX_CONF_UNSET_UINT) {
|
|
786 return "is duplicate" ;
|
|
787 }
|
|
788
|
|
789 value = cf->args->elts;
|
|
790 ecf->connections = ngx_atoi(value[1].data, value[1].len);
|
|
791 if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
|
|
792 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
793 "invalid number \"%V\"", &value[1]);
|
0
|
794
|
|
795 return NGX_CONF_ERROR;
|
|
796 }
|
|
797
|
|
798 cf->cycle->connection_n = ecf->connections;
|
|
799
|
|
800 return NGX_CONF_OK;
|
|
801 }
|
|
802
|
|
803
|
58
|
804 static char *
|
|
805 ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
806 {
|
|
807 ngx_event_conf_t *ecf = conf;
|
|
808
|
|
809 ngx_int_t m;
|
|
810 ngx_str_t *value;
|
|
811 ngx_event_conf_t *old_ecf;
|
|
812 ngx_event_module_t *module;
|
|
813
|
|
814 if (ecf->use != NGX_CONF_UNSET_UINT) {
|
|
815 return "is duplicate" ;
|
|
816 }
|
|
817
|
|
818 value = cf->args->elts;
|
|
819
|
|
820 if (cf->cycle->old_cycle->conf_ctx) {
|
|
821 old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
|
|
822 ngx_event_core_module);
|
|
823 } else {
|
|
824 old_ecf = NULL;
|
|
825 }
|
|
826
|
|
827
|
|
828 for (m = 0; ngx_modules[m]; m++) {
|
|
829 if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
|
|
830 continue;
|
|
831 }
|
|
832
|
|
833 module = ngx_modules[m]->ctx;
|
|
834 if (module->name->len == value[1].len) {
|
|
835 if (ngx_strcmp(module->name->data, value[1].data) == 0) {
|
|
836 ecf->use = ngx_modules[m]->ctx_index;
|
|
837 ecf->name = module->name->data;
|
|
838
|
|
839 if (ngx_process == NGX_PROCESS_SINGLE
|
|
840 && old_ecf
|
|
841 && old_ecf->use != ecf->use)
|
|
842 {
|
|
843 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
16
|
844 "when the server runs without a master process "
|
|
845 "the \"%V\" event type must be the same as "
|
|
846 "in previous configuration - \"%s\" "
|
|
847 "and it can not be changed on the fly, "
|
|
848 "to change it you need to stop server "
|
|
849 "and start it again",
|
|
850 &value[1], old_ecf->name);
|
0
|
851
|
|
852 return NGX_CONF_ERROR;
|
|
853 }
|
|
854
|
|
855 return NGX_CONF_OK;
|
|
856 }
|
|
857 }
|
|
858 }
|
|
859
|
|
860 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
861 "invalid event type \"%V\"", &value[1]);
|
0
|
862
|
|
863 return NGX_CONF_ERROR;
|
|
864 }
|
|
865
|
|
866
|
58
|
867 static char *
|
|
868 ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
869 {
|
|
870 #if (NGX_DEBUG)
|
|
871 ngx_event_conf_t *ecf = conf;
|
|
872
|
|
873 in_addr_t *addr;
|
|
874 ngx_str_t *value;
|
|
875 struct hostent *h;
|
|
876
|
|
877 value = cf->args->elts;
|
|
878
|
|
879 /* AF_INET only */
|
|
880
|
50
|
881 addr = ngx_array_push(&ecf->debug_connection);
|
|
882 if (addr == NULL) {
|
0
|
883 return NGX_CONF_ERROR;
|
|
884 }
|
|
885
|
|
886 *addr = inet_addr((char *) value[1].data);
|
|
887
|
|
888 if (*addr != INADDR_NONE) {
|
|
889 return NGX_OK;
|
|
890 }
|
|
891
|
|
892 h = gethostbyname((char *) value[1].data);
|
|
893
|
|
894 if (h == NULL || h->h_addr_list[0] == NULL) {
|
|
895 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
896 "host \"%s\" not found", value[1].data);
|
0
|
897 return NGX_CONF_ERROR;
|
|
898 }
|
|
899
|
|
900 *addr = *(in_addr_t *)(h->h_addr_list[0]);
|
|
901
|
|
902 #else
|
|
903
|
|
904 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
905 "\"debug_connection\" is ignored, you need to rebuild "
|
|
906 "nginx using --with-debug option to enable it");
|
|
907
|
|
908 #endif
|
|
909
|
|
910 return NGX_OK;
|
|
911 }
|
|
912
|
|
913
|
58
|
914 static void *
|
|
915 ngx_event_create_conf(ngx_cycle_t *cycle)
|
0
|
916 {
|
|
917 ngx_event_conf_t *ecf;
|
|
918
|
50
|
919 ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
|
|
920 if (ecf == NULL) {
|
|
921 return NGX_CONF_ERROR;
|
|
922 }
|
0
|
923
|
|
924 ecf->connections = NGX_CONF_UNSET_UINT;
|
|
925 ecf->use = NGX_CONF_UNSET_UINT;
|
|
926 ecf->multi_accept = NGX_CONF_UNSET;
|
|
927 ecf->accept_mutex = NGX_CONF_UNSET;
|
|
928 ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
|
|
929 ecf->name = (void *) NGX_CONF_UNSET;
|
|
930
|
|
931 #if (NGX_DEBUG)
|
50
|
932
|
|
933 if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
|
|
934 sizeof(in_addr_t)) == NGX_ERROR)
|
|
935 {
|
|
936 return NGX_CONF_ERROR;
|
|
937 }
|
|
938
|
0
|
939 #endif
|
|
940
|
|
941 return ecf;
|
|
942 }
|
|
943
|
|
944
|
58
|
945 static char *
|
|
946 ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
947 {
|
|
948 ngx_event_conf_t *ecf = conf;
|
16
|
949
|
50
|
950 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
|
|
951 int fd;
|
|
952 #endif
|
|
953 #if (NGX_HAVE_RTSIG)
|
|
954 ngx_uint_t rtsig;
|
|
955 #endif
|
16
|
956 ngx_int_t i, connections;
|
|
957 ngx_module_t *module;
|
|
958 ngx_core_conf_t *ccf;
|
|
959 ngx_event_module_t *event_module;
|
|
960
|
|
961 connections = NGX_CONF_UNSET_UINT;
|
|
962 module = NULL;
|
|
963
|
18
|
964 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
|
16
|
965
|
|
966 fd = epoll_create(100);
|
|
967
|
|
968 if (fd != -1) {
|
|
969 close(fd);
|
|
970 connections = DEFAULT_CONNECTIONS;
|
|
971 module = &ngx_epoll_module;
|
|
972
|
|
973 } else if (ngx_errno != NGX_ENOSYS) {
|
|
974 connections = DEFAULT_CONNECTIONS;
|
|
975 module = &ngx_epoll_module;
|
|
976 }
|
|
977
|
|
978 #endif
|
|
979
|
18
|
980 #if (NGX_HAVE_RTSIG)
|
16
|
981
|
|
982 if (module == NULL) {
|
|
983 connections = DEFAULT_CONNECTIONS;
|
|
984 module = &ngx_rtsig_module;
|
|
985 rtsig = 1;
|
50
|
986
|
|
987 } else {
|
|
988 rtsig = 0;
|
16
|
989 }
|
|
990
|
|
991 #endif
|
|
992
|
18
|
993 #if (NGX_HAVE_DEVPOLL)
|
16
|
994
|
|
995 connections = DEFAULT_CONNECTIONS;
|
|
996 module = &ngx_devpoll_module;
|
|
997
|
0
|
998 #endif
|
|
999
|
18
|
1000 #if (NGX_HAVE_KQUEUE)
|
0
|
1001
|
16
|
1002 connections = DEFAULT_CONNECTIONS;
|
|
1003 module = &ngx_kqueue_module;
|
0
|
1004
|
16
|
1005 #endif
|
0
|
1006
|
18
|
1007 #if (NGX_HAVE_SELECT)
|
0
|
1008
|
16
|
1009 if (module == NULL) {
|
0
|
1010
|
50
|
1011 #if (NGX_WIN32 || FD_SETSIZE >= DEFAULT_CONNECTIONS)
|
16
|
1012 connections = DEFAULT_CONNECTIONS;
|
0
|
1013 #else
|
50
|
1014 connections = FD_SETSIZE;
|
16
|
1015 #endif
|
|
1016 module = &ngx_select_module;
|
|
1017 }
|
|
1018
|
0
|
1019 #endif
|
|
1020
|
16
|
1021 if (module == NULL) {
|
|
1022 for (i = 0; ngx_modules[i]; i++) {
|
|
1023 if (ngx_modules[i]->type == NGX_EVENT_MODULE) {
|
|
1024 event_module = ngx_modules[i]->ctx;
|
0
|
1025
|
16
|
1026 if (ngx_strcmp(event_module->name->data, event_core_name.data)
|
|
1027 == 0)
|
|
1028 {
|
|
1029 continue;
|
|
1030 }
|
0
|
1031
|
16
|
1032 module = ngx_modules[i];
|
|
1033 break;
|
0
|
1034 }
|
|
1035 }
|
|
1036 }
|
|
1037
|
16
|
1038 if (module == NULL) {
|
0
|
1039 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
|
|
1040 return NGX_CONF_ERROR;
|
|
1041 }
|
|
1042
|
16
|
1043 ngx_conf_init_unsigned_value(ecf->connections, connections);
|
|
1044 cycle->connection_n = ecf->connections;
|
0
|
1045
|
16
|
1046 ngx_conf_init_unsigned_value(ecf->use, module->ctx_index);
|
0
|
1047
|
16
|
1048 event_module = module->ctx;
|
|
1049 ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
|
0
|
1050
|
|
1051 ngx_conf_init_value(ecf->multi_accept, 0);
|
|
1052 ngx_conf_init_value(ecf->accept_mutex, 1);
|
|
1053 ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
|
|
1054
|
16
|
1055
|
50
|
1056 #if (NGX_HAVE_RTSIG)
|
|
1057
|
|
1058 if (!rtsig) {
|
|
1059 return NGX_CONF_OK;
|
|
1060 }
|
|
1061
|
|
1062 #endif
|
|
1063
|
|
1064 if (ecf->accept_mutex) {
|
16
|
1065 return NGX_CONF_OK;
|
0
|
1066 }
|
16
|
1067
|
|
1068 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
0
|
1069
|
16
|
1070 if (ccf->worker_processes == 0) {
|
|
1071 return NGX_CONF_OK;
|
|
1072 }
|
|
1073
|
|
1074 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
1075 "the \"rtsig\" method requires \"accept_mutex\" to be on");
|
|
1076
|
|
1077 return NGX_CONF_ERROR;
|
0
|
1078 }
|
|
1079
|
|
1080
|
58
|
1081 static char *
|
|
1082 ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data)
|
0
|
1083 {
|
|
1084 #if !(NGX_HAVE_ATOMIC_OPS)
|
|
1085
|
|
1086 ngx_flag_t *fp = data;
|
|
1087
|
|
1088 *fp = 0;
|
|
1089
|
|
1090 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
52
|
1091 "\"accept_mutex\" is not supported on this platform, "
|
|
1092 "ignored");
|
0
|
1093
|
|
1094 #endif
|
|
1095
|
|
1096 return NGX_CONF_OK;
|
|
1097 }
|