fix extension
[nginx.git] / nginx / src / event / ngx_event.c
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     for (m = 0; ngx_modules[m]; m++) {
600         if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
601             continue;
602         }
603
604         if (ngx_modules[m]->ctx_index != ecf->use) {
605             continue;
606         }
607
608         module = ngx_modules[m]->ctx;
609
610         if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
611             /* fatal */
612             exit(2);
613         }
614
615         break;
616     }
617
618 #if !(NGX_WIN32)
619
620     if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
621         struct sigaction  sa;
622         struct itimerval  itv;
623
624         ngx_memzero(&sa, sizeof(struct sigaction));
625         sa.sa_handler = ngx_timer_signal_handler;
626         sigemptyset(&sa.sa_mask);
627
628         if (sigaction(SIGALRM, &sa, NULL) == -1) {
629             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
630                           "sigaction(SIGALRM) failed");
631             return NGX_ERROR;
632         }
633
634         itv.it_interval.tv_sec = ngx_timer_resolution / 1000;
635         itv.it_interval.tv_usec = (ngx_timer_resolution % 1000) * 1000;
636         itv.it_value.tv_sec = ngx_timer_resolution / 1000;
637         itv.it_value.tv_usec = (ngx_timer_resolution % 1000 ) * 1000;
638
639         if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
640             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
641                           "setitimer() failed");
642         }
643     }
644
645     if (ngx_event_flags & NGX_USE_FD_EVENT) {
646         struct rlimit  rlmt;
647
648         if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
649             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
650                           "getrlimit(RLIMIT_NOFILE) failed");
651             return NGX_ERROR;
652         }
653
654         cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
655
656         cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
657                                   cycle->log);
658         if (cycle->files == NULL) {
659             return NGX_ERROR;
660         }
661     }
662
663 #endif
664
665     cycle->connections =
666         ngx_alloc(sizeof(ngx_connection_t) * cycle->connection_n, cycle->log);
667     if (cycle->connections == NULL) {
668         return NGX_ERROR;
669     }
670
671     c = cycle->connections;
672
673     cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
674                                    cycle->log);
675     if (cycle->read_events == NULL) {
676         return NGX_ERROR;
677     }
678
679     rev = cycle->read_events;
680     for (i = 0; i < cycle->connection_n; i++) {
681         rev[i].closed = 1;
682         rev[i].instance = 1;
683 #if (NGX_THREADS)
684         rev[i].lock = &c[i].lock;
685         rev[i].own_lock = &c[i].lock;
686 #endif
687     }
688
689     cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
690                                     cycle->log);
691     if (cycle->write_events == NULL) {
692         return NGX_ERROR;
693     }
694
695     wev = cycle->write_events;
696     for (i = 0; i < cycle->connection_n; i++) {
697         wev[i].closed = 1;
698 #if (NGX_THREADS)
699         wev[i].lock = &c[i].lock;
700         wev[i].own_lock = &c[i].lock;
701 #endif
702     }
703
704     i = cycle->connection_n;
705     next = NULL;
706
707     do {
708         i--;
709
710         c[i].data = next;
711         c[i].read = &cycle->read_events[i];
712         c[i].write = &cycle->write_events[i];
713         c[i].fd = (ngx_socket_t) -1;
714
715         next = &c[i];
716
717 #if (NGX_THREADS)
718         c[i].lock = 0;
719 #endif
720     } while (i);
721
722     cycle->free_connections = next;
723     cycle->free_connection_n = cycle->connection_n;
724
725     /* for each listening socket */
726
727     ls = cycle->listening.elts;
728     for (i = 0; i < cycle->listening.nelts; i++) {
729
730         c = ngx_get_connection(ls[i].fd, cycle->log);
731
732         if (c == NULL) {
733             return NGX_ERROR;
734         }
735
736         c->log = &ls[i].log;
737
738         c->listening = &ls[i];
739         ls[i].connection = c;
740
741         rev = c->read;
742
743         rev->log = c->log;
744         rev->accept = 1;
745
746 #if (NGX_HAVE_DEFERRED_ACCEPT)
747         rev->deferred_accept = ls[i].deferred_accept;
748 #endif
749
750         if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
751             if (ls[i].previous) {
752
753                 /*
754                  * delete the old accept events that were bound to
755                  * the old cycle read events array
756                  */
757
758                 old = ls[i].previous->connection;
759
760                 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
761                     == NGX_ERROR)
762                 {
763                     return NGX_ERROR;
764                 }
765
766                 old->fd = (ngx_socket_t) -1;
767             }
768         }
769
770 #if (NGX_WIN32)
771
772         if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
773             ngx_iocp_conf_t  *iocpcf;
774
775             rev->handler = ngx_event_acceptex;
776
777             if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
778                 return NGX_ERROR;
779             }
780
781             ls[i].log.handler = ngx_acceptex_log_error;
782
783             iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
784             if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
785                 == NGX_ERROR)
786             {
787                 return NGX_ERROR;
788             }
789
790         } else {
791             rev->handler = ngx_event_accept;
792
793             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
794                 return NGX_ERROR;
795             }
796         }
797
798 #else
799
800         rev->handler = ngx_event_accept;
801
802         if (ngx_use_accept_mutex) {
803             continue;
804         }
805
806         if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
807             if (ngx_add_conn(c) == NGX_ERROR) {
808                 return NGX_ERROR;
809             }
810
811         } else {
812             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
813                 return NGX_ERROR;
814             }
815         }
816
817 #endif
818
819     }
820
821     return NGX_OK;
822 }
823
824
825 ngx_int_t
826 ngx_send_lowat(ngx_connection_t *c, size_t lowat)
827 {
828     int  sndlowat;
829
830 #if (NGX_HAVE_LOWAT_EVENT)
831
832     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
833         c->write->available = lowat;
834         return NGX_OK;
835     }
836
837 #endif
838
839     if (lowat == 0 || c->sndlowat) {
840         return NGX_OK;
841     }
842
843     sndlowat = (int) lowat;
844
845     if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
846                    (const void *) &sndlowat, sizeof(int))
847         == -1)
848     {
849         ngx_connection_error(c, ngx_socket_errno,
850                              "setsockopt(SO_SNDLOWAT) failed");
851         return NGX_ERROR;
852     }
853
854     c->sndlowat = 1;
855
856     return NGX_OK;
857 }
858
859
860 static char *
861 ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
862 {
863     char                 *rv;
864     void               ***ctx;
865     ngx_uint_t            i;
866     ngx_conf_t            pcf;
867     ngx_event_module_t   *m;
868
869     /* count the number of the event modules and set up their indices */
870
871     ngx_event_max_module = 0;
872     for (i = 0; ngx_modules[i]; i++) {
873         if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
874             continue;
875         }
876
877         ngx_modules[i]->ctx_index = ngx_event_max_module++;
878     }
879
880     ctx = ngx_pcalloc(cf->pool, sizeof(void *));
881     if (ctx == NULL) {
882         return NGX_CONF_ERROR;
883     }
884
885     *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
886     if (*ctx == NULL) {
887         return NGX_CONF_ERROR;
888     }
889
890     *(void **) conf = ctx;
891
892     for (i = 0; ngx_modules[i]; i++) {
893         if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
894             continue;
895         }
896
897         m = ngx_modules[i]->ctx;
898
899         if (m->create_conf) {
900             (*ctx)[ngx_modules[i]->ctx_index] = m->create_conf(cf->cycle);
901             if ((*ctx)[ngx_modules[i]->ctx_index] == NULL) {
902                 return NGX_CONF_ERROR;
903             }
904         }
905     }
906
907     pcf = *cf;
908     cf->ctx = ctx;
909     cf->module_type = NGX_EVENT_MODULE;
910     cf->cmd_type = NGX_EVENT_CONF;
911
912     rv = ngx_conf_parse(cf, NULL);
913
914     *cf = pcf;
915
916     if (rv != NGX_CONF_OK)
917         return rv;
918
919     for (i = 0; ngx_modules[i]; i++) {
920         if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
921             continue;
922         }
923
924         m = ngx_modules[i]->ctx;
925
926         if (m->init_conf) {
927             rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);
928             if (rv != NGX_CONF_OK) {
929                 return rv;
930             }
931         }
932     }
933
934     return NGX_CONF_OK;
935 }
936
937
938 static char *
939 ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
940 {
941     ngx_event_conf_t  *ecf = conf;
942
943     ngx_str_t  *value;
944
945     if (ecf->connections != NGX_CONF_UNSET_UINT) {
946         return "is duplicate";
947     }
948
949     if (ngx_strcmp(cmd->name.data, "connections") == 0) {
950         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
951                            "the \"connections\" directive is deprecated, "
952                            "use the \"worker_connections\" directive instead");
953     }
954
955     value = cf->args->elts;
956     ecf->connections = ngx_atoi(value[1].data, value[1].len);
957     if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
958         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
959                            "invalid number \"%V\"", &value[1]);
960
961         return NGX_CONF_ERROR;
962     }
963
964     cf->cycle->connection_n = ecf->connections;
965
966     return NGX_CONF_OK;
967 }
968
969
970 static char *
971 ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
972 {
973     ngx_event_conf_t  *ecf = conf;
974
975     ngx_int_t             m;
976     ngx_str_t            *value;
977     ngx_event_conf_t     *old_ecf;
978     ngx_event_module_t   *module;
979
980     if (ecf->use != NGX_CONF_UNSET_UINT) {
981         return "is duplicate";
982     }
983
984     value = cf->args->elts;
985
986     if (cf->cycle->old_cycle->conf_ctx) {
987         old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
988                                      ngx_event_core_module);
989     } else {
990         old_ecf = NULL;
991     }
992
993
994     for (m = 0; ngx_modules[m]; m++) {
995         if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
996             continue;
997         }
998
999         module = ngx_modules[m]->ctx;
1000         if (module->name->len == value[1].len) {
1001             if (ngx_strcmp(module->name->data, value[1].data) == 0) {
1002                 ecf->use = ngx_modules[m]->ctx_index;
1003                 ecf->name = module->name->data;
1004
1005                 if (ngx_process == NGX_PROCESS_SINGLE
1006                     && old_ecf
1007                     && old_ecf->use != ecf->use)
1008                 {
1009                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1010                                "when the server runs without a master process "
1011                                "the \"%V\" event type must be the same as "
1012                                "in previous configuration - \"%s\" "
1013                                "and it can not be changed on the fly, "
1014                                "to change it you need to stop server "
1015                                "and start it again",
1016                                &value[1], old_ecf->name);
1017
1018                     return NGX_CONF_ERROR;
1019                 }
1020
1021                 return NGX_CONF_OK;
1022             }
1023         }
1024     }
1025
1026     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1027                        "invalid event type \"%V\"", &value[1]);
1028
1029     return NGX_CONF_ERROR;
1030 }
1031
1032
1033 static char *
1034 ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1035 {
1036 #if (NGX_DEBUG)
1037     ngx_event_conf_t  *ecf = conf;
1038
1039     ngx_int_t           rc;
1040     ngx_str_t          *value;
1041     ngx_event_debug_t  *dc;
1042     struct hostent     *h;
1043     ngx_inet_cidr_t     in_cidr;
1044
1045     value = cf->args->elts;
1046
1047     /* AF_INET only */
1048
1049     dc = ngx_array_push(&ecf->debug_connection);
1050     if (dc == NULL) {
1051         return NGX_CONF_ERROR;
1052     }
1053
1054     rc = ngx_ptocidr(&value[1], &in_cidr);
1055
1056     if (rc == NGX_DONE) {
1057         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1058                            "low address bits of %V are meaningless", &value[1]);
1059         rc = NGX_OK;
1060     }
1061
1062     if (rc == NGX_OK) {
1063         dc->mask = in_cidr.mask;
1064         dc->addr = in_cidr.addr;
1065         return NGX_CONF_OK;
1066     }
1067
1068     h = gethostbyname((char *) value[1].data);
1069
1070     if (h == NULL || h->h_addr_list[0] == NULL) {
1071         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1072                            "host \"%s\" not found", value[1].data);
1073         return NGX_CONF_ERROR;
1074     }
1075
1076     dc->mask = 0xffffffff;
1077     dc->addr = *(in_addr_t *)(h->h_addr_list[0]);
1078
1079 #else
1080
1081     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1082                        "\"debug_connection\" is ignored, you need to rebuild "
1083                        "nginx using --with-debug option to enable it");
1084
1085 #endif
1086
1087     return NGX_CONF_OK;
1088 }
1089
1090
1091 static void *
1092 ngx_event_create_conf(ngx_cycle_t *cycle)
1093 {
1094     ngx_event_conf_t  *ecf;
1095
1096     ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
1097     if (ecf == NULL) {
1098         return NGX_CONF_ERROR;
1099     }
1100
1101     ecf->connections = NGX_CONF_UNSET_UINT;
1102     ecf->use = NGX_CONF_UNSET_UINT;
1103     ecf->multi_accept = NGX_CONF_UNSET;
1104     ecf->accept_mutex = NGX_CONF_UNSET;
1105     ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
1106     ecf->name = (void *) NGX_CONF_UNSET;
1107
1108 #if (NGX_DEBUG)
1109
1110     if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
1111                        sizeof(ngx_event_debug_t)) == NGX_ERROR)
1112     {
1113         return NGX_CONF_ERROR;
1114     }
1115
1116 #endif
1117
1118     return ecf;
1119 }
1120
1121
1122 static char *
1123 ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
1124 {
1125     ngx_event_conf_t  *ecf = conf;
1126
1127 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1128     int                  fd;
1129 #endif
1130 #if (NGX_HAVE_RTSIG)
1131     ngx_uint_t           rtsig;
1132     ngx_core_conf_t     *ccf;
1133 #endif
1134     ngx_int_t            i;
1135     ngx_module_t        *module;
1136     ngx_event_module_t  *event_module;
1137
1138     module = NULL;
1139
1140 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1141
1142     fd = epoll_create(100);
1143
1144     if (fd != -1) {
1145         close(fd);
1146         module = &ngx_epoll_module;
1147
1148     } else if (ngx_errno != NGX_ENOSYS) {
1149         module = &ngx_epoll_module;
1150     }
1151
1152 #endif
1153
1154 #if (NGX_HAVE_RTSIG)
1155
1156     if (module == NULL) {
1157         module = &ngx_rtsig_module;
1158         rtsig = 1;
1159
1160     } else {
1161         rtsig = 0;
1162     }
1163
1164 #endif
1165
1166 #if (NGX_HAVE_DEVPOLL)
1167
1168     module = &ngx_devpoll_module;
1169
1170 #endif
1171
1172 #if (NGX_HAVE_KQUEUE)
1173
1174     module = &ngx_kqueue_module;
1175
1176 #endif
1177
1178 #if (NGX_HAVE_SELECT)
1179
1180     if (module == NULL) {
1181         module = &ngx_select_module;
1182     }
1183
1184 #endif
1185
1186     if (module == NULL) {
1187         for (i = 0; ngx_modules[i]; i++) {
1188
1189             if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
1190                 continue;
1191             }
1192
1193             event_module = ngx_modules[i]->ctx;
1194
1195             if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0)
1196             {
1197                 continue;
1198             }
1199
1200             module = ngx_modules[i];
1201             break;
1202         }
1203     }
1204
1205     if (module == NULL) {
1206         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
1207         return NGX_CONF_ERROR;
1208     }
1209
1210     ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
1211     cycle->connection_n = ecf->connections;
1212
1213     ngx_conf_init_uint_value(ecf->use, module->ctx_index);
1214
1215     event_module = module->ctx;
1216     ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
1217
1218     ngx_conf_init_value(ecf->multi_accept, 0);
1219     ngx_conf_init_value(ecf->accept_mutex, 1);
1220     ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
1221
1222
1223 #if (NGX_HAVE_RTSIG)
1224
1225     if (!rtsig) {
1226         return NGX_CONF_OK;
1227     }
1228
1229     if (ecf->accept_mutex) {
1230         return NGX_CONF_OK;
1231     }
1232
1233     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1234
1235     if (ccf->worker_processes == 0) {
1236         return NGX_CONF_OK;
1237     }
1238
1239     ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
1240                   "the \"rtsig\" method requires \"accept_mutex\" to be on");
1241
1242     return NGX_CONF_ERROR;
1243
1244 #else
1245
1246     return NGX_CONF_OK;
1247
1248 #endif
1249 }