src/core/ngx_cycle.c - nginx-1.31.3 nginx/ @ 42f8df65b

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>


  8. static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
  9. static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
  10.     ngx_shm_zone_t *shm_zone);
  11. static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
  12. static void ngx_clean_old_cycles(ngx_event_t *ev);
  13. static void ngx_shutdown_timer_handler(ngx_event_t *ev);


  14. volatile ngx_cycle_t  *ngx_cycle;
  15. ngx_array_t            ngx_old_cycles;

  16. static ngx_pool_t     *ngx_temp_pool;
  17. static ngx_event_t     ngx_cleaner_event;
  18. static ngx_event_t     ngx_shutdown_event;

  19. ngx_uint_t             ngx_test_config;
  20. ngx_uint_t             ngx_dump_config;
  21. ngx_uint_t             ngx_quiet_mode;


  22. /* STUB NAME */
  23. static ngx_connection_t  dumb;
  24. /* STUB */


  25. ngx_cycle_t *
  26. ngx_init_cycle(ngx_cycle_t *old_cycle)
  27. {
  28.     void                *rv, *data;
  29.     char               **senv;
  30.     ngx_uint_t           i, n;
  31.     ngx_log_t           *log;
  32.     ngx_time_t          *tp;
  33.     ngx_conf_t           conf;
  34.     ngx_pool_t          *pool;
  35.     ngx_cycle_t         *cycle, **old;
  36.     ngx_shm_zone_t      *shm_zone, *oshm_zone;
  37.     ngx_list_part_t     *part, *opart;
  38.     ngx_open_file_t     *file;
  39.     ngx_listening_t     *ls, *nls;
  40.     ngx_core_conf_t     *ccf, *old_ccf;
  41.     ngx_core_module_t   *module;
  42.     char                 hostname[NGX_MAXHOSTNAMELEN];

  43.     ngx_timezone_update();

  44.     /* force localtime update with a new timezone */

  45.     tp = ngx_timeofday();
  46.     tp->sec = 0;

  47.     ngx_time_update();


  48.     log = old_cycle->log;

  49.     pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
  50.     if (pool == NULL) {
  51.         return NULL;
  52.     }
  53.     pool->log = log;

  54.     cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
  55.     if (cycle == NULL) {
  56.         ngx_destroy_pool(pool);
  57.         return NULL;
  58.     }

  59.     cycle->pool = pool;
  60.     cycle->log = log;
  61.     cycle->old_cycle = old_cycle;

  62.     cycle->conf_prefix.len = old_cycle->conf_prefix.len;
  63.     cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
  64.     if (cycle->conf_prefix.data == NULL) {
  65.         ngx_destroy_pool(pool);
  66.         return NULL;
  67.     }

  68.     cycle->prefix.len = old_cycle->prefix.len;
  69.     cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
  70.     if (cycle->prefix.data == NULL) {
  71.         ngx_destroy_pool(pool);
  72.         return NULL;
  73.     }

  74.     cycle->error_log.len = old_cycle->error_log.len;
  75.     cycle->error_log.data = ngx_pnalloc(pool, old_cycle->error_log.len + 1);
  76.     if (cycle->error_log.data == NULL) {
  77.         ngx_destroy_pool(pool);
  78.         return NULL;
  79.     }
  80.     ngx_cpystrn(cycle->error_log.data, old_cycle->error_log.data,
  81.                 old_cycle->error_log.len + 1);

  82.     cycle->conf_file.len = old_cycle->conf_file.len;
  83.     cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
  84.     if (cycle->conf_file.data == NULL) {
  85.         ngx_destroy_pool(pool);
  86.         return NULL;
  87.     }
  88.     ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
  89.                 old_cycle->conf_file.len + 1);

  90.     if (old_cycle->conf_param.len) {
  91.         cycle->conf_param.len = old_cycle->conf_param.len;
  92.         cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
  93.         if (cycle->conf_param.data == NULL) {
  94.             ngx_destroy_pool(pool);
  95.             return NULL;
  96.         }
  97.     }


  98.     n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;

  99.     if (ngx_array_init(&cycle->paths, pool, n, sizeof(ngx_path_t *))
  100.         != NGX_OK)
  101.     {
  102.         ngx_destroy_pool(pool);
  103.         return NULL;
  104.     }

  105.     ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *));


  106.     if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))
  107.         != NGX_OK)
  108.     {
  109.         ngx_destroy_pool(pool);
  110.         return NULL;
  111.     }

  112.     ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,
  113.                     ngx_str_rbtree_insert_value);

  114.     if (old_cycle->open_files.part.nelts) {
  115.         n = old_cycle->open_files.part.nelts;
  116.         for (part = old_cycle->open_files.part.next; part; part = part->next) {
  117.             n += part->nelts;
  118.         }

  119.     } else {
  120.         n = 20;
  121.     }

  122.     if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
  123.         != NGX_OK)
  124.     {
  125.         ngx_destroy_pool(pool);
  126.         return NULL;
  127.     }


  128.     if (old_cycle->shared_memory.part.nelts) {
  129.         n = old_cycle->shared_memory.part.nelts;
  130.         for (part = old_cycle->shared_memory.part.next; part; part = part->next)
  131.         {
  132.             n += part->nelts;
  133.         }

  134.     } else {
  135.         n = 1;
  136.     }

  137.     if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
  138.         != NGX_OK)
  139.     {
  140.         ngx_destroy_pool(pool);
  141.         return NULL;
  142.     }

  143.     n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;

  144.     if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t))
  145.         != NGX_OK)
  146.     {
  147.         ngx_destroy_pool(pool);
  148.         return NULL;
  149.     }

  150.     ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t));


  151.     ngx_queue_init(&cycle->reusable_connections_queue);


  152.     cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
  153.     if (cycle->conf_ctx == NULL) {
  154.         ngx_destroy_pool(pool);
  155.         return NULL;
  156.     }


  157.     if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {
  158.         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");
  159.         ngx_destroy_pool(pool);
  160.         return NULL;
  161.     }

  162.     /* on Linux gethostname() silently truncates name that does not fit */

  163.     hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
  164.     cycle->hostname.len = ngx_strlen(hostname);

  165.     cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);
  166.     if (cycle->hostname.data == NULL) {
  167.         ngx_destroy_pool(pool);
  168.         return NULL;
  169.     }

  170.     ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);


  171.     if (ngx_cycle_modules(cycle) != NGX_OK) {
  172.         ngx_destroy_pool(pool);
  173.         return NULL;
  174.     }


  175.     for (i = 0; cycle->modules[i]; i++) {
  176.         if (cycle->modules[i]->type != NGX_CORE_MODULE) {
  177.             continue;
  178.         }

  179.         module = cycle->modules[i]->ctx;

  180.         if (module->create_conf) {
  181.             rv = module->create_conf(cycle);
  182.             if (rv == NULL) {
  183.                 ngx_destroy_pool(pool);
  184.                 return NULL;
  185.             }
  186.             cycle->conf_ctx[cycle->modules[i]->index] = rv;
  187.         }
  188.     }


  189.     senv = environ;


  190.     ngx_memzero(&conf, sizeof(ngx_conf_t));
  191.     /* STUB: init array ? */
  192.     conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));
  193.     if (conf.args == NULL) {
  194.         ngx_destroy_pool(pool);
  195.         return NULL;
  196.     }

  197.     conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
  198.     if (conf.temp_pool == NULL) {
  199.         ngx_destroy_pool(pool);
  200.         return NULL;
  201.     }


  202.     conf.ctx = cycle->conf_ctx;
  203.     conf.cycle = cycle;
  204.     conf.pool = pool;
  205.     conf.log = log;
  206.     conf.module_type = NGX_CORE_MODULE;
  207.     conf.cmd_type = NGX_MAIN_CONF;

  208. #if 0
  209.     log->log_level = NGX_LOG_DEBUG_ALL;
  210. #endif

  211.     if (ngx_conf_param(&conf) != NGX_CONF_OK) {
  212.         environ = senv;
  213.         ngx_destroy_cycle_pools(&conf);
  214.         return NULL;
  215.     }

  216.     if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
  217.         environ = senv;
  218.         ngx_destroy_cycle_pools(&conf);
  219.         return NULL;
  220.     }

  221.     if (ngx_test_config && !ngx_quiet_mode) {
  222.         ngx_log_stderr(0, "the configuration file %s syntax is ok",
  223.                        cycle->conf_file.data);
  224.     }

  225.     for (i = 0; cycle->modules[i]; i++) {
  226.         if (cycle->modules[i]->type != NGX_CORE_MODULE) {
  227.             continue;
  228.         }

  229.         module = cycle->modules[i]->ctx;

  230.         if (module->init_conf) {
  231.             if (module->init_conf(cycle,
  232.                                   cycle->conf_ctx[cycle->modules[i]->index])
  233.                 == NGX_CONF_ERROR)
  234.             {
  235.                 environ = senv;
  236.                 ngx_destroy_cycle_pools(&conf);
  237.                 return NULL;
  238.             }
  239.         }
  240.     }

  241.     if (ngx_process == NGX_PROCESS_SIGNALLER) {
  242.         return cycle;
  243.     }

  244.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

  245.     if (ngx_test_config) {

  246.         if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
  247.             goto failed;
  248.         }

  249.     } else if (!ngx_is_init_cycle(old_cycle)) {

  250.         /*
  251.          * we do not create the pid file in the first ngx_init_cycle() call
  252.          * because we need to write the demonized process pid
  253.          */

  254.         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
  255.                                                    ngx_core_module);
  256.         if (ccf->pid.len != old_ccf->pid.len
  257.             || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)
  258.         {
  259.             /* new pid file name */

  260.             if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
  261.                 goto failed;
  262.             }

  263.             ngx_delete_pidfile(old_cycle);
  264.         }
  265.     }


  266.     if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
  267.         goto failed;
  268.     }


  269.     if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {
  270.         goto failed;
  271.     }


  272.     if (ngx_log_open_default(cycle) != NGX_OK) {
  273.         goto failed;
  274.     }

  275.     /* open the new files */

  276.     part = &cycle->open_files.part;
  277.     file = part->elts;

  278.     for (i = 0; /* void */ ; i++) {

  279.         if (i >= part->nelts) {
  280.             if (part->next == NULL) {
  281.                 break;
  282.             }
  283.             part = part->next;
  284.             file = part->elts;
  285.             i = 0;
  286.         }

  287.         if (file[i].name.len == 0) {
  288.             continue;
  289.         }

  290.         file[i].fd = ngx_open_file(file[i].name.data,
  291.                                    NGX_FILE_APPEND,
  292.                                    NGX_FILE_CREATE_OR_OPEN,
  293.                                    NGX_FILE_DEFAULT_ACCESS);

  294.         ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
  295.                        "log: %p %d \"%s\"",
  296.                        &file[i], file[i].fd, file[i].name.data);

  297.         if (file[i].fd == NGX_INVALID_FILE) {
  298.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  299.                           ngx_open_file_n " \"%s\" failed",
  300.                           file[i].name.data);
  301.             goto failed;
  302.         }

  303. #if !(NGX_WIN32)
  304.         if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
  305.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  306.                           "fcntl(FD_CLOEXEC) \"%s\" failed",
  307.                           file[i].name.data);
  308.             goto failed;
  309.         }
  310. #endif
  311.     }

  312.     cycle->log = &cycle->new_log;
  313.     pool->log = &cycle->new_log;


  314.     /* create shared memory */

  315.     part = &cycle->shared_memory.part;
  316.     shm_zone = part->elts;

  317.     for (i = 0; /* void */ ; i++) {

  318.         if (i >= part->nelts) {
  319.             if (part->next == NULL) {
  320.                 break;
  321.             }
  322.             part = part->next;
  323.             shm_zone = part->elts;
  324.             i = 0;
  325.         }

  326.         if (shm_zone[i].shm.size == 0) {
  327.             ngx_log_error(NGX_LOG_EMERG, log, 0,
  328.                           "zero size shared memory zone \"%V\"",
  329.                           &shm_zone[i].shm.name);
  330.             goto failed;
  331.         }

  332.         shm_zone[i].shm.log = cycle->log;

  333.         opart = &old_cycle->shared_memory.part;
  334.         oshm_zone = opart->elts;

  335.         data = NULL;

  336.         for (n = 0; /* void */ ; n++) {

  337.             if (n >= opart->nelts) {
  338.                 if (opart->next == NULL) {
  339.                     break;
  340.                 }
  341.                 opart = opart->next;
  342.                 oshm_zone = opart->elts;
  343.                 n = 0;
  344.             }

  345.             if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
  346.                 continue;
  347.             }

  348.             if (ngx_strncmp(shm_zone[i].shm.name.data,
  349.                             oshm_zone[n].shm.name.data,
  350.                             shm_zone[i].shm.name.len)
  351.                 != 0)
  352.             {
  353.                 continue;
  354.             }

  355.             if (shm_zone[i].tag == oshm_zone[n].tag && shm_zone[i].noreuse) {
  356.                 data = oshm_zone[n].data;
  357.                 break;
  358.             }

  359.             if (shm_zone[i].tag == oshm_zone[n].tag
  360.                 && shm_zone[i].shm.size == oshm_zone[n].shm.size)
  361.             {
  362.                 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
  363. #if (NGX_WIN32)
  364.                 shm_zone[i].shm.handle = oshm_zone[n].shm.handle;
  365. #endif

  366.                 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
  367.                     != NGX_OK)
  368.                 {
  369.                     goto failed;
  370.                 }

  371.                 goto shm_zone_found;
  372.             }

  373.             break;
  374.         }

  375.         if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
  376.             goto failed;
  377.         }

  378.         if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
  379.             goto failed;
  380.         }

  381.         if (shm_zone[i].init(&shm_zone[i], data) != NGX_OK) {
  382.             goto failed;
  383.         }

  384.     shm_zone_found:

  385.         continue;
  386.     }


  387.     /* handle the listening sockets */

  388.     if (old_cycle->listening.nelts) {
  389.         ls = old_cycle->listening.elts;
  390.         for (i = 0; i < old_cycle->listening.nelts; i++) {
  391.             ls[i].remain = 0;
  392.         }

  393.         nls = cycle->listening.elts;
  394.         for (n = 0; n < cycle->listening.nelts; n++) {

  395.             for (i = 0; i < old_cycle->listening.nelts; i++) {
  396.                 if (ls[i].ignore) {
  397.                     continue;
  398.                 }

  399.                 if (ls[i].remain) {
  400.                     continue;
  401.                 }

  402.                 if (ls[i].type != nls[n].type) {
  403.                     continue;
  404.                 }

  405.                 if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,
  406.                                      ls[i].sockaddr, ls[i].socklen, 1)
  407.                     == NGX_OK)
  408.                 {
  409.                     nls[n].fd = ls[i].fd;
  410.                     nls[n].previous = &ls[i];

  411.                     if (ls[i].protocol != nls[n].protocol) {
  412.                         nls[n].change_protocol = 1;

  413.                     } else {
  414.                         nls[n].inherited = ls[i].inherited;
  415.                         ls[i].remain = 1;
  416.                     }

  417.                     if (ls[i].backlog != nls[n].backlog) {
  418.                         nls[n].listen = 1;
  419.                     }

  420. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)

  421.                     /*
  422.                      * FreeBSD, except the most recent versions,
  423.                      * could not remove accept filter
  424.                      */
  425.                     nls[n].deferred_accept = ls[i].deferred_accept;

  426.                     if (ls[i].accept_filter && nls[n].accept_filter) {
  427.                         if (ngx_strcmp(ls[i].accept_filter,
  428.                                        nls[n].accept_filter)
  429.                             != 0)
  430.                         {
  431.                             nls[n].delete_deferred = 1;
  432.                             nls[n].add_deferred = 1;
  433.                         }

  434.                     } else if (ls[i].accept_filter) {
  435.                         nls[n].delete_deferred = 1;

  436.                     } else if (nls[n].accept_filter) {
  437.                         nls[n].add_deferred = 1;
  438.                     }
  439. #endif

  440. #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)

  441.                     if (ls[i].deferred_accept && !nls[n].deferred_accept) {
  442.                         nls[n].delete_deferred = 1;

  443.                     } else if (ls[i].deferred_accept != nls[n].deferred_accept)
  444.                     {
  445.                         nls[n].add_deferred = 1;
  446.                     }
  447. #endif

  448. #if (NGX_HAVE_REUSEPORT)
  449.                     if (nls[n].reuseport && !ls[i].reuseport) {
  450.                         nls[n].add_reuseport = 1;
  451.                     }
  452. #endif

  453.                     break;
  454.                 }
  455.             }

  456.             if (nls[n].fd == (ngx_socket_t) -1) {
  457. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
  458.                 if (nls[n].accept_filter) {
  459.                     nls[n].add_deferred = 1;
  460.                 }
  461. #endif
  462. #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
  463.                 if (nls[n].deferred_accept) {
  464.                     nls[n].add_deferred = 1;
  465.                 }
  466. #endif
  467.             }
  468.         }

  469.     } else {
  470. #if (NGX_HAVE_DEFERRED_ACCEPT)
  471.         ls = cycle->listening.elts;
  472.         for (i = 0; i < cycle->listening.nelts; i++) {
  473. #ifdef SO_ACCEPTFILTER
  474.             if (ls[i].accept_filter) {
  475.                 ls[i].add_deferred = 1;
  476.             }
  477. #endif
  478. #ifdef TCP_DEFER_ACCEPT
  479.             if (ls[i].deferred_accept) {
  480.                 ls[i].add_deferred = 1;
  481.             }
  482. #endif
  483.         }
  484. #endif
  485.     }

  486.     if (ngx_open_listening_sockets(cycle) != NGX_OK) {
  487.         goto failed;
  488.     }

  489.     if (!ngx_test_config) {
  490.         ngx_configure_listening_sockets(cycle);
  491.     }


  492.     /* commit the new cycle configuration */

  493.     if (!ngx_use_stderr) {
  494.         (void) ngx_log_redirect_stderr(cycle);
  495.     }

  496.     pool->log = cycle->log;

  497.     if (ngx_init_modules(cycle) != NGX_OK) {
  498.         /* fatal */
  499.         exit(1);
  500.     }


  501.     /* close and delete stuff that lefts from an old cycle */

  502.     /* free the unnecessary shared memory */

  503.     opart = &old_cycle->shared_memory.part;
  504.     oshm_zone = opart->elts;

  505.     for (i = 0; /* void */ ; i++) {

  506.         if (i >= opart->nelts) {
  507.             if (opart->next == NULL) {
  508.                 goto old_shm_zone_done;
  509.             }
  510.             opart = opart->next;
  511.             oshm_zone = opart->elts;
  512.             i = 0;
  513.         }

  514.         part = &cycle->shared_memory.part;
  515.         shm_zone = part->elts;

  516.         for (n = 0; /* void */ ; n++) {

  517.             if (n >= part->nelts) {
  518.                 if (part->next == NULL) {
  519.                     break;
  520.                 }
  521.                 part = part->next;
  522.                 shm_zone = part->elts;
  523.                 n = 0;
  524.             }

  525.             if (oshm_zone[i].shm.name.len != shm_zone[n].shm.name.len) {
  526.                 continue;
  527.             }

  528.             if (ngx_strncmp(oshm_zone[i].shm.name.data,
  529.                             shm_zone[n].shm.name.data,
  530.                             oshm_zone[i].shm.name.len)
  531.                 != 0)
  532.             {
  533.                 continue;
  534.             }

  535.             if (oshm_zone[i].tag == shm_zone[n].tag
  536.                 && oshm_zone[i].shm.size == shm_zone[n].shm.size
  537.                 && !oshm_zone[i].noreuse)
  538.             {
  539.                 goto live_shm_zone;
  540.             }

  541.             break;
  542.         }

  543.         ngx_shm_free(&oshm_zone[i].shm);

  544.     live_shm_zone:

  545.         continue;
  546.     }

  547. old_shm_zone_done:


  548.     /* close the unnecessary listening sockets */

  549.     ls = old_cycle->listening.elts;
  550.     for (i = 0; i < old_cycle->listening.nelts; i++) {

  551.         if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {
  552.             continue;
  553.         }

  554.         if (ngx_close_socket(ls[i].fd) == -1) {
  555.             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
  556.                           ngx_close_socket_n " listening socket on %V failed",
  557.                           &ls[i].addr_text);
  558.         }

  559. #if (NGX_HAVE_UNIX_DOMAIN)

  560.         if (ls[i].sockaddr->sa_family == AF_UNIX) {
  561.             u_char  *name;

  562.             name = ls[i].addr_text.data + sizeof("unix:") - 1;

  563.             ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
  564.                           "deleting socket %s", name);

  565.             if (ngx_delete_file(name) == NGX_FILE_ERROR) {
  566.                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
  567.                               ngx_delete_file_n " %s failed", name);
  568.             }
  569.         }

  570. #endif
  571.     }


  572.     /* close the unnecessary open files */

  573.     part = &old_cycle->open_files.part;
  574.     file = part->elts;

  575.     for (i = 0; /* void */ ; i++) {

  576.         if (i >= part->nelts) {
  577.             if (part->next == NULL) {
  578.                 break;
  579.             }
  580.             part = part->next;
  581.             file = part->elts;
  582.             i = 0;
  583.         }

  584.         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
  585.             continue;
  586.         }

  587.         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
  588.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  589.                           ngx_close_file_n " \"%s\" failed",
  590.                           file[i].name.data);
  591.         }
  592.     }

  593.     ngx_destroy_pool(conf.temp_pool);

  594.     if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {

  595.         ngx_destroy_pool(old_cycle->pool);
  596.         cycle->old_cycle = NULL;

  597.         return cycle;
  598.     }


  599.     if (ngx_temp_pool == NULL) {
  600.         ngx_temp_pool = ngx_create_pool(128, cycle->log);
  601.         if (ngx_temp_pool == NULL) {
  602.             ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
  603.                           "could not create ngx_temp_pool");
  604.             exit(1);
  605.         }

  606.         n = 10;

  607.         if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n,
  608.                            sizeof(ngx_cycle_t *))
  609.             != NGX_OK)
  610.         {
  611.             exit(1);
  612.         }

  613.         ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *));

  614.         ngx_cleaner_event.handler = ngx_clean_old_cycles;
  615.         ngx_cleaner_event.log = cycle->log;
  616.         ngx_cleaner_event.data = &dumb;
  617.         dumb.fd = (ngx_socket_t) -1;
  618.     }

  619.     ngx_temp_pool->log = cycle->log;

  620.     old = ngx_array_push(&ngx_old_cycles);
  621.     if (old == NULL) {
  622.         exit(1);
  623.     }
  624.     *old = old_cycle;

  625.     if (!ngx_cleaner_event.timer_set) {
  626.         ngx_add_timer(&ngx_cleaner_event, 30000);
  627.         ngx_cleaner_event.timer_set = 1;
  628.     }

  629.     return cycle;


  630. failed:

  631.     if (!ngx_is_init_cycle(old_cycle)) {
  632.         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
  633.                                                    ngx_core_module);
  634.         if (old_ccf->environment) {
  635.             environ = old_ccf->environment;
  636.         }
  637.     }

  638.     /* rollback the new cycle configuration */

  639.     part = &cycle->open_files.part;
  640.     file = part->elts;

  641.     for (i = 0; /* void */ ; i++) {

  642.         if (i >= part->nelts) {
  643.             if (part->next == NULL) {
  644.                 break;
  645.             }
  646.             part = part->next;
  647.             file = part->elts;
  648.             i = 0;
  649.         }

  650.         if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
  651.             continue;
  652.         }

  653.         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
  654.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  655.                           ngx_close_file_n " \"%s\" failed",
  656.                           file[i].name.data);
  657.         }
  658.     }

  659.     /* free the newly created shared memory */

  660.     part = &cycle->shared_memory.part;
  661.     shm_zone = part->elts;

  662.     for (i = 0; /* void */ ; i++) {

  663.         if (i >= part->nelts) {
  664.             if (part->next == NULL) {
  665.                 break;
  666.             }
  667.             part = part->next;
  668.             shm_zone = part->elts;
  669.             i = 0;
  670.         }

  671.         if (shm_zone[i].shm.addr == NULL) {
  672.             continue;
  673.         }

  674.         opart = &old_cycle->shared_memory.part;
  675.         oshm_zone = opart->elts;

  676.         for (n = 0; /* void */ ; n++) {

  677.             if (n >= opart->nelts) {
  678.                 if (opart->next == NULL) {
  679.                     break;
  680.                 }
  681.                 opart = opart->next;
  682.                 oshm_zone = opart->elts;
  683.                 n = 0;
  684.             }

  685.             if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
  686.                 continue;
  687.             }

  688.             if (ngx_strncmp(shm_zone[i].shm.name.data,
  689.                             oshm_zone[n].shm.name.data,
  690.                             shm_zone[i].shm.name.len)
  691.                 != 0)
  692.             {
  693.                 continue;
  694.             }

  695.             if (shm_zone[i].tag == oshm_zone[n].tag
  696.                 && shm_zone[i].shm.size == oshm_zone[n].shm.size
  697.                 && !shm_zone[i].noreuse)
  698.             {
  699.                 goto old_shm_zone_found;
  700.             }

  701.             break;
  702.         }

  703.         ngx_shm_free(&shm_zone[i].shm);

  704.     old_shm_zone_found:

  705.         continue;
  706.     }

  707.     if (ngx_test_config) {
  708.         ngx_destroy_cycle_pools(&conf);
  709.         return NULL;
  710.     }

  711.     ls = cycle->listening.elts;
  712.     for (i = 0; i < cycle->listening.nelts; i++) {
  713.         if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {
  714.             continue;
  715.         }

  716.         if (ngx_close_socket(ls[i].fd) == -1) {
  717.             ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
  718.                           ngx_close_socket_n " %V failed",
  719.                           &ls[i].addr_text);
  720.         }
  721.     }

  722.     ngx_destroy_cycle_pools(&conf);

  723.     return NULL;
  724. }


  725. static void
  726. ngx_destroy_cycle_pools(ngx_conf_t *conf)
  727. {
  728.     ngx_destroy_pool(conf->temp_pool);
  729.     ngx_destroy_pool(conf->pool);
  730. }


  731. static ngx_int_t
  732. ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
  733. {
  734.     u_char           *file;
  735.     ngx_slab_pool_t  *sp;

  736.     sp = (ngx_slab_pool_t *) zn->shm.addr;

  737.     if (zn->shm.exists) {

  738.         if (sp == sp->addr) {
  739.             return NGX_OK;
  740.         }

  741. #if (NGX_WIN32)

  742.         /* remap at the required address */

  743.         if (ngx_shm_remap(&zn->shm, sp->addr) != NGX_OK) {
  744.             return NGX_ERROR;
  745.         }

  746.         sp = (ngx_slab_pool_t *) zn->shm.addr;

  747.         if (sp == sp->addr) {
  748.             return NGX_OK;
  749.         }

  750. #endif

  751.         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
  752.                       "shared zone \"%V\" has no equal addresses: %p vs %p",
  753.                       &zn->shm.name, sp->addr, sp);
  754.         return NGX_ERROR;
  755.     }

  756.     sp->end = zn->shm.addr + zn->shm.size;
  757.     sp->min_shift = 3;
  758.     sp->addr = zn->shm.addr;

  759. #if (NGX_HAVE_ATOMIC_OPS)

  760.     file = NULL;

  761. #else

  762.     file = ngx_pnalloc(cycle->pool,
  763.                        cycle->lock_file.len + zn->shm.name.len + 1);
  764.     if (file == NULL) {
  765.         return NGX_ERROR;
  766.     }

  767.     (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);

  768. #endif

  769.     if (ngx_shmtx_create(&sp->mutex, &sp->lock, file) != NGX_OK) {
  770.         return NGX_ERROR;
  771.     }

  772.     ngx_slab_init(sp);

  773.     return NGX_OK;
  774. }


  775. ngx_int_t
  776. ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
  777. {
  778.     size_t      len;
  779.     ngx_int_t   rc;
  780.     ngx_uint_t  create;
  781.     ngx_file_t  file;
  782.     u_char      pid[NGX_INT64_LEN + 2];

  783.     if (ngx_process > NGX_PROCESS_MASTER) {
  784.         return NGX_OK;
  785.     }

  786.     ngx_memzero(&file, sizeof(ngx_file_t));

  787.     file.name = *name;
  788.     file.log = log;

  789.     create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;

  790.     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
  791.                             create, NGX_FILE_DEFAULT_ACCESS);

  792.     if (file.fd == NGX_INVALID_FILE) {
  793.         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  794.                       ngx_open_file_n " \"%s\" failed", file.name.data);
  795.         return NGX_ERROR;
  796.     }

  797.     rc = NGX_OK;

  798.     if (!ngx_test_config) {
  799.         len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;

  800.         if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
  801.             rc = NGX_ERROR;
  802.         }
  803.     }

  804.     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
  805.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  806.                       ngx_close_file_n " \"%s\" failed", file.name.data);
  807.     }

  808.     return rc;
  809. }


  810. void
  811. ngx_delete_pidfile(ngx_cycle_t *cycle)
  812. {
  813.     u_char           *name;
  814.     ngx_core_conf_t  *ccf;

  815.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

  816.     name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;

  817.     if (ngx_delete_file(name) == NGX_FILE_ERROR) {
  818.         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
  819.                       ngx_delete_file_n " \"%s\" failed", name);
  820.     }
  821. }


  822. ngx_int_t
  823. ngx_signal_process(ngx_cycle_t *cycle, char *sig)
  824. {
  825.     ssize_t           n;
  826.     ngx_pid_t         pid;
  827.     ngx_file_t        file;
  828.     ngx_core_conf_t  *ccf;
  829.     u_char            buf[NGX_INT64_LEN + 2];

  830.     ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");

  831.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

  832.     ngx_memzero(&file, sizeof(ngx_file_t));

  833.     file.name = ccf->pid;
  834.     file.log = cycle->log;

  835.     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,
  836.                             NGX_FILE_OPEN, NGX_FILE_DEFAULT_ACCESS);

  837.     if (file.fd == NGX_INVALID_FILE) {
  838.         ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
  839.                       ngx_open_file_n " \"%s\" failed", file.name.data);
  840.         return 1;
  841.     }

  842.     n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);

  843.     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
  844.         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
  845.                       ngx_close_file_n " \"%s\" failed", file.name.data);
  846.     }

  847.     if (n == NGX_ERROR) {
  848.         return 1;
  849.     }

  850.     while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }

  851.     pid = ngx_atoi(buf, ++n);

  852.     if (pid == (ngx_pid_t) NGX_ERROR) {
  853.         ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
  854.                       "invalid PID number \"%*s\" in \"%s\"",
  855.                       n, buf, file.name.data);
  856.         return 1;
  857.     }

  858.     return ngx_os_signal_process(cycle, sig, pid);

  859. }


  860. static ngx_int_t
  861. ngx_test_lockfile(u_char *file, ngx_log_t *log)
  862. {
  863. #if !(NGX_HAVE_ATOMIC_OPS)
  864.     ngx_fd_t  fd;

  865.     fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
  866.                        NGX_FILE_DEFAULT_ACCESS);

  867.     if (fd == NGX_INVALID_FILE) {
  868.         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  869.                       ngx_open_file_n " \"%s\" failed", file);
  870.         return NGX_ERROR;
  871.     }

  872.     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  873.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  874.                       ngx_close_file_n " \"%s\" failed", file);
  875.     }

  876.     if (ngx_delete_file(file) == NGX_FILE_ERROR) {
  877.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  878.                       ngx_delete_file_n " \"%s\" failed", file);
  879.     }

  880. #endif

  881.     return NGX_OK;
  882. }


  883. void
  884. ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
  885. {
  886.     ngx_fd_t          fd;
  887.     ngx_uint_t        i;
  888.     ngx_list_part_t  *part;
  889.     ngx_open_file_t  *file;

  890.     part = &cycle->open_files.part;
  891.     file = part->elts;

  892.     for (i = 0; /* void */ ; i++) {

  893.         if (i >= part->nelts) {
  894.             if (part->next == NULL) {
  895.                 break;
  896.             }
  897.             part = part->next;
  898.             file = part->elts;
  899.             i = 0;
  900.         }

  901.         if (file[i].name.len == 0) {
  902.             continue;
  903.         }

  904.         if (file[i].flush) {
  905.             file[i].flush(&file[i], cycle->log);
  906.         }

  907.         fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
  908.                            NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);

  909.         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
  910.                        "reopen file \"%s\", old:%d new:%d",
  911.                        file[i].name.data, file[i].fd, fd);

  912.         if (fd == NGX_INVALID_FILE) {
  913.             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  914.                           ngx_open_file_n " \"%s\" failed", file[i].name.data);
  915.             continue;
  916.         }

  917. #if !(NGX_WIN32)
  918.         if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
  919.             ngx_file_info_t  fi;

  920.             if (ngx_file_info(file[i].name.data, &fi) == NGX_FILE_ERROR) {
  921.                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  922.                               ngx_file_info_n " \"%s\" failed",
  923.                               file[i].name.data);

  924.                 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  925.                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  926.                                   ngx_close_file_n " \"%s\" failed",
  927.                                   file[i].name.data);
  928.                 }

  929.                 continue;
  930.             }

  931.             if (fi.st_uid != user) {
  932.                 if (chown((const char *) file[i].name.data, user, -1) == -1) {
  933.                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  934.                                   "chown(\"%s\", %d) failed",
  935.                                   file[i].name.data, user);

  936.                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  937.                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  938.                                       ngx_close_file_n " \"%s\" failed",
  939.                                       file[i].name.data);
  940.                     }

  941.                     continue;
  942.                 }
  943.             }

  944.             if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {

  945.                 fi.st_mode |= (S_IRUSR|S_IWUSR);

  946.                 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
  947.                     ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  948.                                   "chmod() \"%s\" failed", file[i].name.data);

  949.                     if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  950.                         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  951.                                       ngx_close_file_n " \"%s\" failed",
  952.                                       file[i].name.data);
  953.                     }

  954.                     continue;
  955.                 }
  956.             }
  957.         }

  958.         if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
  959.             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  960.                           "fcntl(FD_CLOEXEC) \"%s\" failed",
  961.                           file[i].name.data);

  962.             if (ngx_close_file(fd) == NGX_FILE_ERROR) {
  963.                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  964.                               ngx_close_file_n " \"%s\" failed",
  965.                               file[i].name.data);
  966.             }

  967.             continue;
  968.         }
  969. #endif

  970.         if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
  971.             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
  972.                           ngx_close_file_n " \"%s\" failed",
  973.                           file[i].name.data);
  974.         }

  975.         file[i].fd = fd;
  976.     }

  977.     (void) ngx_log_redirect_stderr(cycle);
  978. }


  979. ngx_shm_zone_t *
  980. ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
  981. {
  982.     ngx_uint_t        i;
  983.     ngx_shm_zone_t   *shm_zone;
  984.     ngx_list_part_t  *part;

  985.     part = &cf->cycle->shared_memory.part;
  986.     shm_zone = part->elts;

  987.     for (i = 0; /* void */ ; i++) {

  988.         if (i >= part->nelts) {
  989.             if (part->next == NULL) {
  990.                 break;
  991.             }
  992.             part = part->next;
  993.             shm_zone = part->elts;
  994.             i = 0;
  995.         }

  996.         if (name->len != shm_zone[i].shm.name.len) {
  997.             continue;
  998.         }

  999.         if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
  1000.             != 0)
  1001.         {
  1002.             continue;
  1003.         }

  1004.         if (tag != shm_zone[i].tag) {
  1005.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1006.                             "the shared memory zone \"%V\" is "
  1007.                             "already declared for a different use",
  1008.                             &shm_zone[i].shm.name);
  1009.             return NULL;
  1010.         }

  1011.         if (shm_zone[i].shm.size == 0) {
  1012.             shm_zone[i].shm.size = size;
  1013.         }

  1014.         if (size && size != shm_zone[i].shm.size) {
  1015.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1016.                             "the size %uz of shared memory zone \"%V\" "
  1017.                             "conflicts with already declared size %uz",
  1018.                             size, &shm_zone[i].shm.name, shm_zone[i].shm.size);
  1019.             return NULL;
  1020.         }

  1021.         return &shm_zone[i];
  1022.     }

  1023.     shm_zone = ngx_list_push(&cf->cycle->shared_memory);

  1024.     if (shm_zone == NULL) {
  1025.         return NULL;
  1026.     }

  1027.     shm_zone->data = NULL;
  1028.     shm_zone->shm.log = cf->cycle->log;
  1029.     shm_zone->shm.addr = NULL;
  1030.     shm_zone->shm.size = size;
  1031.     shm_zone->shm.name = *name;
  1032.     shm_zone->shm.exists = 0;
  1033.     shm_zone->init = NULL;
  1034.     shm_zone->tag = tag;
  1035.     shm_zone->noreuse = 0;

  1036.     return shm_zone;
  1037. }


  1038. static void
  1039. ngx_clean_old_cycles(ngx_event_t *ev)
  1040. {
  1041.     ngx_uint_t     i, n, found, live;
  1042.     ngx_log_t     *log;
  1043.     ngx_cycle_t  **cycle;

  1044.     log = ngx_cycle->log;
  1045.     ngx_temp_pool->log = log;

  1046.     ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");

  1047.     live = 0;

  1048.     cycle = ngx_old_cycles.elts;
  1049.     for (i = 0; i < ngx_old_cycles.nelts; i++) {

  1050.         if (cycle[i] == NULL) {
  1051.             continue;
  1052.         }

  1053.         found = 0;

  1054.         for (n = 0; n < cycle[i]->connection_n; n++) {
  1055.             if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
  1056.                 found = 1;

  1057.                 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%ui", n);

  1058.                 break;
  1059.             }
  1060.         }

  1061.         if (found) {
  1062.             live = 1;
  1063.             continue;
  1064.         }

  1065.         ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %ui", i);

  1066.         ngx_destroy_pool(cycle[i]->pool);
  1067.         cycle[i] = NULL;
  1068.     }

  1069.     ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %ui", live);

  1070.     if (live) {
  1071.         ngx_add_timer(ev, 30000);

  1072.     } else {
  1073.         ngx_destroy_pool(ngx_temp_pool);
  1074.         ngx_temp_pool = NULL;
  1075.         ngx_old_cycles.nelts = 0;
  1076.     }
  1077. }


  1078. void
  1079. ngx_set_shutdown_timer(ngx_cycle_t *cycle)
  1080. {
  1081.     ngx_core_conf_t  *ccf;

  1082.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

  1083.     if (ccf->shutdown_timeout) {
  1084.         ngx_shutdown_event.handler = ngx_shutdown_timer_handler;
  1085.         ngx_shutdown_event.data = cycle;
  1086.         ngx_shutdown_event.log = cycle->log;
  1087.         ngx_shutdown_event.cancelable = 1;

  1088.         ngx_add_timer(&ngx_shutdown_event, ccf->shutdown_timeout);
  1089.     }
  1090. }


  1091. static void
  1092. ngx_shutdown_timer_handler(ngx_event_t *ev)
  1093. {
  1094.     ngx_uint_t         i;
  1095.     ngx_cycle_t       *cycle;
  1096.     ngx_connection_t  *c;

  1097.     cycle = ev->data;

  1098.     c = cycle->connections;

  1099.     for (i = 0; i < cycle->connection_n; i++) {

  1100.         if (c[i].fd == (ngx_socket_t) -1
  1101.             || c[i].read == NULL
  1102.             || c[i].read->accept
  1103.             || c[i].read->channel
  1104.             || c[i].read->resolver)
  1105.         {
  1106.             continue;
  1107.         }

  1108.         ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
  1109.                        "*%uA shutdown timeout", c[i].number);

  1110.         c[i].close = 1;
  1111.         c[i].error = 1;

  1112.         c[i].read->handler(c[i].read);
  1113.     }
  1114. }