Nginx学习之十一-Nginx启动框架处理流程

来源:互联网 发布:jquery ajax提交数据 编辑:程序博客网 时间:2024/05/29 13:41

Nginx启动过程流程图

下面首先给出Nginx启动过程的流程图:


ngx_cycle_t结构体


Nginx的启动初始化在src/core/nginx.c的main函数中完成,当然main函数是整个Nginx的入口,除了完成启动初始化任务以外,也必定是所有功能模块的入口之处。Nginx的初始化工作主要围绕一个类型为ngx_cycle_t类型的全局变量(cycle)展开。

ngx_cycle_t结构体类型:
[cpp] view plaincopyprint?
  1. typedef struct ngx_cycle_s       ngx_cycle_t;  
  2. struct ngx_cycle_s {  
  3.     void                  ****conf_ctx;  //配置上下文数组(含所有模块)  
  4.     ngx_pool_t               *pool;      //内存池  
  5.   
  6.     ngx_log_t                *log;       //日志  
  7.     ngx_log_t                 new_log;  
  8.   
  9.     ngx_connection_t        **files;     //连接文件  
  10.     ngx_connection_t         *free_connections;  //空闲连接  
  11.     ngx_uint_t                free_connection_n; //空闲连接个数  
  12.   
  13.     ngx_queue_t               reusable_connections_queue;  //再利用连接队列  
  14.   
  15.     ngx_array_t               listening;     //监听数组  
  16.     ngx_array_t               pathes;        //路径数组  
  17.     ngx_list_t                open_files;    //打开文件链表  
  18.     ngx_list_t                shared_memory; //共享内存链表  
  19.   
  20.     ngx_uint_t                connection_n;  //连接个数  
  21.     ngx_uint_t                files_n;       //打开文件个数  
  22.   
  23.     ngx_connection_t         *connections;   //连接  
  24.     ngx_event_t              *read_events;   //读事件  
  25.     ngx_event_t              *write_events;  //写事件  
  26.   
  27.     ngx_cycle_t              *old_cycle;     //old cycle指针  
  28.   
  29.     ngx_str_t                 conf_file;     //配置文件  
  30.     ngx_str_t                 conf_param;    //配置参数  
  31.     ngx_str_t                 conf_prefix;   //配置前缀  
  32.     ngx_str_t                 prefix;        //前缀  
  33.     ngx_str_t                 lock_file;     //锁文件  
  34.     ngx_str_t                 hostname;      //主机名  
  35. };  

main函数源代码分析


下面具体看一下main函数为Nginx的启动过程做了哪些初始化。

