test command line options before ngx_log_init() and issue errors to stderr

This commit is contained in:
Igor Sysoev 2009-04-21 13:39:47 +00:00
parent 9a354261f7
commit f873e73b96

View File

@ -10,7 +10,8 @@
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv); static ngx_int_t ngx_get_options(int argc, char *const *argv);
static void ngx_process_options(ngx_cycle_t *cycle);
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *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 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_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
@ -184,6 +185,8 @@ ngx_uint_t ngx_max_module;
static ngx_uint_t ngx_show_version; static ngx_uint_t ngx_show_version;
static ngx_uint_t ngx_show_configure; static ngx_uint_t ngx_show_configure;
static char *ngx_conf_file;
static char *ngx_conf_params;
#if (NGX_WIN32) #if (NGX_WIN32)
static char *ngx_signal; static char *ngx_signal;
#endif #endif
@ -200,6 +203,25 @@ main(int argc, char *const *argv)
ngx_cycle_t *cycle, init_cycle; ngx_cycle_t *cycle, init_cycle;
ngx_core_conf_t *ccf; ngx_core_conf_t *ccf;
if (ngx_get_options(argc, argv) != NGX_OK) {
return 1;
}
if (ngx_show_version) {
ngx_log_stderr("nginx version: " NGINX_VER);
if (ngx_show_configure) {
#ifdef NGX_COMPILER
ngx_log_stderr("built by " NGX_COMPILER);
#endif
ngx_log_stderr("configure arguments: " NGX_CONFIGURE);
}
if (!ngx_test_config) {
return 0;
}
}
#if (NGX_FREEBSD) #if (NGX_FREEBSD)
ngx_debug_init(); ngx_debug_init();
#endif #endif
@ -224,7 +246,10 @@ main(int argc, char *const *argv)
ngx_ssl_init(log); ngx_ssl_init(log);
#endif #endif
/* init_cycle->log is required for signal handlers and ngx_getopt() */ /*
* init_cycle->log is required for signal handlers and
* ngx_process_options()
*/
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
init_cycle.log = log; init_cycle.log = log;
@ -239,30 +264,7 @@ main(int argc, char *const *argv)
return 1; return 1;
} }
if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) { ngx_process_options(&init_cycle);
return 1;
}
if (ngx_show_version) {
ngx_log_stderr("nginx version: " NGINX_VER);
if (ngx_show_configure) {
#ifdef NGX_COMPILER
ngx_log_stderr("built by " NGX_COMPILER);
#endif
ngx_log_stderr("configure arguments: " NGX_CONFIGURE);
}
if (!ngx_test_config) {
return 0;
}
}
if (ngx_test_config) {
log->log_level = NGX_LOG_INFO;
}
if (ngx_os_init(log) != NGX_OK) { if (ngx_os_init(log) != NGX_OK) {
return 1; return 1;
@ -594,14 +596,13 @@ ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
static ngx_int_t static ngx_int_t
ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv) ngx_get_options(int argc, char *const *argv)
{ {
ngx_int_t i; ngx_int_t i;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') { if (argv[i][0] != '-') {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_log_stderr("invalid option: \"%s\"", argv[i]);
"invalid option: \"%s\"", argv[i]);
return NGX_ERROR; return NGX_ERROR;
} }
@ -621,32 +622,27 @@ ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
break; break;
case 'c': case 'c':
if (argv[i + 1] == NULL) { if (argv[++i]) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_conf_file = argv[i];
"the option \"-c\" requires file name"); break;
return NGX_ERROR;
} }
cycle->conf_file.data = (u_char *) argv[++i]; ngx_log_stderr("the option \"-c\" requires file name");
cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); return NGX_ERROR;
break;
case 'g': case 'g':
if (argv[i + 1] == NULL) { if (argv[++i]) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_conf_params = argv[i];
"the option \"-g\" requires parameter"); break;
return NGX_ERROR;
} }
cycle->conf_param.data = (u_char *) argv[++i]; ngx_log_stderr("the option \"-g\" requires parameter");
cycle->conf_param.len = ngx_strlen(cycle->conf_param.data); return NGX_ERROR;
break;
#if (NGX_WIN32) #if (NGX_WIN32)
case 's': case 's':
if (argv[++i] == NULL) { if (argv[++i] == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_log_stderr("the option \"-s\" requires parameter");
"the option \"-s\" requires parameter");
return NGX_ERROR; return NGX_ERROR;
} }
@ -660,23 +656,16 @@ ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
break; break;
} }
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_log_stderr("invalid option: \"-s %s\"", argv[i]);
"invalid option: \"-s %s\"", argv[i]);
return NGX_ERROR; return NGX_ERROR;
#endif #endif
default: default:
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, ngx_log_stderr("invalid option: \"%s\"", argv[i]);
"invalid option: \"%s\"", argv[i]);
return NGX_ERROR; return NGX_ERROR;
} }
} }
if (cycle->conf_file.data == NULL) {
cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
}
return NGX_OK; return NGX_OK;
} }
@ -723,6 +712,29 @@ ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
} }
static void
ngx_process_options(ngx_cycle_t *cycle)
{
if (ngx_conf_file) {
cycle->conf_file.len = ngx_strlen(ngx_conf_file);
cycle->conf_file.data = (u_char *) ngx_conf_file;
} else {
cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
}
if (ngx_conf_params) {
cycle->conf_param.data = (u_char *) ngx_conf_params;
cycle->conf_param.len = ngx_strlen(ngx_conf_params);
}
if (ngx_test_config) {
cycle->log->log_level = NGX_LOG_INFO;
}
}
static void * static void *
ngx_core_module_create_conf(ngx_cycle_t *cycle) ngx_core_module_create_conf(ngx_cycle_t *cycle)
{ {