ngx_cycle_t结构体及ngx_cycle_init函数分析

一  ngx_cycle_t结构体 ngx_cycle_t对应于nginx的一次启动,配置重新加载,或者平滑升级。

struct ngx_cycle_s {void                  ****conf_ctx;//配置上下文ngx_pool_t               *pool;//内存池ngx_log_t                *log;//log对象ngx_log_t                 new_log;//在配置文件中定义的新error_log,将会被临时保存在new_log中,配置文件解析完成后,log重新指向new_logngx_uint_t                log_use_stderr;  /* unsigned  log_use_stderr:1; */ngx_connection_t        **files;//worker进程文件池ngx_connection_t         *free_connections;//当前空闲连接ngx_uint_t                free_connection_n;//当前空闲连接数ngx_module_t            **modules;//nginx的模块信息数组ngx_uint_t                modules_n;//模块数量ngx_uint_t                modules_used;    /* unsigned  modules_used:1; */ngx_queue_t               reusable_connections_queue;ngx_array_t               listening;//监听的socketngx_array_t               paths;//管理的路径ngx_array_t               config_dump;ngx_rbtree_t              config_dump_rbtree;ngx_rbtree_node_t         config_dump_sentinel;ngx_list_t                open_files;//nginx管理的文件ngx_list_t                shared_memory;//nginx管理的共享内存ngx_uint_t                connection_n;//worker进程允许的最大连接数ngx_uint_t                files_n;//worker进程允许打开的最大文件数ngx_connection_t         *connections;//worker进程连接池ngx_event_t              *read_events;//连接读事件ngx_event_t              *write_events;//连接写事件ngx_cycle_t              *old_cycle;//旧cycle,对应于配置重新加载前的cyclengx_str_t                 conf_file;//配置文件路径ngx_str_t                 conf_param;//全局参数(-g选项指定的)ngx_str_t                 conf_prefix;//配置文件路径前缀ngx_str_t                 prefix;//安装路径前缀(configure时指定或者-p选项指定)ngx_str_t                 lock_file;//锁文件路径ngx_str_t                 hostname;//主机名,gethostname获得
};


二 ngx_init_cycle函数解析 函数原型: ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) ngx_init_cycle在启动nginx,以及需要重新加载配置(配置加载或者平滑升级)的时候会被调用。 函数的输入参数old_cycle是一个ngx_cycle_t结构体指针,返回值也是一个ngx_cycle_t指针,从原型可以看出,该函数的基本思想是通过old_cycle所提供的一些信息,创建一个新的cycle。 初次启动时,传入的old_cycle指针指向的是main函数中定义的init_cycle,而在重新加载配置或者平滑升级时,传入的old_cycle则是旧master的cycle。 初次启动时对于ngx_init_cycle的调用的代码如下所示
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
init_cycle.log = log;
ngx_cycle = &init_cycle;init_cycle.pool = ngx_create_pool(1024, log);
...
if (ngx_process_options(&init_cycle) != NGX_OK) {return 1;
}
...
cycle = ngx_init_cycle(&init_cycle);

从上面的代码片段可以看出, 在调用ngx_init_cycle之前,nginx对init_cycle的操作并不多,除了对log,pool成员进行初始化和赋值外,就是调用了ngx_process_options函数。ngx_process_options函数主要是对init_cycle的prefix,conf_prefix,conf_file ,conf_param等与配置文件以及nginx前缀相关的变量进行赋值。
重新加载配置时对于ngx_init_cycle的调用如下所示:
if (ngx_reconfigure) {ngx_reconfigure = 0;...ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");cycle = ngx_init_cycle(cycle);if (cycle == NULL) {cycle = (ngx_cycle_t *) ngx_cycle;continue;}ngx_cycle = cycle;...
}
可以看出,重新加载配置时,将旧的cycle作为参数传递给ngx_init_cycle,如果返回NULL,表示reload失败,依旧使用旧的cycle,否则表示reload成功,将返回值作为新的cycle,并赋值给ngx_cycle。


