亲们,有谁知道扑克牌游戏2人玩法,告诉一下我呗!😜😳

Nginx的启动过程
  主要介绍Nginx的启动过程,可以在/core/nginx.c中找到Nginx的主函数main(),那么就从这里开始分析Nginx的启动过程。
涉及到的基本函数
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
7 #include &ngx_config.h&
8 #include &ngx_core.h&
9 #include &nginx.h&
12 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
13 static ngx_int_t ngx_get_options(int argc, char *const *argv);
14 static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
15 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
16 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
17 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
18 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
19 static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
20 static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
21 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
23 static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
27 static ngx_conf_enum_t
ngx_debug_points[] = {
{ ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
{ ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
{ ngx_null_string, 0 }
34 static ngx_command_t
ngx_core_commands[] = {
{ ngx_string("daemon"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
offsetof(ngx_core_conf_t, daemon),
{ ngx_string("master_process"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
offsetof(ngx_core_conf_t, master),
{ ngx_string("timer_resolution"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
offsetof(ngx_core_conf_t, timer_resolution),
{ ngx_string("pid"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
offsetof(ngx_core_conf_t, pid),
{ ngx_string("lock_file"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
offsetof(ngx_core_conf_t, lock_file),
{ ngx_string("worker_processes"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_set_worker_processes,
{ ngx_string("debug_points"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_enum_slot,
offsetof(ngx_core_conf_t, debug_points),
&ngx_debug_points },
{ ngx_string("user"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
ngx_set_user,
{ ngx_string("worker_priority"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_set_priority,
{ ngx_string("worker_cpu_affinity"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
ngx_set_cpu_affinity,
{ ngx_string("worker_rlimit_nofile"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
offsetof(ngx_core_conf_t, rlimit_nofile),
{ ngx_string("worker_rlimit_core"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_off_slot,
offsetof(ngx_core_conf_t, rlimit_core),
{ ngx_string("worker_rlimit_sigpending"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
offsetof(ngx_core_conf_t, rlimit_sigpending),
{ ngx_string("working_directory"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
offsetof(ngx_core_conf_t, working_directory),
{ ngx_string("env"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_set_env,
141 #if (NGX_THREADS)
{ ngx_string("worker_threads"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
offsetof(ngx_core_conf_t, worker_threads),
{ ngx_string("thread_stack_size"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
offsetof(ngx_core_conf_t, thread_stack_size),
157 #endif
ngx_null_command
163 static ngx_core_module_t
ngx_core_module_ctx = {
ngx_string("core"),
ngx_core_module_create_conf,
ngx_core_module_init_conf
170 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 */
/* init master */
/* init module */
/* init process */
/* init thread */
/* exit thread */
/* exit process */
/* exit master */
NGX_MODULE_V1_PADDING
186 ngx_uint_t
188 static ngx_uint_t
189 static ngx_uint_t
190 static ngx_uint_t
191 static u_char
192 static u_char
*ngx_conf_
193 static u_char
*ngx_conf_
194 static char
197 static char **ngx_os_
200 int ngx_cdecl
201 main(int argc, char *const *argv)
ngx_cycle_t
*cycle, init_
ngx_core_conf_t
ngx_debug_init();
if (ngx_strerror_init() != NGX_OK) {
if (ngx_get_options(argc, argv) != NGX_OK) {
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_LINEFEED
NGX_LINEFEED
"Options:" NGX_LINEFEED
: this help" NGX_LINEFEED
: show version and exit" NGX_LINEFEED
: show version and configure options then exit"
NGX_LINEFEED
: test configuration and exit" NGX_LINEFEED
: suppress non-error messages "
"during configuration testing" NGX_LINEFEED
: send signal to a master process: "
"stop, quit, reopen, reload" NGX_LINEFEED
236 #ifdef NGX_PREFIX
: set prefix path (default: "
NGX_PREFIX ")" NGX_LINEFEED
: set prefix path (default: NONE)" NGX_LINEFEED
241 #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(
251 #ifdef NGX_COMPILER
"built by " NGX_COMPILER NGX_LINEFEED
253 #endif
254 #if (NGX_SSL)
255 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
"TLS SNI support enabled" NGX_LINEFEED
"TLS SNI support disabled" NGX_LINEFEED
259 #endif
260 #endif
"configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
if (!ngx_test_config) {
/* TODO */ ngx_max_sockets = -1;
ngx_time_init();
273 #if (NGX_PCRE)
ngx_regex_init();
275 #endif
ngx_pid = ngx_getpid();
log = ngx_log_init(ngx_prefix);
if (log == NULL) {
/* STUB */
285 #if (NGX_OPENSSL)
ngx_ssl_init(log);
287 #endif
* init_cycle-&log is required for signal handlers and
* ngx_process_options()
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
init_cycle.log =
ngx_cycle = &init_
init_cycle.pool = ngx_create_pool(1024, log);
if (init_cycle.pool == NULL) {
if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
if (ngx_process_options(&init_cycle) != NGX_OK) {
if (ngx_os_init(log) != NGX_OK) {
* ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
if (ngx_crc32_table_init() != NGX_OK) {
if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]-&index = ngx_max_module++;
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);
if (ngx_test_config) {
if (!ngx_quiet_mode) {
ngx_log_stderr(0, "configuration file %s test is successful",
cycle-&conf_file.data);
if (ngx_signal) {
return ngx_signal_process(cycle, ngx_signal);
ngx_os_status(cycle-&log);
ngx_cycle =
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle-&conf_ctx, ngx_core_module);
if (ccf-&master && ngx_process == NGX_PROCESS_SINGLE) {
ngx_process = NGX_PROCESS_MASTER;
365 #if !(NGX_WIN32)
if (ngx_init_signals(cycle-&log) != NGX_OK) {
if (!ngx_inherited && ccf-&daemon) {
if (ngx_daemon(cycle-&log) != NGX_OK) {
ngx_daemonized = 1;
if (ngx_inherited) {
ngx_daemonized = 1;
383 #endif
if (ngx_create_pidfile(&ccf-&pid, cycle-&log) != NGX_OK) {
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");
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) {
ngx_single_process_cycle(cycle);
ngx_master_process_cycle(cycle);
418 static ngx_int_t
419 ngx_add_inherited_sockets(ngx_cycle_t *cycle)
ngx_listening_t
inherited = (u_char *) getenv(NGINX_VAR);
if (inherited == NULL) {
return NGX_OK;
ngx_log_error(NGX_LOG_NOTICE, cycle-&log, 0,
"using inherited sockets from \"%s\"", inherited);
if (ngx_array_init(&cycle-&listening, cycle-&pool, 10,
sizeof(ngx_listening_t))
!= NGX_OK)
return NGX_ERROR;
for (p = inherited, v = *p; p++) {
if (*p == ':' || *p == ';') {
s = ngx_atoi(v, p - v);
if (s == NGX_ERROR) {
ngx_log_error(NGX_LOG_EMERG, cycle-&log, 0,
"invalid socket number \"%s\" in " NGINX_VAR
" environment variable, ignoring the rest"
" of the variable", v);
v = p + 1;
ls = ngx_array_push(&cycle-&listening);
if (ls == NULL) {
return NGX_ERROR;
ngx_memzero(ls, sizeof(ngx_listening_t));
ls-&fd = (ngx_socket_t)
ngx_inherited = 1;
return ngx_set_inherited_sockets(cycle);
471 char **
472 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
ngx_uint_t
ngx_core_conf_t
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle-&conf_ctx, ngx_core_module);
if (last == NULL && ccf-&environment) {
return ccf-&
var = ccf-&env.
for (i = 0; i & ccf-&env. i++) {
if (ngx_strcmp(var[i].data, "TZ") == 0
|| ngx_strncmp(var[i].data, "TZ=", 3) == 0)
var = ngx_array_push(&ccf-&env);
if (var == NULL) {
return NULL;
var-&len = 2;
var-&data = (u_char *) "TZ";
var = ccf-&env.
505 tz_found:
for (i = 0; i & ccf-&env. i++) {
if (var[i].data[var[i].len] == '=') {
for (p = ngx_os_ *p; p++) {
if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
&& (*p)[var[i].len] == '=')
if (last) {
env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle-&log);
env = ngx_palloc(cycle-&pool, (n + 1) * sizeof(char *));
if (env == NULL) {
return NULL;
for (i = 0; i & ccf-&env. i++) {
if (var[i].data[var[i].len] == '=') {
env[n++] = (char *) var[i].
for (p = ngx_os_ *p; p++) {
if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
&& (*p)[var[i].len] == '=')
env[n++] = *p;
env[n] = NULL;
if (last == NULL) {
ccf-&environment =
570 ngx_pid_t
571 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
**env, *var;
ngx_uint_t
ngx_exec_ctx_
ngx_core_conf_t
ngx_listening_t
ngx_memzero(&ctx, sizeof(ngx_exec_ctx_t));
ctx.path = argv[0];
ctx.name = "new binary process";
ctx.argv =
env = ngx_set_environment(cycle, &n);
if (env == NULL) {
return NGX_INVALID_PID;
var = ngx_alloc(sizeof(NGINX_VAR)
+ cycle-&listening.nelts * (NGX_INT32_LEN + 1) + 2,
cycle-&log);
if (var == NULL) {
ngx_free(env);
return NGX_INVALID_PID;
p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
ls = cycle-&listening.
for (i = 0; i & cycle-&listening. i++) {
p = ngx_sprintf(p, "%", ls[i].fd);
*p = '\0';
env[n++] = var;
612 #if (NGX_SETPROCTITLE_USES_ENV)
/* allocate the spare 300 bytes for the new binary process title */
env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
622 #endif
env[n] = NULL;
626 #if (NGX_DEBUG)
for (e = *e; e++) {
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle-&log, 0, "env: %s", *e);
633 #endif
ctx.envp = (char *const *)
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle-&conf_ctx, ngx_core_module);
if (ngx_rename_file(ccf-&pid.data, ccf-&oldpid.data) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, cycle-&log, ngx_errno,
ngx_rename_file_n " %s to %s failed "
"before executing new binary process \"%s\"",
ccf-&pid.data, ccf-&oldpid.data, argv[0]);
ngx_free(env);
ngx_free(var);
return NGX_INVALID_PID;
pid = ngx_execute(cycle, &ctx);
if (pid == NGX_INVALID_PID) {
if (ngx_rename_file(ccf-&oldpid.data, ccf-&pid.data)
== NGX_FILE_ERROR)
ngx_log_error(NGX_LOG_ALERT, cycle-&log, ngx_errno,
ngx_rename_file_n " %s back to %s failed after "
"an attempt to execute new binary process \"%s\"",
ccf-&oldpid.data, ccf-&pid.data, argv[0]);
ngx_free(env);
ngx_free(var);
671 static ngx_int_t
672 ngx_get_options(int argc, char *const *argv)
for (i = 1; i & i++) {
p = (u_char *) argv[i];
if (*p++ != '-') {
ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
return NGX_ERROR;
while (*p) {
switch (*p++) {
ngx_show_version = 1;
ngx_show_help = 1;
ngx_show_version = 1;
ngx_show_version = 1;
ngx_show_configure = 1;
ngx_test_config = 1;
ngx_quiet_mode = 1;
ngx_prefix =
if (argv[++i]) {
ngx_prefix = (u_char *) argv[i];
ngx_log_stderr(0, "option \"-p\" requires directory name");
return NGX_ERROR;
ngx_conf_file =
if (argv[++i]) {
ngx_conf_file = (u_char *) argv[i];
ngx_log_stderr(0, "option \"-c\" requires file name");
return NGX_ERROR;
ngx_conf_params =
if (argv[++i]) {
ngx_conf_params = (u_char *) argv[i];
ngx_log_stderr(0, "option \"-g\" requires parameter");
return NGX_ERROR;
ngx_signal = (char *)
} else if (argv[++i]) {
ngx_signal = argv[i];
ngx_log_stderr(0, "option \"-s\" requires parameter");
return NGX_ERROR;
if (ngx_strcmp(ngx_signal, "stop") == 0
|| ngx_strcmp(ngx_signal, "quit") == 0
|| ngx_strcmp(ngx_signal, "reopen") == 0
|| ngx_strcmp(ngx_signal, "reload") == 0)
ngx_process = NGX_PROCESS_SIGNALLER;
ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
return NGX_ERROR;
ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
return NGX_ERROR;
return NGX_OK;
794 static ngx_int_t
795 ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
797 #if (NGX_FREEBSD)
ngx_os_argv = (char **)
ngx_argc =
ngx_argv = (char **)
ngx_os_argv = (char **)
ngx_argc =
ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle-&log);
if (ngx_argv == NULL) {
return NGX_ERROR;
for (i = 0; i & i++) {
len = ngx_strlen(argv[i]) + 1;
ngx_argv[i] = ngx_alloc(len, cycle-&log);
if (ngx_argv[i] == NULL) {
return NGX_ERROR;
(void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
ngx_argv[i] = NULL;
828 #endif
ngx_os_environ =
return NGX_OK;
836 static ngx_int_t
837 ngx_process_options(ngx_cycle_t *cycle)
if (ngx_prefix) {
len = ngx_strlen(ngx_prefix);
if (len && !ngx_path_separator(p[len - 1])) {
p = ngx_pnalloc(cycle-&pool, len + 1);
if (p == NULL) {
return NGX_ERROR;
ngx_memcpy(p, ngx_prefix, len);
p[len++] = '/';
cycle-&conf_prefix.len =
cycle-&conf_prefix.data =
cycle-&prefix.len =
cycle-&prefix.data =
863 #ifndef NGX_PREFIX
p = ngx_pnalloc(cycle-&pool, NGX_MAX_PATH);
if (p == NULL) {
return NGX_ERROR;
if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
return NGX_ERROR;
len = ngx_strlen(p);
p[len++] = '/';
cycle-&conf_prefix.len =
cycle-&conf_prefix.data =
cycle-&prefix.len =
cycle-&prefix.data =
886 #ifdef NGX_CONF_PREFIX
ngx_str_set(&cycle-&conf_prefix, NGX_CONF_PREFIX);
ngx_str_set(&cycle-&conf_prefix, NGX_PREFIX);
890 #endif
ngx_str_set(&cycle-&prefix, NGX_PREFIX);
893 #endif
if (ngx_conf_file) {
cycle-&conf_file.len = ngx_strlen(ngx_conf_file);
cycle-&conf_file.data = ngx_conf_
ngx_str_set(&cycle-&conf_file, NGX_CONF_PATH);
if (ngx_conf_full_name(cycle, &cycle-&conf_file, 0) != NGX_OK) {
return NGX_ERROR;
for (p = cycle-&conf_file.data + cycle-&conf_file.len - 1;
p & cycle-&conf_file.
if (ngx_path_separator(*p)) {
cycle-&conf_prefix.len = p - ngx_cycle-&conf_file.data + 1;
cycle-&conf_prefix.data = ngx_cycle-&conf_file.
if (ngx_conf_params) {
cycle-&conf_param.len = ngx_strlen(ngx_conf_params);
cycle-&conf_param.data = ngx_conf_
if (ngx_test_config) {
cycle-&log-&log_level = NGX_LOG_INFO;
return NGX_OK;
932 static void *
933 ngx_core_module_create_conf(ngx_cycle_t *cycle)
ngx_core_conf_t
ccf = ngx_pcalloc(cycle-&pool, sizeof(ngx_core_conf_t));
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;
966 #if (NGX_THREADS)
ccf-&worker_threads = NGX_CONF_UNSET;
ccf-&thread_stack_size = NGX_CONF_UNSET_SIZE;
969 #endif
if (ngx_array_init(&ccf-&env, cycle-&pool, 1, sizeof(ngx_str_t))
!= NGX_OK)
return NULL;
981 static char *
982 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
ngx_core_conf_t
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);
993 #if (NGX_HAVE_CPU_AFFINITY)
if (ccf-&cpu_affinity_n
&& ccf-&cpu_affinity_n != 1
&& ccf-&cpu_affinity_n != (ngx_uint_t) ccf-&worker_processes)
ngx_log_error(NGX_LOG_WARN, cycle-&log, 0,
"the number of \"worker_processes\" is not equal to "
"the number of \"worker_cpu_affinity\" masks, "
"using last mask for remaining worker processes");
1005 #endif
1007 #if (NGX_THREADS)
ngx_conf_init_value(ccf-&worker_threads, 0);
ngx_threads_n = ccf-&worker_
ngx_conf_init_size_value(ccf-&thread_stack_size, 2 * 1024 * 1024);
1013 #endif
if (ccf-&pid.len == 0) {
ngx_str_set(&ccf-&pid, NGX_PID_PATH);
if (ngx_conf_full_name(cycle, &ccf-&pid, 0) != NGX_OK) {
return NGX_CONF_ERROR;
ccf-&oldpid.len = ccf-&pid.len + sizeof(NGX_OLDPID_EXT);
ccf-&oldpid.data = ngx_pnalloc(cycle-&pool, ccf-&oldpid.len);
if (ccf-&oldpid.data == NULL) {
return NGX_CONF_ERROR;
ngx_memcpy(ngx_cpymem(ccf-&oldpid.data, ccf-&pid.data, ccf-&pid.len),
NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
1035 #if !(NGX_WIN32)
if (ccf-&user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
struct group
struct passwd
ngx_set_errno(0);
pwd = getpwnam(NGX_USER);
if (pwd == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle-&log, ngx_errno,
"getpwnam(\"" NGX_USER "\") failed");
return NGX_CONF_ERROR;
ccf-&username = NGX_USER;
ccf-&user = pwd-&pw_
ngx_set_errno(0);
grp = getgrnam(NGX_GROUP);
if (grp == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle-&log, ngx_errno,
"getgrnam(\"" NGX_GROUP "\") failed");
return NGX_CONF_ERROR;
ccf-&group = grp-&gr_
if (ccf-&lock_file.len == 0) {
ngx_str_set(&ccf-&lock_file, NGX_LOCK_PATH);
if (ngx_conf_full_name(cycle, &ccf-&lock_file, 0) != NGX_OK) {
return NGX_CONF_ERROR;
lock_file = cycle-&old_cycle-&lock_
if (lock_file.len) {
lock_file.len--;
if (ccf-&lock_file.len != lock_file.len
|| ngx_strncmp(ccf-&lock_file.data, lock_file.data, lock_file.len)
ngx_log_error(NGX_LOG_EMERG, cycle-&log, 0,
"\"lock_file\" could not be changed, ignored");
cycle-&lock_file.len = lock_file.len + 1;
lock_file.len += sizeof(".accept");
cycle-&lock_file.data = ngx_pstrdup(cycle-&pool, &lock_file);
if (cycle-&lock_file.data == NULL) {
return NGX_CONF_ERROR;
cycle-&lock_file.len = ccf-&lock_file.len + 1;
cycle-&lock_file.data = ngx_pnalloc(cycle-&pool,
ccf-&lock_file.len + sizeof(".accept"));
if (cycle-&lock_file.data == NULL) {
return NGX_CONF_ERROR;
ngx_memcpy(ngx_cpymem(cycle-&lock_file.data, ccf-&lock_file.data,
ccf-&lock_file.len),
".accept", sizeof(".accept"));
1110 #endif
return NGX_CONF_OK;
1116 static char *
1117 ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1119 #if (NGX_WIN32)
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"\"user\" is not supported, ignored");
return NGX_CONF_OK;
1126 #else
ngx_core_conf_t
struct passwd
struct group
if (ccf-&user != (uid_t) NGX_CONF_UNSET_UINT) {
return "is duplicate";
if (geteuid() != 0) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"the \"user\" directive makes sense only "
"if the master process runs "
"with super-user privileges, ignored");
return NGX_CONF_OK;
value = (ngx_str_t *) cf-&args-&
ccf-&username = (char *) value[1].
ngx_set_errno(0);
pwd = getpwnam((const char *) value[1].data);
if (pwd == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
"getpwnam(\"%s\") failed", value[1].data);
return NGX_CONF_ERROR;
ccf-&user = pwd-&pw_
group = (char *) ((cf-&args-&nelts == 2) ? value[1].data : value[2].data);
ngx_set_errno(0);
grp = getgrnam(group);
if (grp == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
"getgrnam(\"%s\") failed", group);
return NGX_CONF_ERROR;
ccf-&group = grp-&gr_
return NGX_CONF_OK;
1175 #endif
1179 static char *
1180 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_core_conf_t
*value, *var;
var = ngx_array_push(&ccf-&env);
if (var == NULL) {
return NGX_CONF_ERROR;
value = cf-&args-&
*var = value[1];
for (i = 0; i & value[1]. i++) {
if (value[1].data[i] == '=') {
var-&len =
return NGX_CONF_OK;
return NGX_CONF_OK;
1209 static char *
1210 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_core_conf_t
ngx_uint_t
if (ccf-&priority != 0) {
return "is duplicate";
value = cf-&args-&
if (value[1].data[0] == '-') {
minus = 1;
} else if (value[1].data[0] == '+') {
minus = 0;
minus = 0;
ccf-&priority = ngx_atoi(&value[1].data[n], value[1].len - n);
if (ccf-&priority == NGX_ERROR) {
return "invalid number";
if (minus) {
ccf-&priority = -ccf-&
return NGX_CONF_OK;
1249 static char *
1250 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1252 #if (NGX_HAVE_CPU_AFFINITY)
ngx_core_conf_t
ngx_uint_t
if (ccf-&cpu_affinity) {
return "is duplicate";
mask = ngx_palloc(cf-&pool, (cf-&args-&nelts - 1) * sizeof(uint64_t));
if (mask == NULL) {
return NGX_CONF_ERROR;
ccf-&cpu_affinity_n = cf-&args-&nelts - 1;
ccf-&cpu_affinity =
value = cf-&args-&
for (n = 1; n & cf-&args-& n++) {
if (value[n].len & 64) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"worker_cpu_affinity\" supports up to 64 CPUs only");
return NGX_CONF_ERROR;
mask[n - 1] = 0;
for (i = 0; i & value[n]. i++) {
ch = value[n].data[i];
if (ch == ' ') {
mask[n - 1] &&= 1;
if (ch == '0') {
if (ch == '1') {
mask[n - 1] |= 1;
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid character \"%c\" in \"worker_cpu_affinity\"",
return NGX_CONF_ERROR;
1310 #else
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"\"worker_cpu_affinity\" is not supported "
"on this platform, ignored");
1315 #endif
return NGX_CONF_OK;
1321 uint64_t
1322 ngx_get_cpu_affinity(ngx_uint_t n)
ngx_core_conf_t
ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle-&conf_ctx,
ngx_core_module);
if (ccf-&cpu_affinity == NULL) {
if (ccf-&cpu_affinity_n & n) {
return ccf-&cpu_affinity[n];
return ccf-&cpu_affinity[ccf-&cpu_affinity_n - 1];
1341 static char *
1342 ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_core_conf_t
ccf = (ngx_core_conf_t *)
if (ccf-&worker_processes != NGX_CONF_UNSET) {
return "is duplicate";
value = (ngx_str_t *) cf-&args-&
if (ngx_strcmp(value[1].data, "auto") == 0) {
ccf-&worker_processes = ngx_
return NGX_CONF_OK;
ccf-&worker_processes = ngx_atoi(value[1].data, value[1].len);
if (ccf-&worker_processes == NGX_ERROR) {
return "invalid value";
return NGX_CONF_OK;
  Nginx的启动包括了很多的初始化和处理函数。这些函数相对来说,有一部分非常复杂,暂且从简单开始,从整体上对Ngixnd的启动有一个了解,方便日后的分析与学习。
  主要函数:
//完成socket的继承static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);//对参数选项进行处理
static ngx_int_t ngx_get_options(int argc, char *const *argv);//初始化ngx_cycle内的部分内容
static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);//命令行参数保存到ngx_os_argv、ngx_argc以及ngx_argv全局的变量中
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);//创建模块的配置信息
static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);//初始化配置信息
static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);//设置优先级
static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);//设置CPU亲和性
static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);//设置worker进程
static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Nginx启动的主要流程
&  下图为Nginx的启动时函数的调用过程,其中大部分都是为了Nginx启动的初始化部分。从错误处理、参数设置、时间设置等方面进行初始化,并注册了我们需要的模块,最后根据信号选择单任务模式还是master-worker模式。
  流程图:
  主函数在开始对系统错误、参数、时间、系统变量、日志等进行了初始化。
//初始化系统中错误编号对应的含义
ngx_strerror_init();
//对参数选项进行处理
ngx_get_options(argc, argv);
//时间初始化
ngx_time_init();
//重置pcre内存管理的接口
ngx_regex_init();
//日志初始化
ngx_log_init(ngx_prefix);//创建内存池ngx_create_pool(1024, log);//保存变量ngx_save_argv();//初始化ngx_cycle的prefix, conf_prefix, conf_file, conf_paramngx_process_options();//初始化系统相关变量,如内存页面大小ngx_pagesize,ngx_cacheline_size,最大连接数ngx_max_sockets等ngx_os_init();//初始化CRC表(后续的CRC校验通过查表进行,效率高)ngx_crc32_table_init();
  初始化完成后,需要先调用ngx_add_inherited_sockets函数继承socket,并储存在Listening数组中,在运行时候进行监听。之后就可以调用ngx_init_cycle来初始化ngx_cycle结构体,这个结构体用来存储所有的连接,具体如下:
struct ngx_cycle_s {
****conf_//配置上下文数组(含所有模块)
ngx_pool_t
ngx_connection_t
**//连接文件
ngx_connection_t
*free_//空闲连接
ngx_uint_t
free_connection_n;//空闲连接数
ngx_queue_t
reusable_connections_////再利用连接队列
ngx_array_t
//监听数组
ngx_array_t//路径数组
ngx_list_t
open_//打开文件链表
ngx_list_t
shared_//共享内存链表
ngx_uint_t
connection_n;&&
ngx_uint_t
ngx_connection_t
ngx_event_t
*read_//读事件
ngx_event_t
*write_//写事件
ngx_cycle_t
lock_//锁文件
  调用ngx_init_signals来注册信号。
//信号种类#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
  在进入处理之前,还要调用ngx_create_pidfile来记录进程id。最后,根据接收到的信号,来判断调用ngx_single_process_cycle还是ngx_master_process_cycle(master-worker模式)。
if (ngx_process == NGX_PROCESS_SINGLE) {
ngx_single_process_cycle(cycle);
ngx_master_process_cycle(cycle);
  其中,守护进程函数为ngx_daemon,位于src/os/unix/Ngx_daemon.c
//daemonngx_int_t
ngx_daemon(ngx_log_t *log)
switch (fork()) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
return NGX_ERROR;
breakdefault:
ngx_pid = ngx_getpid();//取得进程识别码
if (setsid() == -1) {
//子进程将重新获得一个新的会话(session)id
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "setsid() failed");
return NGX_ERROR;
fd = open("/dev/null", O_RDWR);
if (fd == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
"open(\"/dev/null\") failed");
return NGX_ERROR;
if (dup2(fd, STDIN_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
return NGX_ERROR;
if (dup2(fd, STDOUT_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
return NGX_ERROR;
if (dup2(fd, STDERR_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
return NGX_ERROR;
if (fd & STDERR_FILENO) {
if (close(fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
return NGX_ERROR;
return NGX_OK;
阅读(...) 评论()

我要回帖

更多关于 扑克牌 的文章

 

随机推荐