[cpp] view plaincopyprint?
  1. //Nginx中main函数,涵盖了Nginx启动过程  
  2. int ngx_cdecl  
  3. main(int argc, char *const *argv)  
  4. {  
  5.     ngx_int_t         i;  
  6.     ngx_log_t        *log;  
  7.     ngx_cycle_t      *cycle, init_cycle;  
  8.     ngx_core_conf_t  *ccf;  
  9.   
  10.     ngx_debug_init();  
  11.   
  12.     if (ngx_strerror_init() != NGX_OK) {  
  13.         return 1;  
  14.     }  
  15.   
  16.     //对参数选项进行处理  
  17.     if (ngx_get_options(argc, argv) != NGX_OK) {  
  18.         return 1;  
  19.     }  
  20.   
  21.     //根据参数选项采取相应动作,比如:显示版本、测试配置等功能  
  22.     if (ngx_show_version) {  
  23.         ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);  
  24.   
  25.         if (ngx_show_help) {  
  26.             ngx_write_stderr(  
  27.                 "Usage: nginx [-?hvVtq] [-s signal] [-c filename] "  
  28.                              "[-p prefix] [-g directives]" NGX_LINEFEED  
  29.                              NGX_LINEFEED  
  30.                 "Options:" NGX_LINEFEED  
  31.                 "  -?,-h         : this help" NGX_LINEFEED  
  32.                 "  -v            : show version and exit" NGX_LINEFEED  
  33.                 "  -V            : show version and configure options then exit"  
  34.                                    NGX_LINEFEED  
  35.                 "  -t            : test configuration and exit" NGX_LINEFEED  
  36.                 "  -q            : suppress non-error messages "  
  37.                                    "during configuration testing" NGX_LINEFEED  
  38.                 "  -s signal     : send signal to a master process: "  
  39.                                    "stop, quit, reopen, reload" NGX_LINEFEED  
  40. #ifdef NGX_PREFIX  
  41.                 "  -p prefix     : set prefix path (default: "  
  42.                                    NGX_PREFIX ")" NGX_LINEFEED  
  43. #else  
  44.                 "  -p prefix     : set prefix path (default: NONE)" NGX_LINEFEED  
  45. #endif  
  46.                 "  -c filename   : set configuration file (default: "  
  47.                                    NGX_CONF_PATH ")" NGX_LINEFEED  
  48.                 "  -g directives : set global directives out of configuration "  
  49.                                    "file" NGX_LINEFEED NGX_LINEFEED  
  50.                 );  
  51.         }  
  52.   
  53.         if (ngx_show_configure) {  
  54.             ngx_write_stderr(  
  55. #ifdef NGX_COMPILER  
  56.                 "built by " NGX_COMPILER NGX_LINEFEED  
  57. #endif  
  58. #if (NGX_SSL)  
  59. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME  
  60.                 "TLS SNI support enabled" NGX_LINEFEED  
  61. #else  
  62.                 "TLS SNI support disabled" NGX_LINEFEED  
  63. #endif  
  64. #endif  
  65.                 "configure arguments:" NGX_CONFIGURE NGX_LINEFEED);  
  66.         }  
  67.   
  68.         if (!ngx_test_config) {  
  69.             return 0;  
  70.         }  
  71.     }  
  72.   
  73.     /* TODO */ ngx_max_sockets = -1;  
  74.   
  75.     //初始化并更新时间  
  76.     ngx_time_init();  
  77.   
  78. #if (NGX_PCRE)  
  79.     ngx_regex_init();  
  80. #endif  
  81.   
  82.     ngx_pid = ngx_getpid();  
  83.   
  84.     //初始化日志  
  85.     log = ngx_log_init(ngx_prefix);  
  86.     if (log == NULL) {  
  87.         return 1;  
  88.     }  
  89.   
  90.     /* STUB */  
  91. #if (NGX_OPENSSL)  
  92.     ngx_ssl_init(log);  
  93. #endif  
  94.   
  95.     /* 
  96.      * init_cycle->log is required for signal handlers and 
  97.      * ngx_process_options() 
  98.      */  
  99.   
  100.     //清零全局变量ngx_cycle,并分配内存池  
  101.     ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));  
  102.     init_cycle.log = log;  
  103.     ngx_cycle = &init_cycle;  
  104.   
  105.     init_cycle.pool = ngx_create_pool(1024, log);  
  106.     if (init_cycle.pool == NULL) {  
  107.         return 1;  
  108.     }  
  109.   
  110.     //将命令行参数保存到ngx_os_argv、ngx_argc以及ngx_argv这几个全局的变量中。这算是一个备份存储,方便以后master进程做热代码替换之用。  
  111.     if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {  
  112.         return 1;  
  113.     }  
  114.   
  115.     //用Nginx运行是携带的目录参数初始化init_cycle  
  116.     if (ngx_process_options(&init_cycle) != NGX_OK) {  
  117.         return 1;  
  118.     }  
  119.   
  120.     //完成操作系统的一些信息提取,信息会被保存到一些全局变量中  
  121.     if (ngx_os_init(log) != NGX_OK) {  
  122.         return 1;  
  123.     }  
  124.   
  125.     /* 
  126.      * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init() 
  127.      */  
  128.   
  129.     //初始化一个做循环冗余校验的表,由此可以看出后续的循环冗余校验将采用高效的查表法  
  130.     if (ngx_crc32_table_init() != NGX_OK) {  
  131.         return 1;  
  132.     }  
  133.   
  134.     //通过环境变量NGINX完成socket的继承,继承来的socket将会放到init_cycle的listening数组中。同时可以读取master进程传递的平滑升级信息等等  
  135.     if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {  
  136.         return 1;  
  137.     }  
  138.   
  139.     //初始化所有模块的index信息,即对所有模块进行编号  
  140.     //ngx_modules数却是在自动编译的时候生成的,位于objs/ngx_modules.c文件中  
  141.     ngx_max_module = 0;  
  142.     for (i = 0; ngx_modules[i]; i++) {  
  143.         ngx_modules[i]->index = ngx_max_module++;  
  144.     }  
  145.   
  146.     //完成很多信息的初始化工作  
  147.     cycle = ngx_init_cycle(&init_cycle);  
  148.     if (cycle == NULL) {  
  149.         if (ngx_test_config) {  
  150.             ngx_log_stderr(0, "configuration file %s test failed",  
  151.                            init_cycle.conf_file.data);  
  152.         }  
  153.   
  154.         return 1;  
  155.     }  
  156.   
  157.     if (ngx_test_config) {  
  158.         if (!ngx_quiet_mode) {  
  159.             ngx_log_stderr(0, "configuration file %s test is successful",  
  160.                            cycle->conf_file.data);  
  161.         }  
  162.   
  163.         return 0;  
  164.     }  
  165.   
  166.     //若有信号  
  167.     if (ngx_signal) {  
  168.         return ngx_signal_process(cycle, ngx_signal);  
  169.     }  
  170.   
  171.     ngx_os_status(cycle->log);  
  172.   
  173.     ngx_cycle = cycle;  
  174.   
  175.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);  
  176.   
  177.     //这个地方需要解释下  
  178.     //ccf->master是从配置文件中解析master_process配置项所得的值,初始化为NGX_CONF_UNSET(-1),在配置项中,如果flag类型的配置项master_process被设置为on,则其值为1,如果为off,则其值为0  
  179.     //ngx_process为全局变量,用于记录要采用的工作模式,未被初始化,因此初始值是0(uint型全局变量会被系统默认初始化为0)  
  180.     //相关宏定义如下  
  181.     //#define NGX_PROCESS_SINGLE     0  
  182.     //#define NGX_PROCESS_MASTER     1  
  183.     //#define NGX_PROCESS_SIGNALLER  2  
  184.     //#define NGX_PROCESS_WORKER     3  
  185.     //#define NGX_PROCESS_HELPER     4  
  186.     //因此,下面的if判断语句的含义就是:用来处理一种特殊情况,即如果在配置项中未设置master_process配置项或者是设置为打开,ngx_process未被设置,采用默认值0,这个时候要采用master工作模式。因为master_process优先级高,且nginx默认采用master模式  
  187.     ///如果在配置项中设置master_process为off,那么if依据不会执行。最终nginx工作模式取决于ngx_proces的初值0,即采用单进程模式  
  188.     if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {  
  189.         ngx_process = NGX_PROCESS_MASTER;  
  190.     }  
  191.   
  192. #if !(NGX_WIN32)  
  193.     //初始化信号;主要完成信号处理程序的注册  
  194.     if (ngx_init_signals(cycle->log) != NGX_OK) {  
  195.         return 1;  
  196.     }  
  197.   
  198.     //若无继承sockets,且设置了守护进程表示,则创建守护进程  
  199.     if (!ngx_inherited && ccf->daemon) {  
  200.         if (ngx_daemon(cycle->log) != NGX_OK) {  
  201.             return 1;  
  202.         }  
  203.   
  204.         ngx_daemonized = 1;  
  205.     }  
  206.   
  207.     if (ngx_inherited) {  
  208.         ngx_daemonized = 1;  
  209.     }  
  210.   
  211. #endif  
  212.   
  213.     //创建进程记录文件;(非NGX_PROCESS_MASTER=1进程,不创建该文件)  
  214.     if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {  
  215.         return 1;  
  216.     }  
  217.   
  218.     if (cycle->log->file->fd != ngx_stderr) {  
  219.   
  220.         if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {  
  221.             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,  
  222.                           ngx_set_stderr_n " failed");  
  223.             return 1;  
  224.         }  
  225.     }  
  226.   
  227.     if (log->file->fd != ngx_stderr) {  
  228.         if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {  
  229.             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,  
  230.                           ngx_close_file_n " built-in log failed");  
  231.         }  
  232.     }  
  233.   
  234.     ngx_use_stderr = 0;  
  235.   
  236.     //进入进程主循环  
  237.     //单进程方式运行Nginx  
  238.     if (ngx_process == NGX_PROCESS_SINGLE) {  
  239.         ngx_single_process_cycle(cycle);  
  240.     } else {  
  241.     //以master多进程方式运行Nginx  
  242.         ngx_master_process_cycle(cycle);  
  243.     }  
  244.   
  245.     return 0;  
  246. }  

