Nginx源码分析3:模块初始化

直接跟代码 :main ->     ngx_preinit_modules(对模块名进行复制) ->        ngx_init_cycle(讲模块对象复制到cycle里面) -> ngx_cycle_modules
   ngx_master_process_cycle ->       ngx_start_worker_processes ->           ngx_worker_process_cycle ->                 ngx_worker_process_init ->                    cycle->modules[i]->init_process(cycle) (每个work进程中初始化这些模块)
1、ngx_preinit_modules    初始化所有模块,并对模块进行编号    ngx_int_ ngx_preinit_modules(void)    {        ngx_uint_t  i;
        for (i = 0; ngx_modules[i]; i++) {            ngx_modules[i]->index = i;            ngx_modules[i]->name = ngx_module_names[i];  //见下面ngx_module_names分析        }
        ngx_modules_n = i;        ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES;
        return NGX_OK;    }
    ngx_module_names分析 :        定义在ngx_module.h中        extern char          *ngx_module_names[];     //一个外部定义                生成原理 :             1. 具体的模块可通过编译前的configure命令进行配置,即设置哪些模块需要编译,哪些不被编译。                当编译的时候,会生成ngx_modules.c的文件,里面就包含模块数组。            2. 新增模块或者减少模块可以在configure命令执行前 auto/modules文件里面修改。             生成的objs/ngx_modules.c文件如下:                         #include <ngx_config.h>                  #include <ngx_core.h>                                  extern ngx_module_t ngx_core_module;                  extern ngx_module_t ngx_errlog_module;                  extern ngx_module_t ngx_conf_module;                  extern ngx_module_t ngx_events_module;                  extern ngx_module_t ngx_event_core_module;                  extern ngx_module_t ngx_epoll_module;                  extern ngx_module_t ngx_http_module;                  ......
                ngx_module_t *ngx_modules[] = {                    &ngx_core_module,                    &ngx_errlog_module,                    &ngx_conf_module,                    &ngx_openssl_module,                    &ngx_regex_module,                    &ngx_events_module,                    &ngx_event_core_module,                    &ngx_epoll_module,                    .....                }
                char *ngx_module_names[] = {                    "ngx_core_module",                    "ngx_errlog_module",                    "ngx_conf_module",                    "ngx_openssl_module",                    "ngx_regex_module",                    "ngx_events_module",1                    "ngx_event_core_module",                    "ngx_epoll_module",                    "ngx_http_modul                    .....                }
2、ngx_init_cycle(讲模块对象复制到cycle里面) -> ngx_cycle_modules     初始化cycle,并初始化模块        ngx_int_t 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;            }
                //在work进程初始化中, 会调用cycle->modules模块的初始化函数            ngx_memcpy(cycle->modules, ngx_modules,                       ngx_modules_n * sizeof(ngx_module_t *));
            cycle->modules_n = ngx_modules_n;
            return NGX_OK;        }
3、work进程中init_process模块        ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)        {                        .......                         for (i = 0; cycle->modules[i]; i++) {                if (cycle->modules[i]->init_process) {                    if (cycle->modules[i]->init_process(cycle) == NGX_ERROR) {                        /* fatal */                        exit(2);                    }                }            }        }    
文章来源: Nginx源码分析3:模块初始化

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Nginx源码分析3:模块初始化