Mercurial > hg > nginx-vendor-1-0
comparison src/event/ngx_event.c @ 0:f0b350454894 NGINX_0_1_0
nginx 0.1.0
*) The first public version.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Mon, 04 Oct 2004 00:00:00 +0400 |
parents | |
children | cc9f381affaa |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0b350454894 |
---|---|
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 | |
15 extern ngx_module_t ngx_select_module; | |
16 extern ngx_event_module_t ngx_select_module_ctx; | |
17 | |
18 #if (HAVE_KQUEUE) | |
19 #include <ngx_kqueue_module.h> | |
20 #endif | |
21 | |
22 #if (HAVE_DEVPOLL) | |
23 extern ngx_module_t ngx_devpoll_module; | |
24 extern ngx_event_module_t ngx_devpoll_module_ctx; | |
25 #endif | |
26 | |
27 #if (HAVE_EPOLL) | |
28 extern ngx_module_t ngx_epoll_module; | |
29 extern ngx_event_module_t ngx_epoll_module_ctx; | |
30 #endif | |
31 | |
32 #if (HAVE_RTSIG) | |
33 extern ngx_module_t ngx_rtsig_module; | |
34 extern ngx_event_module_t ngx_rtsig_module_ctx; | |
35 #endif | |
36 | |
37 #if (HAVE_AIO) | |
38 #include <ngx_aio_module.h> | |
39 #endif | |
40 | |
41 static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle); | |
42 static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle); | |
43 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
44 | |
45 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, | |
46 void *conf); | |
47 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
48 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, | |
49 void *conf); | |
50 | |
51 static void *ngx_event_create_conf(ngx_cycle_t *cycle); | |
52 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf); | |
53 static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data); | |
54 | |
55 | |
56 static ngx_uint_t ngx_event_max_module; | |
57 | |
58 ngx_uint_t ngx_event_flags; | |
59 ngx_event_actions_t ngx_event_actions; | |
60 | |
61 | |
62 ngx_atomic_t connection_counter; | |
63 ngx_atomic_t *ngx_connection_counter = &connection_counter; | |
64 | |
65 | |
66 ngx_atomic_t *ngx_accept_mutex_ptr; | |
67 ngx_atomic_t *ngx_accept_mutex; | |
68 ngx_uint_t ngx_accept_mutex_held; | |
69 ngx_msec_t ngx_accept_mutex_delay; | |
70 ngx_int_t ngx_accept_disabled; | |
71 | |
72 | |
73 #if (NGX_STAT_STUB) | |
74 | |
75 ngx_atomic_t ngx_stat_accepted0; | |
76 ngx_atomic_t *ngx_stat_accepted = &ngx_stat_accepted0; | |
77 ngx_atomic_t ngx_stat_requests0; | |
78 ngx_atomic_t *ngx_stat_requests = &ngx_stat_requests0; | |
79 ngx_atomic_t ngx_stat_active0; | |
80 ngx_atomic_t *ngx_stat_active = &ngx_stat_active0; | |
81 ngx_atomic_t ngx_stat_reading0; | |
82 ngx_atomic_t *ngx_stat_reading = &ngx_stat_reading0; | |
83 ngx_atomic_t ngx_stat_writing0; | |
84 ngx_atomic_t *ngx_stat_writing = &ngx_stat_reading0; | |
85 | |
86 #endif | |
87 | |
88 | |
89 | |
90 static ngx_command_t ngx_events_commands[] = { | |
91 | |
92 { ngx_string("events"), | |
93 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | |
94 ngx_events_block, | |
95 0, | |
96 0, | |
97 NULL }, | |
98 | |
99 ngx_null_command | |
100 }; | |
101 | |
102 | |
103 static ngx_core_module_t ngx_events_module_ctx = { | |
104 ngx_string("events"), | |
105 NULL, | |
106 NULL | |
107 }; | |
108 | |
109 | |
110 ngx_module_t ngx_events_module = { | |
111 NGX_MODULE, | |
112 &ngx_events_module_ctx, /* module context */ | |
113 ngx_events_commands, /* module directives */ | |
114 NGX_CORE_MODULE, /* module type */ | |
115 NULL, /* init module */ | |
116 NULL /* init process */ | |
117 }; | |
118 | |
119 | |
120 static ngx_str_t event_core_name = ngx_string("event_core"); | |
121 | |
122 static ngx_conf_post_t ngx_accept_mutex_post = { ngx_accept_mutex_check } ; | |
123 | |
124 | |
125 static ngx_command_t ngx_event_core_commands[] = { | |
126 | |
127 { ngx_string("connections"), | |
128 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
129 ngx_event_connections, | |
130 0, | |
131 0, | |
132 NULL }, | |
133 | |
134 { ngx_string("use"), | |
135 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
136 ngx_event_use, | |
137 0, | |
138 0, | |
139 NULL }, | |
140 | |
141 { ngx_string("multi_accept"), | |
142 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
143 ngx_conf_set_flag_slot, | |
144 0, | |
145 offsetof(ngx_event_conf_t, multi_accept), | |
146 NULL }, | |
147 | |
148 { ngx_string("accept_mutex"), | |
149 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
150 ngx_conf_set_flag_slot, | |
151 0, | |
152 offsetof(ngx_event_conf_t, accept_mutex), | |
153 &ngx_accept_mutex_post }, | |
154 | |
155 { ngx_string("accept_mutex_delay"), | |
156 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
157 ngx_conf_set_msec_slot, | |
158 0, | |
159 offsetof(ngx_event_conf_t, accept_mutex_delay), | |
160 NULL }, | |
161 | |
162 { ngx_string("debug_connection"), | |
163 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
164 ngx_event_debug_connection, | |
165 0, | |
166 0, | |
167 NULL }, | |
168 | |
169 ngx_null_command | |
170 }; | |
171 | |
172 | |
173 ngx_event_module_t ngx_event_core_module_ctx = { | |
174 &event_core_name, | |
175 ngx_event_create_conf, /* create configuration */ | |
176 ngx_event_init_conf, /* init configuration */ | |
177 | |
178 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } | |
179 }; | |
180 | |
181 | |
182 ngx_module_t ngx_event_core_module = { | |
183 NGX_MODULE, | |
184 &ngx_event_core_module_ctx, /* module context */ | |
185 ngx_event_core_commands, /* module directives */ | |
186 NGX_EVENT_MODULE, /* module type */ | |
187 ngx_event_module_init, /* init module */ | |
188 ngx_event_process_init /* init process */ | |
189 }; | |
190 | |
191 | |
192 static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle) | |
193 { | |
194 #if !(WIN32) | |
195 | |
196 size_t size; | |
197 char *shared; | |
198 ngx_core_conf_t *ccf; | |
199 ngx_event_conf_t *ecf; | |
200 | |
201 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | |
202 | |
203 if (ccf->master == 0 || ngx_accept_mutex_ptr) { | |
204 return NGX_OK; | |
205 } | |
206 | |
207 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); | |
208 | |
209 | |
210 /* TODO: 128 is cache line size */ | |
211 | |
212 size = 128 /* ngx_accept_mutex */ | |
213 + 128; /* ngx_connection_counter */ | |
214 | |
215 #if (NGX_STAT_STUB) | |
216 | |
217 size += 128 /* ngx_stat_accepted */ | |
218 + 128 /* ngx_stat_requests */ | |
219 + 128 /* ngx_stat_active */ | |
220 + 128 /* ngx_stat_reading */ | |
221 + 128; /* ngx_stat_writing */ | |
222 | |
223 #endif | |
224 | |
225 if (!(shared = ngx_create_shared_memory(size, cycle->log))) { | |
226 return NGX_ERROR; | |
227 } | |
228 | |
229 ngx_accept_mutex_ptr = (ngx_atomic_t *) shared; | |
230 ngx_connection_counter = (ngx_atomic_t *) (shared + 128); | |
231 | |
232 #if (NGX_STAT_STUB) | |
233 | |
234 ngx_stat_accepted = (ngx_atomic_t *) (shared + 2 * 128); | |
235 ngx_stat_requests = (ngx_atomic_t *) (shared + 3 * 128); | |
236 ngx_stat_active = (ngx_atomic_t *) (shared + 4 * 128); | |
237 ngx_stat_reading = (ngx_atomic_t *) (shared + 5 * 128); | |
238 ngx_stat_writing = (ngx_atomic_t *) (shared + 6 * 128); | |
239 | |
240 #endif | |
241 | |
242 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
243 "counter: " PTR_FMT ", %d", | |
244 ngx_connection_counter, *ngx_connection_counter); | |
245 | |
246 #endif | |
247 | |
248 return NGX_OK; | |
249 } | |
250 | |
251 | |
252 static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle) | |
253 { | |
254 ngx_uint_t m, i; | |
255 ngx_socket_t fd; | |
256 ngx_event_t *rev, *wev; | |
257 ngx_listening_t *s; | |
258 ngx_connection_t *c; | |
259 ngx_core_conf_t *ccf; | |
260 ngx_event_conf_t *ecf; | |
261 ngx_event_module_t *module; | |
262 #if (WIN32) | |
263 ngx_iocp_conf_t *iocpcf; | |
264 #endif | |
265 | |
266 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | |
267 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); | |
268 | |
269 if (ngx_accept_mutex_ptr && ccf->worker_processes > 1 && ecf->accept_mutex) | |
270 { | |
271 ngx_accept_mutex = ngx_accept_mutex_ptr; | |
272 ngx_accept_mutex_held = 0; | |
273 ngx_accept_mutex_delay = ecf->accept_mutex_delay; | |
274 } | |
275 | |
276 #if (NGX_THREADS) | |
277 if (!(ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0))) { | |
278 return NGX_ERROR; | |
279 } | |
280 #endif | |
281 | |
282 if (ngx_event_timer_init(cycle->log) == NGX_ERROR) { | |
283 return NGX_ERROR; | |
284 } | |
285 | |
286 cycle->connection_n = ecf->connections; | |
287 | |
288 for (m = 0; ngx_modules[m]; m++) { | |
289 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | |
290 continue; | |
291 } | |
292 | |
293 if (ngx_modules[m]->ctx_index == ecf->use) { | |
294 module = ngx_modules[m]->ctx; | |
295 if (module->actions.init(cycle) == NGX_ERROR) { | |
296 /* fatal */ | |
297 exit(2); | |
298 } | |
299 break; | |
300 } | |
301 } | |
302 | |
303 cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, | |
304 cycle->log); | |
305 if (cycle->connections == NULL) { | |
306 return NGX_ERROR; | |
307 } | |
308 | |
309 c = cycle->connections; | |
310 for (i = 0; i < cycle->connection_n; i++) { | |
311 c[i].fd = (ngx_socket_t) -1; | |
312 c[i].data = NULL; | |
313 #if (NGX_THREADS) | |
314 c[i].lock = 0; | |
315 #endif | |
316 } | |
317 | |
318 cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections, | |
319 cycle->log); | |
320 if (cycle->read_events == NULL) { | |
321 return NGX_ERROR; | |
322 } | |
323 | |
324 rev = cycle->read_events; | |
325 for (i = 0; i < cycle->connection_n; i++) { | |
326 rev[i].closed = 1; | |
327 #if (NGX_THREADS) | |
328 rev[i].lock = &c[i].lock; | |
329 rev[i].own_lock = &c[i].lock; | |
330 #endif | |
331 } | |
332 | |
333 cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections, | |
334 cycle->log); | |
335 if (cycle->write_events == NULL) { | |
336 return NGX_ERROR; | |
337 } | |
338 | |
339 wev = cycle->write_events; | |
340 for (i = 0; i < cycle->connection_n; i++) { | |
341 wev[i].closed = 1; | |
342 #if (NGX_THREADS) | |
343 wev[i].lock = &c[i].lock; | |
344 wev[i].own_lock = &c[i].lock; | |
345 #endif | |
346 } | |
347 | |
348 /* for each listening socket */ | |
349 | |
350 s = cycle->listening.elts; | |
351 for (i = 0; i < cycle->listening.nelts; i++) { | |
352 | |
353 fd = s[i].fd; | |
354 | |
355 #if (WIN32) | |
356 /* | |
357 * Winsock assignes a socket number divisible by 4 | |
358 * so to find a connection we divide a socket number by 4. | |
359 */ | |
360 | |
361 fd /= 4; | |
362 #endif | |
363 | |
364 c = &cycle->connections[fd]; | |
365 rev = &cycle->read_events[fd]; | |
366 wev = &cycle->write_events[fd]; | |
367 | |
368 ngx_memzero(c, sizeof(ngx_connection_t)); | |
369 ngx_memzero(rev, sizeof(ngx_event_t)); | |
370 | |
371 c->fd = s[i].fd; | |
372 c->listening = &s[i]; | |
373 | |
374 c->ctx = s[i].ctx; | |
375 c->servers = s[i].servers; | |
376 c->log = s[i].log; | |
377 c->read = rev; | |
378 | |
379 /* required by iocp in "c->write->active = 1" */ | |
380 c->write = wev; | |
381 | |
382 /* required by poll */ | |
383 wev->index = NGX_INVALID_INDEX; | |
384 | |
385 rev->log = c->log; | |
386 rev->data = c; | |
387 rev->index = NGX_INVALID_INDEX; | |
388 | |
389 rev->available = 0; | |
390 | |
391 rev->accept = 1; | |
392 | |
393 #if (HAVE_DEFERRED_ACCEPT) | |
394 rev->deferred_accept = s[i].deferred_accept; | |
395 #endif | |
396 | |
397 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) { | |
398 if (s[i].remain) { | |
399 | |
400 /* | |
401 * delete the old accept events that were bound to | |
402 * the old cycle read events array | |
403 */ | |
404 | |
405 if (ngx_del_event(&cycle->old_cycle->read_events[fd], | |
406 NGX_READ_EVENT, NGX_CLOSE_EVENT) == NGX_ERROR) | |
407 { | |
408 return NGX_ERROR; | |
409 } | |
410 | |
411 cycle->old_cycle->connections[fd].fd = (ngx_socket_t) -1; | |
412 } | |
413 } | |
414 | |
415 #if (WIN32) | |
416 | |
417 if (ngx_event_flags & NGX_USE_IOCP_EVENT) { | |
418 rev->event_handler = &ngx_event_acceptex; | |
419 | |
420 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) { | |
421 return NGX_ERROR; | |
422 } | |
423 | |
424 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module); | |
425 if (ngx_event_post_acceptex(&s[i], iocpcf->post_acceptex) | |
426 == NGX_ERROR) | |
427 { | |
428 return NGX_ERROR; | |
429 } | |
430 | |
431 } else { | |
432 rev->event_handler = &ngx_event_accept; | |
433 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) { | |
434 return NGX_ERROR; | |
435 } | |
436 } | |
437 | |
438 #else | |
439 | |
440 rev->event_handler = &ngx_event_accept; | |
441 | |
442 if (ngx_accept_mutex) { | |
443 continue; | |
444 } | |
445 | |
446 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) { | |
447 if (ngx_add_conn(c) == NGX_ERROR) { | |
448 return NGX_ERROR; | |
449 } | |
450 | |
451 } else { | |
452 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) { | |
453 return NGX_ERROR; | |
454 } | |
455 } | |
456 | |
457 #endif | |
458 } | |
459 | |
460 return NGX_OK; | |
461 } | |
462 | |
463 | |
464 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
465 { | |
466 int m; | |
467 char *rv; | |
468 void ***ctx; | |
469 ngx_conf_t pcf; | |
470 ngx_event_module_t *module; | |
471 | |
472 /* count the number of the event modules and set up their indices */ | |
473 | |
474 ngx_event_max_module = 0; | |
475 for (m = 0; ngx_modules[m]; m++) { | |
476 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | |
477 continue; | |
478 } | |
479 | |
480 ngx_modules[m]->ctx_index = ngx_event_max_module++; | |
481 } | |
482 | |
483 ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR); | |
484 | |
485 ngx_test_null(*ctx, | |
486 ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)), | |
487 NGX_CONF_ERROR); | |
488 | |
489 *(void **) conf = ctx; | |
490 | |
491 for (m = 0; ngx_modules[m]; m++) { | |
492 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | |
493 continue; | |
494 } | |
495 | |
496 module = ngx_modules[m]->ctx; | |
497 | |
498 if (module->create_conf) { | |
499 ngx_test_null((*ctx)[ngx_modules[m]->ctx_index], | |
500 module->create_conf(cf->cycle), | |
501 NGX_CONF_ERROR); | |
502 } | |
503 } | |
504 | |
505 pcf = *cf; | |
506 cf->ctx = ctx; | |
507 cf->module_type = NGX_EVENT_MODULE; | |
508 cf->cmd_type = NGX_EVENT_CONF; | |
509 rv = ngx_conf_parse(cf, NULL); | |
510 *cf = pcf; | |
511 | |
512 if (rv != NGX_CONF_OK) | |
513 return rv; | |
514 | |
515 for (m = 0; ngx_modules[m]; m++) { | |
516 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | |
517 continue; | |
518 } | |
519 | |
520 module = ngx_modules[m]->ctx; | |
521 | |
522 if (module->init_conf) { | |
523 rv = module->init_conf(cf->cycle, | |
524 (*ctx)[ngx_modules[m]->ctx_index]); | |
525 if (rv != NGX_CONF_OK) { | |
526 return rv; | |
527 } | |
528 } | |
529 } | |
530 | |
531 return NGX_CONF_OK; | |
532 } | |
533 | |
534 | |
535 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, | |
536 void *conf) | |
537 { | |
538 ngx_event_conf_t *ecf = conf; | |
539 | |
540 ngx_str_t *value; | |
541 | |
542 if (ecf->connections != NGX_CONF_UNSET_UINT) { | |
543 return "is duplicate" ; | |
544 } | |
545 | |
546 value = cf->args->elts; | |
547 ecf->connections = ngx_atoi(value[1].data, value[1].len); | |
548 if (ecf->connections == (ngx_uint_t) NGX_ERROR) { | |
549 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
550 "invalid number \"%s\"", value[1].data); | |
551 | |
552 return NGX_CONF_ERROR; | |
553 } | |
554 | |
555 cf->cycle->connection_n = ecf->connections; | |
556 | |
557 return NGX_CONF_OK; | |
558 } | |
559 | |
560 | |
561 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
562 { | |
563 ngx_event_conf_t *ecf = conf; | |
564 | |
565 ngx_int_t m; | |
566 ngx_str_t *value; | |
567 ngx_event_conf_t *old_ecf; | |
568 ngx_event_module_t *module; | |
569 | |
570 if (ecf->use != NGX_CONF_UNSET_UINT) { | |
571 return "is duplicate" ; | |
572 } | |
573 | |
574 value = cf->args->elts; | |
575 | |
576 if (cf->cycle->old_cycle->conf_ctx) { | |
577 old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx, | |
578 ngx_event_core_module); | |
579 } else { | |
580 old_ecf = NULL; | |
581 } | |
582 | |
583 | |
584 for (m = 0; ngx_modules[m]; m++) { | |
585 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | |
586 continue; | |
587 } | |
588 | |
589 module = ngx_modules[m]->ctx; | |
590 if (module->name->len == value[1].len) { | |
591 if (ngx_strcmp(module->name->data, value[1].data) == 0) { | |
592 ecf->use = ngx_modules[m]->ctx_index; | |
593 ecf->name = module->name->data; | |
594 | |
595 if (ngx_process == NGX_PROCESS_SINGLE | |
596 && old_ecf | |
597 && old_ecf->use != ecf->use) | |
598 { | |
599 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
600 "when the server runs without a master process " | |
601 "the \"%s\" event type must be the same as " | |
602 "in previous configuration - \"%s\" " | |
603 "and it can not be changed on the fly, " | |
604 "to change it you need to stop server " | |
605 "and start it again", | |
606 value[1].data, old_ecf->name); | |
607 | |
608 return NGX_CONF_ERROR; | |
609 } | |
610 | |
611 return NGX_CONF_OK; | |
612 } | |
613 } | |
614 } | |
615 | |
616 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
617 "invalid event type \"%s\"", value[1].data); | |
618 | |
619 return NGX_CONF_ERROR; | |
620 } | |
621 | |
622 | |
623 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, | |
624 void *conf) | |
625 { | |
626 #if (NGX_DEBUG) | |
627 ngx_event_conf_t *ecf = conf; | |
628 | |
629 in_addr_t *addr; | |
630 ngx_str_t *value; | |
631 struct hostent *h; | |
632 | |
633 value = cf->args->elts; | |
634 | |
635 /* AF_INET only */ | |
636 | |
637 if (!(addr = ngx_push_array(&ecf->debug_connection))) { | |
638 return NGX_CONF_ERROR; | |
639 } | |
640 | |
641 *addr = inet_addr((char *) value[1].data); | |
642 | |
643 if (*addr != INADDR_NONE) { | |
644 return NGX_OK; | |
645 } | |
646 | |
647 h = gethostbyname((char *) value[1].data); | |
648 | |
649 if (h == NULL || h->h_addr_list[0] == NULL) { | |
650 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
651 "host %s not found", value[1].data); | |
652 return NGX_CONF_ERROR; | |
653 } | |
654 | |
655 *addr = *(in_addr_t *)(h->h_addr_list[0]); | |
656 | |
657 #else | |
658 | |
659 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
660 "\"debug_connection\" is ignored, you need to rebuild " | |
661 "nginx using --with-debug option to enable it"); | |
662 | |
663 #endif | |
664 | |
665 return NGX_OK; | |
666 } | |
667 | |
668 | |
669 static void *ngx_event_create_conf(ngx_cycle_t *cycle) | |
670 { | |
671 ngx_event_conf_t *ecf; | |
672 | |
673 ngx_test_null(ecf, ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t)), | |
674 NGX_CONF_ERROR); | |
675 | |
676 ecf->connections = NGX_CONF_UNSET_UINT; | |
677 ecf->use = NGX_CONF_UNSET_UINT; | |
678 ecf->multi_accept = NGX_CONF_UNSET; | |
679 ecf->accept_mutex = NGX_CONF_UNSET; | |
680 ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC; | |
681 ecf->name = (void *) NGX_CONF_UNSET; | |
682 | |
683 #if (NGX_DEBUG) | |
684 ngx_init_array(ecf->debug_connection, cycle->pool, 5, sizeof(in_addr_t), | |
685 NGX_CONF_ERROR); | |
686 #endif | |
687 | |
688 return ecf; | |
689 } | |
690 | |
691 | |
692 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf) | |
693 { | |
694 ngx_event_conf_t *ecf = conf; | |
695 #if (HAVE_RTSIG) | |
696 ngx_core_conf_t *ccf; | |
697 #endif | |
698 | |
699 #if (HAVE_KQUEUE) | |
700 | |
701 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
702 ngx_conf_init_unsigned_value(ecf->use, ngx_kqueue_module.ctx_index); | |
703 ngx_conf_init_ptr_value(ecf->name, ngx_kqueue_module_ctx.name->data); | |
704 | |
705 #elif (HAVE_DEVPOLL) | |
706 | |
707 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
708 ngx_conf_init_unsigned_value(ecf->use, ngx_devpoll_module.ctx_index); | |
709 ngx_conf_init_ptr_value(ecf->name, ngx_devpoll_module_ctx.name->data); | |
710 | |
711 #elif (HAVE_EPOLL) | |
712 | |
713 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
714 ngx_conf_init_unsigned_value(ecf->use, ngx_epoll_module.ctx_index); | |
715 ngx_conf_init_ptr_value(ecf->name, ngx_epoll_module_ctx.name->data); | |
716 | |
717 #elif (HAVE_RTSIG) | |
718 | |
719 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
720 ngx_conf_init_unsigned_value(ecf->use, ngx_rtsig_module.ctx_index); | |
721 ngx_conf_init_ptr_value(ecf->name, ngx_rtsig_module_ctx.name->data); | |
722 | |
723 #elif (HAVE_SELECT) | |
724 | |
725 #if (WIN32) | |
726 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
727 #else | |
728 ngx_conf_init_unsigned_value(ecf->connections, | |
729 FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS); | |
730 #endif | |
731 | |
732 ngx_conf_init_unsigned_value(ecf->use, ngx_select_module.ctx_index); | |
733 ngx_conf_init_ptr_value(ecf->name, ngx_select_module_ctx.name->data); | |
734 | |
735 #else | |
736 | |
737 ngx_int_t i, m; | |
738 ngx_event_module_t *module; | |
739 | |
740 m = -1; | |
741 module = NULL; | |
742 | |
743 for (i = 0; ngx_modules[i]; i++) { | |
744 if (ngx_modules[i]->type == NGX_EVENT_MODULE) { | |
745 module = ngx_modules[i]->ctx; | |
746 | |
747 if (ngx_strcmp(module->name->data, event_core_name.data) == 0) { | |
748 continue; | |
749 } | |
750 | |
751 m = ngx_modules[i]->ctx_index; | |
752 break; | |
753 } | |
754 } | |
755 | |
756 if (m == -1) { | |
757 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); | |
758 return NGX_CONF_ERROR; | |
759 } | |
760 | |
761 ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); | |
762 | |
763 ngx_conf_init_unsigned_value(ecf->use, m); | |
764 ngx_conf_init_ptr_value(ecf->name, module->name->data); | |
765 | |
766 #endif | |
767 | |
768 cycle->connection_n = ecf->connections; | |
769 | |
770 ngx_conf_init_value(ecf->multi_accept, 0); | |
771 ngx_conf_init_value(ecf->accept_mutex, 1); | |
772 ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); | |
773 | |
774 #if (HAVE_RTSIG) | |
775 if (ecf->use == ngx_rtsig_module.ctx_index && ecf->accept_mutex == 0) { | |
776 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, | |
777 ngx_core_module); | |
778 if (ccf->worker_processes) { | |
779 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
780 "the \"rtsig\" method requires " | |
781 "\"accept_mutex\" to be on"); | |
782 return NGX_CONF_ERROR; | |
783 } | |
784 } | |
785 #endif | |
786 | |
787 return NGX_CONF_OK; | |
788 } | |
789 | |
790 | |
791 static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data) | |
792 { | |
793 #if !(NGX_HAVE_ATOMIC_OPS) | |
794 | |
795 ngx_flag_t *fp = data; | |
796 | |
797 *fp = 0; | |
798 | |
799 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
800 "\"accept_mutex\" is not supported, ignored"); | |
801 | |
802 #endif | |
803 | |
804 return NGX_CONF_OK; | |
805 } |