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 static ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle);
0014 static ngx_int_t ngx_disable_accept_events(ngx_cycle_t *cycle, ngx_uint_t all);
0015 static void ngx_close_accepted_connection(ngx_connection_t *c);
0016 #if (NGX_DEBUG)
0017 static void ngx_debug_accepted_connection(ngx_event_conf_t *ecf,
0018     ngx_connection_t *c);
0019 #endif
0020 
0021 
0022 void
0023 ngx_event_accept(ngx_event_t *ev)
0024 {
0025     socklen_t          socklen;
0026     ngx_err_t          err;
0027     ngx_log_t         *log;
0028     ngx_uint_t         level;
0029     ngx_socket_t       s;
0030     ngx_event_t       *rev, *wev;
0031     ngx_sockaddr_t     sa;
0032     ngx_listening_t   *ls;
0033     ngx_connection_t  *c, *lc;
0034     ngx_event_conf_t  *ecf;
0035 #if (NGX_HAVE_ACCEPT4)
0036     static ngx_uint_t  use_accept4 = 1;
0037 #endif
0038 
0039     if (ev->timedout) {
0040         if (ngx_enable_accept_events((ngx_cycle_t *) ngx_cycle) != NGX_OK) {
0041             return;
0042         }
0043 
0044         ev->timedout = 0;
0045     }
0046 
0047     ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
0048 
0049     if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
0050         ev->available = ecf->multi_accept;
0051     }
0052 
0053     lc = ev->data;
0054     ls = lc->listening;
0055     ev->ready = 0;
0056 
0057     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0058                    "accept on %V, ready: %d", &ls->addr_text, ev->available);
0059 
0060     do {
0061         socklen = sizeof(ngx_sockaddr_t);
0062 
0063 #if (NGX_HAVE_ACCEPT4)
0064         if (use_accept4) {
0065             s = accept4(lc->fd, &sa.sockaddr, &socklen, SOCK_NONBLOCK);
0066         } else {
0067             s = accept(lc->fd, &sa.sockaddr, &socklen);
0068         }
0069 #else
0070         s = accept(lc->fd, &sa.sockaddr, &socklen);
0071 #endif
0072 
0073         if (s == (ngx_socket_t) -1) {
0074             err = ngx_socket_errno;
0075 
0076             if (err == NGX_EAGAIN) {
0077                 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, err,
0078                                "accept() not ready");
0079                 return;
0080             }
0081 
0082             level = NGX_LOG_ALERT;
0083 
0084             if (err == NGX_ECONNABORTED) {
0085                 level = NGX_LOG_ERR;
0086 
0087             } else if (err == NGX_EMFILE || err == NGX_ENFILE) {
0088                 level = NGX_LOG_CRIT;
0089             }
0090 
0091 #if (NGX_HAVE_ACCEPT4)
0092             ngx_log_error(level, ev->log, err,
0093                           use_accept4 ? "accept4() failed" : "accept() failed");
0094 
0095             if (use_accept4 && err == NGX_ENOSYS) {
0096                 use_accept4 = 0;
0097                 ngx_inherited_nonblocking = 0;
0098                 continue;
0099             }
0100 #else
0101             ngx_log_error(level, ev->log, err, "accept() failed");
0102 #endif
0103 
0104             if (err == NGX_ECONNABORTED) {
0105                 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0106                     ev->available--;
0107                 }
0108 
0109                 if (ev->available) {
0110                     continue;
0111                 }
0112             }
0113 
0114             if (err == NGX_EMFILE || err == NGX_ENFILE) {
0115                 if (ngx_disable_accept_events((ngx_cycle_t *) ngx_cycle, 1)
0116                     != NGX_OK)
0117                 {
0118                     return;
0119                 }
0120 
0121                 if (ngx_use_accept_mutex) {
0122                     if (ngx_accept_mutex_held) {
0123                         ngx_shmtx_unlock(&ngx_accept_mutex);
0124                         ngx_accept_mutex_held = 0;
0125                     }
0126 
0127                     ngx_accept_disabled = 1;
0128 
0129                 } else {
0130                     ngx_add_timer(ev, ecf->accept_mutex_delay);
0131                 }
0132             }
0133 
0134             return;
0135         }
0136 
0137 #if (NGX_STAT_STUB)
0138         (void) ngx_atomic_fetch_add(ngx_stat_accepted, 1);
0139 #endif
0140 
0141         ngx_accept_disabled = ngx_cycle->connection_n / 8
0142                               - ngx_cycle->free_connection_n;
0143 
0144         c = ngx_get_connection(s, ev->log);
0145 
0146         if (c == NULL) {
0147             if (ngx_close_socket(s) == -1) {
0148                 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
0149                               ngx_close_socket_n " failed");
0150             }
0151 
0152             return;
0153         }
0154 
0155         c->type = SOCK_STREAM;
0156 
0157 #if (NGX_STAT_STUB)
0158         (void) ngx_atomic_fetch_add(ngx_stat_active, 1);
0159 #endif
0160 
0161         c->pool = ngx_create_pool(ls->pool_size, ev->log);
0162         if (c->pool == NULL) {
0163             ngx_close_accepted_connection(c);
0164             return;
0165         }
0166 
0167         if (socklen > (socklen_t) sizeof(ngx_sockaddr_t)) {
0168             socklen = sizeof(ngx_sockaddr_t);
0169         }
0170 
0171         c->sockaddr = ngx_palloc(c->pool, socklen);
0172         if (c->sockaddr == NULL) {
0173             ngx_close_accepted_connection(c);
0174             return;
0175         }
0176 
0177         ngx_memcpy(c->sockaddr, &sa, socklen);
0178 
0179         log = ngx_palloc(c->pool, sizeof(ngx_log_t));
0180         if (log == NULL) {
0181             ngx_close_accepted_connection(c);
0182             return;
0183         }
0184 
0185         /* set a blocking mode for iocp and non-blocking mode for others */
0186 
0187         if (ngx_inherited_nonblocking) {
0188             if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
0189                 if (ngx_blocking(s) == -1) {
0190                     ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
0191                                   ngx_blocking_n " failed");
0192                     ngx_close_accepted_connection(c);
0193                     return;
0194                 }
0195             }
0196 
0197         } else {
0198             if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
0199                 if (ngx_nonblocking(s) == -1) {
0200                     ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
0201                                   ngx_nonblocking_n " failed");
0202                     ngx_close_accepted_connection(c);
0203                     return;
0204                 }
0205             }
0206         }
0207 
0208         *log = ls->log;
0209 
0210         c->recv = ngx_recv;
0211         c->send = ngx_send;
0212         c->recv_chain = ngx_recv_chain;
0213         c->send_chain = ngx_send_chain;
0214 
0215         c->log = log;
0216         c->pool->log = log;
0217 
0218         c->socklen = socklen;
0219         c->listening = ls;
0220         c->local_sockaddr = ls->sockaddr;
0221         c->local_socklen = ls->socklen;
0222 
0223 #if (NGX_HAVE_UNIX_DOMAIN)
0224         if (c->sockaddr->sa_family == AF_UNIX) {
0225             c->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
0226             c->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;
0227 #if (NGX_SOLARIS)
0228             /* Solaris's sendfilev() supports AF_NCA, AF_INET, and AF_INET6 */
0229             c->sendfile = 0;
0230 #endif
0231         }
0232 #endif
0233 
0234         rev = c->read;
0235         wev = c->write;
0236 
0237         wev->ready = 1;
0238 
0239         if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
0240             rev->ready = 1;
0241         }
0242 
0243         if (ev->deferred_accept) {
0244             rev->ready = 1;
0245 #if (NGX_HAVE_KQUEUE || NGX_HAVE_EPOLLRDHUP)
0246             rev->available = 1;
0247 #endif
0248         }
0249 
0250         rev->log = log;
0251         wev->log = log;
0252 
0253         /*
0254          * TODO: MT: - ngx_atomic_fetch_add()
0255          *             or protection by critical section or light mutex
0256          *
0257          * TODO: MP: - allocated in a shared memory
0258          *           - ngx_atomic_fetch_add()
0259          *             or protection by critical section or light mutex
0260          */
0261 
0262         c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
0263 
0264 #if (NGX_STAT_STUB)
0265         (void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
0266 #endif
0267 
0268         if (ls->addr_ntop) {
0269             c->addr_text.data = ngx_pnalloc(c->pool, ls->addr_text_max_len);
0270             if (c->addr_text.data == NULL) {
0271                 ngx_close_accepted_connection(c);
0272                 return;
0273             }
0274 
0275             c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
0276                                              c->addr_text.data,
0277                                              ls->addr_text_max_len, 0);
0278             if (c->addr_text.len == 0) {
0279                 ngx_close_accepted_connection(c);
0280                 return;
0281             }
0282         }
0283 
0284 #if (NGX_DEBUG)
0285         {
0286         ngx_str_t  addr;
0287         u_char     text[NGX_SOCKADDR_STRLEN];
0288 
0289         ngx_debug_accepted_connection(ecf, c);
0290 
0291         if (log->log_level & NGX_LOG_DEBUG_EVENT) {
0292             addr.data = text;
0293             addr.len = ngx_sock_ntop(c->sockaddr, c->socklen, text,
0294                                      NGX_SOCKADDR_STRLEN, 1);
0295 
0296             ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0,
0297                            "*%uA accept: %V fd:%d", c->number, &addr, s);
0298         }
0299 
0300         }
0301 #endif
0302 
0303         if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
0304             if (ngx_add_conn(c) == NGX_ERROR) {
0305                 ngx_close_accepted_connection(c);
0306                 return;
0307             }
0308         }
0309 
0310         log->data = NULL;
0311         log->handler = NULL;
0312 
0313         ls->handler(c);
0314 
0315         if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0316             ev->available--;
0317         }
0318 
0319     } while (ev->available);
0320 }
0321 
0322 
0323 #if !(NGX_WIN32)
0324 
0325 void
0326 ngx_event_recvmsg(ngx_event_t *ev)
0327 {
0328     ssize_t            n;
0329     ngx_log_t         *log;
0330     ngx_err_t          err;
0331     ngx_event_t       *rev, *wev;
0332     struct iovec       iov[1];
0333     struct msghdr      msg;
0334     ngx_sockaddr_t     sa;
0335     ngx_listening_t   *ls;
0336     ngx_event_conf_t  *ecf;
0337     ngx_connection_t  *c, *lc;
0338     static u_char      buffer[65535];
0339 
0340 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
0341 
0342 #if (NGX_HAVE_IP_RECVDSTADDR)
0343     u_char             msg_control[CMSG_SPACE(sizeof(struct in_addr))];
0344 #elif (NGX_HAVE_IP_PKTINFO)
0345     u_char             msg_control[CMSG_SPACE(sizeof(struct in_pktinfo))];
0346 #endif
0347 
0348 #if (NGX_HAVE_INET6 && NGX_HAVE_IPV6_RECVPKTINFO)
0349     u_char             msg_control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
0350 #endif
0351 
0352 #endif
0353 
0354     if (ev->timedout) {
0355         if (ngx_enable_accept_events((ngx_cycle_t *) ngx_cycle) != NGX_OK) {
0356             return;
0357         }
0358 
0359         ev->timedout = 0;
0360     }
0361 
0362     ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
0363 
0364     if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
0365         ev->available = ecf->multi_accept;
0366     }
0367 
0368     lc = ev->data;
0369     ls = lc->listening;
0370     ev->ready = 0;
0371 
0372     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0373                    "recvmsg on %V, ready: %d", &ls->addr_text, ev->available);
0374 
0375     do {
0376         ngx_memzero(&msg, sizeof(struct msghdr));
0377 
0378         iov[0].iov_base = (void *) buffer;
0379         iov[0].iov_len = sizeof(buffer);
0380 
0381         msg.msg_name = &sa;
0382         msg.msg_namelen = sizeof(ngx_sockaddr_t);
0383         msg.msg_iov = iov;
0384         msg.msg_iovlen = 1;
0385 
0386 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
0387 
0388         if (ls->wildcard) {
0389 
0390 #if (NGX_HAVE_IP_RECVDSTADDR || NGX_HAVE_IP_PKTINFO)
0391             if (ls->sockaddr->sa_family == AF_INET) {
0392                 msg.msg_control = &msg_control;
0393                 msg.msg_controllen = sizeof(msg_control);
0394             }
0395 #endif
0396 
0397 #if (NGX_HAVE_INET6 && NGX_HAVE_IPV6_RECVPKTINFO)
0398             if (ls->sockaddr->sa_family == AF_INET6) {
0399                 msg.msg_control = &msg_control6;
0400                 msg.msg_controllen = sizeof(msg_control6);
0401             }
0402 #endif
0403         }
0404 
0405 #endif
0406 
0407         n = recvmsg(lc->fd, &msg, 0);
0408 
0409         if (n == -1) {
0410             err = ngx_socket_errno;
0411 
0412             if (err == NGX_EAGAIN) {
0413                 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, err,
0414                                "recvmsg() not ready");
0415                 return;
0416             }
0417 
0418             ngx_log_error(NGX_LOG_ALERT, ev->log, err, "recvmsg() failed");
0419 
0420             return;
0421         }
0422 
0423 #if (NGX_STAT_STUB)
0424         (void) ngx_atomic_fetch_add(ngx_stat_accepted, 1);
0425 #endif
0426 
0427 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
0428         if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
0429             ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
0430                           "recvmsg() truncated data");
0431             continue;
0432         }
0433 #endif
0434 
0435         ngx_accept_disabled = ngx_cycle->connection_n / 8
0436                               - ngx_cycle->free_connection_n;
0437 
0438         c = ngx_get_connection(lc->fd, ev->log);
0439         if (c == NULL) {
0440             return;
0441         }
0442 
0443         c->shared = 1;
0444         c->type = SOCK_DGRAM;
0445         c->socklen = msg.msg_namelen;
0446 
0447         if (c->socklen > (socklen_t) sizeof(ngx_sockaddr_t)) {
0448             c->socklen = sizeof(ngx_sockaddr_t);
0449         }
0450 
0451 #if (NGX_STAT_STUB)
0452         (void) ngx_atomic_fetch_add(ngx_stat_active, 1);
0453 #endif
0454 
0455         c->pool = ngx_create_pool(ls->pool_size, ev->log);
0456         if (c->pool == NULL) {
0457             ngx_close_accepted_connection(c);
0458             return;
0459         }
0460 
0461         c->sockaddr = ngx_palloc(c->pool, c->socklen);
0462         if (c->sockaddr == NULL) {
0463             ngx_close_accepted_connection(c);
0464             return;
0465         }
0466 
0467         ngx_memcpy(c->sockaddr, msg.msg_name, c->socklen);
0468 
0469         log = ngx_palloc(c->pool, sizeof(ngx_log_t));
0470         if (log == NULL) {
0471             ngx_close_accepted_connection(c);
0472             return;
0473         }
0474 
0475         *log = ls->log;
0476 
0477         c->send = ngx_udp_send;
0478         c->send_chain = ngx_udp_send_chain;
0479 
0480         c->log = log;
0481         c->pool->log = log;
0482 
0483         c->listening = ls;
0484         c->local_sockaddr = ls->sockaddr;
0485         c->local_socklen = ls->socklen;
0486 
0487 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
0488 
0489         if (ls->wildcard) {
0490             struct cmsghdr   *cmsg;
0491             struct sockaddr  *sockaddr;
0492 
0493             sockaddr = ngx_palloc(c->pool, c->local_socklen);
0494             if (sockaddr == NULL) {
0495                 ngx_close_accepted_connection(c);
0496                 return;
0497             }
0498 
0499             ngx_memcpy(sockaddr, c->local_sockaddr, c->local_socklen);
0500             c->local_sockaddr = sockaddr;
0501 
0502             for (cmsg = CMSG_FIRSTHDR(&msg);
0503                  cmsg != NULL;
0504                  cmsg = CMSG_NXTHDR(&msg, cmsg))
0505             {
0506 
0507 #if (NGX_HAVE_IP_RECVDSTADDR)
0508 
0509                 if (cmsg->cmsg_level == IPPROTO_IP
0510                     && cmsg->cmsg_type == IP_RECVDSTADDR
0511                     && sockaddr->sa_family == AF_INET)
0512                 {
0513                     struct in_addr      *addr;
0514                     struct sockaddr_in  *sin;
0515 
0516                     addr = (struct in_addr *) CMSG_DATA(cmsg);
0517                     sin = (struct sockaddr_in *) sockaddr;
0518                     sin->sin_addr = *addr;
0519 
0520                     break;
0521                 }
0522 
0523 #elif (NGX_HAVE_IP_PKTINFO)
0524 
0525                 if (cmsg->cmsg_level == IPPROTO_IP
0526                     && cmsg->cmsg_type == IP_PKTINFO
0527                     && sockaddr->sa_family == AF_INET)
0528                 {
0529                     struct in_pktinfo   *pkt;
0530                     struct sockaddr_in  *sin;
0531 
0532                     pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
0533                     sin = (struct sockaddr_in *) sockaddr;
0534                     sin->sin_addr = pkt->ipi_addr;
0535 
0536                     break;
0537                 }
0538 
0539 #endif
0540 
0541 #if (NGX_HAVE_INET6 && NGX_HAVE_IPV6_RECVPKTINFO)
0542 
0543                 if (cmsg->cmsg_level == IPPROTO_IPV6
0544                     && cmsg->cmsg_type == IPV6_PKTINFO
0545                     && sockaddr->sa_family == AF_INET6)
0546                 {
0547                     struct in6_pktinfo   *pkt6;
0548                     struct sockaddr_in6  *sin6;
0549 
0550                     pkt6 = (struct in6_pktinfo *) CMSG_DATA(cmsg);
0551                     sin6 = (struct sockaddr_in6 *) sockaddr;
0552                     sin6->sin6_addr = pkt6->ipi6_addr;
0553 
0554                     break;
0555                 }
0556 
0557 #endif
0558 
0559             }
0560         }
0561 
0562 #endif
0563 
0564         c->buffer = ngx_create_temp_buf(c->pool, n);
0565         if (c->buffer == NULL) {
0566             ngx_close_accepted_connection(c);
0567             return;
0568         }
0569 
0570         c->buffer->last = ngx_cpymem(c->buffer->last, buffer, n);
0571 
0572         rev = c->read;
0573         wev = c->write;
0574 
0575         wev->ready = 1;
0576 
0577         rev->log = log;
0578         wev->log = log;
0579 
0580         /*
0581          * TODO: MT: - ngx_atomic_fetch_add()
0582          *             or protection by critical section or light mutex
0583          *
0584          * TODO: MP: - allocated in a shared memory
0585          *           - ngx_atomic_fetch_add()
0586          *             or protection by critical section or light mutex
0587          */
0588 
0589         c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
0590 
0591 #if (NGX_STAT_STUB)
0592         (void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
0593 #endif
0594 
0595         if (ls->addr_ntop) {
0596             c->addr_text.data = ngx_pnalloc(c->pool, ls->addr_text_max_len);
0597             if (c->addr_text.data == NULL) {
0598                 ngx_close_accepted_connection(c);
0599                 return;
0600             }
0601 
0602             c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
0603                                              c->addr_text.data,
0604                                              ls->addr_text_max_len, 0);
0605             if (c->addr_text.len == 0) {
0606                 ngx_close_accepted_connection(c);
0607                 return;
0608             }
0609         }
0610 
0611 #if (NGX_DEBUG)
0612         {
0613         ngx_str_t  addr;
0614         u_char     text[NGX_SOCKADDR_STRLEN];
0615 
0616         ngx_debug_accepted_connection(ecf, c);
0617 
0618         if (log->log_level & NGX_LOG_DEBUG_EVENT) {
0619             addr.data = text;
0620             addr.len = ngx_sock_ntop(c->sockaddr, c->socklen, text,
0621                                      NGX_SOCKADDR_STRLEN, 1);
0622 
0623             ngx_log_debug4(NGX_LOG_DEBUG_EVENT, log, 0,
0624                            "*%uA recvmsg: %V fd:%d n:%z",
0625                            c->number, &addr, c->fd, n);
0626         }
0627 
0628         }
0629 #endif
0630 
0631         log->data = NULL;
0632         log->handler = NULL;
0633 
0634         ls->handler(c);
0635 
0636         if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0637             ev->available -= n;
0638         }
0639 
0640     } while (ev->available);
0641 }
0642 
0643 #endif
0644 
0645 
0646 ngx_int_t
0647 ngx_trylock_accept_mutex(ngx_cycle_t *cycle)
0648 {
0649     if (ngx_shmtx_trylock(&ngx_accept_mutex)) {
0650 
0651         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0652                        "accept mutex locked");
0653 
0654         if (ngx_accept_mutex_held && ngx_accept_events == 0) {
0655             return NGX_OK;
0656         }
0657 
0658         if (ngx_enable_accept_events(cycle) == NGX_ERROR) {
0659             ngx_shmtx_unlock(&ngx_accept_mutex);
0660             return NGX_ERROR;
0661         }
0662 
0663         ngx_accept_events = 0;
0664         ngx_accept_mutex_held = 1;
0665 
0666         return NGX_OK;
0667     }
0668 
0669     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
0670                    "accept mutex lock failed: %ui", ngx_accept_mutex_held);
0671 
0672     if (ngx_accept_mutex_held) {
0673         if (ngx_disable_accept_events(cycle, 0) == NGX_ERROR) {
0674             return NGX_ERROR;
0675         }
0676 
0677         ngx_accept_mutex_held = 0;
0678     }
0679 
0680     return NGX_OK;
0681 }
0682 
0683 
0684 static ngx_int_t
0685 ngx_enable_accept_events(ngx_cycle_t *cycle)
0686 {
0687     ngx_uint_t         i;
0688     ngx_listening_t   *ls;
0689     ngx_connection_t  *c;
0690 
0691     ls = cycle->listening.elts;
0692     for (i = 0; i < cycle->listening.nelts; i++) {
0693 
0694         c = ls[i].connection;
0695 
0696         if (c == NULL || c->read->active) {
0697             continue;
0698         }
0699 
0700         if (ngx_add_event(c->read, NGX_READ_EVENT, 0) == NGX_ERROR) {
0701             return NGX_ERROR;
0702         }
0703     }
0704 
0705     return NGX_OK;
0706 }
0707 
0708 
0709 static ngx_int_t
0710 ngx_disable_accept_events(ngx_cycle_t *cycle, ngx_uint_t all)
0711 {
0712     ngx_uint_t         i;
0713     ngx_listening_t   *ls;
0714     ngx_connection_t  *c;
0715 
0716     ls = cycle->listening.elts;
0717     for (i = 0; i < cycle->listening.nelts; i++) {
0718 
0719         c = ls[i].connection;
0720 
0721         if (c == NULL || !c->read->active) {
0722             continue;
0723         }
0724 
0725 #if (NGX_HAVE_REUSEPORT)
0726 
0727         /*
0728          * do not disable accept on worker's own sockets
0729          * when disabling accept events due to accept mutex
0730          */
0731 
0732         if (ls[i].reuseport && !all) {
0733             continue;
0734         }
0735 
0736 #endif
0737 
0738         if (ngx_del_event(c->read, NGX_READ_EVENT, NGX_DISABLE_EVENT)
0739             == NGX_ERROR)
0740         {
0741             return NGX_ERROR;
0742         }
0743     }
0744 
0745     return NGX_OK;
0746 }
0747 
0748 
0749 static void
0750 ngx_close_accepted_connection(ngx_connection_t *c)
0751 {
0752     ngx_socket_t  fd;
0753 
0754     ngx_free_connection(c);
0755 
0756     fd = c->fd;
0757     c->fd = (ngx_socket_t) -1;
0758 
0759     if (!c->shared && ngx_close_socket(fd) == -1) {
0760         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
0761                       ngx_close_socket_n " failed");
0762     }
0763 
0764     if (c->pool) {
0765         ngx_destroy_pool(c->pool);
0766     }
0767 
0768 #if (NGX_STAT_STUB)
0769     (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
0770 #endif
0771 }
0772 
0773 
0774 u_char *
0775 ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len)
0776 {
0777     return ngx_snprintf(buf, len, " while accepting new connection on %V",
0778                         log->data);
0779 }
0780 
0781 
0782 #if (NGX_DEBUG)
0783 
0784 static void
0785 ngx_debug_accepted_connection(ngx_event_conf_t *ecf, ngx_connection_t *c)
0786 {
0787     struct sockaddr_in   *sin;
0788     ngx_cidr_t           *cidr;
0789     ngx_uint_t            i;
0790 #if (NGX_HAVE_INET6)
0791     struct sockaddr_in6  *sin6;
0792     ngx_uint_t            n;
0793 #endif
0794 
0795     cidr = ecf->debug_connection.elts;
0796     for (i = 0; i < ecf->debug_connection.nelts; i++) {
0797         if (cidr[i].family != (ngx_uint_t) c->sockaddr->sa_family) {
0798             goto next;
0799         }
0800 
0801         switch (cidr[i].family) {
0802 
0803 #if (NGX_HAVE_INET6)
0804         case AF_INET6:
0805             sin6 = (struct sockaddr_in6 *) c->sockaddr;
0806             for (n = 0; n < 16; n++) {
0807                 if ((sin6->sin6_addr.s6_addr[n]
0808                     & cidr[i].u.in6.mask.s6_addr[n])
0809                     != cidr[i].u.in6.addr.s6_addr[n])
0810                 {
0811                     goto next;
0812                 }
0813             }
0814             break;
0815 #endif
0816 
0817 #if (NGX_HAVE_UNIX_DOMAIN)
0818         case AF_UNIX:
0819             break;
0820 #endif
0821 
0822         default: /* AF_INET */
0823             sin = (struct sockaddr_in *) c->sockaddr;
0824             if ((sin->sin_addr.s_addr & cidr[i].u.in.mask)
0825                 != cidr[i].u.in.addr)
0826             {
0827                 goto next;
0828             }
0829             break;
0830         }
0831 
0832         c->log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
0833         break;
0834 
0835     next:
0836         continue;
0837     }
0838 }
0839 
0840 #endif