这里继续上文,如果想看main,可以看上一文章:
nginx之main函数
这里单独介绍ngx_init_cycle函数:
该函数会负责初始化ngx_cycle_t的数据结构,解析配置文件,加载所有模块,代开
监听端口,初始化进程间通信的方式等工作。
也是nginx最重要的一个函数。
在nginx_cycle.c中
//这里传入的是main函数中的init_cycle,如果第一次启动那么init_cycle只有log,以及param等参数
main-->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];
//设置系统环境变量的putenv("TZ=UTC");
ngx_timezone_update();
/* force localtime update with a new timezone */
tp = ngx_timeofday();
tp->sec = 0;
ngx_time_update();
//这里的log还是main函数中临时log
log = old_cycle->log;
//创建内存池,此处创建大小为NGX_CYCLE_POOL_SIZE=16384B
pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
if (pool == NULL) {
return NULL;
}
pool->log = log;
//为ngx_cycle_s结构体分配空间,
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;
//配置文件前置路径参数只有在main函数中由,所以在这里通过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->error_log.len = old_cycle->error_log.len;
cycle->error_log.data = ngx_pnalloc(pool, old_cycle->error_log.len + 1);
if (cycle->error_log.data == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
ngx_cpystrn(cycle->error_log.data, old_cycle->error_log.data,
old_cycle->error_log.len + 1);
//配置文件也是从old_cycle中传入
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);
//这里将old param赋值为cycle,后面要用到
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;
}
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);
//为void****分配空间,ngx_max_module就是ngx_module_t *ngx_modules[] + 128
//ngx_preinit_modules()函数中确定
//ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES
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数组拷贝到ngx_cycle_s中来
//ngx_cycle_modules(ngx_cycle_t *cycle){
/*
* create a list of modules to be used for this cycle,
* copy static modules to it
*/
// cycle->modules = ngx_pcalloc(cycle->pool, (ngx_max_module + 1)
// * sizeof(ngx_module_t *));
// if (cycle->modules == NULL) {
// return NGX_ERROR;
// }
// ngx_memcpy(cycle->modules, ngx_modules,
// ngx_modules_n * sizeof(ngx_module_t *));
// cycle->modules_n = ngx_modules_n;
// return NGX_OK;
}
//根据上面可以看到仅仅做了一些拷贝
if (ngx_cycle_modules(cycle) != NGX_OK) {
ngx_destroy_pool(pool);
return NULL;
}
//在这里调用核心模块的create_conf函数,第一节知道,只有ngx_core_module中的ngx_core_module_ctx 才有create_conf,它主要创建了一个结构体ngx_core_conf_t,这个配置文件中存储nginx.conf中不属于任
//何{}block的参数
//其实其余的都是在 ngx_xxxx_block,中开始创建的,所以void**** 中的第一个元素就是ngx_core_conf_t
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;
}
//一般core在第一个所以index=0,void**** 中第一个存储的是ngx_core_conf_t
cycle->conf_ctx[cycle->modules[i]->index] = rv;
}
}
//environshi是个全局变量ngx_set_environment()中设置
senv = environ;
//这里为conf配空间,这里的conf就是上面核心模块在void**** 创建的第一个结构体
//注意这个conf是做什么用的?
//struct ngx_conf_s {
// char *name;
// ngx_array_t *args;
//
// ngx_cycle_t *cycle;
// ngx_pool_t *pool;
// ngx_pool_t *temp_pool;
// ngx_conf_file_t *conf_file;
// ngx_log_t *log;
//
// void *ctx;
// ngx_uint_t module_type;
// ngx_uint_t cmd_type;
// ngx_conf_handler_pt handler;
// void *handler_conf;
//};
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;
//设置这个参数后,解析的时候,直接洗nginx.conf全局的一些配置
// if (cmd->type & NGX_MAIN_CONF) {
// conf = &(((void **) cf->ctx)[cf->cycle->modules[i]->index]);
// }
conf.cmd_type = NGX_MAIN_CONF;
// ngx_conf_param(ngx_conf_t *cf)
//{
// char *rv;
// ngx_str_t *param;
// ngx_buf_t b;
// ngx_conf_file_t conf_file;
//这里的cycle就是我们在ngx_init_cycle()中创建的cycle结构体,conf_param就是main函数中获取到的
// param = &cf->cycle->conf_param;
// if (param->len == 0) {
// return NGX_CONF_OK;
// }
// ngx_memzero(&conf_file, sizeof(ngx_conf_file_t));
// ngx_memzero(&b, sizeof(ngx_buf_t));
//这里十八file_buffer的值设置为param,然后通过ngx_conf_parse来解析
// b.start = param->data;
// b.pos = param->data;
// b.last = param->data + param->len;
// b.end = b.last;
// b.temporary = 1;
// conf_file.file.fd = NGX_INVALID_FILE;
// conf_file.file.name.data = NULL;
// conf_file.line = 0;
// cf->conf_file = &conf_file;
// cf->conf_file->buffer = &b;
**********************这里已经开始解析了,但是解析的是param**********
// rv = ngx_conf_parse(cf, NULL);
**********************end**************************************
// cf->conf_file = NULL;
// return rv;
//}
if (ngx_conf_param(&conf) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf);
return NULL;
}
//这里传入了file路径,将会打开文件,并且将其放入
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf);
return NULL;
}
//这里就是开始解析配置文件了,注意传入的conf,其实在ngx_conf_param中就已经开始解析了,注意参数
//NGX_INVALID_FILE
//ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename){
// char *rv;
// ngx_fd_t fd;
// ngx_int_t rc;
// ngx_buf_t buf;
// ngx_conf_file_t *prev, conf_file;
// enum {
// parse_file = 0,
// parse_block,
// parse_param
// } type;
//#if (NGX_SUPPRESS_WARN)
// fd = NGX_INVALID_FILE;
// prev = NULL;
//#endif
//第一次传入的filename为null,所以直接执行 ,并且fd=NGX_INVALID_FILE,所以type = parse_param;
// if (filename) {
/* open configuration file */
// fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
// if (fd == NGX_INVALID_FILE) {
// ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
// ngx_open_file_n " \"%s\" failed",
// filename->data);
// return NGX_CONF_ERROR;
// }
// prev = cf->conf_file;
// cf->conf_file = &conf_file;
// if (ngx_fd_info(fd, &cf->conf_file->file.info) == NGX_FILE_ERROR) {
// ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
// ngx_fd_info_n " \"%s\" failed", filename->data);
// }
// cf->conf_file->buffer = &buf;
//
// buf.start = ngx_alloc(NGX_CONF_BUFFER, cf->log);
// if (buf.start == NULL) {
// goto failed;
// }
// buf.pos = buf.start;
// buf.last = buf.start;
// buf.end = buf.last + NGX_CONF_BUFFER;
// buf.temporary = 1;
// cf->conf_file->file.fd = fd;
// cf->conf_file->file.name.len = filename->len;
// cf->conf_file->file.name.data = filename->data;
// cf->conf_file->file.offset = 0;
// cf->conf_file->file.log = cf->log;
// cf->conf_file->line = 1;
// type = parse_file;
//
// if (ngx_dump_config
//#if (NGX_DEBUG)
// || 1
//#endif
// )
// {
// if (ngx_conf_add_dump(cf, filename) != NGX_OK) {
// goto failed;
// }
// } else {
// cf->conf_file->dump = NULL;
// }
// } else if (cf->conf_file->file.fd != NGX_INVALID_FILE) {
// type = parse_block;
// } else {
// type = parse_param;
// }
//ngx_conf_parse---->核心for循环
// for(;;){
// rc = ngx_conf_read_token(cf);
/*
* ngx_conf_read_token() may return
*
* NGX_ERROR there is error
* NGX_OK the token terminated by ";" was found
* NGX_CONF_BLOCK_START the token terminated by "{" was found
* NGX_CONF_BLOCK_DONE the "}" was found
* NGX_CONF_FILE_DONE the configuration file is done
*/
// if (rc == NGX_ERROR) {
// goto done;
// }
// if (rc == NGX_CONF_BLOCK_DONE) {
// if (type != parse_block) {
// ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unexpected \"}\"");
// goto failed;
// }
// goto done;
// }
// if (rc == NGX_CONF_FILE_DONE) {
// if (type == parse_block) {
// ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
// "unexpected end of file, expecting \"}\"");
// goto failed;
// }
// goto done;
// }
// if (rc == NGX_CONF_BLOCK_START) {
// if (type == parse_param) {
// ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
// "block directives are not supported "
// "in -g option");
// goto failed;
// }
// }
/* rc == NGX_OK || rc == NGX_CONF_BLOCK_START */
// if (cf->handler) {
/*
* the custom handler, i.e., that is used in the http's
* "types { ... }" directive
*/
// if (rc == NGX_CONF_BLOCK_START) {
// ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unexpected \"{\"");
// goto failed;
// }
// rv = (*cf->handler)(cf, NULL, cf->handler_conf);
// if (rv == NGX_CONF_OK) {
// continue;
// }
// if (rv == NGX_CONF_ERROR) {
// goto failed;
// }
// ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", rv);
// goto failed;
// }
// if (rc == NGX_ERROR) {
// goto failed;
// }
// }
//failed:
// rc = NGX_ERROR;
//done:
// if (filename) {
// if (cf->conf_file->buffer->start) {
// ngx_free(cf->conf_file->buffer->start);
// }
// if (ngx_close_file(fd) == NGX_FILE_ERROR) {
// ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
// ngx_close_file_n " %s failed",
// filename->data);
// rc = NGX_ERROR;
// }
// cf->conf_file = prev;
// }
// if (rc == NGX_ERROR) {
// return NGX_CONF_ERROR;
// }
return NGX_CONF_OK;
// //核心就在这里
// rc = ngx_conf_handler(cf, rc);
//}
//ngx_conf_handler核心代码如下:
//static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
//{
// char *rv;
// void *conf, **confp;
// ngx_uint_t i, found;
// ngx_str_t *name;
// ngx_command_t *cmd;
// name = cf->args->elts;
// found = 0;
//遍历所有commands
// name = cf->args->elts;这个name就是解析到的nginx.conf中的全局 http\rtmp\event
//关于nginx.conf可以参考:
//for (i = 0; cf->cycle->modules[i]; i++) {
// cmd = cf->cycle->modules[i]->commands;
// if (cmd == NULL) {
// continue;
// }
// for ( /* void */ ; cmd->name.len; cmd++) {//简单len比较
// if (name->len != cmd->name.len) {
// continue;
// }
// if (ngx_strcmp(name->data, cmd->name.data) != 0) {//len相等再对str比较
// continue;
// }
// found = 1;//说明字符串跟command中的相同
// //如果模块类型不符返回
// if (cf->cycle->modules[i]->type != NGX_CONF_MODULE
// && cf->cycle->modules[i]->type != cf->module_type)
// {
// continue;
// }
‘‘’
// cmd就是执行核心模块的command中的set,首先解析最前面的全局核心解析项,这个结构体在前面第一次分配了
//其次就是几个大类中的核心配置快了,主要指的是 http 、rtmp 、events
// 遇到这几个首先会分配结构体,然后再解析
// 的ngx_xxxx_block,在block中创建event,rtmp,http核心模块管理的所有子模块。
///
//struct ngx_command_s {
// ngx_str_t name;
// ngx_uint_t type;
// char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
// ngx_uint_t conf;
// ngx_uint_t offset;
// void *post;
};
///
// conf = NULL;
// if (cmd->type & NGX_DIRECT_CONF) {
// conf = ((void **) cf->ctx)[cf->cycle->modules[i]->index];
// } else if (cmd->type & NGX_MAIN_CONF) {
// conf = &(((void **) cf->ctx)[cf->cycle->modules[i]->index]);
// } else if (cf->ctx) {
// //大多数情况在这里cmd->conf,一般都为0
// confp = *(void **) ((char *) cf->ctx + cmd->conf);
// if (confp) {
// conf = confp[cf->cycle->modules[i]->ctx_index];
// }
// }
// rv = cmd->set(cf, cmd, conf);//关键在这里cf为核心cf,conf为新建的一个
//这里看set()函数,这里以 ngx_events_block,为例说明
//ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
//{
// void ***ctx;
// ngx_event_max_module 为ngx_module[]中NGX_EVENT_MODULE最大index+1
// ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
// ctx = ngx_pcalloc(cf->pool, sizeof(void *));
// 这里创建了ngx_event_max_module个void*指针
// *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
// *(void **) conf = ctx;//这里构建数据并传出去
// for (i = 0; cf->cycle->modules[i]; i++) {
// if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
// continue;
// }
// m = cf->cycle->modules[i]->ctx;
// 分别在每一个void* 地址中存放创建的事件结构体
// if (m->create_conf) {
// (*ctx)[cf->cycle->modules[i]->ctx_index] =
// m->create_conf(cf->cycle);
// if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
// return NGX_CONF_ERROR;
// }
// }
// }for
// 执行的是ngx_event_core_module中
//ngx_event_core_module_ctx->ngx_event_core_create_conf()创建的是一个ngx_event_conf_t结构体
//typedef struct {
// ngx_uint_t connections;
// ngx_uint_t use;
// ngx_flag_t multi_accept;
// ngx_flag_t accept_mutex;
// ngx_msec_t accept_mutex_delay;
// u_char *name;
//} ngx_event_conf_t;
// pcf = *cf;
// cf->ctx = ctx;
// cf->module_type = NGX_EVENT_MODULE;
// cf->cmd_type = NGX_EVENT_CONF;
// //这里是个递归解析动作动作,比如http里面的srv和loc,递归到最后就是解析具体字段了
// //总结一下:NGX_CORE_MODULE是核心模块,在解析的时候一般用来创建void****中的具体结构体
// //如果是http的block就是typedef struct {
// void **main_conf;
// void **srv_conf;
// void **loc_conf;
//} ngx_http_conf_ctx_t;
// rv = ngx_conf_parse(cf, NULL);
// *cf = pcf;
// if (rv != NGX_CONF_OK) {
// return rv;
// }
// for (i = 0; cf->cycle->modules[i]; i++) {
// if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
// continue;
// }
// m = cf->cycle->modules[i]->ctx;
// if (m->init_conf) {
// rv = m->init_conf(cf->cycle,
// (*ctx)[cf->cycle->modules[i]->ctx_index]);
// if (rv != NGX_CONF_OK) {
// return rv;
// }
// }
// }
//return NGX_CONF_OK;
//}
}
这个是事件模块在void*** 中的存储结构
//继续ngx_cycle_t主循环
if (ngx_test_config && !ngx_quiet_mode) {
ngx_log_stderr(0, "the configuration file %s syntax is ok",
cycle->conf_file.data);
}
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;
}
}
}
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) {
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
*/
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;
}
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
}
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;
#endif
if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
!= NGX_OK)
{
goto failed;
}
goto shm_zone_found;
}
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 */
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].inherited = ls[i].inherited;
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;
}
#endif
break;
}
}
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
}
}
if (ngx_open_listening_sockets(cycle) != NGX_OK) {
goto failed;
}
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;
if (ngx_init_modules(cycle) != NGX_OK) {
/* fatal */
exit(1);
}
/* 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) {
continue;
}
if (ngx_strncmp(oshm_zone[i].shm.name.data,
shm_zone[n].shm.name.data,
oshm_zone[i].shm.name.len)
!= 0)
{
continue;
}
if (oshm_zone[i].tag == shm_zone[n].tag
&& oshm_zone[i].shm.size == shm_zone[n].shm.size
&& !oshm_zone[i].noreuse)
{
goto live_shm_zone;
}
break;
}
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:
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 */
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);
}
}
/* free the newly created 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.addr == NULL) {
continue;
}
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)
{
goto old_shm_zone_found;
}
break;
}
ngx_shm_free(&shm_zone[i].shm);
old_shm_zone_found:
continue;
}
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;
}