您的位置:首页 > 运维架构 > Nginx

12.Nginx启动流程之ngx_init_cycle

2017-10-31 10:26 393 查看
Nginx的启动流程在ngx_add_inherited_sockets之后,会调用ngx_init_cycle来创建cycle,从下面的代码分析,我们可以看出,在ngx_init_cycle中,Nginx主要进行了以下工作:

1.根据旧cycle来创建新cycle,根据旧cycle的经验,可以推测出创建新cycle所需要的内存空间

2.对新cycle进行初始化工作,包括打开文件、打开监听套接字等

3.解析配置文件

4.模块初始化

5.创建pidfile文件

6.出错回滚及关闭不需要的文件描述符和监听套接字

/* core/ngx_core.h */

typedef struct ngx_cycle_s ngx_cycle_t;

/* core/ngx_cycle.h */

struct ngx_cycle_s { // ngx_cycle_t结构体定义
void ****conf_ctx; // 模块配置上下文数组
ngx_pool_t *pool; // 使用的内存池指针

ngx_log_t *log; // 使用的日志指针
ngx_log_t *new_log; // 新的日志指针

ngx_array_t listening; // 监听套接字数组
ngx_array_t pathes; // 路径数组
ngx_list_t open_files; // 打开文件链表

ngx_uint_t connection_n; // 最大连接数
ngx_connection_t *connections;
ngx_event_t *read_events;
ngx_event_t *write_events;

ngx_cycle_t *old_cycle; // 旧cycle

ngx_str_t conf_file; // 配置文件
ngx_str_t root; // 工作目录
};

/* core/ngx_connection.c */

/* 为cycle打开监听套接字
param cycle: ngx_cycle_t结构体指针
*/
ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle)
{
ngx_uint_t tries, failed, reuseaddr, i;
ngx_err_t err;
ngx_log_t *log;
ngx_socket_t s;
ngx_listening_t *ls;

reuseaddr = 1;
#if (NGX_SUPPRESS_WARN)
failed = 0;
#endif

log = cycle->log;

// tries用于指定重试次数, 这里为5次
for (tries = /* STUB */ 5; tries; tries--) {
failed = 0;

ls = cycle->listening.elts;
// 遍历cycle的监听套接字数组
for (i = 0; i < cycle->listening.nelts; i++) {

if (ls[i].ignore) {
// 如果监听套接字的ignore成员不为0, 那么就直接忽略
continue;
}

if (ls[i].fd != -1) {
// 如果监听套接字的fd不为-1, 那么说明已经打开
continue;
}

if (ls[i].inherited) {
// 如果监听套接字的inherited不为0, 那么说明是继承而来的, 直接跳过
continue;
}

// ngx_socket宏就是调用socket库函数来创建套接字
s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol,
ls[i].flags);

if (s == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_socket_n " %s failed", ls[i].addr_text.data);
return NGX_ERROR;
}

#if (WIN32)
/*
* Winsock assignes a socket number divisible by 4
* so to find a connection we divide a socket number by 4.
*/

if (s % 4) {
ngx_log_error(NGX_LOG_EMERG, ls->log, 0,
ngx_socket_n " created socket %d", s);
return NGX_ERROR;
}
#endif
// 设置套接字选项SO_REUSEADDR, 即允许端口重用
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(const void *) &reuseaddr, sizeof(int)) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
"setsockopt(SO_REUSEADDR) %s failed",
ls[i].addr_text.data);
return NGX_ERROR;
}

if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) {
// 如果不使用异步IO

// 使用fcntl或者ioctl设置套接字为非阻塞
if (ngx_nonblocking(s) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_nonblocking_n " %s failed",
ls[i].addr_text.data);
return NGX_ERROR;
}
}

#if 0
if (ls[i].nonblocking) {
if (ngx_nonblocking(s) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_nonblocking_n " %s failed",
ls[i].addr_text.data);
return NGX_ERROR;
}
}
#endif
// 绑定套接字到监听套接字的sockaddr成员所指地址结构
if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
err = ngx_socket_errno;
ngx_log_error(NGX_LOG_EMERG, log, err,

1060d
"bind() to %s failed", ls[i].addr_text.data);
// 绑定失败的错误不是NGX_EADDRINUSE即EADDRINUSE时, 返回NGX_ERROR
if (err != NGX_EADDRINUSE)
return NGX_ERROR;

