Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.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_get_conf(cycle->conf_ctx, ngx_events_module) == NULL) {
0414         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0415                       "no \"events\" section in configuration");
0416         return NGX_CONF_ERROR;
0417     }
0418 
0419     return NGX_CONF_OK;
0420 }
0421 
0422 
0423 static ngx_int_t
0424 ngx_event_module_init(ngx_cycle_t *cycle)
0425 {
0426     void              ***cf;
0427     u_char              *shared;
0428     size_t               size, cl;
0429     ngx_shm_t            shm;
0430     ngx_time_t          *tp;
0431     ngx_core_conf_t     *ccf;
0432     ngx_event_conf_t    *ecf;
0433 
0434     cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
0435     ecf = (*cf)[ngx_event_core_module.ctx_index];
0436 
0437     if (!ngx_test_config && ngx_process <= NGX_PROCESS_MASTER) {
0438         ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
0439                       "using the \"%s\" event method", ecf->name);
0440     }
0441 
0442     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0443 
0444     ngx_timer_resolution = ccf->timer_resolution;
0445 
0446 #if !(NGX_WIN32)
0447     {
0448     ngx_int_t      limit;
0449     struct rlimit  rlmt;
0450 
0451     if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
0452         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0453                       "getrlimit(RLIMIT_NOFILE) failed, ignored");
0454 
0455     } else {
0456         if (ecf->connections > (ngx_uint_t) rlmt.rlim_cur
0457             && (ccf->rlimit_nofile == NGX_CONF_UNSET
0458                 || ecf->connections > (ngx_uint_t) ccf->rlimit_nofile))
0459         {
0460             limit = (ccf->rlimit_nofile == NGX_CONF_UNSET) ?
0461                          (ngx_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
0462 
0463             ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0464                           "%ui worker_connections exceed "
0465                           "open file resource limit: %i",
0466                           ecf->connections, limit);
0467         }
0468     }
0469     }
0470 #endif /* !(NGX_WIN32) */
0471 
0472 
0473     if (ccf->master == 0) {
0474         return NGX_OK;
0475     }
0476 
0477     if (ngx_accept_mutex_ptr) {
0478         return NGX_OK;
0479     }
0480 
0481 
0482     /* cl should be equal to or greater than cache line size */
0483 
0484     cl = 128;
0485 
0486     size = cl            /* ngx_accept_mutex */
0487            + cl          /* ngx_connection_counter */
0488            + cl;         /* ngx_temp_number */
0489 
0490 #if (NGX_STAT_STUB)
0491 
0492     size += cl           /* ngx_stat_accepted */
0493            + cl          /* ngx_stat_handled */
0494            + cl          /* ngx_stat_requests */
0495            + cl          /* ngx_stat_active */
0496            + cl          /* ngx_stat_reading */
0497            + cl          /* ngx_stat_writing */
0498            + cl;         /* ngx_stat_waiting */
0499 
0500 #endif
0501 
0502     shm.size = size;
0503     ngx_str_set(&shm.name, "nginx_shared_zone");
0504     shm.log = cycle->log;
0505 
0506     if (ngx_shm_alloc(&shm) != NGX_OK) {
0507         return NGX_ERROR;
0508     }
0509 
0510     shared = shm.addr;
0511 
0512     ngx_accept_mutex_ptr = (ngx_atomic_t *) shared;
0513     ngx_accept_mutex.spin = (ngx_uint_t) -1;
0514 
0515     if (ngx_shmtx_create(&ngx_accept_mutex, (ngx_shmtx_sh_t *) shared,
0516                          cycle->lock_file.data)
0517         != NGX_OK)
0518     {
0519         return NGX_ERROR;
0520     }
0521 
0522     ngx_connection_counter = (ngx_atomic_t *) (shared + 1 * cl);
0523 
0524     (void) ngx_atomic_cmp_set(ngx_connection_counter, 0, 1);
0525 
0526     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0527                    "counter: %p, %uA",
0528                    ngx_connection_counter, *ngx_connection_counter);
0529 
0530     ngx_temp_number = (ngx_atomic_t *) (shared + 2 * cl);
0531 
0532     tp = ngx_timeofday();
0533 
0534     ngx_random_number = (tp->msec << 16) + ngx_pid;
0535 
0536 #if (NGX_STAT_STUB)
0537 
0538     ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
0539     ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
0540     ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
0541     ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
0542     ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
0543     ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
0544     ngx_stat_waiting = (ngx_atomic_t *) (shared + 9 * cl);
0545 
0546 #endif
0547 
0548     return NGX_OK;
0549 }
0550 
0551 
0552 #if !(NGX_WIN32)
0553 
0554 static void
0555 ngx_timer_signal_handler(int signo)
0556 {
0557     ngx_event_timer_alarm = 1;
0558 
0559 #if 1
0560     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, "timer signal");
0561 #endif
0562 }
0563 
0564 #endif
0565 
0566 
0567 static ngx_int_t
0568 ngx_event_process_init(ngx_cycle_t *cycle)
0569 {
0570     ngx_uint_t           m, i;
0571     ngx_event_t         *rev, *wev;
0572     ngx_listening_t     *ls;
0573     ngx_connection_t    *c, *next, *old;
0574     ngx_core_conf_t     *ccf;
0575     ngx_event_conf_t    *ecf;
0576     ngx_event_module_t  *module;
0577 
0578     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0579     ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
0580 
0581     if (ccf->master && ccf->worker_processes > 1 && ecf->accept_mutex) {
0582         ngx_use_accept_mutex = 1;
0583         ngx_accept_mutex_held = 0;
0584         ngx_accept_mutex_delay = ecf->accept_mutex_delay;
0585 
0586     } else {
0587         ngx_use_accept_mutex = 0;
0588     }
0589 
0590 #if (NGX_WIN32)
0591 
0592     /*
0593      * disable accept mutex on win32 as it may cause deadlock if
0594      * grabbed by a process which can't accept connections
0595      */
0596 
0597     ngx_use_accept_mutex = 0;
0598 
0599 #endif
0600 
0601     ngx_queue_init(&ngx_posted_accept_events);
0602     ngx_queue_init(&ngx_posted_events);
0603 
0604     if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
0605         return NGX_ERROR;
0606     }
0607 
0608     for (m = 0; cycle->modules[m]; m++) {
0609         if (cycle->modules[m]->type != NGX_EVENT_MODULE) {
0610             continue;
0611         }
0612 
0613         if (cycle->modules[m]->ctx_index != ecf->use) {
0614             continue;
0615         }
0616 
0617         module = cycle->modules[m]->ctx;
0618 
0619         if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
0620             /* fatal */
0621             exit(2);
0622         }
0623 
0624         break;
0625     }
0626 
0627 #if !(NGX_WIN32)
0628 
0629     if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
0630         struct sigaction  sa;
0631         struct itimerval  itv;
0632 
0633         ngx_memzero(&sa, sizeof(struct sigaction));
0634         sa.sa_handler = ngx_timer_signal_handler;
0635         sigemptyset(&sa.sa_mask);
0636 
0637         if (sigaction(SIGALRM, &sa, NULL) == -1) {
0638             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0639                           "sigaction(SIGALRM) failed");
0640             return NGX_ERROR;
0641         }
0642 
0643         itv.it_interval.tv_sec = ngx_timer_resolution / 1000;
0644         itv.it_interval.tv_usec = (ngx_timer_resolution % 1000) * 1000;
0645         itv.it_value.tv_sec = ngx_timer_resolution / 1000;
0646         itv.it_value.tv_usec = (ngx_timer_resolution % 1000 ) * 1000;
0647 
0648         if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
0649             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0650                           "setitimer() failed");
0651         }
0652     }
0653 
0654     if (ngx_event_flags & NGX_USE_FD_EVENT) {
0655         struct rlimit  rlmt;
0656 
0657         if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
0658             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0659                           "getrlimit(RLIMIT_NOFILE) failed");
0660             return NGX_ERROR;
0661         }
0662 
0663         cycle->files_n = (ngx_uint_t) rlmt.rlim_cur;
0664 
0665         cycle->files = ngx_calloc(sizeof(ngx_connection_t *) * cycle->files_n,
0666                                   cycle->log);
0667         if (cycle->files == NULL) {
0668             return NGX_ERROR;
0669         }
0670     }
0671 
0672 #else
0673 
0674     if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
0675         ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0676                       "the \"timer_resolution\" directive is not supported "
0677                       "with the configured event method, ignored");
0678         ngx_timer_resolution = 0;
0679     }
0680 
0681 #endif
0682 
0683     cycle->connections =
0684         ngx_alloc(sizeof(ngx_connection_t) * cycle->connection_n, cycle->log);
0685     if (cycle->connections == NULL) {
0686         return NGX_ERROR;
0687     }
0688 
0689     c = cycle->connections;
0690 
0691     cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
0692                                    cycle->log);
0693     if (cycle->read_events == NULL) {
0694         return NGX_ERROR;
0695     }
0696 
0697     rev = cycle->read_events;
0698     for (i = 0; i < cycle->connection_n; i++) {
0699         rev[i].closed = 1;
0700         rev[i].instance = 1;
0701     }
0702 
0703     cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
0704                                     cycle->log);
0705     if (cycle->write_events == NULL) {
0706         return NGX_ERROR;
0707     }
0708 
0709     wev = cycle->write_events;
0710     for (i = 0; i < cycle->connection_n; i++) {
0711         wev[i].closed = 1;
0712     }
0713 
0714     i = cycle->connection_n;
0715     next = NULL;
0716 
0717     do {
0718         i--;
0719 
0720         c[i].data = next;
0721         c[i].read = &cycle->read_events[i];
0722         c[i].write = &cycle->write_events[i];
0723         c[i].fd = (ngx_socket_t) -1;
0724 
0725         next = &c[i];
0726     } while (i);
0727 
0728     cycle->free_connections = next;
0729     cycle->free_connection_n = cycle->connection_n;
0730 
0731     /* for each listening socket */
0732 
0733     ls = cycle->listening.elts;
0734     for (i = 0; i < cycle->listening.nelts; i++) {
0735 
0736 #if (NGX_HAVE_REUSEPORT)
0737         if (ls[i].reuseport && ls[i].worker != ngx_worker) {
0738             continue;
0739         }
0740 #endif
0741 
0742         c = ngx_get_connection(ls[i].fd, cycle->log);
0743 
0744         if (c == NULL) {
0745             return NGX_ERROR;
0746         }
0747 
0748         c->type = ls[i].type;
0749         c->log = &ls[i].log;
0750 
0751         c->listening = &ls[i];
0752         ls[i].connection = c;
0753 
0754         rev = c->read;
0755 
0756         rev->log = c->log;
0757         rev->accept = 1;
0758 
0759 #if (NGX_HAVE_DEFERRED_ACCEPT)
0760         rev->deferred_accept = ls[i].deferred_accept;
0761 #endif
0762 
0763         if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
0764             if (ls[i].previous) {
0765 
0766                 /*
0767                  * delete the old accept events that were bound to
0768                  * the old cycle read events array
0769                  */
0770 
0771                 old = ls[i].previous->connection;
0772 
0773                 if (ngx_del_event(old->read, NGX_READ_EVENT, NGX_CLOSE_EVENT)
0774                     == NGX_ERROR)
0775                 {
0776                     return NGX_ERROR;
0777                 }
0778 
0779                 old->fd = (ngx_socket_t) -1;
0780             }
0781         }
0782 
0783 #if (NGX_WIN32)
0784 
0785         if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
0786             ngx_iocp_conf_t  *iocpcf;
0787 
0788             rev->handler = ngx_event_acceptex;
0789 
0790             if (ngx_use_accept_mutex) {
0791                 continue;
0792             }
0793 
0794             if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
0795                 return NGX_ERROR;
0796             }
0797 
0798             ls[i].log.handler = ngx_acceptex_log_error;
0799 
0800             iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
0801             if (ngx_event_post_acceptex(&ls[i], iocpcf->post_acceptex)
0802                 == NGX_ERROR)
0803             {
0804                 return NGX_ERROR;
0805             }
0806 
0807         } else {
0808             rev->handler = ngx_event_accept;
0809 
0810             if (ngx_use_accept_mutex) {
0811                 continue;
0812             }
0813 
0814             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0815                 return NGX_ERROR;
0816             }
0817         }
0818 
0819 #else
0820 
0821         rev->handler = (c->type == SOCK_STREAM) ? ngx_event_accept
0822                                                 : ngx_event_recvmsg;
0823 
0824 #if (NGX_HAVE_REUSEPORT)
0825 
0826         if (ls[i].reuseport) {
0827             if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0828                 return NGX_ERROR;
0829             }
0830 
0831             continue;
0832         }
0833 
0834 #endif
0835 
0836         if (ngx_use_accept_mutex) {
0837             continue;
0838         }
0839 
0840 #if (NGX_HAVE_EPOLLEXCLUSIVE)
0841 
0842         if ((ngx_event_flags & NGX_USE_EPOLL_EVENT)
0843             && ccf->worker_processes > 1)
0844         {
0845             if (ngx_add_event(rev, NGX_READ_EVENT, NGX_EXCLUSIVE_EVENT)
0846                 == NGX_ERROR)
0847             {
0848                 return NGX_ERROR;
0849             }
0850 
0851             continue;
0852         }
0853 
0854 #endif
0855 
0856         if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
0857             return NGX_ERROR;
0858         }
0859 
0860 #endif
0861 
0862     }
0863 
0864     return NGX_OK;
0865 }
0866 
0867 
0868 ngx_int_t
0869 ngx_send_lowat(ngx_connection_t *c, size_t lowat)
0870 {
0871     int  sndlowat;
0872 
0873 #if (NGX_HAVE_LOWAT_EVENT)
0874 
0875     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0876         c->write->available = lowat;
0877         return NGX_OK;
0878     }
0879 
0880 #endif
0881 
0882     if (lowat == 0 || c->sndlowat) {
0883         return NGX_OK;
0884     }
0885 
0886     sndlowat = (int) lowat;
0887 
0888     if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT,
0889                    (const void *) &sndlowat, sizeof(int))
0890         == -1)
0891     {
0892         ngx_connection_error(c, ngx_socket_errno,
0893                              "setsockopt(SO_SNDLOWAT) failed");
0894         return NGX_ERROR;
0895     }
0896 
0897     c->sndlowat = 1;
0898 
0899     return NGX_OK;
0900 }
0901 
0902 
0903 static char *
0904 ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0905 {
0906     char                 *rv;
0907     void               ***ctx;
0908     ngx_uint_t            i;
0909     ngx_conf_t            pcf;
0910     ngx_event_module_t   *m;
0911 
0912     if (*(void **) conf) {
0913         return "is duplicate";
0914     }
0915 
0916     /* count the number of the event modules and set up their indices */
0917 
0918     ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
0919 
0920     ctx = ngx_pcalloc(cf->pool, sizeof(void *));
0921     if (ctx == NULL) {
0922         return NGX_CONF_ERROR;
0923     }
0924 
0925     *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
0926     if (*ctx == NULL) {
0927         return NGX_CONF_ERROR;
0928     }
0929 
0930     *(void **) conf = ctx;
0931 
0932     for (i = 0; cf->cycle->modules[i]; i++) {
0933         if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
0934             continue;
0935         }
0936 
0937         m = cf->cycle->modules[i]->ctx;
0938 
0939         if (m->create_conf) {
0940             (*ctx)[cf->cycle->modules[i]->ctx_index] =
0941                                                      m->create_conf(cf->cycle);
0942             if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
0943                 return NGX_CONF_ERROR;
0944             }
0945         }
0946     }
0947 
0948     pcf = *cf;
0949     cf->ctx = ctx;
0950     cf->module_type = NGX_EVENT_MODULE;
0951     cf->cmd_type = NGX_EVENT_CONF;
0952 
0953     rv = ngx_conf_parse(cf, NULL);
0954 
0955     *cf = pcf;
0956 
0957     if (rv != NGX_CONF_OK) {
0958         return rv;
0959     }
0960 
0961     for (i = 0; cf->cycle->modules[i]; i++) {
0962         if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
0963             continue;
0964         }
0965 
0966         m = cf->cycle->modules[i]->ctx;
0967 
0968         if (m->init_conf) {
0969             rv = m->init_conf(cf->cycle,
0970                               (*ctx)[cf->cycle->modules[i]->ctx_index]);
0971             if (rv != NGX_CONF_OK) {
0972                 return rv;
0973             }
0974         }
0975     }
0976 
0977     return NGX_CONF_OK;
0978 }
0979 
0980 
0981 static char *
0982 ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0983 {
0984     ngx_event_conf_t  *ecf = conf;
0985 
0986     ngx_str_t  *value;
0987 
0988     if (ecf->connections != NGX_CONF_UNSET_UINT) {
0989         return "is duplicate";
0990     }
0991 
0992     value = cf->args->elts;
0993     ecf->connections = ngx_atoi(value[1].data, value[1].len);
0994     if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
0995         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0996                            "invalid number \"%V\"", &value[1]);
0997 
0998         return NGX_CONF_ERROR;
0999     }
1000 
1001     cf->cycle->connection_n = ecf->connections;
1002 
1003     return NGX_CONF_OK;
1004 }
1005 
1006 
1007 static char *
1008 ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1009 {
1010     ngx_event_conf_t  *ecf = conf;
1011 
1012     ngx_int_t             m;
1013     ngx_str_t            *value;
1014     ngx_event_conf_t     *old_ecf;
1015     ngx_event_module_t   *module;
1016 
1017     if (ecf->use != NGX_CONF_UNSET_UINT) {
1018         return "is duplicate";
1019     }
1020 
1021     value = cf->args->elts;
1022 
1023     if (cf->cycle->old_cycle->conf_ctx) {
1024         old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
1025                                      ngx_event_core_module);
1026     } else {
1027         old_ecf = NULL;
1028     }
1029 
1030 
1031     for (m = 0; cf->cycle->modules[m]; m++) {
1032         if (cf->cycle->modules[m]->type != NGX_EVENT_MODULE) {
1033             continue;
1034         }
1035 
1036         module = cf->cycle->modules[m]->ctx;
1037         if (module->name->len == value[1].len) {
1038             if (ngx_strcmp(module->name->data, value[1].data) == 0) {
1039                 ecf->use = cf->cycle->modules[m]->ctx_index;
1040                 ecf->name = module->name->data;
1041 
1042                 if (ngx_process == NGX_PROCESS_SINGLE
1043                     && old_ecf
1044                     && old_ecf->use != ecf->use)
1045                 {
1046                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1047                                "when the server runs without a master process "
1048                                "the \"%V\" event type must be the same as "
1049                                "in previous configuration - \"%s\" "
1050                                "and it cannot be changed on the fly, "
1051                                "to change it you need to stop server "
1052                                "and start it again",
1053                                &value[1], old_ecf->name);
1054 
1055                     return NGX_CONF_ERROR;
1056                 }
1057 
1058                 return NGX_CONF_OK;
1059             }
1060         }
1061     }
1062 
1063     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1064                        "invalid event type \"%V\"", &value[1]);
1065 
1066     return NGX_CONF_ERROR;
1067 }
1068 
1069 
1070 static char *
1071 ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1072 {
1073 #if (NGX_DEBUG)
1074     ngx_event_conf_t  *ecf = conf;
1075 
1076     ngx_int_t             rc;
1077     ngx_str_t            *value;
1078     ngx_url_t             u;
1079     ngx_cidr_t            c, *cidr;
1080     ngx_uint_t            i;
1081     struct sockaddr_in   *sin;
1082 #if (NGX_HAVE_INET6)
1083     struct sockaddr_in6  *sin6;
1084 #endif
1085 
1086     value = cf->args->elts;
1087 
1088 #if (NGX_HAVE_UNIX_DOMAIN)
1089 
1090     if (ngx_strcmp(value[1].data, "unix:") == 0) {
1091         cidr = ngx_array_push(&ecf->debug_connection);
1092         if (cidr == NULL) {
1093             return NGX_CONF_ERROR;
1094         }
1095 
1096         cidr->family = AF_UNIX;
1097         return NGX_CONF_OK;
1098     }
1099 
1100 #endif
1101 
1102     rc = ngx_ptocidr(&value[1], &c);
1103 
1104     if (rc != NGX_ERROR) {
1105         if (rc == NGX_DONE) {
1106             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1107                                "low address bits of %V are meaningless",
1108                                &value[1]);
1109         }
1110 
1111         cidr = ngx_array_push(&ecf->debug_connection);
1112         if (cidr == NULL) {
1113             return NGX_CONF_ERROR;
1114         }
1115 
1116         *cidr = c;
1117 
1118         return NGX_CONF_OK;
1119     }
1120 
1121     ngx_memzero(&u, sizeof(ngx_url_t));
1122     u.host = value[1];
1123 
1124     if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
1125         if (u.err) {
1126             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1127                                "%s in debug_connection \"%V\"",
1128                                u.err, &u.host);
1129         }
1130 
1131         return NGX_CONF_ERROR;
1132     }
1133 
1134     cidr = ngx_array_push_n(&ecf->debug_connection, u.naddrs);
1135     if (cidr == NULL) {
1136         return NGX_CONF_ERROR;
1137     }
1138 
1139     ngx_memzero(cidr, u.naddrs * sizeof(ngx_cidr_t));
1140 
1141     for (i = 0; i < u.naddrs; i++) {
1142         cidr[i].family = u.addrs[i].sockaddr->sa_family;
1143 
1144         switch (cidr[i].family) {
1145 
1146 #if (NGX_HAVE_INET6)
1147         case AF_INET6:
1148             sin6 = (struct sockaddr_in6 *) u.addrs[i].sockaddr;
1149             cidr[i].u.in6.addr = sin6->sin6_addr;
1150             ngx_memset(cidr[i].u.in6.mask.s6_addr, 0xff, 16);
1151             break;
1152 #endif
1153 
1154         default: /* AF_INET */
1155             sin = (struct sockaddr_in *) u.addrs[i].sockaddr;
1156             cidr[i].u.in.addr = sin->sin_addr.s_addr;
1157             cidr[i].u.in.mask = 0xffffffff;
1158             break;
1159         }
1160     }
1161 
1162 #else
1163 
1164     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1165                        "\"debug_connection\" is ignored, you need to rebuild "
1166                        "nginx using --with-debug option to enable it");
1167 
1168 #endif
1169 
1170     return NGX_CONF_OK;
1171 }
1172 
1173 
1174 static void *
1175 ngx_event_core_create_conf(ngx_cycle_t *cycle)
1176 {
1177     ngx_event_conf_t  *ecf;
1178 
1179     ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
1180     if (ecf == NULL) {
1181         return NULL;
1182     }
1183 
1184     ecf->connections = NGX_CONF_UNSET_UINT;
1185     ecf->use = NGX_CONF_UNSET_UINT;
1186     ecf->multi_accept = NGX_CONF_UNSET;
1187     ecf->accept_mutex = NGX_CONF_UNSET;
1188     ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
1189     ecf->name = (void *) NGX_CONF_UNSET;
1190 
1191 #if (NGX_DEBUG)
1192 
1193     if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
1194                        sizeof(ngx_cidr_t)) == NGX_ERROR)
1195     {
1196         return NULL;
1197     }
1198 
1199 #endif
1200 
1201     return ecf;
1202 }
1203 
1204 
1205 static char *
1206 ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf)
1207 {
1208     ngx_event_conf_t  *ecf = conf;
1209 
1210 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1211     int                  fd;
1212 #endif
1213     ngx_int_t            i;
1214     ngx_module_t        *module;
1215     ngx_event_module_t  *event_module;
1216 
1217     module = NULL;
1218 
1219 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
1220 
1221     fd = epoll_create(100);
1222 
1223     if (fd != -1) {
1224         (void) close(fd);
1225         module = &ngx_epoll_module;
1226 
1227     } else if (ngx_errno != NGX_ENOSYS) {
1228         module = &ngx_epoll_module;
1229     }
1230 
1231 #endif
1232 
1233 #if (NGX_HAVE_DEVPOLL) && !(NGX_TEST_BUILD_DEVPOLL)
1234 
1235     module = &ngx_devpoll_module;
1236 
1237 #endif
1238 
1239 #if (NGX_HAVE_KQUEUE)
1240 
1241     module = &ngx_kqueue_module;
1242 
1243 #endif
1244 
1245 #if (NGX_HAVE_SELECT)
1246 
1247     if (module == NULL) {
1248         module = &ngx_select_module;
1249     }
1250 
1251 #endif
1252 
1253     if (module == NULL) {
1254         for (i = 0; cycle->modules[i]; i++) {
1255 
1256             if (cycle->modules[i]->type != NGX_EVENT_MODULE) {
1257                 continue;
1258             }
1259 
1260             event_module = cycle->modules[i]->ctx;
1261 
1262             if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0)
1263             {
1264                 continue;
1265             }
1266 
1267             module = cycle->modules[i];
1268             break;
1269         }
1270     }
1271 
1272     if (module == NULL) {
1273         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
1274         return NGX_CONF_ERROR;
1275     }
1276 
1277     ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
1278     cycle->connection_n = ecf->connections;
1279 
1280     ngx_conf_init_uint_value(ecf->use, module->ctx_index);
1281 
1282     event_module = module->ctx;
1283     ngx_conf_init_ptr_value(ecf->name, event_module->name->data);
1284 
1285     ngx_conf_init_value(ecf->multi_accept, 0);
1286     ngx_conf_init_value(ecf->accept_mutex, 0);
1287     ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
1288 
1289     return NGX_CONF_OK;
1290 }