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 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].previous = &ls[i];
0524                     ls[i].remain = 1;
0525 
0526                     if (ls[i].backlog != nls[n].backlog) {
0527                         nls[n].listen = 1;
0528                     }
0529 
0530 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0531 
0532                     /*
0533                      * FreeBSD, except the most recent versions,
0534                      * could not remove accept filter
0535                      */
0536                     nls[n].deferred_accept = ls[i].deferred_accept;
0537 
0538                     if (ls[i].accept_filter && nls[n].accept_filter) {
0539                         if (ngx_strcmp(ls[i].accept_filter,
0540                                        nls[n].accept_filter)
0541                             != 0)
0542                         {
0543                             nls[n].delete_deferred = 1;
0544                             nls[n].add_deferred = 1;
0545                         }
0546 
0547                     } else if (ls[i].accept_filter) {
0548                         nls[n].delete_deferred = 1;
0549 
0550                     } else if (nls[n].accept_filter) {
0551                         nls[n].add_deferred = 1;
0552                     }
0553 #endif
0554 
0555 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0556 
0557                     if (ls[i].deferred_accept && !nls[n].deferred_accept) {
0558                         nls[n].delete_deferred = 1;
0559 
0560                     } else if (ls[i].deferred_accept != nls[n].deferred_accept)
0561                     {
0562                         nls[n].add_deferred = 1;
0563                     }
0564 #endif
0565 
0566 #if (NGX_HAVE_REUSEPORT)
0567                     if (nls[n].reuseport && !ls[i].reuseport) {
0568                         nls[n].add_reuseport = 1;
0569                     }
0570 #endif
0571 
0572                     break;
0573                 }
0574             }
0575 
0576             if (nls[n].fd == (ngx_socket_t) -1) {
0577                 nls[n].open = 1;
0578 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0579                 if (nls[n].accept_filter) {
0580                     nls[n].add_deferred = 1;
0581                 }
0582 #endif
0583 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0584                 if (nls[n].deferred_accept) {
0585                     nls[n].add_deferred = 1;
0586                 }
0587 #endif
0588             }
0589         }
0590 
0591     } else {
0592         ls = cycle->listening.elts;
0593         for (i = 0; i < cycle->listening.nelts; i++) {
0594             ls[i].open = 1;
0595 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
0596             if (ls[i].accept_filter) {
0597                 ls[i].add_deferred = 1;
0598             }
0599 #endif
0600 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
0601             if (ls[i].deferred_accept) {
0602                 ls[i].add_deferred = 1;
0603             }
0604 #endif
0605         }
0606     }
0607 
0608     if (ngx_open_listening_sockets(cycle) != NGX_OK) {
0609         goto failed;
0610     }
0611 
0612     if (!ngx_test_config) {
0613         ngx_configure_listening_sockets(cycle);
0614     }
0615 
0616 
0617     /* commit the new cycle configuration */
0618 
0619     if (!ngx_use_stderr) {
0620         (void) ngx_log_redirect_stderr(cycle);
0621     }
0622 
0623     pool->log = cycle->log;
0624 
0625     if (ngx_init_modules(cycle) != NGX_OK) {
0626         /* fatal */
0627         exit(1);
0628     }
0629 
0630 
0631     /* close and delete stuff that lefts from an old cycle */
0632 
0633     /* free the unnecessary shared memory */
0634 
0635     opart = &old_cycle->shared_memory.part;
0636     oshm_zone = opart->elts;
0637 
0638     for (i = 0; /* void */ ; i++) {
0639 
0640         if (i >= opart->nelts) {
0641             if (opart->next == NULL) {
0642                 goto old_shm_zone_done;
0643             }
0644             opart = opart->next;
0645             oshm_zone = opart->elts;
0646             i = 0;
0647         }
0648 
0649         part = &cycle->shared_memory.part;
0650         shm_zone = part->elts;
0651 
0652         for (n = 0; /* void */ ; n++) {
0653 
0654             if (n >= part->nelts) {
0655                 if (part->next == NULL) {
0656                     break;
0657                 }
0658                 part = part->next;
0659                 shm_zone = part->elts;
0660                 n = 0;
0661             }
0662 
0663             if (oshm_zone[i].shm.name.len != shm_zone[n].shm.name.len) {
0664                 continue;
0665             }
0666 
0667             if (ngx_strncmp(oshm_zone[i].shm.name.data,
0668                             shm_zone[n].shm.name.data,
0669                             oshm_zone[i].shm.name.len)
0670                 != 0)
0671             {
0672                 continue;
0673             }
0674 
0675             if (oshm_zone[i].tag == shm_zone[n].tag
0676                 && oshm_zone[i].shm.size == shm_zone[n].shm.size
0677                 && !oshm_zone[i].noreuse)
0678             {
0679                 goto live_shm_zone;
0680             }
0681 
0682             break;
0683         }
0684 
0685         ngx_shm_free(&oshm_zone[i].shm);
0686 
0687     live_shm_zone:
0688 
0689         continue;
0690     }
0691 
0692 old_shm_zone_done:
0693 
0694 
0695     /* close the unnecessary listening sockets */
0696 
0697     ls = old_cycle->listening.elts;
0698     for (i = 0; i < old_cycle->listening.nelts; i++) {
0699 
0700         if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {
0701             continue;
0702         }
0703 
0704         if (ngx_close_socket(ls[i].fd) == -1) {
0705             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
0706                           ngx_close_socket_n " listening socket on %V failed",
0707                           &ls[i].addr_text);
0708         }
0709 
0710 #if (NGX_HAVE_UNIX_DOMAIN)
0711 
0712         if (ls[i].sockaddr->sa_family == AF_UNIX) {
0713             u_char  *name;
0714 
0715             name = ls[i].addr_text.data + sizeof("unix:") - 1;
0716 
0717             ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
0718                           "deleting socket %s", name);
0719 
0720             if (ngx_delete_file(name) == NGX_FILE_ERROR) {
0721                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
0722                               ngx_delete_file_n " %s failed", name);
0723             }
0724         }
0725 
0726 #endif
0727     }
0728 
0729 
0730     /* close the unnecessary open files */
0731 
0732     part = &old_cycle->open_files.part;
0733     file = part->elts;
0734 
0735     for (i = 0; /* void */ ; i++) {
0736 
0737         if (i >= part->nelts) {
0738             if (part->next == NULL) {
0739                 break;
0740             }
0741             part = part->next;
0742             file = part->elts;
0743             i = 0;
0744         }
0745 
0746         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
0747             continue;
0748         }
0749 
0750         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
0751             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0752                           ngx_close_file_n " \"%s\" failed",
0753                           file[i].name.data);
0754         }
0755     }
0756 
0757     ngx_destroy_pool(conf.temp_pool);
0758 
0759     if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
0760 
0761         ngx_destroy_pool(old_cycle->pool);
0762         cycle->old_cycle = NULL;
0763 
0764         return cycle;
0765     }
0766 
0767 
0768     if (ngx_temp_pool == NULL) {
0769         ngx_temp_pool = ngx_create_pool(128, cycle->log);
0770         if (ngx_temp_pool == NULL) {
0771             ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0772                           "could not create ngx_temp_pool");
0773             exit(1);
0774         }
0775 
0776         n = 10;
0777 
0778         if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n,
0779                            sizeof(ngx_cycle_t *))
0780             != NGX_OK)
0781         {
0782             exit(1);
0783         }
0784 
0785         ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *));
0786 
0787         ngx_cleaner_event.handler = ngx_clean_old_cycles;
0788         ngx_cleaner_event.log = cycle->log;
0789         ngx_cleaner_event.data = &dumb;
0790         dumb.fd = (ngx_socket_t) -1;
0791     }
0792 
0793     ngx_temp_pool->log = cycle->log;
0794 
0795     old = ngx_array_push(&ngx_old_cycles);
0796     if (old == NULL) {
0797         exit(1);
0798     }
0799     *old = old_cycle;
0800 
0801     if (!ngx_cleaner_event.timer_set) {
0802         ngx_add_timer(&ngx_cleaner_event, 30000);
0803         ngx_cleaner_event.timer_set = 1;
0804     }
0805 
0806     return cycle;
0807 
0808 
0809 failed:
0810 
0811     if (!ngx_is_init_cycle(old_cycle)) {
0812         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
0813                                                    ngx_core_module);
0814         if (old_ccf->environment) {
0815             environ = old_ccf->environment;
0816         }
0817     }
0818 
0819     /* rollback the new cycle configuration */
0820 
0821     part = &cycle->open_files.part;
0822     file = part->elts;
0823 
0824     for (i = 0; /* void */ ; i++) {
0825 
0826         if (i >= part->nelts) {
0827             if (part->next == NULL) {
0828                 break;
0829             }
0830             part = part->next;
0831             file = part->elts;
0832             i = 0;
0833         }
0834 
0835         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
0836             continue;
0837         }
0838 
0839         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
0840             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0841                           ngx_close_file_n " \"%s\" failed",
0842                           file[i].name.data);
0843         }
0844     }
0845 
0846     if (ngx_test_config) {
0847         ngx_destroy_cycle_pools(&conf);
0848         return NULL;
0849     }
0850 
0851     ls = cycle->listening.elts;
0852     for (i = 0; i < cycle->listening.nelts; i++) {
0853         if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {
0854             continue;
0855         }
0856 
0857         if (ngx_close_socket(ls[i].fd) == -1) {
0858             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
0859                           ngx_close_socket_n " %V failed",
0860                           &ls[i].addr_text);
0861         }
0862     }
0863 
0864     ngx_destroy_cycle_pools(&conf);
0865 
0866     return NULL;
0867 }
0868 
0869 
0870 static void
0871 ngx_destroy_cycle_pools(ngx_conf_t *conf)
0872 {
0873     ngx_destroy_pool(conf->temp_pool);
0874     ngx_destroy_pool(conf->pool);
0875 }
0876 
0877 
0878 static ngx_int_t
0879 ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
0880 {
0881     u_char           *file;
0882     ngx_slab_pool_t  *sp;
0883 
0884     sp = (ngx_slab_pool_t *) zn->shm.addr;
0885 
0886     if (zn->shm.exists) {
0887 
0888         if (sp == sp->addr) {
0889             return NGX_OK;
0890         }
0891 
0892 #if (NGX_WIN32)
0893 
0894         /* remap at the required address */
0895 
0896         if (ngx_shm_remap(&zn->shm, sp->addr) != NGX_OK) {
0897             return NGX_ERROR;
0898         }
0899 
0900         sp = (ngx_slab_pool_t *) zn->shm.addr;
0901 
0902         if (sp == sp->addr) {
0903             return NGX_OK;
0904         }
0905 
0906 #endif
0907 
0908         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
0909                       "shared zone \"%V\" has no equal addresses: %p vs %p",
0910                       &zn->shm.name, sp->addr, sp);
0911         return NGX_ERROR;
0912     }
0913 
0914     sp->end = zn->shm.addr + zn->shm.size;
0915     sp->min_shift = 3;
0916     sp->addr = zn->shm.addr;
0917 
0918 #if (NGX_HAVE_ATOMIC_OPS)
0919 
0920     file = NULL;
0921 
0922 #else
0923 
0924     file = ngx_pnalloc(cycle->pool, cycle->lock_file.len + zn->shm.name.len);
0925     if (file == NULL) {
0926         return NGX_ERROR;
0927     }
0928 
0929     (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
0930 
0931 #endif
0932 
0933     if (ngx_shmtx_create(&sp->mutex, &sp->lock, file) != NGX_OK) {
0934         return NGX_ERROR;
0935     }
0936 
0937     ngx_slab_init(sp);
0938 
0939     return NGX_OK;
0940 }
0941 
0942 
0943 ngx_int_t
0944 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
0945 {
0946     size_t      len;
0947     ngx_uint_t  create;
0948     ngx_file_t  file;
0949     u_char      pid[NGX_INT64_LEN + 2];
0950 
0951     if (ngx_process > NGX_PROCESS_MASTER) {
0952         return NGX_OK;
0953     }
0954 
0955     ngx_memzero(&file, sizeof(ngx_file_t));
0956 
0957     file.name = *name;
0958     file.log = log;
0959 
0960     create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;
0961 
0962     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
0963                             create, NGX_FILE_DEFAULT_ACCESS);
0964 
0965     if (file.fd == NGX_INVALID_FILE) {
0966         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0967                       ngx_open_file_n " \"%s\" failed", file.name.data);
0968         return NGX_ERROR;
0969     }
0970 
0971     if (!ngx_test_config) {
0972         len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
0973 
0974         if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
0975             return NGX_ERROR;
0976         }
0977     }
0978 
0979     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
0980         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0981                       ngx_close_file_n " \"%s\" failed", file.name.data);
0982     }
0983 
0984     return NGX_OK;
0985 }
0986 
0987 
0988 void
0989 ngx_delete_pidfile(ngx_cycle_t *cycle)
0990 {
0991     u_char           *name;
0992     ngx_core_conf_t  *ccf;
0993 
0994     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
0995 
0996     name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
0997 
0998     if (ngx_delete_file(name) == NGX_FILE_ERROR) {
0999         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1000                       ngx_delete_file_n " \"%s\" failed", name);
1001     }
1002 }
1003 
1004 
1005 ngx_int_t
1006 ngx_signal_process(ngx_cycle_t *cycle, char *sig)
1007 {
1008     ssize_t           n;
1009     ngx_pid_t         pid;
1010     ngx_file_t        file;
1011     ngx_core_conf_t  *ccf;
1012     u_char            buf[NGX_INT64_LEN + 2];
1013 
1014     ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");
1015 
1016     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1017 
1018     ngx_memzero(&file, sizeof(ngx_file_t));
1019 
1020     file.name = ccf->pid;
1021     file.log = cycle->log;
1022 
1023     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,
1024                             NGX_FILE_OPEN, NGX_FILE_DEFAULT_ACCESS);
1025 
1026     if (file.fd == NGX_INVALID_FILE) {
1027         ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
1028                       ngx_open_file_n " \"%s\" failed", file.name.data);
1029         return 1;
1030     }
1031 
1032     n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);
1033 
1034     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1035         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1036                       ngx_close_file_n " \"%s\" failed", file.name.data);
1037     }
1038 
1039     if (n == NGX_ERROR) {
1040         return 1;
1041     }
1042 
1043     while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }
1044 
1045     pid = ngx_atoi(buf, ++n);
1046 
1047     if (pid == (ngx_pid_t) NGX_ERROR) {
1048         ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
1049                       "invalid PID number \"%*s\" in \"%s\"",
1050                       n, buf, file.name.data);
1051         return 1;
1052     }
1053 
1054     return ngx_os_signal_process(cycle, sig, pid);
1055 
1056 }
1057 
1058 
1059 static ngx_int_t
1060 ngx_test_lockfile(u_char *file, ngx_log_t *log)
1061 {
1062 #if !(NGX_HAVE_ATOMIC_OPS)
1063     ngx_fd_t  fd;
1064 
1065     fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
1066                        NGX_FILE_DEFAULT_ACCESS);
1067 
1068     if (fd == NGX_INVALID_FILE) {
1069         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1070                       ngx_open_file_n " \"%s\" failed", file);
1071         return NGX_ERROR;
1072     }
1073 
1074     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1075         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1076                       ngx_close_file_n " \"%s\" failed", file);
1077     }
1078 
1079     if (ngx_delete_file(file) == NGX_FILE_ERROR) {
1080         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1081                       ngx_delete_file_n " \"%s\" failed", file);
1082     }
1083 
1084 #endif
1085 
1086     return NGX_OK;
1087 }
1088 
1089 
1090 void
1091 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
1092 {
1093     ngx_fd_t          fd;
1094     ngx_uint_t        i;
1095     ngx_list_part_t  *part;
1096     ngx_open_file_t  *file;
1097 
1098     part = &cycle->open_files.part;
1099     file = part->elts;
1100 
1101     for (i = 0; /* void */ ; i++) {
1102 
1103         if (i >= part->nelts) {
1104             if (part->next == NULL) {
1105                 break;
1106             }
1107             part = part->next;
1108             file = part->elts;
1109             i = 0;
1110         }
1111 
1112         if (file[i].name.len == 0) {
1113             continue;
1114         }
1115 
1116         if (file[i].flush) {
1117             file[i].flush(&file[i], cycle->log);
1118         }
1119 
1120         fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
1121                            NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
1122 
1123         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1124                        "reopen file \"%s\", old:%d new:%d",
1125                        file[i].name.data, file[i].fd, fd);
1126 
1127         if (fd == NGX_INVALID_FILE) {
1128             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1129                           ngx_open_file_n " \"%s\" failed", file[i].name.data);
1130             continue;
1131         }
1132 
1133 #if !(NGX_WIN32)
1134         if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1135             ngx_file_info_t  fi;
1136 
1137             if (ngx_file_info(file[i].name.data, &fi) == NGX_FILE_ERROR) {
1138                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1139                               ngx_file_info_n " \"%s\" failed",
1140                               file[i].name.data);
1141 
1142                 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1143                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1144                                   ngx_close_file_n " \"%s\" failed",
1145                                   file[i].name.data);
1146                 }
1147 
1148                 continue;
1149             }
1150 
1151             if (fi.st_uid != user) {
1152                 if (chown((const char *) file[i].name.data, user, -1) == -1) {
1153                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1154                                   "chown(\"%s\", %d) failed",
1155                                   file[i].name.data, user);
1156 
1157                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1158                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1159                                       ngx_close_file_n " \"%s\" failed",
1160                                       file[i].name.data);
1161                     }
1162 
1163                     continue;
1164                 }
1165             }
1166 
1167             if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {
1168 
1169                 fi.st_mode |= (S_IRUSR|S_IWUSR);
1170 
1171                 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
1172                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1173                                   "chmod() \"%s\" failed", file[i].name.data);
1174 
1175                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1176                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1177                                       ngx_close_file_n " \"%s\" failed",
1178                                       file[i].name.data);
1179                     }
1180 
1181                     continue;
1182                 }
1183             }
1184         }
1185 
1186         if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1187             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1188                           "fcntl(FD_CLOEXEC) \"%s\" failed",
1189                           file[i].name.data);
1190 
1191             if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1192                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1193                               ngx_close_file_n " \"%s\" failed",
1194                               file[i].name.data);
1195             }
1196 
1197             continue;
1198         }
1199 #endif
1200 
1201         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
1202             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1203                           ngx_close_file_n " \"%s\" failed",
1204                           file[i].name.data);
1205         }
1206 
1207         file[i].fd = fd;
1208     }
1209 
1210     (void) ngx_log_redirect_stderr(cycle);
1211 }
1212 
1213 
1214 ngx_shm_zone_t *
1215 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
1216 {
1217     ngx_uint_t        i;
1218     ngx_shm_zone_t   *shm_zone;
1219     ngx_list_part_t  *part;
1220 
1221     part = &cf->cycle->shared_memory.part;
1222     shm_zone = part->elts;
1223 
1224     for (i = 0; /* void */ ; i++) {
1225 
1226         if (i >= part->nelts) {
1227             if (part->next == NULL) {
1228                 break;
1229             }
1230             part = part->next;
1231             shm_zone = part->elts;
1232             i = 0;
1233         }
1234 
1235         if (name->len != shm_zone[i].shm.name.len) {
1236             continue;
1237         }
1238 
1239         if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
1240             != 0)
1241         {
1242             continue;
1243         }
1244 
1245         if (tag != shm_zone[i].tag) {
1246             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1247                             "the shared memory zone \"%V\" is "
1248                             "already declared for a different use",
1249                             &shm_zone[i].shm.name);
1250             return NULL;
1251         }
1252 
1253         if (shm_zone[i].shm.size == 0) {
1254             shm_zone[i].shm.size = size;
1255         }
1256 
1257         if (size && size != shm_zone[i].shm.size) {
1258             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1259                             "the size %uz of shared memory zone \"%V\" "
1260                             "conflicts with already declared size %uz",
1261                             size, &shm_zone[i].shm.name, shm_zone[i].shm.size);
1262             return NULL;
1263         }
1264 
1265         return &shm_zone[i];
1266     }
1267 
1268     shm_zone = ngx_list_push(&cf->cycle->shared_memory);
1269 
1270     if (shm_zone == NULL) {
1271         return NULL;
1272     }
1273 
1274     shm_zone->data = NULL;
1275     shm_zone->shm.log = cf->cycle->log;
1276     shm_zone->shm.size = size;
1277     shm_zone->shm.name = *name;
1278     shm_zone->shm.exists = 0;
1279     shm_zone->init = NULL;
1280     shm_zone->tag = tag;
1281     shm_zone->noreuse = 0;
1282 
1283     return shm_zone;
1284 }
1285 
1286 
1287 static void
1288 ngx_clean_old_cycles(ngx_event_t *ev)
1289 {
1290     ngx_uint_t     i, n, found, live;
1291     ngx_log_t     *log;
1292     ngx_cycle_t  **cycle;
1293 
1294     log = ngx_cycle->log;
1295     ngx_temp_pool->log = log;
1296 
1297     ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
1298 
1299     live = 0;
1300 
1301     cycle = ngx_old_cycles.elts;
1302     for (i = 0; i < ngx_old_cycles.nelts; i++) {
1303 
1304         if (cycle[i] == NULL) {
1305             continue;
1306         }
1307 
1308         found = 0;
1309 
1310         for (n = 0; n < cycle[i]->connection_n; n++) {
1311             if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
1312                 found = 1;
1313 
1314                 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%ui", n);
1315 
1316                 break;
1317             }
1318         }
1319 
1320         if (found) {
1321             live = 1;
1322             continue;
1323         }
1324 
1325         ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %ui", i);
1326 
1327         ngx_destroy_pool(cycle[i]->pool);
1328         cycle[i] = NULL;
1329     }
1330 
1331     ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %ui", live);
1332 
1333     if (live) {
1334         ngx_add_timer(ev, 30000);
1335 
1336     } else {
1337         ngx_destroy_pool(ngx_temp_pool);
1338         ngx_temp_pool = NULL;
1339         ngx_old_cycles.nelts = 0;
1340     }
1341 }
1342 
1343 
1344 void
1345 ngx_set_shutdown_timer(ngx_cycle_t *cycle)
1346 {
1347     ngx_core_conf_t  *ccf;
1348 
1349     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1350 
1351     if (ccf->shutdown_timeout) {
1352         ngx_shutdown_event.handler = ngx_shutdown_timer_handler;
1353         ngx_shutdown_event.data = cycle;
1354         ngx_shutdown_event.log = cycle->log;
1355         ngx_shutdown_event.cancelable = 1;
1356 
1357         ngx_add_timer(&ngx_shutdown_event, ccf->shutdown_timeout);
1358     }
1359 }
1360 
1361 
1362 static void
1363 ngx_shutdown_timer_handler(ngx_event_t *ev)
1364 {
1365     ngx_uint_t         i;
1366     ngx_cycle_t       *cycle;
1367     ngx_connection_t  *c;
1368 
1369     cycle = ev->data;
1370 
1371     c = cycle->connections;
1372 
1373     for (i = 0; i < cycle->connection_n; i++) {
1374 
1375         if (c[i].fd == (ngx_socket_t) -1
1376             || c[i].read == NULL
1377             || c[i].read->accept
1378             || c[i].read->channel
1379             || c[i].read->resolver)
1380         {
1381             continue;
1382         }
1383 
1384         ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
1385                        "*%uA shutdown timeout", c[i].number);
1386 
1387         c[i].close = 1;
1388         c[i].error = 1;
1389 
1390         c[i].read->handler(c[i].read);
1391     }
1392 }