前言
在分析源码时,我们可以先把握主干,然后其他部分再挨个分析就行了。接下来我们先看看nginx的main
函数干了些什么。
main函数
这里先介绍一些下面会遇到的变量类型:
ngx_int_t
: typedef intptr_t ngx_int_t;
64位机器上,intptr_t
为long int
, 即typedef long int intptr_t;
在32位机器上,intptr_t
为int
,即typedef int intptr_t
。
ngx_log_t
: typedef struct ngx_log_s ngx_log_t
,struct ngx_log_s
存储了日志文件的各种信息
ngx_cycle_t
是很重要的结构体,等下列出它的定义。
ngx_core_conf_t
是core模块存储配置项的结构体。
ngx_core_conf_t
是core模块存储配置项的结构体。
/* src/core/nginx.c */int ngx_cdecl
main(int argc, char *const *argv)
{ngx_int_t i;ngx_log_t *log;ngx_cycle_t *cycle, init_cycle;ngx_core_conf_t *ccf;#if (NGX_FREEBSD)ngx_debug_init();
#endifif (ngx_strerror_init() != NGX_OK) {return 1;}//处理参数,设置对应的标识位等if (ngx_get_options(argc, argv) != NGX_OK) {return 1;}//根据标识位显示版本、测试配置等if (ngx_show_version) {ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);if (ngx_show_help) {ngx_write_stderr("Usage: nginx [-?hvVtq] [-s signal] [-c filename] ""[-p prefix] [-g directives]" NGX_LINEFEEDNGX_LINEFEED"Options:" NGX_LINEFEED" -?,-h : this help" NGX_LINEFEED" -v : show version and exit" NGX_LINEFEED" -V : show version and configure options then exit"NGX_LINEFEED" -t : test configuration and exit" NGX_LINEFEED" -q : suppress non-error messages ""during configuration testing" NGX_LINEFEED" -s signal : send signal to a master process: ""stop, quit, reopen, reload" NGX_LINEFEED
#ifdef NGX_PREFIX" -p prefix : set prefix path (default: "NGX_PREFIX ")" NGX_LINEFEED
#else" -p prefix : set prefix path (default: NONE)" NGX_LINEFEED
#endif" -c filename : set configuration file (default: "NGX_CONF_PATH ")" NGX_LINEFEED" -g directives : set global directives out of configuration ""file" NGX_LINEFEED NGX_LINEFEED);}if (ngx_show_configure) {ngx_write_stderr(
#ifdef NGX_COMPILER"built by " NGX_COMPILER NGX_LINEFEED
#endif
#if (NGX_SSL)
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME"TLS SNI support enabled" NGX_LINEFEED
#else"TLS SNI support disabled" NGX_LINEFEED
#endif
#endif"configure arguments:" NGX_CONFIGURE NGX_LINEFEED);}if (!ngx_test_config) {return 0;}}/* TODO */ ngx_max_sockets = -1;//初始化并更新时间ngx_time_init();#if (NGX_PCRE)ngx_regex_init();
#endif//获取nginx的进程号ngx_pid = ngx_getpid();//初始化日志log = ngx_log_init(ngx_prefix);if (log == NULL) {return 1;}/* STUB */
#if (NGX_OPENSSL)ngx_ssl_init(log);
#endif/** init_cycle->log is required for signal handlers and* ngx_process_options()*//* 初始化ngx_cycle* #define ngx_memzero(buf, n)(void) memset(buf, 0, n)*/ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));init_cycle.log = log;ngx_cycle = &init_cycle;//分配内存池init_cycle.pool = ngx_create_pool(1024, log);if (init_cycle.pool == NULL) {return 1;}//将命令行参数保存到init_cycle结构体中if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {return 1;}//用运行nginx时可能携带的目录参数来初始化cycleif (ngx_process_options(&init_cycle) != NGX_OK) {return 1;}//提取当前操作系统的一些信息if (ngx_os_init(log) != NGX_OK) {return 1;}/** ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()*///初始化一个循环冗余校验的表//后续的循环冗余校验可以直接查表if (ngx_crc32_table_init() != NGX_OK) {return 1;}//这个函数是为了执行不重启服务升级nginx做准备//通过环境变量NGINX_VAR将老的nginx进程需要监听的端口传给新的nginx进程,这样就可以读取平滑升级的信息了if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {return 1;}//对所有模块进行编号//ngx_modules数组在编译时已经生成了ngx_max_module = 0;for (i = 0; ngx_modules[i]; i++) {ngx_modules[i]->index = ngx_max_module++;}//大部分的初始化工作都在ngx_init_cycle中进行cycle = ngx_init_cycle(&init_cycle);if (cycle == NULL) {if (ngx_test_config) {ngx_log_stderr(0, "configuration file %s test failed",init_cycle.conf_file.data);}return 1;}if (ngx_test_config) {if (!ngx_quiet_mode) {ngx_log_stderr(0, "configuration file %s test is successful",cycle->conf_file.data);}return 0;}if (ngx_signal) {return ngx_signal_process(cycle, ngx_signal);}ngx_os_status(cycle->log);ngx_cycle = cycle;//获取存储ngx_core_module模块感兴趣的配置项的结构体指针ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);//#define NGX_PROCESS_SINGLE 0 //#define NGX_PROCESS_MASTER 1 //#define NGX_PROCESS_SIGNALLER 2 //#define NGX_PROCESS_WORKER 3 //#define NGX_PROCESS_HELPER 4 //ccf-master若设置为on(1),并且ngx_process设置为单进程模式时//此时由master进程设置的模式为准,为NGX_PROCESS_MASTERif (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {ngx_process = NGX_PROCESS_MASTER;}#if !(NGX_WIN32)//完成对信号的注册等工作if (ngx_init_signals(cycle->log) != NGX_OK) {return 1;}//若没有继承sockets,并且设置了守护进程标识位//则创建守护进程if (!ngx_inherited && ccf->daemon) {if (ngx_daemon(cycle->log) != NGX_OK) {return 1;}ngx_daemonized = 1;}if (ngx_inherited) {ngx_daemonized = 1;}#endif//创建进程记录文件if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {return 1;}if (cycle->log->file->fd != ngx_stderr) {if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,ngx_set_stderr_n " failed");return 1;}}if (log->file->fd != ngx_stderr) {if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,ngx_close_file_n " built-in log failed");}}ngx_use_stderr = 0;//进入进程主循环//单进程方式运行nginxif (ngx_process == NGX_PROCESS_SINGLE) {ngx_single_process_cycle(cycle);} else {//以master-worker方式运行nginxngx_master_process_cycle(cycle);}return 0;
}
main
函数的代码逻辑大概就是这样,下面介绍一下ngx_cycle_t
结构体,然后分析下ngx_init_cycle
函数。