1 int ngx_cdecl 2 main(int argc, char *const *argv) 3 { 4 ngx_buf_t *b; 5 ngx_log_t *log; 6 ngx_uint_t i; 7 ngx_cycle_t *cycle, init_cycle; 8 ngx_conf_dump_t *cd; 9 ngx_core_conf_t *ccf; 10 11 ngx_debug_init(); 12 13 if (ngx_strerror_init() != NGX_OK) { 14 return 1; 15 } 16 17 //解析命令行参数 18 if (ngx_get_options(argc, argv) != NGX_OK) { 19 return 1; 20 } 21 22 //版本??? 23 if (ngx_show_version) { 24 ngx_show_version_info(); 25 26 if (!ngx_test_config) { 27 return 0; 28 } 29 } 30 31 /* TODO */ ngx_max_sockets = -1; 32 33 //初始化并更新时间 34 ngx_time_init(); 35 36 #if (NGX_PCRE) 37 //PCRE初始化 38 ngx_regex_init(); 39 #endif 40 41 //获取进程pid 42 ngx_pid = ngx_getpid(); 43 ngx_parent = ngx_getppid(); 44 45 //初始化log 46 log = ngx_log_init(ngx_prefix); 47 if (log == NULL) { 48 return 1; 49 } 50 51 /* STUB */ 52 #if (NGX_OPENSSL) 53 ngx_ssl_init(log); 54 #endif 55 56 /* 57 * init_cycle->log is required for signal handlers and 58 * ngx_process_options() 59 */ 60 61 //初始化init_cycle全局变量 62 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); 63 init_cycle.log = log; 64 ngx_cycle = &init_cycle; 65 66 //初始化内存池 67 init_cycle.pool = ngx_create_pool(1024, log); 68 if (init_cycle.pool == NULL) { 69 return 1; 70 } 71 //存储命令行参数到内存池 72 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) { 73 return 1; 74 } 75 76 //存储程序、配置文件的路径以及conf参数 77 if (ngx_process_options(&init_cycle) != NGX_OK) { 78 return 1; 79 } 80 81 //初始化系统相关变量,内存页面大小ngx_pagesize,ngx_cacheline_size,最大连接数ngx_max_sockets等 82 if (ngx_os_init(log) != NGX_OK) { 83 return 1; 84 } 85 86 /* 87 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init() 88 */ 89 90 //初始化循环冗余校验表 91 if (ngx_crc32_table_init() != NGX_OK) { 92 return 1; 93 } 94 95 /* 96 * ngx_slab_sizes_init() requires ngx_pagesize set in ngx_os_init() 97 */ 98 99 // 初始化ngx_slab的一些参数 100 ngx_slab_sizes_init(); 101 // ngx_slab_max_size = ngx_pagesize / 2; 102 // ngx_slab_exact_size = ngx_pagesize / (8 * sizeof(uintptr_t)); 103 104 //热启动平滑过渡,继承原来的套接字 105 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) { 106 return 1; 107 } 108 109 //初始化模块 110 // 设置每个module的index和name 111 if (ngx_preinit_modules() != NGX_OK) { 112 return 1; 113 } 114 115 //初始化cycle 116 cycle = ngx_init_cycle(&init_cycle); 117 if (cycle == NULL) { 118 // cycle->log 失效, 只能使用stderr 119 if (ngx_test_config) { 120 ngx_log_stderr(0, "configuration file %s test failed", 121 init_cycle.conf_file.data); 122 } 123 124 return 1; 125 } 126 127 if (ngx_test_config) { 128 if (!ngx_quiet_mode) { 129 ngx_log_stderr(0, "configuration file %s test is successful", 130 cycle->conf_file.data); 131 } 132 133 if (ngx_dump_config) { 134 cd = cycle->config_dump.elts; 135 136 for (i = 0; i < cycle->config_dump.nelts; i++) { 137 138 ngx_write_stdout("# configuration file "); 139 (void) ngx_write_fd(ngx_stdout, cd[i].name.data, 140 cd[i].name.len); 141 ngx_write_stdout(":" NGX_LINEFEED); 142 143 b = cd[i].buffer; 144 145 (void) ngx_write_fd(ngx_stdout, b->pos, b->last - b->pos); 146 ngx_write_stdout(NGX_LINEFEED); 147 } 148 } 149 150 return 0; 151 } 152 153 //处理信号 154 if (ngx_signal) { 155 return ngx_signal_process(cycle, ngx_signal); 156 } 157 158 159 ngx_os_status(cycle->log); 160 161 ngx_cycle = cycle; 162 163 //获取核心模块配置文件指针 164 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 165 166 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) { 167 ngx_process = NGX_PROCESS_MASTER; 168 } 169 170 #if !(NGX_WIN32) 171 172 if (ngx_init_signals(cycle->log) != NGX_OK) { 173 return 1; 174 } 175 176 if (!ngx_inherited && ccf->daemon) { 177 if (ngx_daemon(cycle->log) != NGX_OK) { 178 return 1; 179 } 180 181 ngx_daemonized = 1; 182 } 183 184 if (ngx_inherited) { 185 ngx_daemonized = 1; 186 } 187 188 #endif 189 190 //创建pid文件 191 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { 192 return 1; 193 } 194 195 if (ngx_log_redirect_stderr(cycle) != NGX_OK) { 196 return 1; 197 } 198 199 if (log->file->fd != ngx_stderr) { 200 if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) { 201 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 202 ngx_close_file_n " built-in log failed"); 203 } 204 } 205 206 ngx_use_stderr = 0; 207 208 //创建子进程 209 if (ngx_process == NGX_PROCESS_SINGLE) { 210 ngx_single_process_cycle(cycle); 211 212 } else { 213 ngx_master_process_cycle(cycle); 214 } 215 216 return 0; 217 }