下面是ngx_init_cycle的代码分析
ngx_cycle_t *
ngx_init_cycle(ngx_cycle_t *old_cycle)
{void                *rv;char               **senv;ngx_uint_t           i, n;ngx_log_t           *log;ngx_time_t          *tp;ngx_conf_t           conf;ngx_pool_t          *pool;ngx_cycle_t         *cycle, **old;ngx_shm_zone_t      *shm_zone, *oshm_zone;ngx_list_part_t     *part, *opart;ngx_open_file_t     *file;ngx_listening_t     *ls, *nls;ngx_core_conf_t     *ccf, *old_ccf;ngx_core_module_t   *module;char                 hostname[NGX_MAXHOSTNAMELEN];ngx_timezone_update();//时区/* force localtime update with a new timezone */tp = ngx_timeofday();tp->sec = 0;ngx_time_update();//先更新时间缓存//根据old_cycle对应成员变量的值,为cycle成员变量分配内存并进行初始化log = old_cycle->log;pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);if (pool == NULL) {return NULL;}pool->log = log;cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));if (cycle == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->pool = pool;cycle->log = log;cycle->old_cycle = old_cycle;cycle->conf_prefix.len = old_cycle->conf_prefix.len;cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);if (cycle->conf_prefix.data == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->prefix.len = old_cycle->prefix.len;cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);if (cycle->prefix.data == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->conf_file.len = old_cycle->conf_file.len;cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);if (cycle->conf_file.data == NULL) {ngx_destroy_pool(pool);return NULL;}ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,old_cycle->conf_file.len + 1);cycle->conf_param.len = old_cycle->conf_param.len;cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);if (cycle->conf_param.data == NULL) {ngx_destroy_pool(pool);return NULL;}/* ----------------------------------------------------* 分配路径,共享内存,文件等资源。* nginx对于初次启动和配置重新加载时对于资源的分配做了区分处理。* 如果是初次启动,使用默认参数,否则以旧cycle的配置为准*/n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;if (ngx_array_init(&cycle->paths, pool, n, sizeof(ngx_path_t *))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *));if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel,ngx_str_rbtree_insert_value);if (old_cycle->open_files.part.nelts) {n = old_cycle->open_files.part.nelts;for (part = old_cycle->open_files.part.next; part; part = part->next) {n += part->nelts;}} else {n = 20;}if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}if (old_cycle->shared_memory.part.nelts) {n = old_cycle->shared_memory.part.nelts;for (part = old_cycle->shared_memory.part.next; part; part = part->next){n += part->nelts;}} else {n = 1;}if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t))!= NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t));/*-----------------------------------------------------*/ngx_queue_init(&cycle->reusable_connections_queue);/* 为模块上下文分配内存 */cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));if (cycle->conf_ctx == NULL) {ngx_destroy_pool(pool);return NULL;}if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");ngx_destroy_pool(pool);return NULL;}/* on Linux gethostname() silently truncates name that does not fit */hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';cycle->hostname.len = ngx_strlen(hostname);cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);if (cycle->hostname.data == NULL) {ngx_destroy_pool(pool);return NULL;}ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);/* 将ngx_modules.c里面定义的ngx_modules数组拷贝到modules变量中 */if (ngx_cycle_modules(cycle) != NGX_OK) {ngx_destroy_pool(pool);return NULL;}/* 执行类型为NGX_CORE_MODULE的模块的create_conf回调,创建模块配置上下文 */for (i = 0; cycle->modules[i]; i++) {if (cycle->modules[i]->type != NGX_CORE_MODULE) {continue;}module = cycle->modules[i]->ctx;if (module->create_conf) {rv = module->create_conf(cycle);if (rv == NULL) {ngx_destroy_pool(pool);return NULL;}cycle->conf_ctx[cycle->modules[i]->index] = rv;}}senv = environ;/* 以下开始准备解析配置文件 */ngx_memzero(&conf, sizeof(ngx_conf_t));/* STUB: init array ? */conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));if (conf.args == NULL) {ngx_destroy_pool(pool);return NULL;}conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);if (conf.temp_pool == NULL) {ngx_destroy_pool(pool);return NULL;}/* 设置配置解析上下文环境 */conf.ctx = cycle->conf_ctx;conf.cycle = cycle;conf.pool = pool;conf.log = log;conf.module_type = NGX_CORE_MODULE;conf.cmd_type = NGX_MAIN_CONF;#if 0log->log_level = NGX_LOG_DEBUG_ALL;
#endif/* 先解析并执行命令行参数(-g选项)中定义的全局配置 */if (ngx_conf_param(&conf) != NGX_CONF_OK) {environ = senv;ngx_destroy_cycle_pools(&conf);return NULL;}/* 解析配置文件,执行指令(创建模块配置上下文,设置参数,执行模块回调) */if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {environ = senv;ngx_destroy_cycle_pools(&conf);return NULL;}if (ngx_test_config && !ngx_quiet_mode) {ngx_log_stderr(0, "the configuration file %s syntax is ok",cycle->conf_file.data);}/* 执行CORE类型模块的init_conf函数 */for (i = 0; cycle->modules[i]; i++) {if (cycle->modules[i]->type != NGX_CORE_MODULE) {continue;}module = cycle->modules[i]->ctx;if (module->init_conf) {if (module->init_conf(cycle,cycle->conf_ctx[cycle->modules[i]->index])== NGX_CONF_ERROR){environ = senv;ngx_destroy_cycle_pools(&conf);return NULL;}}}/* 如果是信号发送进程(以-s RELOAD|REOPEN|QUIT|STOP方式启动),到这里就可以直接返回了*/if (ngx_process == NGX_PROCESS_SIGNALLER) {return cycle;}ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);if (ngx_test_config) {/* 这里如果pid文件存在,就什么都不做,如果不存在,就创建一个空的pid文件 */if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {goto failed;}} else if (!ngx_is_init_cycle(old_cycle)) {/** we do not create the pid file in the first ngx_init_cycle() call* because we need to write the demonized process pid*//* * 如果不是初次启动检查新cycle与旧cycle的pid文件是否一样,如果不一样,就创建新cycle的* pid文件,将主进程pid写入文件中,并且删除旧cycle的pid文件 */old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,ngx_core_module);if (ccf->pid.len != old_ccf->pid.len|| ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0){/* new pid file name */if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {goto failed;}ngx_delete_pidfile(old_cycle);}}/* 测试锁文件是否能正常工作 */if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {goto failed;}/* 创建配置文件中配置的所有路径。统一处理的目的是方便重新加载配置 */if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {goto failed;}/* 检查是否已打开error log,如果没有,就按照默认配置创建并打开error log文件 */if (ngx_log_open_default(cycle)  NGX_OK) {goto failed;}/* open the new files *//* 打开配置文件中配置的所有文件 */part = &cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].name.len == 0) {continue;}file[i].fd = ngx_open_file(file[i].name.data,NGX_FILE_APPEND,NGX_FILE_CREATE_OR_OPEN,NGX_FILE_DEFAULT_ACCESS);ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,"log: %p %d \"%s\"",&file[i], file[i].fd, file[i].name.data);if (file[i].fd == NGX_INVALID_FILE) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_open_file_n " \"%s\" failed",file[i].name.data);goto failed;}#if !(NGX_WIN32)if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,"fcntl(FD_CLOEXEC) \"%s\" failed",file[i].name.data);goto failed;}
#endif}/* 用新的log替换old_cycle里的log */cycle->log = &cycle->new_log;pool->log = &cycle->new_log;/* create shared memory *//* 创建共享内存 */part = &cycle->shared_memory.part;shm_zone = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;shm_zone = part->elts;i = 0;}if (shm_zone[i].shm.size == 0) {ngx_log_error(NGX_LOG_EMERG, log, 0,"zero size shared memory zone \"%V\"",&shm_zone[i].shm.name);goto failed;}shm_zone[i].shm.log = cycle->log;opart = &old_cycle->shared_memory.part;oshm_zone = opart->elts;for (n = 0; /* void */ ; n++) {if (n >= opart->nelts) {if (opart->next == NULL) {break;}opart = opart->next;oshm_zone = opart->elts;n = 0;}if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {continue;}if (ngx_strncmp(shm_zone[i].shm.name.data,oshm_zone[n].shm.name.data,shm_zone[i].shm.name.len)!= 0){continue;}if (shm_zone[i].tag == oshm_zone[n].tag&& shm_zone[i].shm.size == oshm_zone[n].shm.size&& !shm_zone[i].noreuse){shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
#if (NGX_WIN32)shm_zone[i].shm.handle = oshm_zone[n].shm.handle;
#endifif (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)!= NGX_OK){goto failed;}goto shm_zone_found;}ngx_shm_free(&oshm_zone[n].shm);break;}if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {goto failed;}if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {goto failed;}if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {goto failed;}shm_zone_found:continue;}/* handle the listening sockets *//* * 处理监听的socket.* 如果不是初次启动,就从old_cycle的listening socket中获取相关信息,赋给* 新cycle的对应socket。如果是初次启动,就以默认方式配置socket */if (old_cycle->listening.nelts) {ls = old_cycle->listening.elts;for (i = 0; i < old_cycle->listening.nelts; i++) {ls[i].remain = 0;}nls = cycle->listening.elts;for (n = 0; n < cycle->listening.nelts; n++) {for (i = 0; i < old_cycle->listening.nelts; i++) {if (ls[i].ignore) {continue;}if (ls[i].remain) {continue;}if (ls[i].type != nls[n].type) {continue;}if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,ls[i].sockaddr, ls[i].socklen, 1)== NGX_OK){nls[n].fd = ls[i].fd;nls[n].previous = &ls[i];ls[i].remain = 1;if (ls[i].backlog != nls[n].backlog) {nls[n].listen = 1;}#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)/** FreeBSD, except the most recent versions,* could not remove accept filter*/nls[n].deferred_accept = ls[i].deferred_accept;if (ls[i].accept_filter && nls[n].accept_filter) {if (ngx_strcmp(ls[i].accept_filter,nls[n].accept_filter)!= 0){nls[n].delete_deferred = 1;nls[n].add_deferred = 1;}} else if (ls[i].accept_filter) {nls[n].delete_deferred = 1;} else if (nls[n].accept_filter) {nls[n].add_deferred = 1;}
#endif#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (ls[i].deferred_accept && !nls[n].deferred_accept) {nls[n].delete_deferred = 1;} else if (ls[i].deferred_accept != nls[n].deferred_accept){nls[n].add_deferred = 1;}
#endif#if (NGX_HAVE_REUSEPORT)if (nls[n].reuseport && !ls[i].reuseport) {nls[n].add_reuseport = 1;}
#endifbreak;}}if (nls[n].fd == (ngx_socket_t) -1) {nls[n].open = 1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)if (nls[n].accept_filter) {nls[n].add_deferred = 1;}
#endif
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (nls[n].deferred_accept) {nls[n].add_deferred = 1;}
#endif}}} else {ls = cycle->listening.elts;for (i = 0; i < cycle->listening.nelts; i++) {ls[i].open = 1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)if (ls[i].accept_filter) {ls[i].add_deferred = 1;}
#endif
#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)if (ls[i].deferred_accept) {ls[i].add_deferred = 1;}
#endif}}/* 打开监听的socket,配置socket的REUSEADDR|REUSEPORT|NONBLOCKING等选项,* 最后执行bind,listen等操作 */if (ngx_open_listening_sockets(cycle) != NGX_OK) {goto failed;}/* 配置socket的接收|发送缓存,是否长连接,以及其他内核相关参数 */if (!ngx_test_config) {ngx_configure_listening_sockets(cycle);}/* commit the new cycle configuration *//* 标准错误重定向 */if (!ngx_use_stderr) {(void) ngx_log_redirect_stderr(cycle);}pool->log = cycle->log;/* 依次执行所有模块init_module回调 */if (ngx_init_modules(cycle) != NGX_OK) {/* fatal */exit(1);}/* 到这里,新的cycle初始化已经完成。* 下面是对旧的cycle资源的回收和释放,* 包括释放共享内存,关闭旧的cycle中不需要保留的listening socket,* 关闭打开的文件描述符,释放内存池等 *//* close and delete stuff that lefts from an old cycle *//* free the unnecessary shared memory */opart = &old_cycle->shared_memory.part;oshm_zone = opart->elts;for (i = 0; /* void */ ; i++) {if (i >= opart->nelts) {if (opart->next == NULL) {goto old_shm_zone_done;}opart = opart->next;oshm_zone = opart->elts;i = 0;}part = &cycle->shared_memory.part;shm_zone = part->elts;for (n = 0; /* void */ ; n++) {if (n >= part->nelts) {if (part->next == NULL) {break;}part = part->next;shm_zone = part->elts;n = 0;}if (oshm_zone[i].shm.name.len == shm_zone[n].shm.name.len&& ngx_strncmp(oshm_zone[i].shm.name.data,shm_zone[n].shm.name.data,oshm_zone[i].shm.name.len)== 0){goto live_shm_zone;}}ngx_shm_free(&oshm_zone[i].shm);live_shm_zone:continue;}old_shm_zone_done:/* close the unnecessary listening sockets */ls = old_cycle->listening.elts;for (i = 0; i < old_cycle->listening.nelts; i++) {if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {continue;}if (ngx_close_socket(ls[i].fd) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,ngx_close_socket_n " listening socket on %V failed",&ls[i].addr_text);}#if (NGX_HAVE_UNIX_DOMAIN)if (ls[i].sockaddr->sa_family == AF_UNIX) {u_char  *name;name = ls[i].addr_text.data + sizeof("unix:") - 1;ngx_log_error(NGX_LOG_WARN, cycle->log, 0,"deleting socket %s", name);if (ngx_delete_file(name) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,ngx_delete_file_n " %s failed", name);}}#endif}/* close the unnecessary open files */part = &old_cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {continue;}if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_close_file_n " \"%s\" failed",file[i].name.data);}}ngx_destroy_pool(conf.temp_pool);if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {ngx_destroy_pool(old_cycle->pool);cycle->old_cycle = NULL;return cycle;}if (ngx_temp_pool == NULL) {ngx_temp_pool = ngx_create_pool(128, cycle->log);if (ngx_temp_pool == NULL) {ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,"could not create ngx_temp_pool");exit(1);}n = 10;if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n,sizeof(ngx_cycle_t *))!= NGX_OK){exit(1);}ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *));ngx_cleaner_event.handler = ngx_clean_old_cycles;ngx_cleaner_event.log = cycle->log;ngx_cleaner_event.data = &dumb;dumb.fd = (ngx_socket_t) -1;}ngx_temp_pool->log = cycle->log;old = ngx_array_push(&ngx_old_cycles);if (old == NULL) {exit(1);}*old = old_cycle;if (!ngx_cleaner_event.timer_set) {ngx_add_timer(&ngx_cleaner_event, 30000);ngx_cleaner_event.timer_set = 1;}return cycle;failed:/* 初始化新cycle失败,执行回滚 */if (!ngx_is_init_cycle(old_cycle)) {old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,ngx_core_module);if (old_ccf->environment) {environ = old_ccf->environment;}}/* rollback the new cycle configuration *//* 回滚主要是关闭新cycle中打开的socket,文件描述符,以及释放内存池 */part = &cycle->open_files.part;file = part->elts;for (i = 0; /* void */ ; i++) {if (i >= part->nelts) {if (part->next == NULL) {break;}part = part->next;file = part->elts;i = 0;}if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {continue;}if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,ngx_close_file_n " \"%s\" failed",file[i].name.data);}}if (ngx_test_config) {ngx_destroy_cycle_pools(&conf);return NULL;}ls = cycle->listening.elts;for (i = 0; i < cycle->listening.nelts; i++) {if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {continue;}if (ngx_close_socket(ls[i].fd) == -1) {ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,ngx_close_socket_n " %V failed",&ls[i].addr_text);}}ngx_destroy_cycle_pools(&conf);return NULL;
}



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部