// 绑定失败的错误是NGX_EADDRINUSE即EADDRINUSE时, 关闭套接字
if (ngx_close_socket(s) == -1)
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);

failed = 1;
continue;
}

// 调用listen使套接字转变为一个被动套接字, backlog由ls[i]的backlog成员指定
if (listen(s, ls[i].backlog) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
"listen() to %s failed", ls[i].addr_text.data);
return NGX_ERROR;
}

// 置ls[i]的fd成员为刚创建的套接字描述符
ls[i].fd = s;
}

if (!failed)
// 如果上述步骤都没有出错, 那么退出重试循环
break;

ngx_log_error(NGX_LOG_NOTICE, log, 0,
"try again to bind() after 500ms");
// 等待500ms后进行下一次重试
ngx_msleep(500);
}

if (failed) {
// 如果重试次数耗尽, 但是依旧出错, 那么返回NGX_ERROR
ngx_log_error(NGX_LOG_EMERG, log, 0, "still can not bind()");
return NGX_ERROR;
}

return NGX_OK;
}

/* core/ngx_cycle.c */

volatile ngx_cycle_t *ngx_cycle;
ngx_array_t ngx_old_cycles;
static ngx_pool_t *ngx_temp_pool;
static ngx_event_t ngx_cleaner_event;

#ifdef NGX_ERROR_LOG_PATH
static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
#else
static ngx_str_t error_log = ngx_null_string;
#endif

/* 创建pidfile文件
param cycle : 新cycle指针
old_cycle: 旧cycle指针
return : NGX_OK/NGX_ERROR
*/
ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle)
{
ngx_uint_t trunc;
size_t len;
// NGX_INT64_LEN宏表示64位有符号整数的十进制字符串形式的最大长度, 值为20
u_char *name, pid[NGX_INT64_LEN + 1];
ngx_file_t file;
ngx_core_conf_t *ccf, *old_ccf;

if (!ngx_test_config && old_cycle && old_cycle->conf_ctx == NULL) {
return NGX_OK;
}

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

if (!ngx_test_config && old_cycle) {
old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
ngx_core_module);
// 判断旧cycle和新cycle的pidfile文件名是否一样, 如果一样, 则说明可以沿用旧cycle下的pidfile文件
if (ccf->pid.len == old_ccf->pid.len
&& ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0)
{
return NGX_OK;
}
}

// 使用pid无符号字符数组来存放当前进程PID的十进制字符串形式
len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid);

// 初始化ngx_file_t结构体类型的file变量为0
ngx_memzero(&file, sizeof(ngx_file_t));
// 如果当前进程是子进程且ngx_inherited为1, 那么pidfile文件名为newpid, 反之则为pid
file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid;
file.log = cycle->log;
// 如果只是测试配置文件, 那么不截断文件, 反之则截断
trunc = ngx_test_config ? 0: NGX_FILE_TRUNCATE;

// ngx_open_file宏实质就是调用open来打开和创建文件
// 这里就是打开pidfile文件
file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
NGX_FILE_CREATE_OR_OPEN|trunc);

if (file.fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", file.name.data);
return NGX_ERROR;
}

if (!ngx_test_config) {
// 将pid无符号字符数组存放的数据写入pidfile文件
if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
return NGX_ERROR;
}
}
// 关闭打开的pidfile文件
if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", file.name.data);
}

// 删除旧cycle的pidfile文件
ngx_delete_pidfile(old_cycle);

return NGX_OK;
}

/* 根据旧cycle创建新cycle
*/
ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)
{
void *rv;
ngx_uint_t i, n, failed;
ngx_log_t *log;
ngx_conf_t conf;
ngx_pool_t *pool;
ngx_cycle_t *cycle, **old;
ngx_socket_t fd;
ngx_list_part_t *part;
ngx_open_file_t *file;
ngx_listening_t *ls, *nls;
ngx_core_module_t *module;

log = old_cycle->log;

// 创建一个大小为16KB的内存池pool
if (!(pool = ngx_create_pool(16 * 1024, log))) {
return NULL;
}
pool->log = log;

// 从内存池pool中申请一块ngx_cycle_t结构体大小的内存空间
// 用于存放新的cycle
if (!(cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)))) {
ngx_destroy_pool(pool);
return NULL;
}
cycle->pool = pool; // 设置使用的内存池
cycle->log = log; // 设置使用的日志句柄
cycle->old_cycle = old_cycle; // 设置旧cycle
cycle->conf_file = old_cycle->conf_file; // 设置配置文件
cycle->root.len = sizeof(NGX_PREFIX) - 1; // 设置工作目录字符串长度
cycle->root.data = (u_char *) NGX_PREFIX; // 设置工作目录字符串数据

