3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.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
,
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
,
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
;
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
;
83 static ngx_command_t ngx_events_commands
[] = {
85 { ngx_string("events"),
86 NGX_MAIN_CONF
|NGX_CONF_BLOCK
|NGX_CONF_NOARGS
,
96 static ngx_core_module_t ngx_events_module_ctx
= {
103 ngx_module_t ngx_events_module
= {
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
,
131 { ngx_string("connections"),
132 NGX_EVENT_CONF
|NGX_CONF_TAKE1
,
133 ngx_event_connections
,
139 NGX_EVENT_CONF
|NGX_CONF_TAKE1
,
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
),
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
),
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
),
166 { ngx_string("debug_connection"),
167 NGX_EVENT_CONF
|NGX_CONF_TAKE1
,
168 ngx_event_debug_connection
,
177 ngx_event_module_t ngx_event_core_module_ctx
= {
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
= {
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
203 ngx_process_events_and_timers(ngx_cycle_t
*cycle
)
206 ngx_msec_t timer
, delta
;
208 if (ngx_timer_resolution
) {
209 timer
= NGX_TIMER_INFINITE
;
213 timer
= ngx_event_find_timer();
214 flags
= NGX_UPDATE_TIME
;
218 if (timer
== NGX_TIMER_INFINITE
|| timer
> 500) {
225 if (ngx_use_accept_mutex
) {
226 if (ngx_accept_disabled
> 0) {
227 ngx_accept_disabled
--;
230 if (ngx_trylock_accept_mutex(cycle
) == NGX_ERROR
) {
234 if (ngx_accept_mutex_held
) {
235 flags
|= NGX_POST_EVENTS
;
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
);
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
) {
273 ngx_wakeup_worker_thread(cycle
);
276 ngx_event_process_posted(cycle
, &ngx_posted_events
);
283 ngx_handle_read_event(ngx_event_t
*rev
, ngx_uint_t flags
)
285 if (ngx_event_flags
& NGX_USE_CLEAR_EVENT
) {
289 if (!rev
->active
&& !rev
->ready
) {
290 if (ngx_add_event(rev
, NGX_READ_EVENT
, NGX_CLEAR_EVENT
)
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
)
313 if (rev
->active
&& (rev
->ready
|| (flags
& NGX_CLOSE_EVENT
))) {
314 if (ngx_del_event(rev
, NGX_READ_EVENT
, NGX_LEVEL_EVENT
| flags
)
323 } else if (ngx_event_flags
& NGX_USE_EVENTPORT_EVENT
) {
327 if (!rev
->active
&& !rev
->ready
) {
328 if (ngx_add_event(rev
, NGX_READ_EVENT
, 0) == NGX_ERROR
) {
335 if (rev
->oneshot
&& !rev
->ready
) {
336 if (ngx_del_event(rev
, NGX_READ_EVENT
, 0) == NGX_ERROR
) {
344 /* aio, iocp, rtsig */
351 ngx_handle_write_event(ngx_event_t
*wev
, size_t lowat
)
358 if (ngx_send_lowat(c
, lowat
) == NGX_ERROR
) {
363 if (ngx_event_flags
& NGX_USE_CLEAR_EVENT
) {
367 if (!wev
->active
&& !wev
->ready
) {
368 if (ngx_add_event(wev
, NGX_WRITE_EVENT
,
369 NGX_CLEAR_EVENT
| (lowat
? NGX_LOWAT_EVENT
: 0))
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
)
392 if (wev
->active
&& wev
->ready
) {
393 if (ngx_del_event(wev
, NGX_WRITE_EVENT
, NGX_LEVEL_EVENT
)
402 } else if (ngx_event_flags
& NGX_USE_EVENTPORT_EVENT
) {
406 if (!wev
->active
&& !wev
->ready
) {
407 if (ngx_add_event(wev
, NGX_WRITE_EVENT
, 0) == NGX_ERROR
) {
414 if (wev
->oneshot
&& wev
->ready
) {
415 if (ngx_del_event(wev
, NGX_WRITE_EVENT
, 0) == NGX_ERROR
) {
423 /* aio, iocp, rtsig */
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
;
443 ngx_event_module_init(ngx_cycle_t
*cycle
)
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
;
470 if (getrlimit(RLIMIT_NOFILE
, &rlmt
) == -1) {
471 ngx_log_error(NGX_LOG_ALERT
, cycle
->log
, ngx_errno
,
472 "getrlimit(RLIMIT_NOFILE) failed, ignored");
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) {
496 if (ngx_accept_mutex_ptr
) {
501 /* cl should be equal to or greater than cache line size */
505 size
= cl
/* ngx_accept_mutex */
506 + cl
/* ngx_connection_counter */
507 + cl
; /* ngx_temp_number */
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 */
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
) {
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
)
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,
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
;
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
);
575 ngx_timer_signal_handler(int signo
)
577 ngx_event_timer_alarm
= 1;
580 ngx_log_debug0(NGX_LOG_DEBUG_EVENT
, ngx_cycle
->log
, 0, "timer signal");
588 ngx_event_process_init(ngx_cycle_t
*cycle
)
591 ngx_event_t
*rev
, *wev
;
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
;
607 ngx_use_accept_mutex
= 0;
611 ngx_posted_events_mutex
= ngx_mutex_init(cycle
->log
, 0);
612 if (ngx_posted_events_mutex
== NULL
) {
617 if (ngx_event_timer_init(cycle
->log
) == NGX_ERROR
) {
621 for (m
= 0; ngx_modules
[m
]; m
++) {
622 if (ngx_modules
[m
]->type
!= NGX_EVENT_MODULE
) {
626 if (ngx_modules
[m
]->ctx_index
!= ecf
->use
) {
630 module
= ngx_modules
[m
]->ctx
;
632 if (module
->actions
.init(cycle
, ngx_timer_resolution
) != NGX_OK
) {
642 if (ngx_timer_resolution
&& !(ngx_event_flags
& NGX_USE_TIMER_EVENT
)) {
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");
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
) {
670 if (getrlimit(RLIMIT_NOFILE
, &rlmt
) == -1) {
671 ngx_log_error(NGX_LOG_ALERT
, cycle
->log
, ngx_errno
,
672 "getrlimit(RLIMIT_NOFILE) failed");
676 cycle
->files_n
= (ngx_uint_t
) rlmt
.rlim_cur
;
678 cycle
->files
= ngx_calloc(sizeof(ngx_connection_t
*) * cycle
->files_n
,
680 if (cycle
->files
== NULL
) {
688 ngx_alloc(sizeof(ngx_connection_t
) * cycle
->connection_n
, cycle
->log
);
689 if (cycle
->connections
== NULL
) {
693 c
= cycle
->connections
;
695 cycle
->read_events
= ngx_alloc(sizeof(ngx_event_t
) * cycle
->connection_n
,
697 if (cycle
->read_events
== NULL
) {
701 rev
= cycle
->read_events
;
702 for (i
= 0; i
< cycle
->connection_n
; i
++) {
706 rev
[i
].lock
= &c
[i
].lock
;
707 rev
[i
].own_lock
= &c
[i
].lock
;
711 cycle
->write_events
= ngx_alloc(sizeof(ngx_event_t
) * cycle
->connection_n
,
713 if (cycle
->write_events
== NULL
) {
717 wev
= cycle
->write_events
;
718 for (i
= 0; i
< cycle
->connection_n
; i
++) {
721 wev
[i
].lock
= &c
[i
].lock
;
722 wev
[i
].own_lock
= &c
[i
].lock
;
726 i
= cycle
->connection_n
;
733 c
[i
].read
= &cycle
->read_events
[i
];
734 c
[i
].write
= &cycle
->write_events
[i
];
735 c
[i
].fd
= (ngx_socket_t
) -1;
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
);
760 c
->listening
= &ls
[i
];
761 ls
[i
].connection
= c
;
768 #if (NGX_HAVE_DEFERRED_ACCEPT)
769 rev
->deferred_accept
= ls
[i
].deferred_accept
;
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
)
788 old
->fd
= (ngx_socket_t
) -1;
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
) {
803 if (ngx_add_event(rev
, 0, NGX_IOCP_ACCEPT
) == 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
)
817 rev
->handler
= ngx_event_accept
;
819 if (ngx_use_accept_mutex
) {
823 if (ngx_add_event(rev
, NGX_READ_EVENT
, 0) == NGX_ERROR
) {
830 rev
->handler
= ngx_event_accept
;
832 if (ngx_use_accept_mutex
) {
836 if (ngx_event_flags
& NGX_USE_RTSIG_EVENT
) {
837 if (ngx_add_conn(c
) == NGX_ERROR
) {
842 if (ngx_add_event(rev
, NGX_READ_EVENT
, 0) == NGX_ERROR
) {
856 ngx_send_lowat(ngx_connection_t
*c
, size_t lowat
)
860 #if (NGX_HAVE_LOWAT_EVENT)
862 if (ngx_event_flags
& NGX_USE_KQUEUE_EVENT
) {
863 c
->write
->available
= lowat
;
869 if (lowat
== 0 || c
->sndlowat
) {
873 sndlowat
= (int) lowat
;
875 if (setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
876 (const void *) &sndlowat
, sizeof(int))
879 ngx_connection_error(c
, ngx_socket_errno
,
880 "setsockopt(SO_SNDLOWAT) failed");
891 ngx_events_block(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
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
) {
911 ngx_modules
[i
]->ctx_index
= ngx_event_max_module
++;
914 ctx
= ngx_pcalloc(cf
->pool
, sizeof(void *));
916 return NGX_CONF_ERROR
;
919 *ctx
= ngx_pcalloc(cf
->pool
, ngx_event_max_module
* sizeof(void *));
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
) {
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
;
943 cf
->module_type
= NGX_EVENT_MODULE
;
944 cf
->cmd_type
= NGX_EVENT_CONF
;
946 rv
= ngx_conf_parse(cf
, NULL
);
950 if (rv
!= NGX_CONF_OK
)
953 for (i
= 0; ngx_modules
[i
]; i
++) {
954 if (ngx_modules
[i
]->type
!= NGX_EVENT_MODULE
) {
958 m
= ngx_modules
[i
]->ctx
;
961 rv
= m
->init_conf(cf
->cycle
, (*ctx
)[ngx_modules
[i
]->ctx_index
]);
962 if (rv
!= NGX_CONF_OK
) {
973 ngx_event_connections(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
975 ngx_event_conf_t
*ecf
= conf
;
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
;
1005 ngx_event_use(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1007 ngx_event_conf_t
*ecf
= conf
;
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
);
1028 for (m
= 0; ngx_modules
[m
]; m
++) {
1029 if (ngx_modules
[m
]->type
!= NGX_EVENT_MODULE
) {
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
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
;
1060 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1061 "invalid event type \"%V\"", &value
[1]);
1063 return NGX_CONF_ERROR
;
1068 ngx_event_debug_connection(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
1071 ngx_event_conf_t
*ecf
= conf
;
1076 ngx_cidr_t c
, *cidr
;
1078 struct sockaddr_in
*sin
;
1079 #if (NGX_HAVE_INET6)
1080 struct sockaddr_in6
*sin6
;
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
);
1090 return NGX_CONF_ERROR
;
1093 cidr
->family
= AF_UNIX
;
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",
1108 cidr
= ngx_array_push(&ecf
->debug_connection
);
1110 return NGX_CONF_ERROR
;
1118 ngx_memzero(&u
, sizeof(ngx_url_t
));
1121 if (ngx_inet_resolve_host(cf
->pool
, &u
) != NGX_OK
) {
1123 ngx_conf_log_error(NGX_LOG_EMERG
, cf
, 0,
1124 "%s in debug_connection \"%V\"",
1128 return NGX_CONF_ERROR
;
1131 cidr
= ngx_array_push_n(&ecf
->debug_connection
, u
.naddrs
);
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)
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);
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;
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");
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
));
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
;
1190 if (ngx_array_init(&ecf
->debug_connection
, cycle
->pool
, 4,
1191 sizeof(ngx_cidr_t
)) == NGX_ERROR
)
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)
1210 #if (NGX_HAVE_RTSIG)
1212 ngx_core_conf_t
*ccf
;
1215 ngx_module_t
*module
;
1216 ngx_event_module_t
*event_module
;
1220 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1222 fd
= epoll_create(100);
1226 module
= &ngx_epoll_module
;
1228 } else if (ngx_errno
!= NGX_ENOSYS
) {
1229 module
= &ngx_epoll_module
;
1234 #if (NGX_HAVE_RTSIG)
1236 if (module
== NULL
) {
1237 module
= &ngx_rtsig_module
;
1246 #if (NGX_HAVE_DEVPOLL)
1248 module
= &ngx_devpoll_module
;
1252 #if (NGX_HAVE_KQUEUE)
1254 module
= &ngx_kqueue_module
;
1258 #if (NGX_HAVE_SELECT)
1260 if (module
== NULL
) {
1261 module
= &ngx_select_module
;
1266 if (module
== NULL
) {
1267 for (i
= 0; ngx_modules
[i
]; i
++) {
1269 if (ngx_modules
[i
]->type
!= NGX_EVENT_MODULE
) {
1273 event_module
= ngx_modules
[i
]->ctx
;
1275 if (ngx_strcmp(event_module
->name
->data
, event_core_name
.data
) == 0)
1280 module
= ngx_modules
[i
];
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)
1309 if (ecf
->accept_mutex
) {
1313 ccf
= (ngx_core_conf_t
*) ngx_get_conf(cycle
->conf_ctx
, ngx_core_module
);
1315 if (ccf
->worker_processes
== 0) {
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
;