关于master进程和worker进程的工作原理,参考:http://blog.csdn.net/xiajun07061225/article/details/9241179

下面分析初始化过程中比较重要的一个函数ngx_init_cycle()。
cycle就是周期的意思,对应着一次启动过程。也就是说,不论发生了上节介绍的三种启动方式的哪一种,nginx都会创建一个新的cycle与这次启动对应。
ngx_init_cycle提供的是配置解析接口。接口是一个切入点,通过少量代码提供一个完整功能的调用。配置解析接口分为两个阶段,一个是准备阶段,另一个就是真正开始调用配置解析。其核心代码如下:
[cpp] view plaincopyprint?
  1. ngx_cycle_t *  
  2. ngx_init_cycle(ngx_cycle_t *old_cycle)  
  3. {  
  4.     void                *rv;  
  5.     char               **senv, **env;  
  6.     ngx_uint_t           i, n;  
  7.     ngx_log_t           *log;  
  8.     ngx_time_t          *tp;  
  9.     ngx_conf_t           conf;  
  10.     ngx_pool_t          *pool;  
  11.     ngx_cycle_t         *cycle, **old;  
  12.     ngx_shm_zone_t      *shm_zone, *oshm_zone;  
  13.     ngx_list_part_t     *part, *opart;  
  14.     ngx_open_file_t     *file;  
  15.     ngx_listening_t     *ls, *nls;  
  16.     ngx_core_conf_t     *ccf, *old_ccf;  
  17.     ngx_core_module_t   *module;  
  18.     char                 hostname[NGX_MAXHOSTNAMELEN];  
  19.   
  20.     //下面几行代码对时区和时间进行一次更新操作  
  21.     ngx_timezone_update();  
  22.   
  23.     /* force localtime update with a new timezone */  
  24.   
  25.     tp = ngx_timeofday();  
  26.     tp->sec = 0;  
  27.   
  28.     ngx_time_update();  
  29.   
  30.   
  31.     log = old_cycle->log;  
  32.   
  33.     //创建内存池  
  34.     pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);  
  35.     if (pool == NULL) {  
  36.         return NULL;  
  37.     }  
  38.     pool->log = log;  
  39.   
  40.     //在内存池上分配一个ngx_cycle_t对象  
  41.     cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));  
  42.     if (cycle == NULL) {  
  43.         ngx_destroy_pool(pool);  
  44.         return NULL;  
  45.     }  
  46.   
  47.     cycle->pool = pool;  
  48.     cycle->log = log;  
  49.     cycle->new_log.log_level = NGX_LOG_ERR;  
  50.     cycle->old_cycle = old_cycle;  
  51.   
  52.     cycle->conf_prefix.len = old_cycle->conf_prefix.len;  
  53.     cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);  
  54.     if (cycle->conf_prefix.data == NULL) {  
  55.         ngx_destroy_pool(pool);  
  56.         return NULL;  
  57.     }  
  58.   
  59.     cycle->prefix.len = old_cycle->prefix.len;  
  60.     cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);  
  61.     if (cycle->prefix.data == NULL) {  
  62.         ngx_destroy_pool(pool);  
  63.         return NULL;  
  64.     }  
  65.   
  66.     cycle->conf_file.len = old_cycle->conf_file.len;  
  67.     cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);  
  68.     if (cycle->conf_file.data == NULL) {  
  69.         ngx_destroy_pool(pool);  
  70.         return NULL;  
  71.     }  
  72.     ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,  
  73.                 old_cycle->conf_file.len + 1);  
  74.   
  75.     cycle->conf_param.len = old_cycle->conf_param.len;  
  76.     cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);  
  77.     if (cycle->conf_param.data == NULL) {  
  78.         ngx_destroy_pool(pool);  
  79.         return NULL;  
  80.     }  
  81.   
  82.   
  83.     n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;  
  84.   
  85.     cycle->paths.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));  
  86.     if (cycle->paths.elts == NULL) {  
  87.         ngx_destroy_pool(pool);  
  88.         return NULL;  
  89.     }  
  90.   
  91.     cycle->paths.nelts = 0;  
  92.     cycle->paths.size = sizeof(ngx_path_t *);  
  93.     cycle->paths.nalloc = n;  
  94.     cycle->paths.pool = pool;  
  95.   
  96.   
  97.     //打开的文件个数计算  
  98.     if (old_cycle->open_files.part.nelts) {  
  99.         n = old_cycle->open_files.part.nelts;  
  100.         for (part = old_cycle->open_files.part.next; part; part = part->next) {  
  101.             n += part->nelts;  
  102.         }  
  103.   
  104.     } else {  
  105.     //采用一个系统默认值  
  106.         n = 20;  
  107.     }  
  108.   
  109.     //初始化一个单链表容器,维护打开的文件  
  110.     if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))  
  111.         != NGX_OK)  
  112.     {  
  113.         ngx_destroy_pool(pool);  
  114.         return NULL;  
  115.     }  
  116.   
  117.   
  118.     if (old_cycle->shared_memory.part.nelts) {  
  119.         n = old_cycle->shared_memory.part.nelts;  
  120.         for (part = old_cycle->shared_memory.part.next; part; part = part->next)  
  121.         {  
  122.             n += part->nelts;  
  123.         }  
  124.   
  125.     } else {  
  126.         n = 1;  
  127.     }  
  128.   
  129.     //初始化单链表容器,维护共享内存  
  130.     if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))  
  131.         != NGX_OK)  
  132.     {  
  133.         ngx_destroy_pool(pool);  
  134.         return NULL;  
  135.     }  
  136.   
  137.     //初始化listening数组(存储监听端口和相关参数ngx_listening_t类型的动态数组)  
  138.     n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;  
  139.   
  140.     cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));  
  141.     if (cycle->listening.elts == NULL) {  
  142.         ngx_destroy_pool(pool);  
  143.         return NULL;  
  144.     }  
  145.   
  146.     cycle->listening.nelts = 0;  
  147.     cycle->listening.size = sizeof(ngx_listening_t);  
  148.     cycle->listening.nalloc = n;  
  149.     cycle->listening.pool = pool;  
  150.   
  151.   
  152.     ngx_queue_init(&cycle->reusable_connections_queue);  
  153.   
  154.     //初始化conf_ctx指针数组(保存所有模块存储配置项结构体的指针)  
  155.     cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));  
  156.     if (cycle->conf_ctx == NULL) {  
  157.         ngx_destroy_pool(pool);  
  158.         return NULL;  
  159.     }  
  160.   
  161.   
  162.     if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {  
  163.         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");  
  164.         ngx_destroy_pool(pool);  
  165.         return NULL;  
  166.     }  
  167.   
  168.     /* on Linux gethostname() silently truncates name that does not fit */  
  169.   
  170.     hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';  
  171.     cycle->hostname.len = ngx_strlen(hostname);  
  172.   
  173.     cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);  
  174.     if (cycle->hostname.data == NULL) {  
  175.         ngx_destroy_pool(pool);  
  176.         return NULL;  
  177.     }  
  178.   
  179.     ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);  
  180.   
  181.   
  182.     for (i = 0; ngx_modules[i]; i++) {  
  183.     //非核心模块跳过  
  184.         if (ngx_modules[i]->type != NGX_CORE_MODULE) {  
  185.             continue;  
  186.         }  
  187.   
  188.         module = ngx_modules[i]->ctx;  
  189.   
  190.     //调用所有核心模块的create_conf方法,构造用于存储配置项的结构体  
  191.     //那么非核心模块怎么办呢?其实这些模块大多从属于一个核心模块,如每个http模块都由ngx_http_module管理,这样ngx_http_module在解析自己感兴趣的配置项时,将会调用所有http模块约定的方法来创建存储配置项的结构体  
  192.         if (module->create_conf) {  
  193.             rv = module->create_conf(cycle);  
  194.             if (rv == NULL) {  
  195.                 ngx_destroy_pool(pool);  
  196.                 return NULL;  
  197.             }  
  198.             cycle->conf_ctx[ngx_modules[i]->index] = rv;  
  199.         }  
  200.     }  
  201.   
  202.   
  203.     senv = environ;  
  204.   
  205.   
  206.     ngx_memzero(&conf, sizeof(ngx_conf_t));  
  207.     /* STUB: init array ? */  
  208.     conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));  
  209.     if (conf.args == NULL) {  
  210.         ngx_destroy_pool(pool);  
  211.         return NULL;  
  212.     }  
  213.   
  214.     conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);  
  215.     if (conf.temp_pool == NULL) {  
  216.         ngx_destroy_pool(pool);  
  217.         return NULL;  
  218.     }  
  219.   
  220.   
  221.     conf.ctx = cycle->conf_ctx;  
  222.     conf.cycle = cycle;  
  223.     conf.pool = pool;  
  224.     conf.log = log;  
  225.     conf.module_type = NGX_CORE_MODULE;  
  226.     conf.cmd_type = NGX_MAIN_CONF;  
  227.   
  228. #if 0  
  229.     log->log_level = NGX_LOG_DEBUG_ALL;  
  230. #endif  
  231.   
  232.     //配置项解析  
  233.     if (ngx_conf_param(&conf) != NGX_CONF_OK) {  
  234.         environ = senv;  
  235.         ngx_destroy_cycle_pools(&conf);  
  236.         return NULL;  
  237.     }  
  238.   
  239.     if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {  
  240.         environ = senv;  
  241.         ngx_destroy_cycle_pools(&conf);  
  242.         return NULL;  
  243.     }  
  244.   
  245.     if (ngx_test_config && !ngx_quiet_mode) {  
  246.         ngx_log_stderr(0, "the configuration file %s syntax is ok",  
  247.                        cycle->conf_file.data);  
  248.     }  
  249.   
  250.     //调用所有核心模块的init_conf方法,这一步骤的目的在于让所有核心模块在解析玩配置项后可以做综合性处理  
  251.     for (i = 0; ngx_modules[i]; i++) {  
  252.         if (ngx_modules[i]->type != NGX_CORE_MODULE) {  
  253.             continue;  
  254.         }  
  255.   
  256.         module = ngx_modules[i]->ctx;  
  257.   
  258.         if (module->init_conf) {  
  259.             if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])  
  260.                 == NGX_CONF_ERROR)  
  261.             {  
  262.                 environ = senv;  
  263.                 ngx_destroy_cycle_pools(&conf);  
  264.                 return NULL;  
  265.             }  
  266.         }  
  267.     }  
  268.   
  269.     if (ngx_process == NGX_PROCESS_SIGNALLER) {  
  270.         return cycle;  
  271.     }  
  272.   
  273.     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);  
  274.   
  275.     if (ngx_test_config) {  
  276.   
  277.         if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {  
  278.             goto failed;  
  279.         }  
  280.   
  281.     } else if (!ngx_is_init_cycle(old_cycle)) {  
  282.   
  283.         /* 
  284.          * we do not create the pid file in the first ngx_init_cycle() call 
  285.          * because we need to write the demonized process pid 
  286.          */  
  287.   
  288.         old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,  
  289.                                                    ngx_core_module);  
  290.         if (ccf->pid.len != old_ccf->pid.len  
  291.             || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)  
  292.         {  
  293.             /* new pid file name */  
  294.   
  295.             if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {  
  296.                 goto failed;  
  297.             }  
  298.   
  299.             ngx_delete_pidfile(old_cycle);  
  300.         }  
  301.     }  
  302.   
  303.   
  304.     if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {  
  305.         goto failed;  
  306.     }  
  307.   
  308.   
  309.     if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {  
  310.         goto failed;  
  311.     }  
  312.   
  313.   
  314.     if (cycle->new_log.file == NULL) {  
  315.         cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);  
  316.         if (cycle->new_log.file == NULL) {  
  317.             goto failed;  
  318.         }  
  319.     }  
  320.   
  321.     //创建目录、打开文件、初始化共享内存等进程间通信方式  
  322.     /* open the new files */  
  323.   
  324.     part = &cycle->open_files.part;  
  325.     file = part->elts;  
  326.   
  327.     for (i = 0; /* void */ ; i++) {  
  328.   
  329.         if (i >= part->nelts) {  
  330.             if (part->next == NULL) {  
  331.                 break;  
  332.             }  
  333.             part = part->next;  
  334.             file = part->elts;  
  335.             i = 0;  
  336.         }  
  337.   
  338.         if (file[i].name.len == 0) {  
  339.             continue;  
  340.         }  
  341.   
  342.         file[i].fd = ngx_open_file(file[i].name.data,  
  343.                                    NGX_FILE_APPEND,  
  344.                                    NGX_FILE_CREATE_OR_OPEN,  
  345.                                    NGX_FILE_DEFAULT_ACCESS);  
  346.   
  347.         ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,  
  348.                        "log: %p %d \"%s\"",  
  349.                        &file[i], file[i].fd, file[i].name.data);  
  350.   
  351.         if (file[i].fd == NGX_INVALID_FILE) {  
  352.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,  
  353.                           ngx_open_file_n " \"%s\" failed",  
  354.                           file[i].name.data);  
  355.             goto failed;  
  356.         }  
  357.   
  358. #if !(NGX_WIN32)  
  359.         if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {  
  360.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,  
  361.                           "fcntl(FD_CLOEXEC) \"%s\" failed",  
  362.                           file[i].name.data);  
  363.             goto failed;  
  364.         }  
  365. #endif  
  366.     }  
  367.   
  368.     cycle->log = &cycle->new_log;  
  369.     pool->log = &cycle->new_log;  
  370.   
  371.   
  372.     /* create shared memory */  
  373.   
  374.     part = &cycle->shared_memory.part;  
  375.     shm_zone = part->elts;  
  376.   
  377.     for (i = 0; /* void */ ; i++) {  
  378.   
  379.         if (i >= part->nelts) {  
  380.             if (part->next == NULL) {  
  381.                 break;  
  382.             }  
  383.             part = part->next;  
  384.             shm_zone = part->elts;  
  385.             i = 0;  
  386.         }  
  387.   
  388.         if (shm_zone[i].shm.size == 0) {  
  389.             ngx_log_error(NGX_LOG_EMERG, log, 0,  
  390.                           "zero size shared memory zone \"%V\"",  
  391.                           &shm_zone[i].shm.name);  
  392.             goto failed;  
  393.         }  
  394.   
  395.         shm_zone[i].shm.log = cycle->log;  
  396.   
  397.         opart = &old_cycle->shared_memory.part;  
  398.         oshm_zone = opart->elts;  
  399.   
  400.         for (n = 0; /* void */ ; n++) {  
  401.   
  402.             if (n >= opart->nelts) {  
  403.                 if (opart->next == NULL) {  
  404.                     break;  
  405.                 }  
  406.                 opart = opart->next;  
  407.                 oshm_zone = opart->elts;  
  408.                 n = 0;  
  409.             }  
  410.   
  411.             if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {  
  412.                 continue;  
  413.             }  
  414.   
  415.             if (ngx_strncmp(shm_zone[i].shm.name.data,  
  416.                             oshm_zone[n].shm.name.data,  
  417.                             shm_zone[i].shm.name.len)  
  418.                 != 0)  
  419.             {  
  420.                 continue;  
  421.             }  
  422.   
  423.             if (shm_zone[i].tag == oshm_zone[n].tag  
  424.                 && shm_zone[i].shm.size == oshm_zone[n].shm.size)  
  425.             {  
  426.                 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;  
  427.   
  428.                 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)  
  429.                     != NGX_OK)  
  430.                 {  
  431.                     goto failed;  
  432.                 }  
  433.   
  434.                 goto shm_zone_found;  
  435.             }  
  436.   
  437.             ngx_shm_free(&oshm_zone[n].shm);  
  438.   
  439.             break;  
  440.         }  
  441.   
  442.         if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {  
  443.             goto failed;  
  444.         }  
  445.   
  446.         if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {  
  447.             goto failed;  
  448.         }  
  449.   
  450.         if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {  
  451.             goto failed;  
  452.         }  
  453.   
  454.     shm_zone_found:  
  455.   
  456.         continue;  
  457.     }  
  458.   
  459.   
  460.     /* handle the listening sockets */  
  461.     //打开从配置文件中读取到的监听端口  
  462.     if (old_cycle->listening.nelts) {  
  463.         ls = old_cycle->listening.elts;  
  464.         for (i = 0; i < old_cycle->listening.nelts; i++) {  
  465.             ls[i].remain = 0;  
  466.         }  
  467.   
  468.         nls = cycle->listening.elts;  
  469.         for (n = 0; n < cycle->listening.nelts; n++) {  
  470.   
  471.             for (i = 0; i < old_cycle->listening.nelts; i++) {  
  472.                 if (ls[i].ignore) {  
  473.                     continue;  
  474.                 }  
  475.   
  476.                 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK)  
  477.                 {  
  478.                     nls[n].fd = ls[i].fd;  
  479.                     nls[n].previous = &ls[i];  
  480.                     ls[i].remain = 1;  
  481.   
  482.                     if (ls[n].backlog != nls[i].backlog) {  
  483.                         nls[n].listen = 1;  
  484.                     }  
  485.   
  486. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)  
  487.   
  488.                     /* 
  489.                      * FreeBSD, except the most recent versions, 
  490.                      * could not remove accept filter 
  491.                      */  
  492.                     nls[n].deferred_accept = ls[i].deferred_accept;  
  493.   
  494.                     if (ls[i].accept_filter && nls[n].accept_filter) {  
  495.                         if (ngx_strcmp(ls[i].accept_filter,  
  496.                                        nls[n].accept_filter)  
  497.                             != 0)  
  498.                         {  
  499.                             nls[n].delete_deferred = 1;  
  500.                             nls[n].add_deferred = 1;  
  501.                         }  
  502.   
  503.                     } else if (ls[i].accept_filter) {  
  504.                         nls[n].delete_deferred = 1;  
  505.   
  506.                     } else if (nls[n].accept_filter) {  
  507.                         nls[n].add_deferred = 1;  
  508.                     }  
  509. #endif  
  510.   
  511. #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)  
  512.   
  513.                     if (ls[n].deferred_accept && !nls[n].deferred_accept) {  
  514.                         nls[n].delete_deferred = 1;  
  515.   
  516.                     } else if (ls[i].deferred_accept != nls[n].deferred_accept)  
  517.                     {  
  518.                         nls[n].add_deferred = 1;  
  519.                     }  
  520. #endif  
  521.                     break;  
  522.                 }  
  523.             }  
  524.   
  525.             if (nls[n].fd == -1) {  
  526.                 nls[n].open = 1;  
  527.             }  
  528.         }  
  529.   
  530.     } else {  
  531.         ls = cycle->listening.elts;  
  532.         for (i = 0; i < cycle->listening.nelts; i++) {  
  533.             ls[i].open = 1;  
  534. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)  
  535.             if (ls[i].accept_filter) {  
  536.                 ls[i].add_deferred = 1;  
  537.             }  
  538. #endif  
  539. #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)  
  540.             if (ls[i].deferred_accept) {  
  541.                 ls[i].add_deferred = 1;  
  542.             }  
  543. #endif  
  544.         }  
  545.     }  
  546.   
  547.     if (ngx_open_listening_sockets(cycle) != NGX_OK) {  
  548.         goto failed;  
  549.     }  
  550.   
  551.     if (!ngx_test_config) {  
  552.         ngx_configure_listening_sockets(cycle);  
  553.     }  
  554.   
  555.   
  556.     /* commit the new cycle configuration */  
  557.   
  558.     if (!ngx_use_stderr && cycle->log->file->fd != ngx_stderr) {  
  559.   
  560.         if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {  
  561.             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,  
  562.                           ngx_set_stderr_n " failed");  
  563.         }  
  564.     }  
  565.   
  566.     pool->log = cycle->log;  
  567.   
  568.     //调用所有模块的init_module方法  
  569.     for (i = 0; ngx_modules[i]; i++) {  
  570.         if (ngx_modules[i]->init_module) {  
  571.             if (ngx_modules[i]->init_module(cycle) != NGX_OK) {  
  572.                 /* fatal */  
  573.                 exit(1);  
  574.             }  
  575.         }  
  576.     }  
  577.   
  578.     return NULL;  
  579. }  

0 0
原创粉丝点击