// 获取旧cycle的路径数组的元素个数n
n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
// 从内存池pool申请n个ngx_path_t结构体大小的内存空间
if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) {
ngx_destroy_pool(pool);
return NULL;
}
// 设置路径数组的当前元素个数为0
cycle->pathes.nelts = 0;
// 设置路径数组的元素大小为ngx_path_t结构体大小
cycle->pathes.size = sizeof(ngx_path_t *);
// 设置路径数组的初始容量为n
cycle->pathes.nalloc = n;
// 设置路径数组使用的内存池为pool
cycle->pathes.pool = pool;

if (old_cycle->open_files.part.nelts) {
// 如果旧cycle的打开文件链表的头节点的数据区的已有元素个数不为0, 说明链表不为空

// 使用n统计旧cycle的打开文件链表各节点数据区的已有元素个数之和
n = old_cycle->open_files.part.nelts;
for (part = old_cycle->open_files.part.next; part; part = part->next) {
n += part->nelts;
}

} else {
// 如果旧cycle的打开文件链表为空, 那么置n为20
n = 20;
}

// 初始化新cycle的打开文件链表: 使用的内存池为pool, 头节点的数据区可以存放n个ngx_open_file_t结构体
if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
== NGX_ERROR)
{
ngx_destroy_pool(pool);
return NULL;
}

// 使用旧cycle创建一个日志
if (!(cycle->new_log = ngx_log_create_errlog(cycle, NULL))) {
ngx_destroy_pool(pool);
return NULL;
}

// 设置新cycle的新日志的文件名
cycle->new_log->file->name = error_log;

// 获取旧cycle的监听套接字数组的已有元素个数n, 默认为10
n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
// 从内存池pool申请n个ngx_listening_t结构体大小的内存空间
cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
if (cycle->listening.elts == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
// 设置监听套接字数组的已有元素个数为0
cycle->listening.nelts = 0;
// 设置监听套接字数组的元素大小为ngx_listening_t结构体大小
cycle->listening.size = sizeof(ngx_listening_t);
// 设置监听套接字数组的初始容量为n
cycle->listening.nalloc = n;
// 设置监听套接字数组使用的内存池为pool
cycle->listening.pool = pool;

// 全局变量ngx_max_module用于统计Nginx的模块数量
// 从内存池pool申请ngx_max_module个指针大小的内存空间
cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
if (cycle->conf_ctx == NULL) {
ngx_destroy_pool(pool);
return NULL;
}

// 全局变量ngx_modules数组用于存放Nginx的所有模块结构体
// 这里对ngx_modules数组进行遍历
for (i = 0; ngx_modules[i]; i++) {
// 只对NGX_CORE_MODULE类型的模块进行处理
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
continue;
}

module = ngx_modules[i]->ctx;

if (module->create_conf) {
// 如果模块的create_conf函数指针不为空, 那么调用create_conf来创建模块的配置信息
rv = module->create_conf(cycle);
if (rv == NGX_CONF_ERROR) {
// 一旦某个模块的create_conf出错, 立刻返回
ngx_destroy_pool(pool);
return NULL;
}
// 在新cycle的模块配置上下文数组的相应位置记录create_conf的返回值
cycle->conf_ctx[ngx_modules[i]->index] = rv;
}
}

// 初始化ngx_conf_t结构体类型的conf变量为0
ngx_memzero(&conf, sizeof(ngx_conf_t));
// 从内存池pool申请一块10个ngx_str_t结构体大小的内存空间
conf.args = ngx_create_array(pool, 10, sizeof(ngx_str_t));
if (conf.args == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
// 设置conf的ctx成员为新cycle的模块配置上下文数组首地址
conf.ctx = cycle->conf_ctx;
// 设置conf的cycle成员为新cycle
conf.cycle = cycle;
// 设置conf使用的内存池为pool
conf.pool = pool;
// 设置conf使用的日志为log
conf.log = log;
// 设置conf的module_type成员为NGX_CORE_MODULE
conf.module_type = NGX_CORE_MODULE;
// 设置conf的cmd_type成员为NGX_MAIN_CONF
conf.cmd_type = NGX_MAIN_CONF;

// 进行配置文件解析
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
ngx_destroy_pool(pool);
return NULL;
}

