1. ngx_listening_t 結構體
ngx_cycle_t 對象中有一個動態數組成員叫做 listening,它的每個數組元素都是 ngx_listening_t 結構體,而每個 ngx_listening_t 結構體又代表着 Nginx 服務器監聽的一個端口。
typedef struct ngx_listening_s ngx_listening_t;
struct ngx_listening_s {
// socket 套接字句柄
ngx_socket_t fd;
// 監聽 sockaddr 地址
struct sockaddr *sockaddr;
// sockaddr 地址長度
socklen_t socklen; /* size of sockaddr */
// 存儲 IP 地址的字符串 addr_text 最大長度,即它指定了 addr_text 所分配的內存大小
size_t addr_text_max_len;
// 以字符串形式存儲IP地址
ngx_str_t addr_text;
// 套接字類型
int type;
// TCP 實現監聽時的 backlog 隊列,它表示允許正在通過三次握手建立 TCP
// 連接但沒有任何進程開始處理的連接最大個數
int backlog;
// 內核中對於這個套接字的接收緩存區大小
int rcvbuf;
// 內核中對於這個套接字的發送緩沖區大小
int sndbuf;
#if (NGX_HAVE_KEEPALIVE_TUNABLE)
int keepidle;
int keepintvl;
int keepcnt;
#endif
// 當新的 TCP 連接建立成功后調用的回調處理函數
/* handler of accepted connection */
ngx_connection_handler_pt handler;
// 實際上框架並不使用 servers 指針,它更多的是作為一個保留指針,目前
// 主要用於HTTP或mail等模塊,用於保存當前監聽端口對應着的所有主機名
void *servers; /* array of ngx_http_in_addr_t, for example */
ngx_log_t log;
ngx_log_t *logp;
// 如果為新的 TCP 連接創建內存池,則內存池的大小為 pool_size
size_t pool_size;
/* should be here because of the AcceptEx() preread */
size_t post_accept_buffer_size;
// TCP_DEFER_ACCEPT選項將在建立TCP連接成功且接收到用戶的請求數據后,才向對監聽套接字
// 感興趣的進程發送事件通知,而連接建立成功后,如果post_accept_timeout秒后仍然
// 沒有收到用戶數據,則內核直接丟棄連接
/* should be here because of the deferred accept */
ngx_msec_t post_accept_timeout;
// 前一個ngx_listening_t結構,多個ngx_listening_t結構體之間由previous指針組成單鏈表
ngx_listening_t *previous;
// 當前監聽句柄對應着的ngx_connection_t結構體
ngx_connection_t *connection;
ngx_uint_t worker;
// 標志位,為1則表示當前監聽句柄有效,且執行ngx_init_cycle時不關閉監聽端口,
// 為0時則正常關閉。該標志位框架代碼會自動設置
unsigned open:1;
// 標志位,為1表示使用已有的ngx_cycle_t來初始化新的ngx_cycle_t結構體時,不關閉
// 原先打開的監聽端口,這對運行中升級程序很有用,remain為0時,表示正常關閉
// 曾經打開的監聽端口。該標志位框架代碼會自動設置
unsigned remain:1;
// 標志位,為1時表示跳過設置當前ngx_listening_t結構體中的套接字,為0時正常
// 初始化套接字。該標志位框架代碼會自動設置
unsigned ignore:1;
// 表示是否已經綁定。
unsigned bound:1; /* already bound */
// 表示當前監聽套接字是否來自前一個進程(如升級Nginx),如果為1,則表示來自前
// 一個進程。一般會保留之前已經設置好的套接字,不做改變
unsigned inherited:1; /* inherited from previous process */
unsigned nonblocking_accept:1;
// 標志位,為1時表示當前結構體對應的套接字已經監聽
unsigned listen:1;
// 表示套接字是否是非阻塞的
unsigned nonblocking:1;
unsigned shared:1; /* shared between threads or processes */
// 標志位,為1時表示Nginx會將網絡地址轉變為字符串形式的地址
unsigned addr_ntop:1;
unsigned wildcard:1;
#if (NGX_HAVE_INET6)
unsigned ipv6only:1;
#endif
unsigned reuseport:1;
unsigned add_reuseport:1;
unsigned keepalive:2;
unsigned deferred_accept:1;
unsigned delete_deferred:1;
unsigned add_deferred:1;
#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
char *accept_filter;
#endif
#if (NGX_HAVE_SETFIB)
int setfib;
#endif
#if (NGX_HAVE_TCP_FASTOPEN)
int fastopen;
#endif
};
2. ngx_cycle_t 結構體
typedef struct ngx_cycle_s ngx_cycle_t;
struct ngx_cycle_s {
/*
* 保存着所有模塊存儲配置項的結構體的指針,它首先是一個數組,每個數組
* 成員又是一個指針,這個指針指向另一個存儲着指針的數組
*/
void ****conf_ctx;
/*
* 用於該 ngx_cycle_t 的內存池
*/
ngx_pool_t *pool;
/*
* 日志模塊中提供了生成基本ngx_lot_t日志對象的功能,這里的log實際上是在還沒有執行
* ngx_init_cycle 方法前,也就是還沒有解析配置前,如果有信息需要輸出到日志,就會
* 暫時使用log對象,它會輸出到屏幕。在ngx_init_cycle方法執行后,將會根據nginx.conf
* 配置文件中的配置項,構造出正確的日志文件,此時會對log重新賦值.
*/
ngx_log_t *log;
/*
* 由 nginx.conf 配置文件讀取到日志文件路徑后,將開始初始化 error_log 日志文件,
* 由於 log 對象還在用於輸出日志到屏幕,這時會用new_log對象暫時性地替代log日志,
* 待初始化完成后,會用 new_log 的地址覆蓋上面的log指針
*/
ngx_log_t new_log;
ngx_uint_t log_use_stderr; /* unsigned log_use_stderr:1; */
/*
* 對於 poll、rtsig 這樣的事件模塊,會以有效文件句柄數來預先建立這些 ngx_connection_t
* 結構體,以加速事件的收集、分發。這時 files 就會保存所有 ngx_connection_t 的指針
* 組成的數組,files_n 就是指針的總數,而文件句柄的值用來訪問 files 數組成員
*/
ngx_connection_t **files;
/*
* 可用連接池
*/
ngx_connection_t *free_connections;
/*
* 可用連接池中連接的總數
*/
ngx_uint_t free_connection_n;
/*
* 保存着當前 Nginx 所編譯進來的所有模塊 ngx_module_t 結構體的指針
* 它是一個數組,每個數組元素是指向 ngx_module_t 的指針
*/
ngx_module_t **modules;
/* modules 數組中元素的個數 */
ngx_uint_t modules_n;
ngx_uint_t modules_used; /* unsigned modules_used:1; */
ngx_queue_t reusable_connections_queue;
ngx_uint_t reusable_connections_n;
/*
* 動態數組,每個數組元素存儲着 ngx_listening_t 成員,表示監聽端口及相關的參數
*/
ngx_array_t listening;
/*
* 動態數組容器,它保存着 Nginx 所要操作的目錄。如果有目錄不存在,則會試圖創建,
* 而創建目錄失敗將會導致 Nginx 啟動失敗。例如,上傳文件的臨時目錄也在 pathes
* 中,如果沒有權限創建,則會導致 Nginx 無法啟動.
*/
ngx_array_t paths;
ngx_array_t config_dump;
ngx_rbtree_t config_dump_rbtree;
ngx_rbtree_node_t config_dump_sentinel;
/*
* 單鏈表容器,元素類型是 ngx_open_file_t 結構體,它表示 Nginx 已經打開的所有
* 文件。事實上,Nginx 框架不會向 open_files 鏈表中添加文件,而是由對此感興趣
* 的模塊向其中添加文件路徑名,Nginx 框架會在 ngx_init_cycle 方法中打開這些
* 文件.
*/
ngx_list_t open_files;
/*
* 單鏈表容器,元素類型是 ngx_shm_zone_t 結構體,每個元素表示一塊共享內存
*/
ngx_list_t shared_memory;
/*
* 當前進程中所有連接對象的總數
*/
ngx_uint_t connection_n;
ngx_uint_t files_n;
/*
* 指向當前進程中的所有連接對象
*/
ngx_connection_t *connections;
/*
* 指向當前進程中的所有讀事件對象,connection_n 同時表示所有讀事件的總數
*/
ngx_event_t *read_events;
/*
* 指向當前進程中的所有寫事件對象,connection_n 同時表示所有寫事件的總數
*/
ngx_event_t *write_events;
/*
* 舊的 ngx_cycle_t 對象用於引用上一個 ngx_cycle_t 對象中的成員。例如
* ngx_init_cycle 方法,在啟動初期,需要建立一個臨時的 ngx_cycle_t 對象
* 保存一些變量,再調用 ngx_init_cycle 方法時就可以把舊的 ngx_cycle_t
* 對象傳進去,而這時 old_cycle 對象就會保存這個前期的 ngx_cycle_t 對象
*/
ngx_cycle_t *old_cycle;
/*
* 配置文件相對於安裝目錄的路徑名稱
*/
ngx_str_t conf_file;
/*
* Nginx 處理配置文件時需要特殊處理的在命令行攜帶的參數,一般是
* -g 選項攜帶的參數
*/
ngx_str_t conf_param;
/*
* Nginx 配置文件所在目錄的路徑
*/
ngx_str_t conf_prefix;
/*
* Nginx 安裝目錄的路徑
*/
ngx_str_t prefix;
/*
* 用於進程間同步的文件鎖名稱
*/
ngx_str_t lock_file;
/*
* 使用 gethostname 系統調用得到的主機名
*/
ngx_str_t hostname;
};
3. ngx_init_cycle
/*
* @old_cycle: 表示臨時的 ngx_cycle_t 指針,一般僅用來傳遞 ngx_cycle_t 結構體中
* 的配置文件路徑等參數.
*
* 返回初始化成功的完整的 ngx_cycle_t 結構體,該函數將會負責初始化 ngx_cycle_t
* 中的數據結構、解析配置文件、加載所有模塊、打開監聽端口、初始化進程間通信方式
* 等工作。如果失敗,則返回 NULL 空指針.
*/
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();
log = old_cycle->log;
/* 為 ngx_cycle_t 創建一個 16k 大小內存池 */
pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
if (pool == NULL) {
return NULL;
}
pool->log = log;
/* 為 ngx_cycle_t 分配內存 */
cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
if (cycle == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
/* 設置該 ngx_cycle_t 使用的內存池 */
cycle->pool = pool;
cycle->log = log;
/* old_cycle 一般僅用來傳遞 ngx_cycle_t 結構體中的配置文件路徑等參數. */
cycle->old_cycle = old_cycle;
/* 假設設置 --prefix=/usr/local/nginx */
cycle->conf_prefix.len = old_cycle->conf_prefix.len;
/* Nginx 配置文件所在目錄的路徑: /usr/local/nginx/conf/ */
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;
/* Nginx 安裝目錄的路徑: /usr/local/nginx/ */
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;
/* 配置文件相對於安裝目錄的路徑名稱: /usr/local/nginx/conf/nginx.conf */
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);
/* Nginx 處理配置文件時需要特殊處理的在命令行攜帶的參數,
* 一般是 -g 選項所攜帶的參數 */
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 所有要操作的目錄。如果有目錄不能存在,
* 則會試圖創建,而創建目錄失敗將會導致 Nginx 啟動失敗。如,上傳文件
* 的臨時目錄也在 pathes 中,如果沒有權限創建,則會導致 Nginx 無法啟動.
*
* 這里,若 old->cycle->paths 中沒有元素,則默認為 10
*/
n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;
/* 創建 cycle->paths 數組 */
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;
}
/* 初始化一棵紅黑樹 config_dump_rbtree */
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;
}
/* 若 old_cycle->open_files 鏈表為空,則默認設置下面創建的 open_files 鏈表
* 元素個數為 20 */
} else {
n = 20;
}
/* 創建 open_files 鏈表,元素類型是 ngx_open_file_t 結構體,它表示 Nginx 已經
* 打開的所有文件。事實上,Nginx 框架不會向 open_files 鏈表中添加文件,而是
* 由對此感興趣的模塊向其中添加文件路徑名,Nginx框架會在 ngx_init_cycle 方法
* 中打開這些文件 */
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;
}
/* 若 old_cycle->shared_memory 鏈表文件,則默認下面創建的 shared_memory 鏈表
* 元素個數為 1 */
} else {
n = 1;
}
/* 創建 shared_memory 鏈表,元素類型是 ngx_shm_zone_t 結構體,每個元素表示
* 一塊共享內存 */
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;
/* 創建 listening 數組,每個數組元素存儲着 ngx_listening_t 成員,表示監聽端口
* 及相關的參數 */
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));
/* 初始化 reusable_connections_queue 雙向鏈表容器,元素類型是 ngx_connection_t
* 結構體,表示可重復使用連接隊列 */
ngx_queue_init(&cycle->reusable_connections_queue);
/* conf_ctx 保存着所有模塊存儲配置項的結構體的指針,它首先是一個數組,每個數組成員
* 又是一個指針,這個指針指向另一個存儲着指針的數組。該數組的最大值 ngx_max_module
* 在 ngx_preinit_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;
}
/* 將獲取到的主機名按小寫方式保存到 cycle->hostname 中 */
ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);
/* 將全局變量 ngx_modules 數組所包含的所有模塊都復制到 cycle->modules 中
* 該 modules 數組的元素個數為 cycle->modules_n */
if (ngx_cycle_modules(cycle) != NGX_OK) {
ngx_destroy_pool(pool);
return NULL;
}
/* 遍歷所有的核心模塊 */
for (i = 0; cycle->modules[i]; i++) {
if (cycle->modules[i]->type != NGX_CORE_MODULE) {
continue;
}
/* 獲取該模塊對象的上下文結構體,對於核心模塊
* 其上下文結構體固定為 ngx_core_module_t */
module = cycle->modules[i]->ctx;
if (module->create_conf) {
/* 調用核心模塊的 create_conf 函數,創建實際的配置信息存儲空間
* 並初始化該配置信息結構體 */
rv = module->create_conf(cycle);
if (rv == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
/* 將返回的配置信息結構體保存到該核心模塊在 cycle->conf_ctx 數組
* 對應下標處 */
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;
}
/* 指向保存着所有 Nginx 模塊的配置結構體數組 */
conf.ctx = cycle->conf_ctx;
/* 指向當前的核心結構體 ngx_cycle_t */
conf.cycle = cycle;
/* 指向當前核心結構體 ngx_cycle_t 所用的內存池 */
conf.pool = pool;
conf.log = log;
/* 默認初始化當前模塊的類型為核心模塊 */
conf.module_type = NGX_CORE_MODULE;
conf.cmd_type = NGX_MAIN_CONF;
#if 0
log->log_level = NGX_LOG_DEBUG_ALL;
#endif
/* 解析通過命令行傳入的參數,若沒有,則可忽略 */
if (ngx_conf_param(&conf) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf);
return NULL;
}
/* 開始解析配置文件中的所有配置項, conf_file 保存着配置文件
* 的絕對路徑 */
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);
}
/* 遍歷所有非核心模塊 */
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) {
/* 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;
}
/* 從cycle->conf_ctx數組中取出ngx_core_module的配置信息結構體 */
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);
}
}
/* 若當前系統不支持原子操作時則檢測用於文件鎖的文件是否打開,
* 若失敗,則返回 error */
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 */
/* 該 shared_memory 是由配置有啟用共享內存的模塊來添加的 */
part = &cycle->shared_memory.part;
shm_zone = part->elts;
/* 該 for 循環是檢測新的將要創建的共享內存是否與舊的共享內存
* 有沖突 */
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;
}
/* 銷毀舊的共享內存 */
ngx_shm_free(&oshm_zone[n].shm);
break;
}
/* mmap 映射一塊共享內存 */
if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
goto failed;
}
/* 調用 slab 機制划分該共享內存 */
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].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 {
/* listening: 動態數組,每個數組元素存儲着ngx_listening_t成員,
* 表示監聽端口及相關的參數 */
ls = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
/* 標志位,為1則表示在當前監聽句柄有效,且執行ngx_init_cycle時
* 不關閉監聽端口,為0時則正常關閉。 */
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
}
}
/* 遍歷nginx.conf中配置的所有需要監聽的端口,創建socket */
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;
/* 調用所有模塊的init_module方法 */
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
&& 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);
/* 若為 master_process 模式 */
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);
}
}
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;
}
