ngx_core_module模块的初始化

来源:互联网 发布:北欧家具知乎 编辑:程序博客网 时间:2024/04/29 04:33
这个应该是要分析的第一个模块吧,首先看该模块的定义(src/core/nginx.c):
static ngx_core_module_t  ngx_core_module_ctx = {    ngx_string("core"),    ngx_core_module_create_conf,   //创建配置的函数    ngx_core_module_init_conf     //初始化配置的函数};ngx_module_t  ngx_core_module = {    NGX_MODULE_V1,    &ngx_core_module_ctx,                  /* module context */   //模块的上下文    ngx_core_commands,                     /* module directives */  //模块的一些命令    NGX_CORE_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    NULL,                                  /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};
嗯,一看就知道该模块是属于core模块类型,前一篇文章已经知道,每一个一个core模块在ngx_init_cycle当中都会首先调用create_conf函数创建该模块的配置,然后调用init_conf函数初始化配置,这里我们首先看core模块的配置结构的定义(src/core/ngx_cycle.h):
  //core类型的模块的配置结构的定义typedef struct {     ngx_flag_t               daemon;     ngx_flag_t               master;     ngx_msec_t               timer_resolution;     ngx_int_t                worker_processes;     ngx_int_t                debug_points;     ngx_int_t                rlimit_nofile;     ngx_int_t                rlimit_sigpending;     off_t                    rlimit_core;     int                      priority;     ngx_uint_t               cpu_affinity_n;     uint64_t                *cpu_affinity;     char                    *username;  //用户名     ngx_uid_t                user;   //用户id     ngx_gid_t                group;   //组id     ngx_str_t                working_directory;     ngx_str_t                lock_file;     ngx_str_t                pid;     ngx_str_t                oldpid;     ngx_array_t              env;     char                   **environment;#if (NGX_THREADS)     ngx_int_t                worker_threads;     size_t                   thread_stack_size;#endif} ngx_core_conf_t;
首先ngx_core_module模块会调用create_conf函数来创建该配置结构,并会将其保存到ngx_cycle的conf_ctx相应的区域中,该函数对应的是ngx_core_module_create_conf函数(src/core/nginx.c):
static void *ngx_core_module_create_conf(ngx_cycle_t *cycle){    ngx_core_conf_t  *ccf;  //申明配置结构的指针    ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));  //在全局变量ngx_cycle的内存池中分配配置结构的内存    if (ccf == NULL) {        return NULL;    }    /*     * set by ngx_pcalloc()     *     *     ccf->pid = NULL;     *     ccf->oldpid = NULL;     *     ccf->priority = 0;     *     ccf->cpu_affinity_n = 0;     *     ccf->cpu_affinity = NULL;     */    ccf->daemon = NGX_CONF_UNSET;    ccf->master = NGX_CONF_UNSET;    ccf->timer_resolution = NGX_CONF_UNSET_MSEC;    ccf->worker_processes = NGX_CONF_UNSET;    ccf->debug_points = NGX_CONF_UNSET;    ccf->rlimit_nofile = NGX_CONF_UNSET;    ccf->rlimit_core = NGX_CONF_UNSET;    ccf->rlimit_sigpending = NGX_CONF_UNSET;    ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;    ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;#if (NGX_THREADS)    ccf->worker_threads = NGX_CONF_UNSET;    ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;#endif    if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))        != NGX_OK)    {        return NULL;    }    return ccf;}

在ngx_init_cycle函数中,我们可以知道core类型在调用create_conf之后,会接着调用init_conf函数来具体初始化具体的配置结构的一些信息。ngx_core_module模块也就会调用相应的ngx_core_module_init_conf函数来初始化其配置结构的相应的信息,函数主要分一些步骤:

(1)初始化daemon,master等,采用的是直接赋值的方式:

ngx_conf_init_value(ccf->daemon, 1);    ngx_conf_init_value(ccf->master, 1);    ngx_conf_init_msec_value(ccf->timer_resolution, 0);    ngx_conf_init_value(ccf->worker_processes, 1);    ngx_conf_init_value(ccf->debug_points, 0);
(2)初始化pid以及oldpid

(3)初始化user,username,usergroup

(4)初始化lock_file