Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

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 static ngx_int_t ngx_select_init(ngx_cycle_t *cycle, ngx_msec_t timer);
0014 static void ngx_select_done(ngx_cycle_t *cycle);
0015 static ngx_int_t ngx_select_add_event(ngx_event_t *ev, ngx_int_t event,
0016     ngx_uint_t flags);
0017 static ngx_int_t ngx_select_del_event(ngx_event_t *ev, ngx_int_t event,
0018     ngx_uint_t flags);
0019 static ngx_int_t ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
0020     ngx_uint_t flags);
0021 static void ngx_select_repair_fd_sets(ngx_cycle_t *cycle);
0022 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf);
0023 
0024 
0025 static fd_set         master_read_fd_set;
0026 static fd_set         master_write_fd_set;
0027 static fd_set         work_read_fd_set;
0028 static fd_set         work_write_fd_set;
0029 
0030 static ngx_int_t      max_fd;
0031 static ngx_uint_t     nevents;
0032 
0033 static ngx_event_t  **event_index;
0034 
0035 
0036 static ngx_str_t           select_name = ngx_string("select");
0037 
0038 static ngx_event_module_t  ngx_select_module_ctx = {
0039     &select_name,
0040     NULL,                                  /* create configuration */
0041     ngx_select_init_conf,                  /* init configuration */
0042 
0043     {
0044         ngx_select_add_event,              /* add an event */
0045         ngx_select_del_event,              /* delete an event */
0046         ngx_select_add_event,              /* enable an event */
0047         ngx_select_del_event,              /* disable an event */
0048         NULL,                              /* add an connection */
0049         NULL,                              /* delete an connection */
0050         NULL,                              /* trigger a notify */
0051         ngx_select_process_events,         /* process the events */
0052         ngx_select_init,                   /* init the events */
0053         ngx_select_done                    /* done the events */
0054     }
0055 
0056 };
0057 
0058 ngx_module_t  ngx_select_module = {
0059     NGX_MODULE_V1,
0060     &ngx_select_module_ctx,                /* module context */
0061     NULL,                                  /* module directives */
0062     NGX_EVENT_MODULE,                      /* module type */
0063     NULL,                                  /* init master */
0064     NULL,                                  /* init module */
0065     NULL,                                  /* init process */
0066     NULL,                                  /* init thread */
0067     NULL,                                  /* exit thread */
0068     NULL,                                  /* exit process */
0069     NULL,                                  /* exit master */
0070     NGX_MODULE_V1_PADDING
0071 };
0072 
0073 
0074 static ngx_int_t
0075 ngx_select_init(ngx_cycle_t *cycle, ngx_msec_t timer)
0076 {
0077     ngx_event_t  **index;
0078 
0079     if (event_index == NULL) {
0080         FD_ZERO(&master_read_fd_set);
0081         FD_ZERO(&master_write_fd_set);
0082         nevents = 0;
0083     }
0084 
0085     if (ngx_process >= NGX_PROCESS_WORKER
0086         || cycle->old_cycle == NULL
0087         || cycle->old_cycle->connection_n < cycle->connection_n)
0088     {
0089         index = ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
0090                           cycle->log);
0091         if (index == NULL) {
0092             return NGX_ERROR;
0093         }
0094 
0095         if (event_index) {
0096             ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
0097             ngx_free(event_index);
0098         }
0099 
0100         event_index = index;
0101     }
0102 
0103     ngx_io = ngx_os_io;
0104 
0105     ngx_event_actions = ngx_select_module_ctx.actions;
0106 
0107     ngx_event_flags = NGX_USE_LEVEL_EVENT;
0108 
0109     max_fd = -1;
0110 
0111     return NGX_OK;
0112 }
0113 
0114 
0115 static void
0116 ngx_select_done(ngx_cycle_t *cycle)
0117 {
0118     ngx_free(event_index);
0119 
0120     event_index = NULL;
0121 }
0122 
0123 
0124 static ngx_int_t
0125 ngx_select_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
0126 {
0127     ngx_connection_t  *c;
0128 
0129     c = ev->data;
0130 
0131     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0132                    "select add event fd:%d ev:%i", c->fd, event);
0133 
0134     if (ev->index != NGX_INVALID_INDEX) {
0135         ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
0136                       "select event fd:%d ev:%i is already set", c->fd, event);
0137         return NGX_OK;
0138     }
0139 
0140     if ((event == NGX_READ_EVENT && ev->write)
0141         || (event == NGX_WRITE_EVENT && !ev->write))
0142     {
0143         ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
0144                       "invalid select %s event fd:%d ev:%i",
0145                       ev->write ? "write" : "read", c->fd, event);
0146         return NGX_ERROR;
0147     }
0148 
0149     if (event == NGX_READ_EVENT) {
0150         FD_SET(c->fd, &master_read_fd_set);
0151 
0152     } else if (event == NGX_WRITE_EVENT) {
0153         FD_SET(c->fd, &master_write_fd_set);
0154     }
0155 
0156     if (max_fd != -1 && max_fd < c->fd) {
0157         max_fd = c->fd;
0158     }
0159 
0160     ev->active = 1;
0161 
0162     event_index[nevents] = ev;
0163     ev->index = nevents;
0164     nevents++;
0165 
0166     return NGX_OK;
0167 }
0168 
0169 
0170 static ngx_int_t
0171 ngx_select_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
0172 {
0173     ngx_event_t       *e;
0174     ngx_connection_t  *c;
0175 
0176     c = ev->data;
0177 
0178     ev->active = 0;
0179 
0180     if (ev->index == NGX_INVALID_INDEX) {
0181         return NGX_OK;
0182     }
0183 
0184     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0185                    "select del event fd:%d ev:%i", c->fd, event);
0186 
0187     if (event == NGX_READ_EVENT) {
0188         FD_CLR(c->fd, &master_read_fd_set);
0189 
0190     } else if (event == NGX_WRITE_EVENT) {
0191         FD_CLR(c->fd, &master_write_fd_set);
0192     }
0193 
0194     if (max_fd == c->fd) {
0195         max_fd = -1;
0196     }
0197 
0198     if (ev->index < --nevents) {
0199         e = event_index[nevents];
0200         event_index[ev->index] = e;
0201         e->index = ev->index;
0202     }
0203 
0204     ev->index = NGX_INVALID_INDEX;
0205 
0206     return NGX_OK;
0207 }
0208 
0209 
0210 static ngx_int_t
0211 ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
0212     ngx_uint_t flags)
0213 {
0214     int                ready, nready;
0215     ngx_err_t          err;
0216     ngx_uint_t         i, found;
0217     ngx_event_t       *ev;
0218     ngx_queue_t       *queue;
0219     struct timeval     tv, *tp;
0220     ngx_connection_t  *c;
0221 
0222     if (max_fd == -1) {
0223         for (i = 0; i < nevents; i++) {
0224             c = event_index[i]->data;
0225             if (max_fd < c->fd) {
0226                 max_fd = c->fd;
0227             }
0228         }
0229 
0230         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0231                        "change max_fd: %i", max_fd);
0232     }
0233 
0234 #if (NGX_DEBUG)
0235     if (cycle->log->log_level & NGX_LOG_DEBUG_ALL) {
0236         for (i = 0; i < nevents; i++) {
0237             ev = event_index[i];
0238             c = ev->data;
0239             ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0240                            "select event: fd:%d wr:%d", c->fd, ev->write);
0241         }
0242 
0243         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0244                        "max_fd: %i", max_fd);
0245     }
0246 #endif
0247 
0248     if (timer == NGX_TIMER_INFINITE) {
0249         tp = NULL;
0250 
0251     } else {
0252         tv.tv_sec = (long) (timer / 1000);
0253         tv.tv_usec = (long) ((timer % 1000) * 1000);
0254         tp = &tv;
0255     }
0256 
0257     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0258                    "select timer: %M", timer);
0259 
0260     work_read_fd_set = master_read_fd_set;
0261     work_write_fd_set = master_write_fd_set;
0262 
0263     ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp);
0264 
0265     err = (ready == -1) ? ngx_errno : 0;
0266 
0267     if (flags & NGX_UPDATE_TIME || ngx_event_timer_alarm) {
0268         ngx_time_update();
0269     }
0270 
0271     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0272                    "select ready %d", ready);
0273 
0274     if (err) {
0275         ngx_uint_t  level;
0276 
0277         if (err == NGX_EINTR) {
0278 
0279             if (ngx_event_timer_alarm) {
0280                 ngx_event_timer_alarm = 0;
0281                 return NGX_OK;
0282             }
0283 
0284             level = NGX_LOG_INFO;
0285 
0286         } else {
0287             level = NGX_LOG_ALERT;
0288         }
0289 
0290         ngx_log_error(level, cycle->log, err, "select() failed");
0291 
0292         if (err == NGX_EBADF) {
0293             ngx_select_repair_fd_sets(cycle);
0294         }
0295 
0296         return NGX_ERROR;
0297     }
0298 
0299     if (ready == 0) {
0300         if (timer != NGX_TIMER_INFINITE) {
0301             return NGX_OK;
0302         }
0303 
0304         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
0305                       "select() returned no events without timeout");
0306         return NGX_ERROR;
0307     }
0308 
0309     nready = 0;
0310 
0311     for (i = 0; i < nevents; i++) {
0312         ev = event_index[i];
0313         c = ev->data;
0314         found = 0;
0315 
0316         if (ev->write) {
0317             if (FD_ISSET(c->fd, &work_write_fd_set)) {
0318                 found = 1;
0319                 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0320                                "select write %d", c->fd);
0321             }
0322 
0323         } else {
0324             if (FD_ISSET(c->fd, &work_read_fd_set)) {
0325                 found = 1;
0326                 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0327                                "select read %d", c->fd);
0328             }
0329         }
0330 
0331         if (found) {
0332             ev->ready = 1;
0333 
0334             queue = ev->accept ? &ngx_posted_accept_events
0335                                : &ngx_posted_events;
0336 
0337             ngx_post_event(ev, queue);
0338 
0339             nready++;
0340         }
0341     }
0342 
0343     if (ready != nready) {
0344         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
0345                       "select ready != events: %d:%d", ready, nready);
0346 
0347         ngx_select_repair_fd_sets(cycle);
0348     }
0349 
0350     return NGX_OK;
0351 }
0352 
0353 
0354 static void
0355 ngx_select_repair_fd_sets(ngx_cycle_t *cycle)
0356 {
0357     int           n;
0358     socklen_t     len;
0359     ngx_err_t     err;
0360     ngx_socket_t  s;
0361 
0362     for (s = 0; s <= max_fd; s++) {
0363 
0364         if (FD_ISSET(s, &master_read_fd_set) == 0) {
0365             continue;
0366         }
0367 
0368         len = sizeof(int);
0369 
0370         if (getsockopt(s, SOL_SOCKET, SO_TYPE, &n, &len) == -1) {
0371             err = ngx_socket_errno;
0372 
0373             ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
0374                           "invalid descriptor #%d in read fd_set", s);
0375 
0376             FD_CLR(s, &master_read_fd_set);
0377         }
0378     }
0379 
0380     for (s = 0; s <= max_fd; s++) {
0381 
0382         if (FD_ISSET(s, &master_write_fd_set) == 0) {
0383             continue;
0384         }
0385 
0386         len = sizeof(int);
0387 
0388         if (getsockopt(s, SOL_SOCKET, SO_TYPE, &n, &len) == -1) {
0389             err = ngx_socket_errno;
0390 
0391             ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
0392                           "invalid descriptor #%d in write fd_set", s);
0393 
0394             FD_CLR(s, &master_write_fd_set);
0395         }
0396     }
0397 
0398     max_fd = -1;
0399 }
0400 
0401 
0402 static char *
0403 ngx_select_init_conf(ngx_cycle_t *cycle, void *conf)
0404 {
0405     ngx_event_conf_t  *ecf;
0406 
0407     ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
0408 
0409     if (ecf->use != ngx_select_module.ctx_index) {
0410         return NGX_CONF_OK;
0411     }
0412 
0413     /* disable warning: the default FD_SETSIZE is 1024U in FreeBSD 5.x */
0414 
0415     if (cycle->connection_n > FD_SETSIZE) {
0416         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0417                       "the maximum number of files "
0418                       "supported by select() is %ud", FD_SETSIZE);
0419         return NGX_CONF_ERROR;
0420     }
0421 
0422     return NGX_CONF_OK;
0423 }