if (ngx_test_config) {
ngx_log_error(NGX_LOG_INFO, log, 0,
"the configuration file %s syntax is ok",
cycle->conf_file.data);
}

for (i = 0; ngx_modules[i]; i++) {
// 只对NGX_CORE_MODULE类型的模块进行处理
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
continue;
}

module = ngx_modules[i]->ctx;

if (module->init_conf) {
// 如果模块的init_conf函数指针不为空, 那么调用init_conf来初始化模块的配置信息
if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
== NGX_CONF_ERROR)
{
ngx_destroy_pool(pool);
return NULL;
}
}
}

failed = 0;

#if !(WIN32)
// 创建pidfile文件
if (ngx_create_pidfile(cycle, old_cycle) == NGX_ERROR) {
failed = 1;
}
#endif

if (!failed) {
// 如果创建pidfile文件没有失败

// 以下是遍历新cycle的打开文件链表
part = &cycle->open_files.part;
file = part->elts;

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

if (i >= part->nelts) {
// 如果遍历到当前节点的数据区的结尾
if (part->next == NULL) {
// 如果当前节点是链表的尾节点, 那么遍历结束
break;
}
// part指向下一个节点, file指向下一个节点的数据区首地址, i重置为0,
// 也就是遍历下一个节点的数据区
part = part->next;
file = part->elts;
i = 0;
}

if (file[i].name.data == NULL) {
// 如果file[i]的name成员的数据为空, 也就是没有指明文件名, 那么跳过
continue;
}
// 打开file[i]的name成员的数据所指向的文件名
file[i].fd = ngx_open_file(file[i].name.data,
NGX_FILE_RDWR,
NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);

#if 0
log->log_level = NGX_LOG_DEBUG_ALL;
#endif
ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
"log: %0X %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);
failed = 1;
break;
}

#if (WIN32)
if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_file_append_mode_n " \"%s\" failed",
file[i].name.data);
failed = 1;
break;
}
#else
// 调用fcntl对打开的文件描述符设置FD_CLOEXEC,
// 也就是在使用exec系列函数后, 此描述符将被关闭
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);
failed = 1;
break;
}
#endif
}
}
// 设置新cycle的log成员为其new_log成员
cycle->log = cycle->new_log;
// 修改内存池pool使用的log为新cycle的new_log
pool->log = cycle->new_log;

if (cycle->log->log_level == 0) {
// 如果新cycle的log的日志级别为0即NGX_LOG_STDERR, 那么修改日志级别为NGX_LOG_ERR
cycle->log->log_level = NGX_LOG_ERR;
}

if (!failed) {
// 如果前面的步骤没有失败

if (old_cycle->listening.nelts) {
// 如果旧cycle的监听套接字数组不为空

ls = old_cycle->listening.elts;
for (i = 0; i < old_cycle->listening.nelts; i++) {
// 遍历旧cycle的监听套接字数组, 设置每个监听套接字的remain成员为0
ls[i].remain = 0;
}

nls = cycle->listening.elts;
// 对新cycle的监听套接字数组进行遍历, 上面设置该数组为空的, 按道理这里应该不会执行循环体
for (n = 0; n < cycle->listening.nelts; n++) {
for (i = 0; i < old_cycle->listening.nelts; i++) {
// 遍历旧cycle的监听套接字数组
if (ls[i].ignore) {
continue;
}

// 找到与nls
的sockaddr成员所指套接字地址相同的ls[i]
if (ngx_memcmp(nls
.sockaddr,
ls[i].sockaddr, ls[i].socklen) == 0)
{
fd = ls[i].fd;
#if (WIN32)
/*
* Winsock assignes a socket number divisible by 4 so
* to find a connection we divide a socket number by 4.
*/

fd /= 4;
#endif
if (fd >= (ngx_socket_t) cycle->connection_n) {
// 如果fd达到新cycle的最大连接数, 那么退出当前循环
ngx_log_error(NGX_LOG_EMERG, log, 0,
"%d connections is not enough to hold "
"an open listening socket on %s, "
"required at least %d connections",
cycle->connection_n,
ls[i].addr_text.data, fd);
failed = 1;
break;
}
// ls[i]的fd成员赋值给nls
的fd成员
nls
.fd = ls[i].fd;
nls[i].remain = 1;
ls[i].remain = 1;
break;
}
}

if (nls
.fd == -1) {
nls
.new = 1;
}
}

} else {
// 如果旧cycle的监听套接字数组为空

ls = cycle->listening.elts;
// 置新cycle的监听套接字数组的每个元素的new成员为1
for (i = 0; i < cycle->listening.nelts; i++) {
ls[i].new = 1;
}
}

if (!ngx_test_config && !failed) {
// 如果不是在测试配置文件并且上述步骤没有失败

// 为cycle打开监听套接字
if (ngx_open_listening_sockets(cycle) == NGX_ERROR) {
failed = 1;
}
}
}

