Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / event / ngx_event.c
blobb7205f45b07c37e6c6733d740597a360b0cc7b8f
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_event.h>
13 #define DEFAULT_CONNECTIONS 512
16 extern ngx_module_t ngx_kqueue_module;
17 extern ngx_module_t ngx_eventport_module;
18 extern ngx_module_t ngx_devpoll_module;
19 extern ngx_module_t ngx_epoll_module;
20 extern ngx_module_t ngx_rtsig_module;
21 extern ngx_module_t ngx_select_module;
24 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
25 static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);
26 static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle);
27 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
29 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
30 void *conf);
31 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
32 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
33 void *conf);
35 static void *ngx_event_core_create_conf(ngx_cycle_t *cycle);
36 static char *ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf);
39 static ngx_uint_t ngx_timer_resolution;
40 sig_atomic_t ngx_event_timer_alarm;
42 static ngx_uint_t ngx_event_max_module;
44 ngx_uint_t ngx_event_flags;
45 ngx_event_actions_t ngx_event_actions;
48 static ngx_atomic_t connection_counter = 1;
49 ngx_atomic_t *ngx_connection_counter = &connection_counter;
52 ngx_atomic_t *ngx_accept_mutex_ptr;
53 ngx_shmtx_t ngx_accept_mutex;
54 ngx_uint_t ngx_use_accept_mutex;
55 ngx_uint_t ngx_accept_events;
56 ngx_uint_t ngx_accept_mutex_held;
57 ngx_msec_t ngx_accept_mutex_delay;
58 ngx_int_t ngx_accept_disabled;
59 ngx_file_t ngx_accept_mutex_lock_file;
62 #if (NGX_STAT_STUB)
64 ngx_atomic_t ngx_stat_accepted0;
65 ngx_atomic_t *ngx_stat_accepted = &ngx_stat_accepted0;
66 ngx_atomic_t ngx_stat_handled0;
67 ngx_atomic_t *ngx_stat_handled = &ngx_stat_handled0;
68 ngx_atomic_t ngx_stat_requests0;
69 ngx_atomic_t *ngx_stat_requests = &ngx_stat_requests0;
70 ngx_atomic_t ngx_stat_active0;
71 ngx_atomic_t *ngx_stat_active = &ngx_stat_active0;
72 ngx_atomic_t ngx_stat_reading0;
73 ngx_atomic_t *ngx_stat_reading = &ngx_stat_reading0;
74 ngx_atomic_t ngx_stat_writing0;
75 ngx_atomic_t *ngx_stat_writing = &ngx_stat_writing0;
76 ngx_atomic_t ngx_stat_waiting0;
77 ngx_atomic_t *ngx_stat_waiting = &ngx_stat_waiting0;
79 #endif
83 static ngx_command_t ngx_events_commands[] = {
85 { ngx_string("events"),
86 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
87 ngx_events_block,
90 NULL },
92 ngx_null_command
96 static ngx_core_module_t ngx_events_module_ctx = {
97 ngx_string("events"),
98 NULL,
99 ngx_event_init_conf
103 ngx_module_t ngx_events_module = {
104 NGX_MODULE_V1,
105 &ngx_events_module_ctx, /* module context */
106 ngx_events_commands, /* module directives */
107 NGX_CORE_MODULE, /* module type */
108 NULL, /* init master */
109 NULL, /* init module */
110 NULL, /* init process */
111 NULL, /* init thread */
112 NULL, /* exit thread */
113 NULL, /* exit process */
114 NULL, /* exit master */
115 NGX_MODULE_V1_PADDING
119 static ngx_str_t event_core_name = ngx_string("event_core");
122 static ngx_command_t ngx_event_core_commands[] = {
124 { ngx_string("worker_connections"),
125 NGX_EVENT_CONF|NGX_CONF_TAKE1,
126 ngx_event_connections,
129 NULL },
131 { ngx_string("connections"),
132 NGX_EVENT_CONF|NGX_CONF_TAKE1,
133 ngx_event_connections,
136 NULL },
138 { ngx_string("use"),
139 NGX_EVENT_CONF|NGX_CONF_TAKE1,
140 ngx_event_use,
143 NULL },
145 { ngx_string("multi_accept"),
146 NGX_EVENT_CONF|NGX_CONF_FLAG,
147 ngx_conf_set_flag_slot,
149 offsetof(ngx_event_conf_t, multi_accept),
150 NULL },
152 { ngx_string("accept_mutex"),
153 NGX_EVENT_CONF|NGX_CONF_FLAG,
154 ngx_conf_set_flag_slot,
156 offsetof(ngx_event_conf_t, accept_mutex),
157 NULL },
159 { ngx_string("accept_mutex_delay"),
160 NGX_EVENT_CONF|NGX_CONF_TAKE1,
161 ngx_conf_set_msec_slot,
163 offsetof(ngx_event_conf_t, accept_mutex_delay),
164 NULL },
166 { ngx_string("debug_connection"),
167 NGX_EVENT_CONF|NGX_CONF_TAKE1,
168 ngx_event_debug_connection,
171 NULL },
173 ngx_null_command
177 ngx_event_module_t ngx_event_core_module_ctx = {
178 &event_core_name,
179 ngx_event_core_create_conf, /* create configuration */
180 ngx_event_core_init_conf, /* init configuration */
182 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
186 ngx_module_t ngx_event_core_module = {
187 NGX_MODULE_V1,
188 &ngx_event_core_module_ctx, /* module context */
189 ngx_event_core_commands, /* module directives */
190 NGX_EVENT_MODULE, /* module type */
191 NULL, /* init master */
192 ngx_event_module_init, /* init module */
193 ngx_event_process_init, /* init process */
194 NULL, /* init thread */
195 NULL, /* exit thread */
196 NULL, /* exit process */
197 NULL, /* exit master */
198 NGX_MODULE_V1_PADDING
202 void
203 ngx_process_events_and_timers(ngx_cycle_t *cycle)
205 ngx_uint_t flags;
206 ngx_msec_t timer, delta;
208 if (ngx_timer_resolution) {
209 timer = NGX_TIMER_INFINITE;
210 flags = 0;
212 } else {
213 timer = ngx_event_find_timer();
214 flags = NGX_UPDATE_TIME;
216 #if (NGX_THREADS)
218 if (timer == NGX_TIMER_INFINITE || timer > 500) {
219 timer = 500;
222 #endif
225 if (ngx_use_accept_mutex) {
226 if (ngx_accept_disabled > 0) {
227 ngx_accept_disabled--;
229 } else {
230 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
231 return;
234 if (ngx_accept_mutex_held) {
235 flags |= NGX_POST_EVENTS;
237 } else {
238 if (timer == NGX_TIMER_INFINITE
239 || timer > ngx_accept_mutex_delay)
241 timer = ngx_accept_mutex_delay;
247 delta = ngx_current_msec;
249 (void) ngx_process_events(cycle, timer, flags);
251 delta = ngx_current_msec - delta;
253 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
254 "timer delta: %M", delta);
256 if (ngx_posted_accept_events) {
257 ngx_event_process_posted(cycle, &ngx_posted_accept_events);
260 if (ngx_accept_mutex_held) {
261 ngx_shmtx_unlock(&ngx_accept_mutex);
264 if (delta) {
265 ngx_event_expire_timers();
268 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
269 "posted events %p", ngx_posted_events);
271 if (ngx_posted_events) {
272 if (ngx_threaded) {
273 ngx_wakeup_worker_thread(cycle);
275 } else {
276 ngx_event_process_posted(cycle, &ngx_posted_events);
282 ngx_int_t
283 ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags)
285 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
287 /* kqueue, epoll */
289 if (!rev->active && !rev->ready) {
290 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)
291 == NGX_ERROR)
293 return NGX_ERROR;
297 return NGX_OK;
299 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
301 /* select, poll, /dev/poll */
303 if (!rev->active && !rev->ready) {
304 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT)
305 == NGX_ERROR)
307 return NGX_ERROR;
310 return NGX_OK;
313 if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {
314 if (ngx_del_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT | flags)
315 == NGX_ERROR)
317 return NGX_ERROR;
320 return NGX_OK;
323 } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
325 /* event ports */
327 if (!rev->active && !rev->ready) {
328 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
329 return NGX_ERROR;
332 return NGX_OK;
335 if (rev->oneshot && !rev->ready) {
336 if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
337 return NGX_ERROR;
340 return NGX_OK;
344 /* aio, iocp, rtsig */
346 return NGX_OK;
350 ngx_int_t
351 ngx_handle_write_event(ngx_event_t *wev, size_t lowat)
353 ngx_connection_t *c;
355 if (lowat) {
356 c = wev->data;
358 if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
359 return NGX_ERROR;
363 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
365 /* kqueue, epoll */
367 if (!wev->active && !wev->ready) {
368 if (ngx_add_event(wev, NGX_WRITE_EVENT,
369 NGX_CLEAR_EVENT | (lowat ? NGX_LOWAT_EVENT : 0))
370 == NGX_ERROR)
372 return NGX_ERROR;
376 return NGX_OK;
378 } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
380 /* select, poll, /dev/poll */
382 if (!wev->active && !wev->ready) {
383 if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
384 == NGX_ERROR)
386 return NGX_ERROR;
389 return NGX_OK;
392 if (wev->active && wev->ready) {
393 if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
394 == NGX_ERROR)
396 return NGX_ERROR;
399 return NGX_OK;
402 } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
404 /* event ports */
406 if (!wev->active && !wev->ready) {
407 if (ngx_add_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
408 return NGX_ERROR;
411 return NGX_OK;
414 if (wev->oneshot && wev->ready) {
415 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
416 return NGX_ERROR;
419 return NGX_OK;
423 /* aio, iocp, rtsig */
425 return NGX_OK;
429 static char *
430 ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
432 if (ngx_get_conf(cycle->conf_ctx, ngx_events_module) == NULL) {
433 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
434 "no \"events\" section in configuration");
435 return NGX_CONF_ERROR;
438 return NGX_CONF_OK;
442 static ngx_int_t
443 ngx_event_module_init(ngx_cycle_t *cycle)
445 void ***cf;
446 u_char *shared;
447 size_t size, cl;
448 ngx_shm_t shm;
449 ngx_time_t *tp;
450 ngx_core_conf_t *ccf;
451 ngx_event_conf_t *ecf;
453 cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
454 ecf = (*cf)[ngx_event_core_module.ctx_index];
456 if (!ngx_test_config && ngx_process <= NGX_PROCESS_MASTER) {
457 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
458 "using the \"%s\" event method", ecf->name);
461 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
463 ngx_timer_resolution = ccf->timer_resolution;
465 #if !(NGX_WIN32)
467 ngx_int_t limit;
468 struct rlimit rlmt;
470 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
471 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
472 "getrlimit(RLIMIT_NOFILE) failed, ignored");
474 } else {
475 if (ecf->connections > (ngx_uint_t) rlmt.rlim_cur
476 && (ccf->rlimit_nofile == NGX_CONF_UNSET
477 || ecf->connections > (ngx_uint_t) ccf->rlimit_nofile))
479 limit = (ccf->rlimit_nofile == NGX_CONF_UNSET) ?
480 (ngx_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
482 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
483 "%ui worker_connections exceed "
484 "open file resource limit: %i",
485 ecf->connections, limit);
489 #endif /* !(NGX_WIN32) */
492 if (ccf->master == 0) {
493 return NGX_OK;
496 if (ngx_accept_mutex_ptr) {
497 return NGX_OK;
501 /* cl should be equal to or greater than cache line size */
503 cl = 128;
505 size = cl /* ngx_accept_mutex */
506 + cl /* ngx_connection_counter */
507 + cl; /* ngx_temp_number */
509 #if (NGX_STAT_STUB)
511 size += cl /* ngx_stat_accepted */
512 + cl /* ngx_stat_handled */
513 + cl /* ngx_stat_requests */
514 + cl /* ngx_stat_active */
515 + cl /* ngx_stat_reading */
516 + cl /* ngx_stat_writing */
517 + cl; /* ngx_stat_waiting */
519 #endif
521 shm.size = size;
522 shm.name.len = sizeof("nginx_shared_zone");
523 shm.name.data = (u_char *) "nginx_shared_zone";
524 shm.log = cycle->log;
526 if (ngx_shm_alloc(&shm) != NGX_OK) {
527 return NGX_ERROR;
530 shared = shm.addr;
532 ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
533 ngx_accept_mutex.spin = (ngx_uint_t) -1;
535 if (ngx_shmtx_create(&ngx_accept_mutex, (ngx_shmtx_sh_t *) shared,
536 cycle->lock_file.data)
537 != NGX_OK)
539 return NGX_ERROR;
542 ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
544 (void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
546 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
547 "counter: %p, %d",
548 ngx_connection_counter, *ngx_connection_counter);
550 ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
552 tp = ngx_timeofday();
554 ngx_random_number = (tp->msec << 16) + ngx_pid;
556 #if (NGX_STAT_STUB)
558 ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
559 ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
560 ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
561 ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
562 ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
563 ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
564 ngx_stat_waiting = (ngx_atomic_t *) (shared + 9 * cl);
566 #endif
568 return NGX_OK;
572 #if !(NGX_WIN32)
574 static void
575 ngx_timer_signal_handler(int signo)
577 ngx_event_timer_alarm = 1;
579 #if 1
580 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, "timer signal");
581 #endif
584 #endif
587 static ngx_int_t
588 ngx_event_process_init(ngx_cycle_t *cycle)
590 ngx_uint_t m, i;
591 ngx_event_t *rev, *wev;
592 ngx_listening_t *ls;
593 ngx_connection_t *c, *next, *old;
594 ngx_core_conf_t *ccf;
595 ngx_event_conf_t *ecf;
596 ngx_event_module_t *module;
598 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
599 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
601 if (ccf->master && ccf->worker_processes > 1 && ecf->accept_mutex) {
602 ngx_use_accept_mutex = 1;
603 ngx_accept_mutex_held = 0;
604 ngx_accept_mutex_delay = ecf->accept_mutex_delay;
606 } else {
607 ngx_use_accept_mutex = 0;
610 #if (NGX_THREADS)
611 ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0);
612 if (ngx_posted_events_mutex == NULL) {
613 return NGX_ERROR;
615 #endif
617 if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
618 return NGX_ERROR;
621 for (m = 0; ngx_modules[m]; m++) {
622 if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
623 continue;
626 if (ngx_modules[m]->ctx_index != ecf->use) {
627 continue;
630 module = ngx_modules[m]->ctx;
632 if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
633 /* fatal */
634 exit(2);
637 break;
640 #if !(NGX_WIN32)
642 if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
643 struct sigaction sa;
644 struct itimerval itv;
646 ngx_memzero(&sa, sizeof(struct sigaction));
647 sa.sa_handler = ngx_timer_signal_handler;
648 sigemptyset(&sa.sa_mask);
650 if (sigaction(SIGALRM, &sa, NULL) == -1) {
651 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
652 "sigaction(SIGALRM) failed");
653 return NGX_ERROR;
656 itv.it_interval.tv_sec = ngx_timer_resolution / 1000;
657 itv.it_interval.tv_usec = (ngx_timer_resolution % 1000) * 1000;
658 itv.it_value.tv_sec = ngx_timer_resolution / 1000;
659 itv.it_value.tv_usec = (ngx_timer_resolution % 1000 ) * 1000;
661 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
662 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
663 "setitimer() failed");
667 if (ngx_event_flags & NGX_USE_FD_EVENT) {
668 struct rlimit rlmt;
670 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
671 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
672 "getrlimit(RLIMIT_NOFILE) failed");
673 return NGX_ERROR;
676 cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
678 cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
679 cycle->log);
680 if (cycle->files == NULL) {
681 return NGX_ERROR;
685 #endif
687 cycle->connections =
688 ngx_alloc(sizeof(ngx_connection_t) * cycle->connection_n, cycle->log);
689 if (cycle->connections == NULL) {
690 return NGX_ERROR;
693 c = cycle->connections;
695 cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
696 cycle->log);
697 if (cycle->read_events == NULL) {
698 return NGX_ERROR;
701 rev = cycle->read_events;
702 for (i = 0; i < cycle->connection_n; i++) {
703 rev[i].closed = 1;
704 rev[i].instance = 1;
705 #if (NGX_THREADS)
706 rev[i].lock = &c[i].lock;
707 rev[i].own_lock = &c[i].lock;
708 #endif
711 cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
712 cycle->log);
713 if (cycle->write_events == NULL) {
714 return NGX_ERROR;
717 wev = cycle->write_events;
718 for (i = 0; i < cycle->connection_n; i++) {
719 wev[i].closed = 1;
720 #if (NGX_THREADS)
721 wev[i].lock = &c[i].lock;
722 wev[i].own_lock = &c[i].lock;
723 #endif
726 i = cycle->connection_n;
727 next = NULL;
729 do {
730 i--;
732 c[i].data = next;
733 c[i].read = &cycle->read_events[i];
734 c[i].write = &cycle->write_events[i];
735 c[i].fd = (ngx_socket_t) -1;
737 next = &c[i];
739 #if (NGX_THREADS)
740 c[i].lock = 0;
741 #endif
742 } while (i);
744 cycle->free_connections = next;
745 cycle->free_connection_n = cycle->connection_n;
747 /* for each listening socket */
749 ls = cycle->listening.elts;
750 for (i = 0; i < cycle->listening.nelts; i++) {
752 c = ngx_get_connection(ls[i].fd, cycle->log);
754 if (c == NULL) {
755 return NGX_ERROR;
758 c->log = &ls[i].log;
760 c->listening = &ls[i];
761 ls[i].connection = c;
763 rev = c->read;
765 rev->log = c->log;
766 rev->accept = 1;
768 #if (NGX_HAVE_DEFERRED_ACCEPT)
769 rev->deferred_accept = ls[i].deferred_accept;
770 #endif
772 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
773 if (ls[i].previous) {
776 * delete the old accept events that were bound to
777 * the old cycle read events array
780 old = ls[i].previous->connection;
782 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
783 == NGX_ERROR)
785 return NGX_ERROR;
788 old->fd = (ngx_socket_t) -1;
792 #if (NGX_WIN32)
794 if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
795 ngx_iocp_conf_t *iocpcf;
797 rev->handler = ngx_event_acceptex;
799 if (ngx_use_accept_mutex) {
800 continue;
803 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
804 return NGX_ERROR;
807 ls[i].log.handler = ngx_acceptex_log_error;
809 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
810 if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
811 == NGX_ERROR)
813 return NGX_ERROR;
816 } else {
817 rev->handler = ngx_event_accept;
819 if (ngx_use_accept_mutex) {
820 continue;
823 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
824 return NGX_ERROR;
828 #else
830 rev->handler = ngx_event_accept;
832 if (ngx_use_accept_mutex) {
833 continue;
836 if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
837 if (ngx_add_conn(c) == NGX_ERROR) {
838 return NGX_ERROR;
841 } else {
842 if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
843 return NGX_ERROR;
847 #endif
851 return NGX_OK;
855 ngx_int_t
856 ngx_send_lowat(ngx_connection_t *c, size_t lowat)
858 int sndlowat;
860 #if (NGX_HAVE_LOWAT_EVENT)
862 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
863 c->write->available = lowat;
864 return NGX_OK;
867 #endif
869 if (lowat == 0 || c->sndlowat) {
870 return NGX_OK;
873 sndlowat = (int) lowat;
875 if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
876 (const void *) &sndlowat, sizeof(int))
877 == -1)
879 ngx_connection_error(c, ngx_socket_errno,
880 "setsockopt(SO_SNDLOWAT) failed");
881 return NGX_ERROR;
884 c->sndlowat = 1;
886 return NGX_OK;
890 static char *
891 ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
893 char *rv;
894 void ***ctx;
895 ngx_uint_t i;
896 ngx_conf_t pcf;
897 ngx_event_module_t *m;
899 if (*(void **) conf) {
900 return "is duplicate";
903 /* count the number of the event modules and set up their indices */
905 ngx_event_max_module = 0;
906 for (i = 0; ngx_modules[i]; i++) {
907 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
908 continue;
911 ngx_modules[i]->ctx_index = ngx_event_max_module++;
914 ctx = ngx_pcalloc(cf->pool, sizeof(void *));
915 if (ctx == NULL) {
916 return NGX_CONF_ERROR;
919 *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
920 if (*ctx == NULL) {
921 return NGX_CONF_ERROR;
924 *(void **) conf = ctx;
926 for (i = 0; ngx_modules[i]; i++) {
927 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
928 continue;
931 m = ngx_modules[i]->ctx;
933 if (m->create_conf) {
934 (*ctx)[ngx_modules[i]->ctx_index] = m->create_conf(cf->cycle);
935 if ((*ctx)[ngx_modules[i]->ctx_index] == NULL) {
936 return NGX_CONF_ERROR;
941 pcf = *cf;
942 cf->ctx = ctx;
943 cf->module_type = NGX_EVENT_MODULE;
944 cf->cmd_type = NGX_EVENT_CONF;
946 rv = ngx_conf_parse(cf, NULL);
948 *cf = pcf;
950 if (rv != NGX_CONF_OK)
951 return rv;
953 for (i = 0; ngx_modules[i]; i++) {
954 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
955 continue;
958 m = ngx_modules[i]->ctx;
960 if (m->init_conf) {
961 rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);
962 if (rv != NGX_CONF_OK) {
963 return rv;
968 return NGX_CONF_OK;
972 static char *
973 ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
975 ngx_event_conf_t *ecf = conf;
977 ngx_str_t *value;
979 if (ecf->connections != NGX_CONF_UNSET_UINT) {
980 return "is duplicate";
983 if (ngx_strcmp(cmd->name.data, "connections") == 0) {
984 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
985 "the \"connections\" directive is deprecated, "
986 "use the \"worker_connections\" directive instead");
989 value = cf->args->elts;
990 ecf->connections = ngx_atoi(value[1].data, value[1].len);
991 if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
992 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
993 "invalid number \"%V\"", &value[1]);
995 return NGX_CONF_ERROR;
998 cf->cycle->connection_n = ecf->connections;
1000 return NGX_CONF_OK;
1004 static char *
1005 ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1007 ngx_event_conf_t *ecf = conf;
1009 ngx_int_t m;
1010 ngx_str_t *value;
1011 ngx_event_conf_t *old_ecf;
1012 ngx_event_module_t *module;
1014 if (ecf->use != NGX_CONF_UNSET_UINT) {
1015 return "is duplicate";
1018 value = cf->args->elts;
1020 if (cf->cycle->old_cycle->conf_ctx) {
1021 old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
1022 ngx_event_core_module);
1023 } else {
1024 old_ecf = NULL;
1028 for (m = 0; ngx_modules[m]; m++) {
1029 if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
1030 continue;
1033 module = ngx_modules[m]->ctx;
1034 if (module->name->len == value[1].len) {
1035 if (ngx_strcmp(module->name->data, value[1].data) == 0) {
1036 ecf->use = ngx_modules[m]->ctx_index;
1037 ecf->name = module->name->data;
1039 if (ngx_process == NGX_PROCESS_SINGLE
1040 && old_ecf
1041 && old_ecf->use != ecf->use)
1043 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1044 "when the server runs without a master process "
1045 "the \"%V\" event type must be the same as "
1046 "in previous configuration - \"%s\" "
1047 "and it cannot be changed on the fly, "
1048 "to change it you need to stop server "
1049 "and start it again",
1050 &value[1], old_ecf->name);
1052 return NGX_CONF_ERROR;
1055 return NGX_CONF_OK;
1060 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1061 "invalid event type \"%V\"", &value[1]);
1063 return NGX_CONF_ERROR;
1067 static char *
1068 ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1070 #if (NGX_DEBUG)
1071 ngx_event_conf_t *ecf = conf;
1073 ngx_int_t rc;
1074 ngx_str_t *value;
1075 ngx_url_t u;
1076 ngx_cidr_t c, *cidr;
1077 ngx_uint_t i;
1078 struct sockaddr_in *sin;
1079 #if (NGX_HAVE_INET6)
1080 struct sockaddr_in6 *sin6;
1081 #endif
1083 value = cf->args->elts;
1085 #if (NGX_HAVE_UNIX_DOMAIN)
1087 if (ngx_strcmp(value[1].data, "unix:") == 0) {
1088 cidr = ngx_array_push(&ecf->debug_connection);
1089 if (cidr == NULL) {
1090 return NGX_CONF_ERROR;
1093 cidr->family = AF_UNIX;
1094 return NGX_CONF_OK;
1097 #endif
1099 rc = ngx_ptocidr(&value[1], &c);
1101 if (rc != NGX_ERROR) {
1102 if (rc == NGX_DONE) {
1103 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1104 "low address bits of %V are meaningless",
1105 &value[1]);
1108 cidr = ngx_array_push(&ecf->debug_connection);
1109 if (cidr == NULL) {
1110 return NGX_CONF_ERROR;
1113 *cidr = c;
1115 return NGX_CONF_OK;
1118 ngx_memzero(&u, sizeof(ngx_url_t));
1119 u.host = value[1];
1121 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
1122 if (u.err) {
1123 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1124 "%s in debug_connection \"%V\"",
1125 u.err, &u.host);
1128 return NGX_CONF_ERROR;
1131 cidr = ngx_array_push_n(&ecf->debug_connection, u.naddrs);
1132 if (cidr == NULL) {
1133 return NGX_CONF_ERROR;
1136 ngx_memzero(cidr, u.naddrs * sizeof(ngx_cidr_t));
1138 for (i = 0; i < u.naddrs; i++) {
1139 cidr[i].family = u.addrs[i].sockaddr->sa_family;
1141 switch (cidr[i].family) {
1143 #if (NGX_HAVE_INET6)
1144 case AF_INET6:
1145 sin6 = (struct sockaddr_in6 *) u.addrs[i].sockaddr;
1146 cidr[i].u.in6.addr = sin6->sin6_addr;
1147 ngx_memset(cidr[i].u.in6.mask.s6_addr, 0xff, 16);
1148 break;
1149 #endif
1151 default: /* AF_INET */
1152 sin = (struct sockaddr_in *) u.addrs[i].sockaddr;
1153 cidr[i].u.in.addr = sin->sin_addr.s_addr;
1154 cidr[i].u.in.mask = 0xffffffff;
1155 break;
1159 #else
1161 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1162 "\"debug_connection\" is ignored, you need to rebuild "
1163 "nginx using --with-debug option to enable it");
1165 #endif
1167 return NGX_CONF_OK;
1171 static void *
1172 ngx_event_core_create_conf(ngx_cycle_t *cycle)
1174 ngx_event_conf_t *ecf;
1176 ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
1177 if (ecf == NULL) {
1178 return NULL;
1181 ecf->connections = NGX_CONF_UNSET_UINT;
1182 ecf->use = NGX_CONF_UNSET_UINT;
1183 ecf->multi_accept = NGX_CONF_UNSET;
1184 ecf->accept_mutex = NGX_CONF_UNSET;
1185 ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
1186 ecf->name = (void *) NGX_CONF_UNSET;
1188 #if (NGX_DEBUG)
1190 if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
1191 sizeof(ngx_cidr_t)) == NGX_ERROR)
1193 return NULL;
1196 #endif
1198 return ecf;
1202 static char *
1203 ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)
1205 ngx_event_conf_t *ecf = conf;
1207 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1208 int fd;
1209 #endif
1210 #if (NGX_HAVE_RTSIG)
1211 ngx_uint_t rtsig;
1212 ngx_core_conf_t *ccf;
1213 #endif
1214 ngx_int_t i;
1215 ngx_module_t *module;
1216 ngx_event_module_t *event_module;
1218 module = NULL;
1220 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1222 fd = epoll_create(100);
1224 if (fd != -1) {
1225 (void) close(fd);
1226 module = &ngx_epoll_module;
1228 } else if (ngx_errno != NGX_ENOSYS) {
1229 module = &ngx_epoll_module;
1232 #endif
1234 #if (NGX_HAVE_RTSIG)
1236 if (module == NULL) {
1237 module = &ngx_rtsig_module;
1238 rtsig = 1;
1240 } else {
1241 rtsig = 0;
1244 #endif
1246 #if (NGX_HAVE_DEVPOLL)
1248 module = &ngx_devpoll_module;
1250 #endif
1252 #if (NGX_HAVE_KQUEUE)
1254 module = &ngx_kqueue_module;
1256 #endif
1258 #if (NGX_HAVE_SELECT)
1260 if (module == NULL) {
1261 module = &ngx_select_module;
1264 #endif
1266 if (module == NULL) {
1267 for (i = 0; ngx_modules[i]; i++) {
1269 if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
1270 continue;
1273 event_module = ngx_modules[i]->ctx;
1275 if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0)
1277 continue;
1280 module = ngx_modules[i];
1281 break;
1285 if (module == NULL) {
1286 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
1287 return NGX_CONF_ERROR;
1290 ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
1291 cycle->connection_n = ecf->connections;
1293 ngx_conf_init_uint_value(ecf->use, module->ctx_index);
1295 event_module = module->ctx;
1296 ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
1298 ngx_conf_init_value(ecf->multi_accept, 0);
1299 ngx_conf_init_value(ecf->accept_mutex, 1);
1300 ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
1303 #if (NGX_HAVE_RTSIG)
1305 if (!rtsig) {
1306 return NGX_CONF_OK;
1309 if (ecf->accept_mutex) {
1310 return NGX_CONF_OK;
1313 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1315 if (ccf->worker_processes == 0) {
1316 return NGX_CONF_OK;
1319 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
1320 "the \"rtsig\" method requires \"accept_mutex\" to be on");
1322 return NGX_CONF_ERROR;
1324 #else
1326 return NGX_CONF_OK;
1328 #endif