nginx源码阅读(二).初始化:main函数及ngx_init_cycle函数
生活随笔
收集整理的這篇文章主要介紹了
nginx源码阅读(二).初始化:main函数及ngx_init_cycle函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
在分析源碼時,我們可以先把握主干,然后其他部分再挨個分析就行了。接下來我們先看看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函數。
總結
以上是生活随笔為你收集整理的nginx源码阅读(二).初始化:main函数及ngx_init_cycle函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 龙票剧情介绍
- 下一篇: Linux下send错误代码32