if (failed) {
// 如果上述步骤出错, 进行回滚操作

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

// 遍历新cycle的打开文件链表
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_fileno)
{
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) {
// 如果是在测试配置文件, 那么销毁内存池pool后就可以返回了
// 因为在测试配置文件时, 是不需要执行打开监听套接字的操作的
ngx_destroy_pool(pool);
return NULL;
}

ls = cycle->listening.elts;
// 遍历cycle的监听套接字数组
for (i = 0; i < cycle->listening.nelts; i++) {
if (ls[i].fd == -1 || !ls[i].new) {
continue;
}
// 关闭监听套接字
if (ngx_close_socket(ls[i].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
}
}

// 销毁内存池pool并返回
ngx_destroy_pool(pool);
return NULL;
}

#if !(WIN32)

if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
// 不是在测试配置文件且新cycle的日志文件描述符不为标准错误文件描述符
ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
"dup2: %0X %d \"%s\"",
cycle->log->file,
cycle->log->file->fd, cycle->log->file->name.data);
// 重定向标准错误到新cycle的日志文件
if (dup2(cycle->log->file->fd, STDERR_FILENO) == NGX_ERROR) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
"dup2(STDERR) failed");
exit(1);
}
}

#endif

pool->log = cycle->log;

for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_module) {
// 如果模块的init_module函数指针不为空, 那么调用init_module来进行模块初始化工作
if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) {
exit(1);
}
}
}

ls = old_cycle->listening.elts;
// 遍历旧cycle的监听套接字数组
for (i = 0; i < old_cycle->listening.nelts; i++) {
if (ls[i].remain) {
// 如果remain成员不为0, 即表示保留, 那么跳过
continue;
}
// 关闭监听套接字
if (ngx_close_socket(ls[i].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
}
}

part = &old_cycle->open_files.part;
file = part->elts;
// 遍历新cycle的打开文件链表
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_fileno) {
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 (old_cycle->connections == NULL) {
ngx_destroy_pool(old_cycle->pool);
return cycle;
}

if (ngx_process == NGX_PROCESS_MASTER) {
ngx_destroy_pool(old_cycle->pool);
return cycle;
}

if (ngx_temp_pool == NULL) {
// 如果ngx_temp_pool指针为空

// 创建大小为128字节的内存池作为ngx_temp_pool
ngx_temp_pool = ngx_create_pool(128, cycle->log);
if (ngx_temp_pool == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"can not create ngx_temp_pool");
exit(1);
}

n = 10;
// 从内存池ngx_temp_pool申请10个指针大小的内存空间给ngx_old_cycles数组
ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
n * sizeof(ngx_cycle_t *));
if (ngx_old_cycles.elts == NULL) {
exit(1);
}
// ngx_old_cycles数组的初始元素为0个
ngx_old_cycles.nelts = 0;
// ngx_old_cycles数组的元素大小即为指针大小
ngx_old_cycles.size = sizeof(ngx_cycle_t *);
// ngx_old_cycles数组的初始容量为10
ngx_old_cycles.nalloc = n;
// ngx_old_cycles数组使用的内存池为ngx_temp_pool
ngx_old_cycles.pool = ngx_temp_pool;

ngx_cleaner_event.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;

// 从ngx_old_cycles数组分配一个元素的内存
old = ngx_push_array(&ngx_old_cycles);
if (old == NULL) {
exit(1);
}
// 将旧cycle存放进ngx_old_cycles数组
*old = old_cycle;

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

// 返回新cycle
return cycle;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: