Mercurial > hg > nginx
comparison src/core/nginx.c @ 2730:95a17423afdb
test command line options before ngx_log_init() and issue errors to stderr
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 21 Apr 2009 13:39:47 +0000 |
parents | d43d73277c5c |
children | edc01cc34b3e |
comparison
equal
deleted
inserted
replaced
2729:e85497f36011 | 2730:95a17423afdb |
---|---|
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <nginx.h> | 9 #include <nginx.h> |
10 | 10 |
11 | 11 |
12 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); | 12 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); |
13 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv); | 13 static ngx_int_t ngx_get_options(int argc, char *const *argv); |
14 static void ngx_process_options(ngx_cycle_t *cycle); | |
14 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv); | 15 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv); |
15 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); | 16 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); |
16 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); | 17 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); |
17 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 18 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
18 static char *ngx_set_env(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); |
182 | 183 |
183 ngx_uint_t ngx_max_module; | 184 ngx_uint_t ngx_max_module; |
184 | 185 |
185 static ngx_uint_t ngx_show_version; | 186 static ngx_uint_t ngx_show_version; |
186 static ngx_uint_t ngx_show_configure; | 187 static ngx_uint_t ngx_show_configure; |
188 static char *ngx_conf_file; | |
189 static char *ngx_conf_params; | |
187 #if (NGX_WIN32) | 190 #if (NGX_WIN32) |
188 static char *ngx_signal; | 191 static char *ngx_signal; |
189 #endif | 192 #endif |
190 | 193 |
191 | 194 |
198 ngx_int_t i; | 201 ngx_int_t i; |
199 ngx_log_t *log; | 202 ngx_log_t *log; |
200 ngx_cycle_t *cycle, init_cycle; | 203 ngx_cycle_t *cycle, init_cycle; |
201 ngx_core_conf_t *ccf; | 204 ngx_core_conf_t *ccf; |
202 | 205 |
206 if (ngx_get_options(argc, argv) != NGX_OK) { | |
207 return 1; | |
208 } | |
209 | |
210 if (ngx_show_version) { | |
211 ngx_log_stderr("nginx version: " NGINX_VER); | |
212 | |
213 if (ngx_show_configure) { | |
214 #ifdef NGX_COMPILER | |
215 ngx_log_stderr("built by " NGX_COMPILER); | |
216 #endif | |
217 ngx_log_stderr("configure arguments: " NGX_CONFIGURE); | |
218 } | |
219 | |
220 if (!ngx_test_config) { | |
221 return 0; | |
222 } | |
223 } | |
224 | |
203 #if (NGX_FREEBSD) | 225 #if (NGX_FREEBSD) |
204 ngx_debug_init(); | 226 ngx_debug_init(); |
205 #endif | 227 #endif |
206 | 228 |
207 /* TODO */ ngx_max_sockets = -1; | 229 /* TODO */ ngx_max_sockets = -1; |
222 /* STUB */ | 244 /* STUB */ |
223 #if (NGX_OPENSSL) | 245 #if (NGX_OPENSSL) |
224 ngx_ssl_init(log); | 246 ngx_ssl_init(log); |
225 #endif | 247 #endif |
226 | 248 |
227 /* init_cycle->log is required for signal handlers and ngx_getopt() */ | 249 /* |
250 * init_cycle->log is required for signal handlers and | |
251 * ngx_process_options() | |
252 */ | |
228 | 253 |
229 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); | 254 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); |
230 init_cycle.log = log; | 255 init_cycle.log = log; |
231 ngx_cycle = &init_cycle; | 256 ngx_cycle = &init_cycle; |
232 | 257 |
237 | 262 |
238 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) { | 263 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) { |
239 return 1; | 264 return 1; |
240 } | 265 } |
241 | 266 |
242 if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) { | 267 ngx_process_options(&init_cycle); |
243 return 1; | |
244 } | |
245 | |
246 if (ngx_show_version) { | |
247 | |
248 ngx_log_stderr("nginx version: " NGINX_VER); | |
249 | |
250 if (ngx_show_configure) { | |
251 #ifdef NGX_COMPILER | |
252 ngx_log_stderr("built by " NGX_COMPILER); | |
253 #endif | |
254 | |
255 ngx_log_stderr("configure arguments: " NGX_CONFIGURE); | |
256 } | |
257 | |
258 if (!ngx_test_config) { | |
259 return 0; | |
260 } | |
261 } | |
262 | |
263 if (ngx_test_config) { | |
264 log->log_level = NGX_LOG_INFO; | |
265 } | |
266 | 268 |
267 if (ngx_os_init(log) != NGX_OK) { | 269 if (ngx_os_init(log) != NGX_OK) { |
268 return 1; | 270 return 1; |
269 } | 271 } |
270 | 272 |
592 return pid; | 594 return pid; |
593 } | 595 } |
594 | 596 |
595 | 597 |
596 static ngx_int_t | 598 static ngx_int_t |
597 ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv) | 599 ngx_get_options(int argc, char *const *argv) |
598 { | 600 { |
599 ngx_int_t i; | 601 ngx_int_t i; |
600 | 602 |
601 for (i = 1; i < argc; i++) { | 603 for (i = 1; i < argc; i++) { |
602 if (argv[i][0] != '-') { | 604 if (argv[i][0] != '-') { |
603 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 605 ngx_log_stderr("invalid option: \"%s\"", argv[i]); |
604 "invalid option: \"%s\"", argv[i]); | |
605 return NGX_ERROR; | 606 return NGX_ERROR; |
606 } | 607 } |
607 | 608 |
608 switch (argv[i][1]) { | 609 switch (argv[i][1]) { |
609 | 610 |
619 case 't': | 620 case 't': |
620 ngx_test_config = 1; | 621 ngx_test_config = 1; |
621 break; | 622 break; |
622 | 623 |
623 case 'c': | 624 case 'c': |
624 if (argv[i + 1] == NULL) { | 625 if (argv[++i]) { |
625 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 626 ngx_conf_file = argv[i]; |
626 "the option \"-c\" requires file name"); | 627 break; |
627 return NGX_ERROR; | |
628 } | 628 } |
629 | 629 |
630 cycle->conf_file.data = (u_char *) argv[++i]; | 630 ngx_log_stderr("the option \"-c\" requires file name"); |
631 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); | 631 return NGX_ERROR; |
632 break; | |
633 | 632 |
634 case 'g': | 633 case 'g': |
635 if (argv[i + 1] == NULL) { | 634 if (argv[++i]) { |
636 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 635 ngx_conf_params = argv[i]; |
637 "the option \"-g\" requires parameter"); | 636 break; |
638 return NGX_ERROR; | |
639 } | 637 } |
640 | 638 |
641 cycle->conf_param.data = (u_char *) argv[++i]; | 639 ngx_log_stderr("the option \"-g\" requires parameter"); |
642 cycle->conf_param.len = ngx_strlen(cycle->conf_param.data); | 640 return NGX_ERROR; |
643 break; | |
644 | 641 |
645 #if (NGX_WIN32) | 642 #if (NGX_WIN32) |
646 case 's': | 643 case 's': |
647 if (argv[++i] == NULL) { | 644 if (argv[++i] == NULL) { |
648 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 645 ngx_log_stderr("the option \"-s\" requires parameter"); |
649 "the option \"-s\" requires parameter"); | |
650 return NGX_ERROR; | 646 return NGX_ERROR; |
651 } | 647 } |
652 | 648 |
653 if (ngx_strcmp(argv[i], "stop") == 0 | 649 if (ngx_strcmp(argv[i], "stop") == 0 |
654 || ngx_strcmp(argv[i], "quit") == 0 | 650 || ngx_strcmp(argv[i], "quit") == 0 |
658 ngx_process = NGX_PROCESS_SIGNALLER; | 654 ngx_process = NGX_PROCESS_SIGNALLER; |
659 ngx_signal = argv[i]; | 655 ngx_signal = argv[i]; |
660 break; | 656 break; |
661 } | 657 } |
662 | 658 |
663 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 659 ngx_log_stderr("invalid option: \"-s %s\"", argv[i]); |
664 "invalid option: \"-s %s\"", argv[i]); | |
665 return NGX_ERROR; | 660 return NGX_ERROR; |
666 #endif | 661 #endif |
667 | 662 |
668 default: | 663 default: |
669 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | 664 ngx_log_stderr("invalid option: \"%s\"", argv[i]); |
670 "invalid option: \"%s\"", argv[i]); | |
671 return NGX_ERROR; | 665 return NGX_ERROR; |
672 } | 666 } |
673 } | |
674 | |
675 if (cycle->conf_file.data == NULL) { | |
676 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1; | |
677 cycle->conf_file.data = (u_char *) NGX_CONF_PATH; | |
678 } | 667 } |
679 | 668 |
680 return NGX_OK; | 669 return NGX_OK; |
681 } | 670 } |
682 | 671 |
718 #endif | 707 #endif |
719 | 708 |
720 ngx_os_environ = environ; | 709 ngx_os_environ = environ; |
721 | 710 |
722 return NGX_OK; | 711 return NGX_OK; |
712 } | |
713 | |
714 | |
715 static void | |
716 ngx_process_options(ngx_cycle_t *cycle) | |
717 { | |
718 if (ngx_conf_file) { | |
719 cycle->conf_file.len = ngx_strlen(ngx_conf_file); | |
720 cycle->conf_file.data = (u_char *) ngx_conf_file; | |
721 | |
722 } else { | |
723 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1; | |
724 cycle->conf_file.data = (u_char *) NGX_CONF_PATH; | |
725 } | |
726 | |
727 if (ngx_conf_params) { | |
728 cycle->conf_param.data = (u_char *) ngx_conf_params; | |
729 cycle->conf_param.len = ngx_strlen(ngx_conf_params); | |
730 } | |
731 | |
732 if (ngx_test_config) { | |
733 cycle->log->log_level = NGX_LOG_INFO; | |
734 } | |
723 } | 735 } |
724 | 736 |
725 | 737 |
726 static void * | 738 static void * |
727 ngx_core_module_create_conf(ngx_cycle_t *cycle) | 739 ngx_core_module_create_conf(ngx_cycle_t *cycle) |