Nginx源码阅读(main)

原帖地址:https://blog.csdn.net/hz5034/article/details/54630909

main()执行流程

这里写图片描述

main()代码解析

/* src/core/nginx.c */

int ngx_cdecl // #define ngx_cdecl,一个空的define,跨平台支持
main(int argc, char *const *argv) { ngx_buf_t *b; ngx_log_t *log; ngx_uint_t i; ngx_cycle_t *cycle, init_cycle; ngx_conf_dump_t *cd; ngx_core_conf_t *ccf; ngx_debug_init(); // 在Linux系统中,#define ngx_debug_init(),一个空的define /* 创建一个包含NGX_SYS_NERR个ngx_str_t的数组ngx_sys_errlist,用于记录错误码[0,NGX_SYS_NERR-1]对应的错误信息 NGX_SYS_NERR定义在objs/ngx_auto_config.h中,这是一个auto性质的文件,只有在执行了./configure后才能生成这个文件 在Linux系统中NGX_SYS_NERR=132,表示有132个错误码 */ if (ngx_strerror_init() != NGX_OK) { return 1; } // 解析命令行参数,对于-s,ngx_process = NGX_PROCESS_SIGNALLER; if (ngx_get_options(argc, argv) != NGX_OK) { return 1; } if (ngx_show_version) { ngx_show_version_info(); if (!ngx_test_config) { return 0; } } /* TODO */ ngx_max_sockets = -1; // extern ngx_int_t ngx_max_sockets; ngx_time_init(); // 初始化时间 #if (NGX_PCRE) ngx_regex_init(); // 若启用了PCRE(Perl Compatible Regular Expressions),则初始化正则表达式 #endif ngx_pid = ngx_getpid(); // #define ngx_getpid getpid log = ngx_log_init(ngx_prefix); // 初始化日志 if (log == NULL) { return 1; } /* STUB */ #if (NGX_OPENSSL) ngx_ssl_init(log); // 若启用了NGX_OPENSSL,则初始化ssl #endif /* * init_cycle->log is required for signal handlers and * ngx_process_options() */ ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); // 清零init_cycle init_cycle.log = log; ngx_cycle = &init_cycle; // volatile ngx_cycle_t *ngx_cycle; init_cycle.pool = ngx_create_pool(1024, log); // 创建大小为1024B的内存池 if (init_cycle.pool == NULL) { return 1; } // 将命令行参数保存到全局变量ngx_argv中 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) { return 1; } // 初始化init_cycle的conf_prefix、prefix、conf_file、conf_param等 if (ngx_process_options(&init_cycle) != NGX_OK) { return 1; } // 初始化系统变量ngx_pagesize、ngx_cacheline_size、ngx_max_sockets等 if (ngx_os_init(log) != NGX_OK) { return 1; } /* * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init() */ // 初始化CRC(Cyclic Redundancy Check)表,缓存对齐 if (ngx_crc32_table_init() != NGX_OK) { return 1; } // 将环境变量NGINX中的socket保存到init_cycle的listening数组中 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) { return 1; } // 遍历全局数组ngx_modules,根据各模块在ngx_modules中的顺序,设置各模块的index if (ngx_preinit_modules() != NGX_OK) { return 1; } // 初始化init_cycle,详见数据结构ngx_cycle_t 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); } if (ngx_dump_config) { cd = cycle->config_dump.elts; for (i = 0; i < cycle->config_dump.nelts; i++) { ngx_write_stdout("# configuration file "); (void) ngx_write_fd(ngx_stdout, cd[i].name.data, cd[i].name.len); ngx_write_stdout(":" NGX_LINEFEED); b = cd[i].buffer; (void) ngx_write_fd(ngx_stdout, b->pos, b->last - b->pos); ngx_write_stdout(NGX_LINEFEED); } } return 0; } // 若有信号,则处理信号 if (ngx_signal) { return ngx_signal_process(cycle, ngx_signal); } ngx_os_status(cycle->log); ngx_cycle = cycle; // ccf指向存储ngx_core_module配置项的数据结构 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); /* 在ngx_core_module_create_conf()中,ccf->master初始化为-1; 在ngx_core_module_init_conf()中,若ccf->master == -1,则ccf->master = 1,否则ccf->master不变; ngx_process是一个未初始化的全局变量,默认值是0(即NGX_PROCESS_SINGLE) */ if (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; } // 若无继承socket,且设置了守护进程标识,则调用ngx_daemon()创建守护进程。 if (!ngx_inherited && ccf->daemon) { if (ngx_daemon(cycle->log) != NGX_OK) { return 1; } ngx_daemonized = 1; } if (ngx_inherited) { ngx_daemonized = 1; } #endif // 创建pid文件 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { return 1; } if (ngx_log_redirect_stderr(cycle) != NGX_OK) { 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; if (ngx_process == NGX_PROCESS_SINGLE) { // 单进程工作模式:系统中只有一个进程,该进程既是master进程,也是worker进程 ngx_single_process_cycle(cycle); } else { // 多进程工作模式:系统中有一个master进程,多个worker进程 ngx_master_process_cycle(cycle); } return 0; }

猜你喜欢

转载自www.cnblogs.com/blackhumour2018/p/9427822.html