Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.19.2 ]​[ nginx-1.18.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 void ngx_destroy_cycle_pools(ngx_conf_t *conf);
0014 static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
0015     ngx_shm_zone_t *shm_zone);
0016 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
0017 static void ngx_clean_old_cycles(ngx_event_t *ev);
0018 static void ngx_shutdown_timer_handler(ngx_event_t *ev);
0019 
0020 
0021 volatile ngx_cycle_t  *ngx_cycle;
0022 ngx_array_t            ngx_old_cycles;
0023 
0024 static ngx_pool_t     *ngx_temp_pool;
0025 static ngx_event_t     ngx_cleaner_event;
0026 static ngx_event_t     ngx_shutdown_event;
0027 
0028 ngx_uint_t             ngx_test_config;
0029 ngx_uint_t             ngx_dump_config;
0030 ngx_uint_t             ngx_quiet_mode;
0031 
0032 
0033 /* STUB NAME */
0034 static ngx_connection_t  dumb;
0035 /* STUB */
0036 
0037 
0038 ngx_cycle_t *
0039 ngx_init_cycle(ngx_cycle_t *old_cycle)
0040 {
0041     void                *rv;
0042     char               **senv;
0043     ngx_uint_t           i, n;
0044     ngx_log_t           *log;
0045     ngx_time_t          *tp;
0046     ngx_conf_t           conf;
0047     ngx_pool_t          *pool;
0048     ngx_cycle_t         *cycle, **old;
0049     ngx_shm_zone_t      *shm_zone, *oshm_zone;
0050     ngx_list_part_t     *part, *opart;
0051     ngx_open_file_t     *file;
0052     ngx_listening_t     *ls, *nls;
0053     ngx_core_conf_t     *ccf, *old_ccf;
0054     ngx_core_module_t   *module;
0055     char                 hostname[NGX_MAXHOSTNAMELEN];
0056 
0057     ngx_timezone_update();
0058 
0059     /* force localtime update with a new timezone */
0060 
0061     tp = ngx_timeofday();
0062     tp->sec = 0;
0063 
0064     ngx_time_update();
0065 
0066 
0067     log = old_cycle->log;
0068 
0069     pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
0070     if (pool == NULL) {
0071         return NULL;
0072     }
0073     pool->log = log;
0074 
0075     cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
0076     if (cycle == NULL) {
0077         ngx_destroy_pool(pool);
0078         return NULL;
0079     }
0080 
0081     cycle->pool = pool;
0082     cycle->log = log;
0083     cycle->old_cycle = old_cycle;
0084 
0085     cycle->conf_prefix.len = old_cycle->conf_prefix.len;
0086     cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
0087     if (cycle->conf_prefix.data == NULL) {
0088         ngx_destroy_pool(pool);
0089         return NULL;
0090     }
0091 
0092     cycle->prefix.len = old_cycle->prefix.len;
0093     cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
0094     if (cycle->prefix.data == NULL) {
0095         ngx_destroy_pool(pool);
0096         return NULL;
0097     }
0098 
0099     cycle->conf_file.len = old_cycle->conf_file.len;
0100     cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
0101     if (cycle->conf_file.data == NULL) {
0102         ngx_destroy_pool(pool);
0103         return NULL;
0104     }
0105     ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
0106                 old_cycle->conf_file.len + 1);
0107 
0108     cycle->conf_param.len = old_cycle->conf_param.len;
0109     cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
0110     if (cycle->conf_param.data == NULL) {
0111         ngx_destroy_pool(pool);
0112         return NULL;
0113     }
0114 
0115 
0116     n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;
0117 
0118     if (ngx_array_init(&cycle->paths, pool, n, sizeof(ngx_path_t *))
0119         != NGX_OK)
0120     {
0121         ngx_destroy_pool(pool);
0122         return NULL;
0123     }
0124 
0125     ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *));
0126 
0127 
0128     if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))
0129         != NGX_OK)
0130     {
0131         ngx_destroy_pool(pool);
0132         return NULL;
0133     }
0134 
0135     ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,
0136                     ngx_str_rbtree_insert_value);
0137 
0138     if (old_cycle->open_files.part.nelts) {
0139         n = old_cycle->open_files.part.nelts;
0140         for (part = old_cycle->open_files.part.next; part; part = part->next) {
0141             n += part->nelts;
0142         }
0143 
0144     } else {
0145         n = 20;
0146     }
0147 
0148     if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
0149         != NGX_OK)
0150     {
0151         ngx_destroy_pool(pool);
0152         return NULL;
0153     }
0154 
0155 
0156     if (old_cycle->shared_memory.part.nelts) {
0157         n = old_cycle->shared_memory.part.nelts;
0158         for (part = old_cycle->shared_memory.part.next; part; part = part->next)
0159         {
0160             n += part->nelts;
0161         }
0162 
0163     } else {
0164         n = 1;
0165     }
0166 
0167     if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
0168         != NGX_OK)
0169     {
0170         ngx_destroy_pool(pool);
0171         return NULL;
0172     }
0173 
0174     n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
0175 
0176     if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t))
0177         != NGX_OK)
0178     {
0179         ngx_destroy_pool(pool);
0180         return NULL;
0181     }
0182 
0183     ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t));
0184 
0185 
0186     ngx_queue_init(&cycle->reusable_connections_queue);
0187 
0188 
0189     cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
0190     if (cycle->conf_ctx == NULL) {
0191         ngx_destroy_pool(pool);
0192         return NULL;
0193     }
0194 
0195 
0196     if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {
0197         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");
0198         ngx_destroy_pool(pool);
0199         return NULL;
0200     }
0201 
0202     /* on Linux gethostname() silently truncates name that does not fit */
0203 
0204     hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
0205     cycle->hostname.len = ngx_strlen(hostname);
0206 
0207     cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);
0208     if (cycle->hostname.data == NULL) {
0209         ngx_destroy_pool(pool);
0210         return NULL;
0211     }
0212 
0213     ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);
0214 
0215 
0216     if (ngx_cycle_modules(cycle) != NGX_OK) {
0217         ngx_destroy_pool(pool);
0218         return NULL;
0219     }
0220 
0221 
0222     for (i = 0; cycle->modules[i]; i++) {
0223         if (cycle->modules[i]->type != NGX_CORE_MODULE) {
0224             continue;
0225         }
0226 
0227         module = cycle->modules[i]->ctx;
0228 
0229         if (module->create_conf) {
0230             rv = module->create_conf(cycle);
0231             if (rv == NULL) {
0232                 ngx_destroy_pool(pool);
0233                 return NULL;
0234             }
0235             cycle->conf_ctx[cycle->modules[i]->index] = rv;
0236         }
0237     }
0238 
0239 
0240     senv = environ;
0241 
0242 
0243     ngx_memzero(&conf, sizeof(ngx_conf_t));
0244     /* STUB: init array ? */
0245     conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));
0246     if (conf.args == NULL) {
0247         ngx_destroy_pool(pool);
0248         return NULL;
0249     }
0250 
0251     conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
0252     if (conf.temp_pool == NULL) {
0253         ngx_destroy_pool(pool);
0254         return NULL;
0255     }
0256 
0257 
0258     conf.ctx = cycle->conf_ctx;
0259     conf.cycle = cycle;
0260     conf.pool = pool;
0261     conf.log = log;
0262     conf.module_type = NGX_CORE_MODULE;
0263     conf.cmd_type = NGX_MAIN_CONF;
0264 
0265 #if 0
0266     log->log_level = NGX_LOG_DEBUG_ALL;
0267 #endif
0268 
0269     if (ngx_conf_param(&conf) != NGX_CONF_OK) {
0270         environ = senv;
0271         ngx_destroy_cycle_pools(&conf);
0272         return NULL;
0273     }
0274 
0275     if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
0276         environ = senv;
0277         ngx_destroy_cycle_pools(&conf);
0278         return NULL;
0279     }
0280 
0281     if (ngx_test_config && !ngx_quiet_mode) {
0282         ngx_log_stderr(0, "the configuration file %s syntax is ok",
0283                        cycle->conf_file.data);
0284     }
0285 
0286     for (i = 0; cycle->modules[i]; i++) {
0287         if (cycle->modules[i]->type != NGX_CORE_MODULE) {
0288             continue;
0289         }
0290 
0291         module = cycle->modules[i]->ctx;
0292 
0293         if (module->init_conf) {
0294             if (module->init_conf(cycle,
0295                                   cycle->conf_ctx[cycle->modules[i]->index])
0296                 == NGX_CONF_ERROR)
0297             {
0298                 environ = senv;
0299                 ngx_destroy_cycle_pools(&conf);
0300                 return NULL;
0301             }
0302         }
0303     }
0304 
0305     if (ngx_process == NGX_PROCESS_SIGNALLER) {
0306         return cycle;
0307     }
0308 
0309     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0310 
0311     if (ngx_test_config) {
0312 
0313         if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
0314             goto failed;
0315         }
0316 
0317     } else if (!ngx_is_init_cycle(old_cycle)) {
0318 
0319         /*
0320          * we do not create the pid file in the first ngx_init_cycle() call
0321          * because we need to write the demonized process pid
0322          */
0323 
0324         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
0325                                                    ngx_core_module);
0326         if (ccf->pid.len != old_ccf->pid.len
0327             || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)
0328         {
0329             /* new pid file name */
0330 
0331             if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
0332                 goto failed;
0333             }
0334 
0335             ngx_delete_pidfile(old_cycle);
0336         }
0337     }
0338 
0339 
0340     if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
0341         goto failed;
0342     }
0343 
0344 
0345     if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {
0346         goto failed;
0347     }
0348 
0349 
0350     if (ngx_log_open_default(cycle) != NGX_OK) {
0351         goto failed;
0352     }
0353 
0354     /* open the new files */
0355 
0356     part = &cycle->open_files.part;
0357     file = part->elts;
0358 
0359     for (i = 0; /* void */ ; i++) {
0360 
0361         if (i >= part->nelts) {
0362             if (part->next == NULL) {
0363                 break;
0364             }
0365             part = part->next;
0366             file = part->elts;
0367             i = 0;
0368         }
0369 
0370         if (file[i].name.len == 0) {
0371             continue;
0372         }
0373 
0374         file[i].fd = ngx_open_file(file[i].name.data,
0375                                    NGX_FILE_APPEND,
0376                                    NGX_FILE_CREATE_OR_OPEN,
0377                                    NGX_FILE_DEFAULT_ACCESS);
0378 
0379         ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
0380                        "log: %p %d \"%s\"",
0381                        &file[i], file[i].fd, file[i].name.data);
0382 
0383         if (file[i].fd == NGX_INVALID_FILE) {
0384             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0385                           ngx_open_file_n " \"%s\" failed",
0386                           file[i].name.data);
0387             goto failed;
0388         }
0389 
0390 #if !(NGX_WIN32)
0391         if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
0392             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0393                           "fcntl(FD_CLOEXEC) \"%s\" failed",
0394                           file[i].name.data);
0395             goto failed;
0396         }
0397 #endif
0398     }
0399 
0400     cycle->log = &cycle->new_log;
0401     pool->log = &cycle->new_log;
0402 
0403 
0404     /* create shared memory */
0405 
0406     part = &cycle->shared_memory.part;
0407     shm_zone = part->elts;
0408 
0409     for (i = 0; /* void */ ; i++) {
0410 
0411         if (i >= part->nelts) {
0412             if (part->next == NULL) {
0413                 break;
0414             }
0415             part = part->next;
0416             shm_zone = part->elts;
0417             i = 0;
0418         }
0419 
0420         if (shm_zone[i].shm.size == 0) {
0421             ngx_log_error(NGX_LOG_EMERG, log, 0,
0422                           "zero size shared memory zone \"%V\"",
0423                           &shm_zone[i].shm.name);
0424             goto failed;
0425         }
0426 
0427         shm_zone[i].shm.log = cycle->log;
0428 
0429         opart = &old_cycle->shared_memory.part;
0430         oshm_zone = opart->elts;
0431 
0432         for (n = 0; /* void */ ; n++) {
0433 
0434             if (n >= opart->nelts) {
0435                 if (opart->next == NULL) {
0436                     break;
0437                 }
0438                 opart = opart->next;
0439                 oshm_zone = opart->elts;
0440                 n = 0;
0441             }
0442 
0443             if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
0444                 continue;
0445             }
0446 
0447             if (ngx_strncmp(shm_zone[i].shm.name.data,
0448                             oshm_zone[n].shm.name.data,
0449                             shm_zone[i].shm.name.len)
0450                 != 0)
0451             {
0452                 continue;
0453             }
0454 
0455             if (shm_zone[i].tag == oshm_zone[n].tag
0456                 && shm_zone[i].shm.size == oshm_zone[n].shm.size
0457                 && !shm_zone[i].noreuse)
0458             {
0459                 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
0460 #if (NGX_WIN32)
0461                 shm_zone[i].shm.handle = oshm_zone[n].shm.handle;
0462 #endif
0463 
0464                 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
0465                     != NGX_OK)
0466                 {
0467                     goto failed;
0468                 }
0469 
0470                 goto shm_zone_found;
0471             }
0472 
0473             break;
0474         }
0475 
0476         if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
0477             goto failed;
0478         }
0479 
0480         if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
0481             goto failed;
0482         }
0483 
0484         if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
0485             goto failed;
0486         }
0487 
0488     shm_zone_found:
0489 
0490         continue;
0491     }
0492 
0493 
0494     /* handle the listening sockets */
0495 
0496     if (old_cycle->listening.nelts) {
0497         ls = old_cycle->listening.elts;
0498         for (i = 0; i < old_cycle->listening.nelts; i++) {
0499             ls[i].remain = 0;
0500         }
0501 
0502         nls = cycle->listening.elts;
0503         for (n = 0; n < cycle->listening.nelts; n++) {
0504 
0505             for (i = 0; i < old_cycle->listening.nelts; i++) {
0506                 if (ls[i].ignore) {
0507                     continue;
0508                 }
0509 
0510                 if (ls[i].remain) {
0511                     continue;
0512                 }
0513 
0514                 if (ls[i].type != nls[n].type) {
0515                     continue;
0516                 }
0517 
0518                 if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,
0519                                      ls[i].sockaddr, ls[i].socklen, 1)
0520                     == NGX_OK)
0521                 {
0522                     nls[n].fd = ls[i].fd;
0523                     nls[n].inherited = ls[i].inherited;
0524                     nls[n].previous = &ls[i];
0525                     ls[i].remain = 1;
0526 
0527                     if (ls[i].backlog != nls[n].backlog) {
0528                         nls[n].listen = 1;
0529                     }
0530 
0531 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0532 
0533                     /*
0534                      * FreeBSD, except the most recent versions,
0535                      * could not remove accept filter
0536                      */
0537                     nls[n].deferred_accept = ls[i].deferred_accept;
0538 
0539                     if (ls[i].accept_filter && nls[n].accept_filter) {
0540                         if (ngx_strcmp(ls[i].accept_filter,
0541                                        nls[n].accept_filter)
0542                             != 0)
0543                         {
0544                             nls[n].delete_deferred = 1;
0545                             nls[n].add_deferred = 1;
0546                         }
0547 
0548                     } else if (ls[i].accept_filter) {
0549                         nls[n].delete_deferred = 1;
0550 
0551                     } else if (nls[n].accept_filter) {
0552                         nls[n].add_deferred = 1;
0553                     }
0554 #endif
0555 
0556 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0557 
0558                     if (ls[i].deferred_accept && !nls[n].deferred_accept) {
0559                         nls[n].delete_deferred = 1;
0560 
0561                     } else if (ls[i].deferred_accept != nls[n].deferred_accept)
0562                     {
0563                         nls[n].add_deferred = 1;
0564                     }
0565 #endif
0566 
0567 #if (NGX_HAVE_REUSEPORT)
0568                     if (nls[n].reuseport && !ls[i].reuseport) {
0569                         nls[n].add_reuseport = 1;
0570                     }
0571 #endif
0572 
0573                     break;
0574                 }
0575             }
0576 
0577             if (nls[n].fd == (ngx_socket_t) -1) {
0578                 nls[n].open = 1;
0579 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0580                 if (nls[n].accept_filter) {
0581                     nls[n].add_deferred = 1;
0582                 }
0583 #endif
0584 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0585                 if (nls[n].deferred_accept) {
0586                     nls[n].add_deferred = 1;
0587                 }
0588 #endif
0589             }
0590         }
0591 
0592     } else {
0593         ls = cycle->listening.elts;
0594         for (i = 0; i < cycle->listening.nelts; i++) {
0595             ls[i].open = 1;
0596 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0597             if (ls[i].accept_filter) {
0598                 ls[i].add_deferred = 1;
0599             }
0600 #endif
0601 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0602             if (ls[i].deferred_accept) {
0603                 ls[i].add_deferred = 1;
0604             }
0605 #endif
0606         }
0607     }
0608 
0609     if (ngx_open_listening_sockets(cycle) != NGX_OK) {
0610         goto failed;
0611     }
0612 
0613     if (!ngx_test_config) {
0614         ngx_configure_listening_sockets(cycle);
0615     }
0616 
0617 
0618     /* commit the new cycle configuration */
0619 
0620     if (!ngx_use_stderr) {
0621         (void) ngx_log_redirect_stderr(cycle);
0622     }
0623 
0624     pool->log = cycle->log;
0625 
0626     if (ngx_init_modules(cycle) != NGX_OK) {
0627         /* fatal */
0628         exit(1);
0629     }
0630 
0631 
0632     /* close and delete stuff that lefts from an old cycle */
0633 
0634     /* free the unnecessary shared memory */
0635 
0636     opart = &old_cycle->shared_memory.part;
0637     oshm_zone = opart->elts;
0638 
0639     for (i = 0; /* void */ ; i++) {
0640 
0641         if (i >= opart->nelts) {
0642             if (opart->next == NULL) {
0643                 goto old_shm_zone_done;
0644             }
0645             opart = opart->next;
0646             oshm_zone = opart->elts;
0647             i = 0;
0648         }
0649 
0650         part = &cycle->shared_memory.part;
0651         shm_zone = part->elts;
0652 
0653         for (n = 0; /* void */ ; n++) {
0654 
0655             if (n >= part->nelts) {
0656                 if (part->next == NULL) {
0657                     break;
0658                 }
0659                 part = part->next;
0660                 shm_zone = part->elts;
0661                 n = 0;
0662             }
0663 
0664             if (oshm_zone[i].shm.name.len != shm_zone[n].shm.name.len) {
0665                 continue;
0666             }
0667 
0668             if (ngx_strncmp(oshm_zone[i].shm.name.data,
0669                             shm_zone[n].shm.name.data,
0670                             oshm_zone[i].shm.name.len)
0671                 != 0)
0672             {
0673                 continue;
0674             }
0675 
0676             if (oshm_zone[i].tag == shm_zone[n].tag
0677                 && oshm_zone[i].shm.size == shm_zone[n].shm.size
0678                 && !oshm_zone[i].noreuse)
0679             {
0680                 goto live_shm_zone;
0681             }
0682 
0683             break;
0684         }
0685 
0686         ngx_shm_free(&oshm_zone[i].shm);
0687 
0688     live_shm_zone:
0689 
0690         continue;
0691     }
0692 
0693 old_shm_zone_done:
0694 
0695 
0696     /* close the unnecessary listening sockets */
0697 
0698     ls = old_cycle->listening.elts;
0699     for (i = 0; i < old_cycle->listening.nelts; i++) {
0700 
0701         if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {
0702             continue;
0703         }
0704 
0705         if (ngx_close_socket(ls[i].fd) == -1) {
0706             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
0707                           ngx_close_socket_n " listening socket on %V failed",
0708                           &ls[i].addr_text);
0709         }
0710 
0711 #if (NGX_HAVE_UNIX_DOMAIN)
0712 
0713         if (ls[i].sockaddr->sa_family == AF_UNIX) {
0714             u_char  *name;
0715 
0716             name = ls[i].addr_text.data + sizeof("unix:") - 1;
0717 
0718             ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0719                           "deleting socket %s", name);
0720 
0721             if (ngx_delete_file(name) == NGX_FILE_ERROR) {
0722                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
0723                               ngx_delete_file_n " %s failed", name);
0724             }
0725         }
0726 
0727 #endif
0728     }
0729 
0730 
0731     /* close the unnecessary open files */
0732 
0733     part = &old_cycle->open_files.part;
0734     file = part->elts;
0735 
0736     for (i = 0; /* void */ ; i++) {
0737 
0738         if (i >= part->nelts) {
0739             if (part->next == NULL) {
0740                 break;
0741             }
0742             part = part->next;
0743             file = part->elts;
0744             i = 0;
0745         }
0746 
0747         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
0748             continue;
0749         }
0750 
0751         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
0752             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0753                           ngx_close_file_n " \"%s\" failed",
0754                           file[i].name.data);
0755         }
0756     }
0757 
0758     ngx_destroy_pool(conf.temp_pool);
0759 
0760     if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
0761 
0762         ngx_destroy_pool(old_cycle->pool);
0763         cycle->old_cycle = NULL;
0764 
0765         return cycle;
0766     }
0767 
0768 
0769     if (ngx_temp_pool == NULL) {
0770         ngx_temp_pool = ngx_create_pool(128, cycle->log);
0771         if (ngx_temp_pool == NULL) {
0772             ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0773                           "could not create ngx_temp_pool");
0774             exit(1);
0775         }
0776 
0777         n = 10;
0778 
0779         if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n,
0780                            sizeof(ngx_cycle_t *))
0781             != NGX_OK)
0782         {
0783             exit(1);
0784         }
0785 
0786         ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *));
0787 
0788         ngx_cleaner_event.handler = ngx_clean_old_cycles;
0789         ngx_cleaner_event.log = cycle->log;
0790         ngx_cleaner_event.data = &dumb;
0791         dumb.fd = (ngx_socket_t) -1;
0792     }
0793 
0794     ngx_temp_pool->log = cycle->log;
0795 
0796     old = ngx_array_push(&ngx_old_cycles);
0797     if (old == NULL) {
0798         exit(1);
0799     }
0800     *old = old_cycle;
0801 
0802     if (!ngx_cleaner_event.timer_set) {
0803         ngx_add_timer(&ngx_cleaner_event, 30000);
0804         ngx_cleaner_event.timer_set = 1;
0805     }
0806 
0807     return cycle;
0808 
0809 
0810 failed:
0811 
0812     if (!ngx_is_init_cycle(old_cycle)) {
0813         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
0814                                                    ngx_core_module);
0815         if (old_ccf->environment) {
0816             environ = old_ccf->environment;
0817         }
0818     }
0819 
0820     /* rollback the new cycle configuration */
0821 
0822     part = &cycle->open_files.part;
0823     file = part->elts;
0824 
0825     for (i = 0; /* void */ ; i++) {
0826 
0827         if (i >= part->nelts) {
0828             if (part->next == NULL) {
0829                 break;
0830             }
0831             part = part->next;
0832             file = part->elts;
0833             i = 0;
0834         }
0835 
0836         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
0837             continue;
0838         }
0839 
0840         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
0841             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0842                           ngx_close_file_n " \"%s\" failed",
0843                           file[i].name.data);
0844         }
0845     }
0846 
0847     /* free the newly created shared memory */
0848 
0849     part = &cycle->shared_memory.part;
0850     shm_zone = part->elts;
0851 
0852     for (i = 0; /* void */ ; i++) {
0853 
0854         if (i >= part->nelts) {
0855             if (part->next == NULL) {
0856                 break;
0857             }
0858             part = part->next;
0859             shm_zone = part->elts;
0860             i = 0;
0861         }
0862 
0863         if (shm_zone[i].shm.addr == NULL) {
0864             continue;
0865         }
0866 
0867         opart = &old_cycle->shared_memory.part;
0868         oshm_zone = opart->elts;
0869 
0870         for (n = 0; /* void */ ; n++) {
0871 
0872             if (n >= opart->nelts) {
0873                 if (opart->next == NULL) {
0874                     break;
0875                 }
0876                 opart = opart->next;
0877                 oshm_zone = opart->elts;
0878                 n = 0;
0879             }
0880 
0881             if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
0882                 continue;
0883             }
0884 
0885             if (ngx_strncmp(shm_zone[i].shm.name.data,
0886                             oshm_zone[n].shm.name.data,
0887                             shm_zone[i].shm.name.len)
0888                 != 0)
0889             {
0890                 continue;
0891             }
0892 
0893             if (shm_zone[i].tag == oshm_zone[n].tag
0894                 && shm_zone[i].shm.size == oshm_zone[n].shm.size
0895                 && !shm_zone[i].noreuse)
0896             {
0897                 goto old_shm_zone_found;
0898             }
0899 
0900             break;
0901         }
0902 
0903         ngx_shm_free(&shm_zone[i].shm);
0904 
0905     old_shm_zone_found:
0906 
0907         continue;
0908     }
0909 
0910     if (ngx_test_config) {
0911         ngx_destroy_cycle_pools(&conf);
0912         return NULL;
0913     }
0914 
0915     ls = cycle->listening.elts;
0916     for (i = 0; i < cycle->listening.nelts; i++) {
0917         if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {
0918             continue;
0919         }
0920 
0921         if (ngx_close_socket(ls[i].fd) == -1) {
0922             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
0923                           ngx_close_socket_n " %V failed",
0924                           &ls[i].addr_text);
0925         }
0926     }
0927 
0928     ngx_destroy_cycle_pools(&conf);
0929 
0930     return NULL;
0931 }
0932 
0933 
0934 static void
0935 ngx_destroy_cycle_pools(ngx_conf_t *conf)
0936 {
0937     ngx_destroy_pool(conf->temp_pool);
0938     ngx_destroy_pool(conf->pool);
0939 }
0940 
0941 
0942 static ngx_int_t
0943 ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
0944 {
0945     u_char           *file;
0946     ngx_slab_pool_t  *sp;
0947 
0948     sp = (ngx_slab_pool_t *) zn->shm.addr;
0949 
0950     if (zn->shm.exists) {
0951 
0952         if (sp == sp->addr) {
0953             return NGX_OK;
0954         }
0955 
0956 #if (NGX_WIN32)
0957 
0958         /* remap at the required address */
0959 
0960         if (ngx_shm_remap(&zn->shm, sp->addr) != NGX_OK) {
0961             return NGX_ERROR;
0962         }
0963 
0964         sp = (ngx_slab_pool_t *) zn->shm.addr;
0965 
0966         if (sp == sp->addr) {
0967             return NGX_OK;
0968         }
0969 
0970 #endif
0971 
0972         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0973                       "shared zone \"%V\" has no equal addresses: %p vs %p",
0974                       &zn->shm.name, sp->addr, sp);
0975         return NGX_ERROR;
0976     }
0977 
0978     sp->end = zn->shm.addr + zn->shm.size;
0979     sp->min_shift = 3;
0980     sp->addr = zn->shm.addr;
0981 
0982 #if (NGX_HAVE_ATOMIC_OPS)
0983 
0984     file = NULL;
0985 
0986 #else
0987 
0988     file = ngx_pnalloc(cycle->pool,
0989                        cycle->lock_file.len + zn->shm.name.len + 1);
0990     if (file == NULL) {
0991         return NGX_ERROR;
0992     }
0993 
0994     (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
0995 
0996 #endif
0997 
0998     if (ngx_shmtx_create(&sp->mutex, &sp->lock, file) != NGX_OK) {
0999         return NGX_ERROR;
1000     }
1001 
1002     ngx_slab_init(sp);
1003 
1004     return NGX_OK;
1005 }
1006 
1007 
1008 ngx_int_t
1009 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
1010 {
1011     size_t      len;
1012     ngx_int_t   rc;
1013     ngx_uint_t  create;
1014     ngx_file_t  file;
1015     u_char      pid[NGX_INT64_LEN + 2];
1016 
1017     if (ngx_process > NGX_PROCESS_MASTER) {
1018         return NGX_OK;
1019     }
1020 
1021     ngx_memzero(&file, sizeof(ngx_file_t));
1022 
1023     file.name = *name;
1024     file.log = log;
1025 
1026     create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;
1027 
1028     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
1029                             create, NGX_FILE_DEFAULT_ACCESS);
1030 
1031     if (file.fd == NGX_INVALID_FILE) {
1032         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1033                       ngx_open_file_n " \"%s\" failed", file.name.data);
1034         return NGX_ERROR;
1035     }
1036 
1037     rc = NGX_OK;
1038 
1039     if (!ngx_test_config) {
1040         len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
1041 
1042         if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
1043             rc = NGX_ERROR;
1044         }
1045     }
1046 
1047     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1048         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1049                       ngx_close_file_n " \"%s\" failed", file.name.data);
1050     }
1051 
1052     return rc;
1053 }
1054 
1055 
1056 void
1057 ngx_delete_pidfile(ngx_cycle_t *cycle)
1058 {
1059     u_char           *name;
1060     ngx_core_conf_t  *ccf;
1061 
1062     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1063 
1064     name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
1065 
1066     if (ngx_delete_file(name) == NGX_FILE_ERROR) {
1067         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1068                       ngx_delete_file_n " \"%s\" failed", name);
1069     }
1070 }
1071 
1072 
1073 ngx_int_t
1074 ngx_signal_process(ngx_cycle_t *cycle, char *sig)
1075 {
1076     ssize_t           n;
1077     ngx_pid_t         pid;
1078     ngx_file_t        file;
1079     ngx_core_conf_t  *ccf;
1080     u_char            buf[NGX_INT64_LEN + 2];
1081 
1082     ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");
1083 
1084     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1085 
1086     ngx_memzero(&file, sizeof(ngx_file_t));
1087 
1088     file.name = ccf->pid;
1089     file.log = cycle->log;
1090 
1091     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,
1092                             NGX_FILE_OPEN, NGX_FILE_DEFAULT_ACCESS);
1093 
1094     if (file.fd == NGX_INVALID_FILE) {
1095         ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
1096                       ngx_open_file_n " \"%s\" failed", file.name.data);
1097         return 1;
1098     }
1099 
1100     n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);
1101 
1102     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1103         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1104                       ngx_close_file_n " \"%s\" failed", file.name.data);
1105     }
1106 
1107     if (n == NGX_ERROR) {
1108         return 1;
1109     }
1110 
1111     while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }
1112 
1113     pid = ngx_atoi(buf, ++n);
1114 
1115     if (pid == (ngx_pid_t) NGX_ERROR) {
1116         ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
1117                       "invalid PID number \"%*s\" in \"%s\"",
1118                       n, buf, file.name.data);
1119         return 1;
1120     }
1121 
1122     return ngx_os_signal_process(cycle, sig, pid);
1123 
1124 }
1125 
1126 
1127 static ngx_int_t
1128 ngx_test_lockfile(u_char *file, ngx_log_t *log)
1129 {
1130 #if !(NGX_HAVE_ATOMIC_OPS)
1131     ngx_fd_t  fd;
1132 
1133     fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
1134                        NGX_FILE_DEFAULT_ACCESS);
1135 
1136     if (fd == NGX_INVALID_FILE) {
1137         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1138                       ngx_open_file_n " \"%s\" failed", file);
1139         return NGX_ERROR;
1140     }
1141 
1142     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1143         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1144                       ngx_close_file_n " \"%s\" failed", file);
1145     }
1146 
1147     if (ngx_delete_file(file) == NGX_FILE_ERROR) {
1148         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1149                       ngx_delete_file_n " \"%s\" failed", file);
1150     }
1151 
1152 #endif
1153 
1154     return NGX_OK;
1155 }
1156 
1157 
1158 void
1159 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
1160 {
1161     ngx_fd_t          fd;
1162     ngx_uint_t        i;
1163     ngx_list_part_t  *part;
1164     ngx_open_file_t  *file;
1165 
1166     part = &cycle->open_files.part;
1167     file = part->elts;
1168 
1169     for (i = 0; /* void */ ; i++) {
1170 
1171         if (i >= part->nelts) {
1172             if (part->next == NULL) {
1173                 break;
1174             }
1175             part = part->next;
1176             file = part->elts;
1177             i = 0;
1178         }
1179 
1180         if (file[i].name.len == 0) {
1181             continue;
1182         }
1183 
1184         if (file[i].flush) {
1185             file[i].flush(&file[i], cycle->log);
1186         }
1187 
1188         fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
1189                            NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
1190 
1191         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1192                        "reopen file \"%s\", old:%d new:%d",
1193                        file[i].name.data, file[i].fd, fd);
1194 
1195         if (fd == NGX_INVALID_FILE) {
1196             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1197                           ngx_open_file_n " \"%s\" failed", file[i].name.data);
1198             continue;
1199         }
1200 
1201 #if !(NGX_WIN32)
1202         if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1203             ngx_file_info_t  fi;
1204 
1205             if (ngx_file_info(file[i].name.data, &fi) == NGX_FILE_ERROR) {
1206                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1207                               ngx_file_info_n " \"%s\" failed",
1208                               file[i].name.data);
1209 
1210                 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1211                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1212                                   ngx_close_file_n " \"%s\" failed",
1213                                   file[i].name.data);
1214                 }
1215 
1216                 continue;
1217             }
1218 
1219             if (fi.st_uid != user) {
1220                 if (chown((const char *) file[i].name.data, user, -1) == -1) {
1221                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1222                                   "chown(\"%s\", %d) failed",
1223                                   file[i].name.data, user);
1224 
1225                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1226                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1227                                       ngx_close_file_n " \"%s\" failed",
1228                                       file[i].name.data);
1229                     }
1230 
1231                     continue;
1232                 }
1233             }
1234 
1235             if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {
1236 
1237                 fi.st_mode |= (S_IRUSR|S_IWUSR);
1238 
1239                 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
1240                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1241                                   "chmod() \"%s\" failed", file[i].name.data);
1242 
1243                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1244                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1245                                       ngx_close_file_n " \"%s\" failed",
1246                                       file[i].name.data);
1247                     }
1248 
1249                     continue;
1250                 }
1251             }
1252         }
1253 
1254         if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1255             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1256                           "fcntl(FD_CLOEXEC) \"%s\" failed",
1257                           file[i].name.data);
1258 
1259             if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1260                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1261                               ngx_close_file_n " \"%s\" failed",
1262                               file[i].name.data);
1263             }
1264 
1265             continue;
1266         }
1267 #endif
1268 
1269         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
1270             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1271                           ngx_close_file_n " \"%s\" failed",
1272                           file[i].name.data);
1273         }
1274 
1275         file[i].fd = fd;
1276     }
1277 
1278     (void) ngx_log_redirect_stderr(cycle);
1279 }
1280 
1281 
1282 ngx_shm_zone_t *
1283 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
1284 {
1285     ngx_uint_t        i;
1286     ngx_shm_zone_t   *shm_zone;
1287     ngx_list_part_t  *part;
1288 
1289     part = &cf->cycle->shared_memory.part;
1290     shm_zone = part->elts;
1291 
1292     for (i = 0; /* void */ ; i++) {
1293 
1294         if (i >= part->nelts) {
1295             if (part->next == NULL) {
1296                 break;
1297             }
1298             part = part->next;
1299             shm_zone = part->elts;
1300             i = 0;
1301         }
1302 
1303         if (name->len != shm_zone[i].shm.name.len) {
1304             continue;
1305         }
1306 
1307         if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
1308             != 0)
1309         {
1310             continue;
1311         }
1312 
1313         if (tag != shm_zone[i].tag) {
1314             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1315                             "the shared memory zone \"%V\" is "
1316                             "already declared for a different use",
1317                             &shm_zone[i].shm.name);
1318             return NULL;
1319         }
1320 
1321         if (shm_zone[i].shm.size == 0) {
1322             shm_zone[i].shm.size = size;
1323         }
1324 
1325         if (size && size != shm_zone[i].shm.size) {
1326             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1327                             "the size %uz of shared memory zone \"%V\" "
1328                             "conflicts with already declared size %uz",
1329                             size, &shm_zone[i].shm.name, shm_zone[i].shm.size);
1330             return NULL;
1331         }
1332 
1333         return &shm_zone[i];
1334     }
1335 
1336     shm_zone = ngx_list_push(&cf->cycle->shared_memory);
1337 
1338     if (shm_zone == NULL) {
1339         return NULL;
1340     }
1341 
1342     shm_zone->data = NULL;
1343     shm_zone->shm.log = cf->cycle->log;
1344     shm_zone->shm.addr = NULL;
1345     shm_zone->shm.size = size;
1346     shm_zone->shm.name = *name;
1347     shm_zone->shm.exists = 0;
1348     shm_zone->init = NULL;
1349     shm_zone->tag = tag;
1350     shm_zone->noreuse = 0;
1351 
1352     return shm_zone;
1353 }
1354 
1355 
1356 static void
1357 ngx_clean_old_cycles(ngx_event_t *ev)
1358 {
1359     ngx_uint_t     i, n, found, live;
1360     ngx_log_t     *log;
1361     ngx_cycle_t  **cycle;
1362 
1363     log = ngx_cycle->log;
1364     ngx_temp_pool->log = log;
1365 
1366     ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
1367 
1368     live = 0;
1369 
1370     cycle = ngx_old_cycles.elts;
1371     for (i = 0; i < ngx_old_cycles.nelts; i++) {
1372 
1373         if (cycle[i] == NULL) {
1374             continue;
1375         }
1376 
1377         found = 0;
1378 
1379         for (n = 0; n < cycle[i]->connection_n; n++) {
1380             if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
1381                 found = 1;
1382 
1383                 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%ui", n);
1384 
1385                 break;
1386             }
1387         }
1388 
1389         if (found) {
1390             live = 1;
1391             continue;
1392         }
1393 
1394         ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %ui", i);
1395 
1396         ngx_destroy_pool(cycle[i]->pool);
1397         cycle[i] = NULL;
1398     }
1399 
1400     ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %ui", live);
1401 
1402     if (live) {
1403         ngx_add_timer(ev, 30000);
1404 
1405     } else {
1406         ngx_destroy_pool(ngx_temp_pool);
1407         ngx_temp_pool = NULL;
1408         ngx_old_cycles.nelts = 0;
1409     }
1410 }
1411 
1412 
1413 void
1414 ngx_set_shutdown_timer(ngx_cycle_t *cycle)
1415 {
1416     ngx_core_conf_t  *ccf;
1417 
1418     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1419 
1420     if (ccf->shutdown_timeout) {
1421         ngx_shutdown_event.handler = ngx_shutdown_timer_handler;
1422         ngx_shutdown_event.data = cycle;
1423         ngx_shutdown_event.log = cycle->log;
1424         ngx_shutdown_event.cancelable = 1;
1425 
1426         ngx_add_timer(&ngx_shutdown_event, ccf->shutdown_timeout);
1427     }
1428 }
1429 
1430 
1431 static void
1432 ngx_shutdown_timer_handler(ngx_event_t *ev)
1433 {
1434     ngx_uint_t         i;
1435     ngx_cycle_t       *cycle;
1436     ngx_connection_t  *c;
1437 
1438     cycle = ev->data;
1439 
1440     c = cycle->connections;
1441 
1442     for (i = 0; i < cycle->connection_n; i++) {
1443 
1444         if (c[i].fd == (ngx_socket_t) -1
1445             || c[i].read == NULL
1446             || c[i].read->accept
1447             || c[i].read->channel
1448             || c[i].read->resolver)
1449         {
1450             continue;
1451         }
1452 
1453         ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
1454                        "*%uA shutdown timeout", c[i].number);
1455 
1456         c[i].close = 1;
1457         c[i].error = 1;
1458 
1459         c[i].read->handler(c[i].read);
1460     }
1461 }