Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 
0012 
0013 #define DEFAULT_CONNECTIONS  512
0014 
0015 
0016 extern ngx_module_t ngx_kqueue_module;
0017 extern ngx_module_t ngx_eventport_module;
0018 extern ngx_module_t ngx_devpoll_module;
0019 extern ngx_module_t ngx_epoll_module;
0020 extern ngx_module_t ngx_select_module;
0021 
0022 
0023 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
0024 static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);
0025 static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle);
0026 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0027 
0028 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
0029     void *conf);
0030 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0031 static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd,
0032     void *conf);
0033 
0034 static void *ngx_event_core_create_conf(ngx_cycle_t *cycle);
0035 static char *ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf);
0036 
0037 
0038 static ngx_uint_t     ngx_timer_resolution;
0039 sig_atomic_t          ngx_event_timer_alarm;
0040 
0041 static ngx_uint_t     ngx_event_max_module;
0042 
0043 ngx_uint_t            ngx_event_flags;
0044 ngx_event_actions_t   ngx_event_actions;
0045 
0046 
0047 static ngx_atomic_t   connection_counter = 1;
0048 ngx_atomic_t         *ngx_connection_counter = &connection_counter;
0049 
0050 
0051 ngx_atomic_t         *ngx_accept_mutex_ptr;
0052 ngx_shmtx_t           ngx_accept_mutex;
0053 ngx_uint_t            ngx_use_accept_mutex;
0054 ngx_uint_t            ngx_accept_events;
0055 ngx_uint_t            ngx_accept_mutex_held;
0056 ngx_msec_t            ngx_accept_mutex_delay;
0057 ngx_int_t             ngx_accept_disabled;
0058 
0059 
0060 #if (NGX_STAT_STUB)
0061 
0062 static ngx_atomic_t   ngx_stat_accepted0;
0063 ngx_atomic_t         *ngx_stat_accepted = &ngx_stat_accepted0;
0064 static ngx_atomic_t   ngx_stat_handled0;
0065 ngx_atomic_t         *ngx_stat_handled = &ngx_stat_handled0;
0066 static ngx_atomic_t   ngx_stat_requests0;
0067 ngx_atomic_t         *ngx_stat_requests = &ngx_stat_requests0;
0068 static ngx_atomic_t   ngx_stat_active0;
0069 ngx_atomic_t         *ngx_stat_active = &ngx_stat_active0;
0070 static ngx_atomic_t   ngx_stat_reading0;
0071 ngx_atomic_t         *ngx_stat_reading = &ngx_stat_reading0;
0072 static ngx_atomic_t   ngx_stat_writing0;
0073 ngx_atomic_t         *ngx_stat_writing = &ngx_stat_writing0;
0074 static ngx_atomic_t   ngx_stat_waiting0;
0075 ngx_atomic_t         *ngx_stat_waiting = &ngx_stat_waiting0;
0076 
0077 #endif
0078 
0079 
0080 
0081 static ngx_command_t  ngx_events_commands[] = {
0082 
0083     { ngx_string("events"),
0084       NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
0085       ngx_events_block,
0086       0,
0087       0,
0088       NULL },
0089 
0090       ngx_null_command
0091 };
0092 
0093 
0094 static ngx_core_module_t  ngx_events_module_ctx = {
0095     ngx_string("events"),
0096     NULL,
0097     ngx_event_init_conf
0098 };
0099 
0100 
0101 ngx_module_t  ngx_events_module = {
0102     NGX_MODULE_V1,
0103     &ngx_events_module_ctx,                /* module context */
0104     ngx_events_commands,                   /* module directives */
0105     NGX_CORE_MODULE,                       /* module type */
0106     NULL,                                  /* init master */
0107     NULL,                                  /* init module */
0108     NULL,                                  /* init process */
0109     NULL,                                  /* init thread */
0110     NULL,                                  /* exit thread */
0111     NULL,                                  /* exit process */
0112     NULL,                                  /* exit master */
0113     NGX_MODULE_V1_PADDING
0114 };
0115 
0116 
0117 static ngx_str_t  event_core_name = ngx_string("event_core");
0118 
0119 
0120 static ngx_command_t  ngx_event_core_commands[] = {
0121 
0122     { ngx_string("worker_connections"),
0123       NGX_EVENT_CONF|NGX_CONF_TAKE1,
0124       ngx_event_connections,
0125       0,
0126       0,
0127       NULL },
0128 
0129     { ngx_string("use"),
0130       NGX_EVENT_CONF|NGX_CONF_TAKE1,
0131       ngx_event_use,
0132       0,
0133       0,
0134       NULL },
0135 
0136     { ngx_string("multi_accept"),
0137       NGX_EVENT_CONF|NGX_CONF_FLAG,
0138       ngx_conf_set_flag_slot,
0139       0,
0140       offsetof(ngx_event_conf_t, multi_accept),
0141       NULL },
0142 
0143     { ngx_string("accept_mutex"),
0144       NGX_EVENT_CONF|NGX_CONF_FLAG,
0145       ngx_conf_set_flag_slot,
0146       0,
0147       offsetof(ngx_event_conf_t, accept_mutex),
0148       NULL },
0149 
0150     { ngx_string("accept_mutex_delay"),
0151       NGX_EVENT_CONF|NGX_CONF_TAKE1,
0152       ngx_conf_set_msec_slot,
0153       0,
0154       offsetof(ngx_event_conf_t, accept_mutex_delay),
0155       NULL },
0156 
0157     { ngx_string("debug_connection"),
0158       NGX_EVENT_CONF|NGX_CONF_TAKE1,
0159       ngx_event_debug_connection,
0160       0,
0161       0,
0162       NULL },
0163 
0164       ngx_null_command
0165 };
0166 
0167 
0168 static ngx_event_module_t  ngx_event_core_module_ctx = {
0169     &event_core_name,
0170     ngx_event_core_create_conf,            /* create configuration */
0171     ngx_event_core_init_conf,              /* init configuration */
0172 
0173     { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
0174 };
0175 
0176 
0177 ngx_module_t  ngx_event_core_module = {
0178     NGX_MODULE_V1,
0179     &ngx_event_core_module_ctx,            /* module context */
0180     ngx_event_core_commands,               /* module directives */
0181     NGX_EVENT_MODULE,                      /* module type */
0182     NULL,                                  /* init master */
0183     ngx_event_module_init,                 /* init module */
0184     ngx_event_process_init,                /* init process */
0185     NULL,                                  /* init thread */
0186     NULL,                                  /* exit thread */
0187     NULL,                                  /* exit process */
0188     NULL,                                  /* exit master */
0189     NGX_MODULE_V1_PADDING
0190 };
0191 
0192 
0193 void
0194 ngx_process_events_and_timers(ngx_cycle_t *cycle)
0195 {
0196     ngx_uint_t  flags;
0197     ngx_msec_t  timer, delta;
0198 
0199     if (ngx_timer_resolution) {
0200         timer = NGX_TIMER_INFINITE;
0201         flags = 0;
0202 
0203     } else {
0204         timer = ngx_event_find_timer();
0205         flags = NGX_UPDATE_TIME;
0206 
0207 #if (NGX_WIN32)
0208 
0209         /* handle signals from master in case of network inactivity */
0210 
0211         if (timer == NGX_TIMER_INFINITE || timer > 500) {
0212             timer = 500;
0213         }
0214 
0215 #endif
0216     }
0217 
0218     if (ngx_use_accept_mutex) {
0219         if (ngx_accept_disabled > 0) {
0220             ngx_accept_disabled--;
0221 
0222         } else {
0223             if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
0224                 return;
0225             }
0226 
0227             if (ngx_accept_mutex_held) {
0228                 flags |= NGX_POST_EVENTS;
0229 
0230             } else {
0231                 if (timer == NGX_TIMER_INFINITE
0232                     || timer > ngx_accept_mutex_delay)
0233                 {
0234                     timer = ngx_accept_mutex_delay;
0235                 }
0236             }
0237         }
0238     }
0239 
0240     delta = ngx_current_msec;
0241 
0242     (void) ngx_process_events(cycle, timer, flags);
0243 
0244     delta = ngx_current_msec - delta;
0245 
0246     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0247                    "timer delta: %M", delta);
0248 
0249     ngx_event_process_posted(cycle, &ngx_posted_accept_events);
0250 
0251     if (ngx_accept_mutex_held) {
0252         ngx_shmtx_unlock(&ngx_accept_mutex);
0253     }
0254 
0255     if (delta) {
0256         ngx_event_expire_timers();
0257     }
0258 
0259     ngx_event_process_posted(cycle, &ngx_posted_events);
0260 }
0261 
0262 
0263 ngx_int_t
0264 ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags)
0265 {
0266     if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
0267 
0268         /* kqueue, epoll */
0269 
0270         if (!rev->active && !rev->ready) {
0271             if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)
0272                 == NGX_ERROR)
0273             {
0274                 return NGX_ERROR;
0275             }
0276         }
0277 
0278         return NGX_OK;
0279 
0280     } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
0281 
0282         /* select, poll, /dev/poll */
0283 
0284         if (!rev->active && !rev->ready) {
0285             if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT)
0286                 == NGX_ERROR)
0287             {
0288                 return NGX_ERROR;
0289             }
0290 
0291             return NGX_OK;
0292         }
0293 
0294         if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {
0295             if (ngx_del_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT | flags)
0296                 == NGX_ERROR)
0297             {
0298                 return NGX_ERROR;
0299             }
0300 
0301             return NGX_OK;
0302         }
0303 
0304     } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
0305 
0306         /* event ports */
0307 
0308         if (!rev->active && !rev->ready) {
0309             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0310                 return NGX_ERROR;
0311             }
0312 
0313             return NGX_OK;
0314         }
0315 
0316         if (rev->oneshot && !rev->ready) {
0317             if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0318                 return NGX_ERROR;
0319             }
0320 
0321             return NGX_OK;
0322         }
0323     }
0324 
0325     /* iocp */
0326 
0327     return NGX_OK;
0328 }
0329 
0330 
0331 ngx_int_t
0332 ngx_handle_write_event(ngx_event_t *wev, size_t lowat)
0333 {
0334     ngx_connection_t  *c;
0335 
0336     if (lowat) {
0337         c = wev->data;
0338 
0339         if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
0340             return NGX_ERROR;
0341         }
0342     }
0343 
0344     if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
0345 
0346         /* kqueue, epoll */
0347 
0348         if (!wev->active && !wev->ready) {
0349             if (ngx_add_event(wev, NGX_WRITE_EVENT,
0350                               NGX_CLEAR_EVENT | (lowat ? NGX_LOWAT_EVENT : 0))
0351                 == NGX_ERROR)
0352             {
0353                 return NGX_ERROR;
0354             }
0355         }
0356 
0357         return NGX_OK;
0358 
0359     } else if (ngx_event_flags & NGX_USE_LEVEL_EVENT) {
0360 
0361         /* select, poll, /dev/poll */
0362 
0363         if (!wev->active && !wev->ready) {
0364             if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
0365                 == NGX_ERROR)
0366             {
0367                 return NGX_ERROR;
0368             }
0369 
0370             return NGX_OK;
0371         }
0372 
0373         if (wev->active && wev->ready) {
0374             if (ngx_del_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT)
0375                 == NGX_ERROR)
0376             {
0377                 return NGX_ERROR;
0378             }
0379 
0380             return NGX_OK;
0381         }
0382 
0383     } else if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
0384 
0385         /* event ports */
0386 
0387         if (!wev->active && !wev->ready) {
0388             if (ngx_add_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
0389                 return NGX_ERROR;
0390             }
0391 
0392             return NGX_OK;
0393         }
0394 
0395         if (wev->oneshot && wev->ready) {
0396             if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
0397                 return NGX_ERROR;
0398             }
0399 
0400             return NGX_OK;
0401         }
0402     }
0403 
0404     /* iocp */
0405 
0406     return NGX_OK;
0407 }
0408 
0409 
0410 static char *
0411 ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
0412 {
0413 #if (NGX_HAVE_REUSEPORT)
0414     ngx_uint_t        i;
0415     ngx_listening_t  *ls;
0416 #endif
0417 
0418     if (ngx_get_conf(cycle->conf_ctx, ngx_events_module) == NULL) {
0419         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0420                       "no \"events\" section in configuration");
0421         return NGX_CONF_ERROR;
0422     }
0423 
0424     if (cycle->connection_n < cycle->listening.nelts + 1) {
0425 
0426         /*
0427          * there should be at least one connection for each listening
0428          * socket, plus an additional connection for channel
0429          */
0430 
0431         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0432                       "%ui worker_connections are not enough "
0433                       "for %ui listening sockets",
0434                       cycle->connection_n, cycle->listening.nelts);
0435 
0436         return NGX_CONF_ERROR;
0437     }
0438 
0439 #if (NGX_HAVE_REUSEPORT)
0440 
0441     ls = cycle->listening.elts;
0442     for (i = 0; i < cycle->listening.nelts; i++) {
0443 
0444         if (!ls[i].reuseport || ls[i].worker != 0) {
0445             continue;
0446         }
0447 
0448         if (ngx_clone_listening(cycle, &ls[i]) != NGX_OK) {
0449             return NGX_CONF_ERROR;
0450         }
0451 
0452         /* cloning may change cycle->listening.elts */
0453 
0454         ls = cycle->listening.elts;
0455     }
0456 
0457 #endif
0458 
0459     return NGX_CONF_OK;
0460 }
0461 
0462 
0463 static ngx_int_t
0464 ngx_event_module_init(ngx_cycle_t *cycle)
0465 {
0466     void              ***cf;
0467     u_char              *shared;
0468     size_t               size, cl;
0469     ngx_shm_t            shm;
0470     ngx_time_t          *tp;
0471     ngx_core_conf_t     *ccf;
0472     ngx_event_conf_t    *ecf;
0473 
0474     cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
0475     ecf = (*cf)[ngx_event_core_module.ctx_index];
0476 
0477     if (!ngx_test_config && ngx_process <= NGX_PROCESS_MASTER) {
0478         ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
0479                       "using the \"%s\" event method", ecf->name);
0480     }
0481 
0482     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0483 
0484     ngx_timer_resolution = ccf->timer_resolution;
0485 
0486 #if !(NGX_WIN32)
0487     {
0488     ngx_int_t      limit;
0489     struct rlimit  rlmt;
0490 
0491     if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
0492         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0493                       "getrlimit(RLIMIT_NOFILE) failed, ignored");
0494 
0495     } else {
0496         if (ecf->connections > (ngx_uint_t) rlmt.rlim_cur
0497             && (ccf->rlimit_nofile == NGX_CONF_UNSET
0498                 || ecf->connections > (ngx_uint_t) ccf->rlimit_nofile))
0499         {
0500             limit = (ccf->rlimit_nofile == NGX_CONF_UNSET) ?
0501                          (ngx_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
0502 
0503             ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0504                           "%ui worker_connections exceed "
0505                           "open file resource limit: %i",
0506                           ecf->connections, limit);
0507         }
0508     }
0509     }
0510 #endif /* !(NGX_WIN32) */
0511 
0512 
0513     if (ccf->master == 0) {
0514         return NGX_OK;
0515     }
0516 
0517     if (ngx_accept_mutex_ptr) {
0518         return NGX_OK;
0519     }
0520 
0521 
0522     /* cl should be equal to or greater than cache line size */
0523 
0524     cl = 128;
0525 
0526     size = cl            /* ngx_accept_mutex */
0527            + cl          /* ngx_connection_counter */
0528            + cl;         /* ngx_temp_number */
0529 
0530 #if (NGX_STAT_STUB)
0531 
0532     size += cl           /* ngx_stat_accepted */
0533            + cl          /* ngx_stat_handled */
0534            + cl          /* ngx_stat_requests */
0535            + cl          /* ngx_stat_active */
0536            + cl          /* ngx_stat_reading */
0537            + cl          /* ngx_stat_writing */
0538            + cl;         /* ngx_stat_waiting */
0539 
0540 #endif
0541 
0542     shm.size = size;
0543     ngx_str_set(&shm.name, "nginx_shared_zone");
0544     shm.log = cycle->log;
0545 
0546     if (ngx_shm_alloc(&shm) != NGX_OK) {
0547         return NGX_ERROR;
0548     }
0549 
0550     shared = shm.addr;
0551 
0552     ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
0553     ngx_accept_mutex.spin = (ngx_uint_t) -1;
0554 
0555     if (ngx_shmtx_create(&ngx_accept_mutex, (ngx_shmtx_sh_t *) shared,
0556                          cycle->lock_file.data)
0557         != NGX_OK)
0558     {
0559         return NGX_ERROR;
0560     }
0561 
0562     ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
0563 
0564     (void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
0565 
0566     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0567                    "counter: %p, %uA",
0568                    ngx_connection_counter, *ngx_connection_counter);
0569 
0570     ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
0571 
0572     tp = ngx_timeofday();
0573 
0574     ngx_random_number = (tp->msec << 16) + ngx_pid;
0575 
0576 #if (NGX_STAT_STUB)
0577 
0578     ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
0579     ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
0580     ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
0581     ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
0582     ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
0583     ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
0584     ngx_stat_waiting = (ngx_atomic_t *) (shared + 9 * cl);
0585 
0586 #endif
0587 
0588     return NGX_OK;
0589 }
0590 
0591 
0592 #if !(NGX_WIN32)
0593 
0594 static void
0595 ngx_timer_signal_handler(int signo)
0596 {
0597     ngx_event_timer_alarm = 1;
0598 
0599 #if 1
0600     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, "timer signal");
0601 #endif
0602 }
0603 
0604 #endif
0605 
0606 
0607 static ngx_int_t
0608 ngx_event_process_init(ngx_cycle_t *cycle)
0609 {
0610     ngx_uint_t           m, i;
0611     ngx_event_t         *rev, *wev;
0612     ngx_listening_t     *ls;
0613     ngx_connection_t    *c, *next, *old;
0614     ngx_core_conf_t     *ccf;
0615     ngx_event_conf_t    *ecf;
0616     ngx_event_module_t  *module;
0617 
0618     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0619     ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
0620 
0621     if (ccf->master && ccf->worker_processes > 1 && ecf->accept_mutex) {
0622         ngx_use_accept_mutex = 1;
0623         ngx_accept_mutex_held = 0;
0624         ngx_accept_mutex_delay = ecf->accept_mutex_delay;
0625 
0626     } else {
0627         ngx_use_accept_mutex = 0;
0628     }
0629 
0630 #if (NGX_WIN32)
0631 
0632     /*
0633      * disable accept mutex on win32 as it may cause deadlock if
0634      * grabbed by a process which can't accept connections
0635      */
0636 
0637     ngx_use_accept_mutex = 0;
0638 
0639 #endif
0640 
0641     ngx_queue_init(&ngx_posted_accept_events);
0642     ngx_queue_init(&ngx_posted_events);
0643 
0644     if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
0645         return NGX_ERROR;
0646     }
0647 
0648     for (m = 0; cycle->modules[m]; m++) {
0649         if (cycle->modules[m]->type != NGX_EVENT_MODULE) {
0650             continue;
0651         }
0652 
0653         if (cycle->modules[m]->ctx_index != ecf->use) {
0654             continue;
0655         }
0656 
0657         module = cycle->modules[m]->ctx;
0658 
0659         if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
0660             /* fatal */
0661             exit(2);
0662         }
0663 
0664         break;
0665     }
0666 
0667 #if !(NGX_WIN32)
0668 
0669     if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
0670         struct sigaction  sa;
0671         struct itimerval  itv;
0672 
0673         ngx_memzero(&sa, sizeof(struct sigaction));
0674         sa.sa_handler = ngx_timer_signal_handler;
0675         sigemptyset(&sa.sa_mask);
0676 
0677         if (sigaction(SIGALRM, &sa, NULL) == -1) {
0678             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0679                           "sigaction(SIGALRM) failed");
0680             return NGX_ERROR;
0681         }
0682 
0683         itv.it_interval.tv_sec = ngx_timer_resolution / 1000;
0684         itv.it_interval.tv_usec = (ngx_timer_resolution % 1000) * 1000;
0685         itv.it_value.tv_sec = ngx_timer_resolution / 1000;
0686         itv.it_value.tv_usec = (ngx_timer_resolution % 1000 ) * 1000;
0687 
0688         if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
0689             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0690                           "setitimer() failed");
0691         }
0692     }
0693 
0694     if (ngx_event_flags & NGX_USE_FD_EVENT) {
0695         struct rlimit  rlmt;
0696 
0697         if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
0698             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0699                           "getrlimit(RLIMIT_NOFILE) failed");
0700             return NGX_ERROR;
0701         }
0702 
0703         cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
0704 
0705         cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
0706                                   cycle->log);
0707         if (cycle->files == NULL) {
0708             return NGX_ERROR;
0709         }
0710     }
0711 
0712 #else
0713 
0714     if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
0715         ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0716                       "the \"timer_resolution\" directive is not supported "
0717                       "with the configured event method, ignored");
0718         ngx_timer_resolution = 0;
0719     }
0720 
0721 #endif
0722 
0723     cycle->connections =
0724         ngx_alloc(sizeof(ngx_connection_t) * cycle->connection_n, cycle->log);
0725     if (cycle->connections == NULL) {
0726         return NGX_ERROR;
0727     }
0728 
0729     c = cycle->connections;
0730 
0731     cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
0732                                    cycle->log);
0733     if (cycle->read_events == NULL) {
0734         return NGX_ERROR;
0735     }
0736 
0737     rev = cycle->read_events;
0738     for (i = 0; i < cycle->connection_n; i++) {
0739         rev[i].closed = 1;
0740         rev[i].instance = 1;
0741     }
0742 
0743     cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
0744                                     cycle->log);
0745     if (cycle->write_events == NULL) {
0746         return NGX_ERROR;
0747     }
0748 
0749     wev = cycle->write_events;
0750     for (i = 0; i < cycle->connection_n; i++) {
0751         wev[i].closed = 1;
0752     }
0753 
0754     i = cycle->connection_n;
0755     next = NULL;
0756 
0757     do {
0758         i--;
0759 
0760         c[i].data = next;
0761         c[i].read = &cycle->read_events[i];
0762         c[i].write = &cycle->write_events[i];
0763         c[i].fd = (ngx_socket_t) -1;
0764 
0765         next = &c[i];
0766     } while (i);
0767 
0768     cycle->free_connections = next;
0769     cycle->free_connection_n = cycle->connection_n;
0770 
0771     /* for each listening socket */
0772 
0773     ls = cycle->listening.elts;
0774     for (i = 0; i < cycle->listening.nelts; i++) {
0775 
0776 #if (NGX_HAVE_REUSEPORT)
0777         if (ls[i].reuseport && ls[i].worker != ngx_worker) {
0778             continue;
0779         }
0780 #endif
0781 
0782         c = ngx_get_connection(ls[i].fd, cycle->log);
0783 
0784         if (c == NULL) {
0785             return NGX_ERROR;
0786         }
0787 
0788         c->type = ls[i].type;
0789         c->log = &ls[i].log;
0790 
0791         c->listening = &ls[i];
0792         ls[i].connection = c;
0793 
0794         rev = c->read;
0795 
0796         rev->log = c->log;
0797         rev->accept = 1;
0798 
0799 #if (NGX_HAVE_DEFERRED_ACCEPT)
0800         rev->deferred_accept = ls[i].deferred_accept;
0801 #endif
0802 
0803         if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
0804             if (ls[i].previous) {
0805 
0806                 /*
0807                  * delete the old accept events that were bound to
0808                  * the old cycle read events array
0809                  */
0810 
0811                 old = ls[i].previous->connection;
0812 
0813                 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
0814                     == NGX_ERROR)
0815                 {
0816                     return NGX_ERROR;
0817                 }
0818 
0819                 old->fd = (ngx_socket_t) -1;
0820             }
0821         }
0822 
0823 #if (NGX_WIN32)
0824 
0825         if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
0826             ngx_iocp_conf_t  *iocpcf;
0827 
0828             rev->handler = ngx_event_acceptex;
0829 
0830             if (ngx_use_accept_mutex) {
0831                 continue;
0832             }
0833 
0834             if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
0835                 return NGX_ERROR;
0836             }
0837 
0838             ls[i].log.handler = ngx_acceptex_log_error;
0839 
0840             iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
0841             if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
0842                 == NGX_ERROR)
0843             {
0844                 return NGX_ERROR;
0845             }
0846 
0847         } else {
0848             rev->handler = ngx_event_accept;
0849 
0850             if (ngx_use_accept_mutex) {
0851                 continue;
0852             }
0853 
0854             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0855                 return NGX_ERROR;
0856             }
0857         }
0858 
0859 #else
0860 
0861         rev->handler = (c->type == SOCK_STREAM) ? ngx_event_accept
0862                                                 : ngx_event_recvmsg;
0863 
0864 #if (NGX_HAVE_REUSEPORT)
0865 
0866         if (ls[i].reuseport) {
0867             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0868                 return NGX_ERROR;
0869             }
0870 
0871             continue;
0872         }
0873 
0874 #endif
0875 
0876         if (ngx_use_accept_mutex) {
0877             continue;
0878         }
0879 
0880 #if (NGX_HAVE_EPOLLEXCLUSIVE)
0881 
0882         if ((ngx_event_flags & NGX_USE_EPOLL_EVENT)
0883             && ccf->worker_processes > 1)
0884         {
0885             if (ngx_add_event(rev, NGX_READ_EVENT, NGX_EXCLUSIVE_EVENT)
0886                 == NGX_ERROR)
0887             {
0888                 return NGX_ERROR;
0889             }
0890 
0891             continue;
0892         }
0893 
0894 #endif
0895 
0896         if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0897             return NGX_ERROR;
0898         }
0899 
0900 #endif
0901 
0902     }
0903 
0904     return NGX_OK;
0905 }
0906 
0907 
0908 ngx_int_t
0909 ngx_send_lowat(ngx_connection_t *c, size_t lowat)
0910 {
0911     int  sndlowat;
0912 
0913 #if (NGX_HAVE_LOWAT_EVENT)
0914 
0915     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0916         c->write->available = lowat;
0917         return NGX_OK;
0918     }
0919 
0920 #endif
0921 
0922     if (lowat == 0 || c->sndlowat) {
0923         return NGX_OK;
0924     }
0925 
0926     sndlowat = (int) lowat;
0927 
0928     if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
0929                    (const void *) &sndlowat, sizeof(int))
0930         == -1)
0931     {
0932         ngx_connection_error(c, ngx_socket_errno,
0933                              "setsockopt(SO_SNDLOWAT) failed");
0934         return NGX_ERROR;
0935     }
0936 
0937     c->sndlowat = 1;
0938 
0939     return NGX_OK;
0940 }
0941 
0942 
0943 static char *
0944 ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0945 {
0946     char                 *rv;
0947     void               ***ctx;
0948     ngx_uint_t            i;
0949     ngx_conf_t            pcf;
0950     ngx_event_module_t   *m;
0951 
0952     if (*(void **) conf) {
0953         return "is duplicate";
0954     }
0955 
0956     /* count the number of the event modules and set up their indices */
0957 
0958     ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
0959 
0960     ctx = ngx_pcalloc(cf->pool, sizeof(void *));
0961     if (ctx == NULL) {
0962         return NGX_CONF_ERROR;
0963     }
0964 
0965     *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
0966     if (*ctx == NULL) {
0967         return NGX_CONF_ERROR;
0968     }
0969 
0970     *(void **) conf = ctx;
0971 
0972     for (i = 0; cf->cycle->modules[i]; i++) {
0973         if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
0974             continue;
0975         }
0976 
0977         m = cf->cycle->modules[i]->ctx;
0978 
0979         if (m->create_conf) {
0980             (*ctx)[cf->cycle->modules[i]->ctx_index] =
0981                                                      m->create_conf(cf->cycle);
0982             if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
0983                 return NGX_CONF_ERROR;
0984             }
0985         }
0986     }
0987 
0988     pcf = *cf;
0989     cf->ctx = ctx;
0990     cf->module_type = NGX_EVENT_MODULE;
0991     cf->cmd_type = NGX_EVENT_CONF;
0992 
0993     rv = ngx_conf_parse(cf, NULL);
0994 
0995     *cf = pcf;
0996 
0997     if (rv != NGX_CONF_OK) {
0998         return rv;
0999     }
1000 
1001     for (i = 0; cf->cycle->modules[i]; i++) {
1002         if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
1003             continue;
1004         }
1005 
1006         m = cf->cycle->modules[i]->ctx;
1007 
1008         if (m->init_conf) {
1009             rv = m->init_conf(cf->cycle,
1010                               (*ctx)[cf->cycle->modules[i]->ctx_index]);
1011             if (rv != NGX_CONF_OK) {
1012                 return rv;
1013             }
1014         }
1015     }
1016 
1017     return NGX_CONF_OK;
1018 }
1019 
1020 
1021 static char *
1022 ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1023 {
1024     ngx_event_conf_t  *ecf = conf;
1025 
1026     ngx_str_t  *value;
1027 
1028     if (ecf->connections != NGX_CONF_UNSET_UINT) {
1029         return "is duplicate";
1030     }
1031 
1032     value = cf->args->elts;
1033     ecf->connections = ngx_atoi(value[1].data, value[1].len);
1034     if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
1035         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1036                            "invalid number \"%V\"", &value[1]);
1037 
1038         return NGX_CONF_ERROR;
1039     }
1040 
1041     cf->cycle->connection_n = ecf->connections;
1042 
1043     return NGX_CONF_OK;
1044 }
1045 
1046 
1047 static char *
1048 ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1049 {
1050     ngx_event_conf_t  *ecf = conf;
1051 
1052     ngx_int_t             m;
1053     ngx_str_t            *value;
1054     ngx_event_conf_t     *old_ecf;
1055     ngx_event_module_t   *module;
1056 
1057     if (ecf->use != NGX_CONF_UNSET_UINT) {
1058         return "is duplicate";
1059     }
1060 
1061     value = cf->args->elts;
1062 
1063     if (cf->cycle->old_cycle->conf_ctx) {
1064         old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
1065                                      ngx_event_core_module);
1066     } else {
1067         old_ecf = NULL;
1068     }
1069 
1070 
1071     for (m = 0; cf->cycle->modules[m]; m++) {
1072         if (cf->cycle->modules[m]->type != NGX_EVENT_MODULE) {
1073             continue;
1074         }
1075 
1076         module = cf->cycle->modules[m]->ctx;
1077         if (module->name->len == value[1].len) {
1078             if (ngx_strcmp(module->name->data, value[1].data) == 0) {
1079                 ecf->use = cf->cycle->modules[m]->ctx_index;
1080                 ecf->name = module->name->data;
1081 
1082                 if (ngx_process == NGX_PROCESS_SINGLE
1083                     && old_ecf
1084                     && old_ecf->use != ecf->use)
1085                 {
1086                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1087                                "when the server runs without a master process "
1088                                "the \"%V\" event type must be the same as "
1089                                "in previous configuration - \"%s\" "
1090                                "and it cannot be changed on the fly, "
1091                                "to change it you need to stop server "
1092                                "and start it again",
1093                                &value[1], old_ecf->name);
1094 
1095                     return NGX_CONF_ERROR;
1096                 }
1097 
1098                 return NGX_CONF_OK;
1099             }
1100         }
1101     }
1102 
1103     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1104                        "invalid event type \"%V\"", &value[1]);
1105 
1106     return NGX_CONF_ERROR;
1107 }
1108 
1109 
1110 static char *
1111 ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1112 {
1113 #if (NGX_DEBUG)
1114     ngx_event_conf_t  *ecf = conf;
1115 
1116     ngx_int_t             rc;
1117     ngx_str_t            *value;
1118     ngx_url_t             u;
1119     ngx_cidr_t            c, *cidr;
1120     ngx_uint_t            i;
1121     struct sockaddr_in   *sin;
1122 #if (NGX_HAVE_INET6)
1123     struct sockaddr_in6  *sin6;
1124 #endif
1125 
1126     value = cf->args->elts;
1127 
1128 #if (NGX_HAVE_UNIX_DOMAIN)
1129 
1130     if (ngx_strcmp(value[1].data, "unix:") == 0) {
1131         cidr = ngx_array_push(&ecf->debug_connection);
1132         if (cidr == NULL) {
1133             return NGX_CONF_ERROR;
1134         }
1135 
1136         cidr->family = AF_UNIX;
1137         return NGX_CONF_OK;
1138     }
1139 
1140 #endif
1141 
1142     rc = ngx_ptocidr(&value[1], &c);
1143 
1144     if (rc != NGX_ERROR) {
1145         if (rc == NGX_DONE) {
1146             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1147                                "low address bits of %V are meaningless",
1148                                &value[1]);
1149         }
1150 
1151         cidr = ngx_array_push(&ecf->debug_connection);
1152         if (cidr == NULL) {
1153             return NGX_CONF_ERROR;
1154         }
1155 
1156         *cidr = c;
1157 
1158         return NGX_CONF_OK;
1159     }
1160 
1161     ngx_memzero(&u, sizeof(ngx_url_t));
1162     u.host = value[1];
1163 
1164     if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
1165         if (u.err) {
1166             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1167                                "%s in debug_connection \"%V\"",
1168                                u.err, &u.host);
1169         }
1170 
1171         return NGX_CONF_ERROR;
1172     }
1173 
1174     cidr = ngx_array_push_n(&ecf->debug_connection, u.naddrs);
1175     if (cidr == NULL) {
1176         return NGX_CONF_ERROR;
1177     }
1178 
1179     ngx_memzero(cidr, u.naddrs * sizeof(ngx_cidr_t));
1180 
1181     for (i = 0; i < u.naddrs; i++) {
1182         cidr[i].family = u.addrs[i].sockaddr->sa_family;
1183 
1184         switch (cidr[i].family) {
1185 
1186 #if (NGX_HAVE_INET6)
1187         case AF_INET6:
1188             sin6 = (struct sockaddr_in6 *) u.addrs[i].sockaddr;
1189             cidr[i].u.in6.addr = sin6->sin6_addr;
1190             ngx_memset(cidr[i].u.in6.mask.s6_addr, 0xff, 16);
1191             break;
1192 #endif
1193 
1194         default: /* AF_INET */
1195             sin = (struct sockaddr_in *) u.addrs[i].sockaddr;
1196             cidr[i].u.in.addr = sin->sin_addr.s_addr;
1197             cidr[i].u.in.mask = 0xffffffff;
1198             break;
1199         }
1200     }
1201 
1202 #else
1203 
1204     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1205                        "\"debug_connection\" is ignored, you need to rebuild "
1206                        "nginx using --with-debug option to enable it");
1207 
1208 #endif
1209 
1210     return NGX_CONF_OK;
1211 }
1212 
1213 
1214 static void *
1215 ngx_event_core_create_conf(ngx_cycle_t *cycle)
1216 {
1217     ngx_event_conf_t  *ecf;
1218 
1219     ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
1220     if (ecf == NULL) {
1221         return NULL;
1222     }
1223 
1224     ecf->connections = NGX_CONF_UNSET_UINT;
1225     ecf->use = NGX_CONF_UNSET_UINT;
1226     ecf->multi_accept = NGX_CONF_UNSET;
1227     ecf->accept_mutex = NGX_CONF_UNSET;
1228     ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
1229     ecf->name = (void *) NGX_CONF_UNSET;
1230 
1231 #if (NGX_DEBUG)
1232 
1233     if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
1234                        sizeof(ngx_cidr_t)) == NGX_ERROR)
1235     {
1236         return NULL;
1237     }
1238 
1239 #endif
1240 
1241     return ecf;
1242 }
1243 
1244 
1245 static char *
1246 ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)
1247 {
1248     ngx_event_conf_t  *ecf = conf;
1249 
1250 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1251     int                  fd;
1252 #endif
1253     ngx_int_t            i;
1254     ngx_module_t        *module;
1255     ngx_event_module_t  *event_module;
1256 
1257     module = NULL;
1258 
1259 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1260 
1261     fd = epoll_create(100);
1262 
1263     if (fd != -1) {
1264         (void) close(fd);
1265         module = &ngx_epoll_module;
1266 
1267     } else if (ngx_errno != NGX_ENOSYS) {
1268         module = &ngx_epoll_module;
1269     }
1270 
1271 #endif
1272 
1273 #if (NGX_HAVE_DEVPOLL) && !(NGX_TEST_BUILD_DEVPOLL)
1274 
1275     module = &ngx_devpoll_module;
1276 
1277 #endif
1278 
1279 #if (NGX_HAVE_KQUEUE)
1280 
1281     module = &ngx_kqueue_module;
1282 
1283 #endif
1284 
1285 #if (NGX_HAVE_SELECT)
1286 
1287     if (module == NULL) {
1288         module = &ngx_select_module;
1289     }
1290 
1291 #endif
1292 
1293     if (module == NULL) {
1294         for (i = 0; cycle->modules[i]; i++) {
1295 
1296             if (cycle->modules[i]->type != NGX_EVENT_MODULE) {
1297                 continue;
1298             }
1299 
1300             event_module = cycle->modules[i]->ctx;
1301 
1302             if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0)
1303             {
1304                 continue;
1305             }
1306 
1307             module = cycle->modules[i];
1308             break;
1309         }
1310     }
1311 
1312     if (module == NULL) {
1313         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
1314         return NGX_CONF_ERROR;
1315     }
1316 
1317     ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
1318     cycle->connection_n = ecf->connections;
1319 
1320     ngx_conf_init_uint_value(ecf->use, module->ctx_index);
1321 
1322     event_module = module->ctx;
1323     ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
1324 
1325     ngx_conf_init_value(ecf->multi_accept, 0);
1326     ngx_conf_init_value(ecf->accept_mutex, 0);
1327     ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
1328 
1329     return NGX_CONF_OK;
1330 }