本文主要是介绍nginx0.1.0之event模块初始化源码分析(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下面是nginx中几个重要的数据结构。
struct ngx_command_s {ngx_str_t name; // 命令名字int type; // 命令的属性,参数个数、上下文等char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); // 处理该命令的函数int conf;int offset;void *post;
};struct {ngx_str_t name;void *(*create_conf)(ngx_cycle_t *cycle);char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
} ngx_core_module_t; struct ngx_module_s {ngx_uint_t ctx_index;ngx_uint_t index;void *ctx;ngx_command_t *commands;ngx_uint_t type;ngx_int_t (*init_module)(ngx_cycle_t *cycle);ngx_int_t (*init_process)(ngx_cycle_t *cycle);
#if 0ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
#endif
};
下面是event模块的配置
static ngx_command_t ngx_events_commands[] = {{ ngx_string("events"),NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,ngx_events_block,0,0,NULL },ngx_null_command
};static ngx_core_module_t ngx_events_module_ctx = {ngx_string("events"),NULL,NULL
}; ngx_module_t ngx_events_module = {NGX_MODULE,&ngx_events_module_ctx, /* module context */ngx_events_commands, /* module directives */NGX_CORE_MODULE, /* module type */NULL, /* init module */NULL /* init process */
};
nginx模块初始化的流程在下面的代码中,核心模块的初始化,各核心模块首先在create_conf中创建保存配置的数据结构,然后在ngx_conf_parse中,通过解析命令,执行对应的命令处理函数,完成赋值和各核心模块的子模块初始化。最后,如果在ngx_conf_parse时,没有设置值,则执行init_conf函数进行默认初始化。
// 执行核心模块的钩子函数,该版本只有ngx_core_module模块定义了这个钩子for (i = 0; ngx_modules[i]; i++) {if (ngx_modules[i]->type != NGX_CORE_MODULE) {continue;}module = ngx_modules[i]->ctx;// 用于存储各类型模块下子模块的配置if (module->create_conf) {// 分配一块内存存储子模块的数据结构,如ngx_core_module_create_conf函数rv = module->create_conf(cycle);if (rv == NGX_CONF_ERROR) {ngx_destroy_pool(pool);return NULL;}cycle->conf_ctx[ngx_modules[i]->index] = rv;}}// 初始化保存指令信息的结构体ngx_memzero(&conf, sizeof(ngx_conf_t));/* STUB: init array ? */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;conf.cycle = cycle;conf.pool = pool;conf.log = log;conf.module_type = NGX_CORE_MODULE;conf.cmd_type = NGX_MAIN_CONF;// 解析配置文件,把结果写入confif (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {ngx_destroy_pool(pool);return NULL;}for (i = 0; ngx_modules[i]; i++) {if (ngx_modules[i]->type != NGX_CORE_MODULE) {continue;}module = ngx_modules[i]->ctx;// 如ngx_core_module_init_conf函数if (module->init_conf) {// cycle->conf_ctx[ngx_modules[i]->index]由crete_init函数创建if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])== NGX_CONF_ERROR){ngx_destroy_pool(pool);return NULL;}}}
从上面的代码中我们知道event模块没有实现create_conf和init_conf函数,那么event模块初始化的时机是什么时候?答案是在解析到event命令的时候。由event模块对应的配置可知,当解析到event命令的时候,会执行ngx_events_block函数。代码如下。
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{int m;char *rv;void ***ctx;ngx_conf_t pcf;ngx_event_module_t *module;/* count the number of the event modules and set up their indices */ngx_event_max_module = 0;// 初始化每个子模块的序号for (m = 0; ngx_modules[m]; m++) {if (ngx_modules[m]->type != NGX_EVENT_MODULE) {continue;}ngx_modules[m]->ctx_index = ngx_event_max_module++;}// ctx指向一个指针ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR);// ctx指向的指针再指向一个指针数组ngx_test_null(*ctx,ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)),NGX_CONF_ERROR);// event是NGX_MAIN_CONF类型的模块,conf为四级指针*(void **) conf = ctx;for (m = 0; ngx_modules[m]; m++) {if (ngx_modules[m]->type != NGX_EVENT_MODULE) {continue;}// event类型的模块的ctxmodule = ngx_modules[m]->ctx;// 把create_conf返回的数据结构存储在上面开辟的数组里if (module->create_conf) {ngx_test_null((*ctx)[ngx_modules[m]->ctx_index],module->create_conf(cf->cycle),NGX_CONF_ERROR);}}pcf = *cf;// 修改当前的上下文和作用域信息cf->ctx = ctx;cf->module_type = NGX_EVENT_MODULE;cf->cmd_type = NGX_EVENT_CONF;// 继续解析,对event模块的子模块的字段进行赋值rv = ngx_conf_parse(cf, NULL);*cf = pcf;if (rv != NGX_CONF_OK)return rv;// 如果ngx_conf_parse没有进行赋值,则执行init_conf函数时进行默认初始化for (m = 0; ngx_modules[m]; m++) {if (ngx_modules[m]->type != NGX_EVENT_MODULE) {continue;}module = ngx_modules[m]->ctx;// 初始化create_conf创建的结构体if (module->init_conf) {// 如果在ngx_conf_parse里没有对模块的配置进行初始化则这里进行默认初始化,一般是在cmd的set函数进行初始化rv = module->init_conf(cf->cycle,(*ctx)[ngx_modules[m]->ctx_index]);if (rv != NGX_CONF_OK) {return rv;}}}return NGX_CONF_OK;
}
这篇关于nginx0.1.0之event模块初始化源码分析(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!