~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/event/ngx_event.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

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