comparison src/core/nginx.c @ 482:392c16f2d858 NGINX_0_7_53

nginx 0.7.53 *) Change: now a log set by --error-log-path is created from the very start-up. *) Feature: now the start up errors and warnings are outputted to an error_log and stderr. *) Feature: the empty --prefix= configure parameter forces nginx to use a directory where it was run as prefix. *) Feature: the -p switch. *) Feature: the -s switch on Unix platforms. *) Feature: the -? and -h switches. Thanks to Jerome Loyet. *) Feature: now switches may be set in condensed form. *) Bugfix: nginx/Windows did not work if configuration file was given by the -c switch. *) Bugfix: temporary files might be not removed if the "proxy_store", "fastcgi_store", "proxy_cache", or "fastcgi_cache" were used. Thanks to Maxim Dounin. *) Bugfix: an incorrect value was passed to mail proxy authentication server in "Auth-Method" header line; the bug had appeared in 0.7.34. Thanks to Simon Lecaille. *) Bugfix: system error text descriptions were not logged on Linux; the bug had appeared in 0.7.45. *) Bugfix: the "fastcgi_cache_min_uses" directive did not work. Thanks to Andrew Vorobyoff.
author Igor Sysoev <http://sysoev.ru>
date Mon, 27 Apr 2009 00:00:00 +0400
parents 549994537f15
children ed5e10fb40fc
comparison
equal deleted inserted replaced
481:0c98173187ac 482:392c16f2d858
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 ngx_int_t 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);
180 }; 181 };
181 182
182 183
183 ngx_uint_t ngx_max_module; 184 ngx_uint_t ngx_max_module;
184 185
186 static ngx_uint_t ngx_show_help;
185 static ngx_uint_t ngx_show_version; 187 static ngx_uint_t ngx_show_version;
186 static ngx_uint_t ngx_show_configure; 188 static ngx_uint_t ngx_show_configure;
187 #if (NGX_WIN32) 189 static u_char *ngx_prefix;
190 static u_char *ngx_conf_file;
191 static u_char *ngx_conf_params;
188 static char *ngx_signal; 192 static char *ngx_signal;
189 #endif
190 193
191 194
192 static char **ngx_os_environ; 195 static char **ngx_os_environ;
193 196
194 197
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(0, "nginx version: " NGINX_VER);
212
213 if (ngx_show_help) {
214 ngx_log_stderr(0,
215 "Usage: nginx [-?hvVt] [-s signal] [-c filename] "
216 "[-g directives]" CRLF CRLF
217 "Options:" CRLF
218 " -?,-h : this help" CRLF
219 " -v : show version and exit" CRLF
220 " -V : show version and configure options then exit"
221 CRLF
222 " -t : test configuration and exit" CRLF
223 " -s signal : send signal to a master process: "
224 "stop, quit, reopen, reload" CRLF
225 #ifdef NGX_PREFIX
226 " -p prefix : set prefix path (default: "
227 NGX_PREFIX ")" CRLF
228 #else
229 " -p prefix : set prefix path (default: NONE)" CRLF
230 #endif
231 " -c filename : set configuration file (default: "
232 NGX_CONF_PATH ")" CRLF
233 " -g directives : set global directives out of configuration "
234 "file" CRLF
235 );
236 }
237
238 if (ngx_show_configure) {
239 #ifdef NGX_COMPILER
240 ngx_log_stderr(0, "built by " NGX_COMPILER);
241 #endif
242 ngx_log_stderr(0, "configure arguments:" NGX_CONFIGURE);
243 }
244
245 if (!ngx_test_config) {
246 return 0;
247 }
248 }
249
203 #if (NGX_FREEBSD) 250 #if (NGX_FREEBSD)
204 ngx_debug_init(); 251 ngx_debug_init();
205 #endif 252 #endif
206 253
207 /* TODO */ ngx_max_sockets = -1; 254 /* TODO */ ngx_max_sockets = -1;
212 ngx_regex_init(); 259 ngx_regex_init();
213 #endif 260 #endif
214 261
215 ngx_pid = ngx_getpid(); 262 ngx_pid = ngx_getpid();
216 263
217 log = ngx_log_init(); 264 log = ngx_log_init(ngx_prefix);
218 if (log == NULL) { 265 if (log == NULL) {
219 return 1; 266 return 1;
220 } 267 }
221 268
222 /* STUB */ 269 /* STUB */
223 #if (NGX_OPENSSL) 270 #if (NGX_OPENSSL)
224 ngx_ssl_init(log); 271 ngx_ssl_init(log);
225 #endif 272 #endif
226 273
227 /* init_cycle->log is required for signal handlers and ngx_getopt() */ 274 /*
275 * init_cycle->log is required for signal handlers and
276 * ngx_process_options()
277 */
228 278
229 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); 279 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
230 init_cycle.log = log; 280 init_cycle.log = log;
231 ngx_cycle = &init_cycle; 281 ngx_cycle = &init_cycle;
232 282
237 287
238 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) { 288 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
239 return 1; 289 return 1;
240 } 290 }
241 291
242 if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) { 292 if (ngx_process_options(&init_cycle) != NGX_OK) {
243 return 1; 293 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 } 294 }
266 295
267 if (ngx_os_init(log) != NGX_OK) { 296 if (ngx_os_init(log) != NGX_OK) {
268 return 1; 297 return 1;
269 } 298 }
286 } 315 }
287 316
288 cycle = ngx_init_cycle(&init_cycle); 317 cycle = ngx_init_cycle(&init_cycle);
289 if (cycle == NULL) { 318 if (cycle == NULL) {
290 if (ngx_test_config) { 319 if (ngx_test_config) {
291 ngx_log_stderr("the configuration file %s test failed", 320 ngx_log_stderr(0, "configuration file %s test failed",
292 init_cycle.conf_file.data); 321 init_cycle.conf_file.data);
293 } 322 }
294 323
295 return 1; 324 return 1;
296 } 325 }
297 326
298 if (ngx_test_config) { 327 if (ngx_test_config) {
299 ngx_log_stderr("the configuration file %s was tested successfully", 328 ngx_log_stderr(0, "configuration file %s test is successful",
300 cycle->conf_file.data); 329 cycle->conf_file.data);
301 return 0; 330 return 0;
302 } 331 }
303 332
304 ngx_os_status(cycle->log); 333 ngx_os_status(cycle->log);
309 338
310 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) { 339 if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
311 ngx_process = NGX_PROCESS_MASTER; 340 ngx_process = NGX_PROCESS_MASTER;
312 } 341 }
313 342
314 #if (NGX_WIN32)
315
316 if (ngx_signal) { 343 if (ngx_signal) {
317 return ngx_signal_process(cycle, ngx_signal); 344 return ngx_signal_process(cycle, ngx_signal);
318 } 345 }
319 346
320 #else 347 #if !(NGX_WIN32)
321 348
322 if (ngx_init_signals(cycle->log) != NGX_OK) { 349 if (ngx_init_signals(cycle->log) != NGX_OK) {
323 return 1; 350 return 1;
324 } 351 }
325 352
334 #endif 361 #endif
335 362
336 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { 363 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
337 return 1; 364 return 1;
338 } 365 }
366
367 if (cycle->log->file->fd != ngx_stderr) {
368
369 if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
370 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
371 ngx_set_stderr_n " failed");
372 return 1;
373 }
374 }
375
376 ngx_use_stderr = 0;
339 377
340 if (ngx_process == NGX_PROCESS_SINGLE) { 378 if (ngx_process == NGX_PROCESS_SINGLE) {
341 ngx_single_process_cycle(cycle); 379 ngx_single_process_cycle(cycle);
342 380
343 } else { 381 } else {
509 ngx_pid_t pid; 547 ngx_pid_t pid;
510 ngx_exec_ctx_t ctx; 548 ngx_exec_ctx_t ctx;
511 ngx_core_conf_t *ccf; 549 ngx_core_conf_t *ccf;
512 ngx_listening_t *ls; 550 ngx_listening_t *ls;
513 551
552 ngx_memzero(&ctx, sizeof(ngx_exec_ctx_t));
553
514 ctx.path = argv[0]; 554 ctx.path = argv[0];
515 ctx.name = "new binary process"; 555 ctx.name = "new binary process";
516 ctx.argv = argv; 556 ctx.argv = argv;
517 557
518 n = 2; 558 n = 2;
592 return pid; 632 return pid;
593 } 633 }
594 634
595 635
596 static ngx_int_t 636 static ngx_int_t
597 ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv) 637 ngx_get_options(int argc, char *const *argv)
598 { 638 {
599 ngx_int_t i; 639 u_char *p;
640 ngx_int_t i;
600 641
601 for (i = 1; i < argc; i++) { 642 for (i = 1; i < argc; i++) {
602 if (argv[i][0] != '-') { 643
603 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, 644 p = (u_char *) argv[i];
604 "invalid option: \"%s\"", argv[i]); 645
646 if (*p++ != '-') {
647 ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
605 return NGX_ERROR; 648 return NGX_ERROR;
606 } 649 }
607 650
608 switch (argv[i][1]) { 651 while (*p) {
609 652
610 case 'v': 653 switch (*p++) {
611 ngx_show_version = 1; 654
612 break; 655 case '?':
613 656 case 'h':
614 case 'V': 657 ngx_show_version = 1;
615 ngx_show_version = 1; 658 ngx_show_help = 1;
616 ngx_show_configure = 1; 659 break;
617 break; 660
618 661 case 'v':
619 case 't': 662 ngx_show_version = 1;
620 ngx_test_config = 1; 663 break;
621 break; 664
622 665 case 'V':
623 case 'c': 666 ngx_show_version = 1;
624 if (argv[i + 1] == NULL) { 667 ngx_show_configure = 1;
625 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, 668 break;
626 "the option \"-c\" requires file name"); 669
670 case 't':
671 ngx_test_config = 1;
672 break;
673
674 case 'p':
675 if (*p) {
676 ngx_prefix = p;
677 goto next;
678 }
679
680 if (argv[++i]) {
681 ngx_prefix = (u_char *) argv[i];
682 goto next;
683 }
684
685 ngx_log_stderr(0, "option \"-p\" requires directory name");
686 return NGX_ERROR;
687
688 case 'c':
689 if (*p) {
690 ngx_conf_file = p;
691 goto next;
692 }
693
694 if (argv[++i]) {
695 ngx_conf_file = (u_char *) argv[i];
696 goto next;
697 }
698
699 ngx_log_stderr(0, "option \"-c\" requires file name");
700 return NGX_ERROR;
701
702 case 'g':
703 if (*p) {
704 ngx_conf_params = p;
705 goto next;
706 }
707
708 if (argv[++i]) {
709 ngx_conf_params = (u_char *) argv[i];
710 goto next;
711 }
712
713 ngx_log_stderr(0, "option \"-g\" requires parameter");
714 return NGX_ERROR;
715
716 case 's':
717 if (*p) {
718 ngx_signal = (char *) p;
719
720 } else if (argv[++i]) {
721 ngx_signal = argv[i];
722
723 } else {
724 ngx_log_stderr(0, "option \"-s\" requires parameter");
725 return NGX_ERROR;
726 }
727
728 if (ngx_strcmp(ngx_signal, "stop") == 0
729 || ngx_strcmp(ngx_signal, "quit") == 0
730 || ngx_strcmp(ngx_signal, "reopen") == 0
731 || ngx_strcmp(ngx_signal, "reload") == 0)
732 {
733 ngx_process = NGX_PROCESS_SIGNALLER;
734 goto next;
735 }
736
737 ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
738 return NGX_ERROR;
739
740 default:
741 ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
627 return NGX_ERROR; 742 return NGX_ERROR;
628 } 743 }
629 744 }
630 cycle->conf_file.data = (u_char *) argv[++i]; 745
631 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); 746 next:
632 break; 747
633 748 continue;
634 case 'g':
635 if (argv[i + 1] == NULL) {
636 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
637 "the option \"-g\" requires parameter");
638 return NGX_ERROR;
639 }
640
641 cycle->conf_param.data = (u_char *) argv[++i];
642 cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);
643 break;
644
645 #if (NGX_WIN32)
646 case 's':
647 if (argv[++i] == NULL) {
648 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
649 "the option \"-s\" requires parameter");
650 return NGX_ERROR;
651 }
652
653 if (ngx_strcmp(argv[i], "stop") == 0
654 || ngx_strcmp(argv[i], "quit") == 0
655 || ngx_strcmp(argv[i], "reopen") == 0
656 || ngx_strcmp(argv[i], "reload") == 0)
657 {
658 ngx_process = NGX_PROCESS_SIGNALLER;
659 ngx_signal = argv[i];
660 break;
661 }
662
663 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
664 "invalid option: \"-s %s\"", argv[i]);
665 return NGX_ERROR;
666 #endif
667
668 default:
669 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
670 "invalid option: \"%s\"", argv[i]);
671 return NGX_ERROR;
672 }
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 } 749 }
679 750
680 return NGX_OK; 751 return NGX_OK;
681 } 752 }
682 753
716 ngx_argv[i] = NULL; 787 ngx_argv[i] = NULL;
717 788
718 #endif 789 #endif
719 790
720 ngx_os_environ = environ; 791 ngx_os_environ = environ;
792
793 return NGX_OK;
794 }
795
796
797 static ngx_int_t
798 ngx_process_options(ngx_cycle_t *cycle)
799 {
800 u_char *p;
801 size_t len;
802
803 if (ngx_prefix) {
804 len = ngx_strlen(ngx_prefix);
805 p = ngx_prefix;
806
807 if (!ngx_path_separator(*p)) {
808 p = ngx_pnalloc(cycle->pool, len + 1);
809 if (p == NULL) {
810 return NGX_ERROR;
811 }
812
813 ngx_memcpy(p, ngx_prefix, len);
814 p[len++] = '/';
815 }
816
817 cycle->conf_prefix.len = len;
818 cycle->conf_prefix.data = p;
819 cycle->prefix.len = len;
820 cycle->prefix.data = p;
821
822 } else {
823
824 #ifndef NGX_PREFIX
825
826 p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
827 if (p == NULL) {
828 return NGX_ERROR;
829 }
830
831 if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
832 ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
833 return NGX_ERROR;
834 }
835
836 len = ngx_strlen(p);
837
838 p[len++] = '/';
839
840 cycle->conf_prefix.len = len;
841 cycle->conf_prefix.data = p;
842 cycle->prefix.len = len;
843 cycle->prefix.data = p;
844
845 #else
846
847 #ifdef NGX_CONF_PREFIX
848 cycle->conf_prefix.len = sizeof(NGX_CONF_PREFIX) - 1;
849 cycle->conf_prefix.data = (u_char *) NGX_CONF_PREFIX;
850 #else
851 cycle->conf_prefix.len = sizeof(NGX_PREFIX) - 1;
852 cycle->conf_prefix.data = (u_char *) NGX_PREFIX;
853 #endif
854 cycle->prefix.len = sizeof(NGX_PREFIX) - 1;
855 cycle->prefix.data = (u_char *) NGX_PREFIX;
856
857 #endif
858 }
859
860 if (ngx_conf_file) {
861 cycle->conf_file.len = ngx_strlen(ngx_conf_file);
862 cycle->conf_file.data = ngx_conf_file;
863
864 } else {
865 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
866 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
867 }
868
869 if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
870 return NGX_ERROR;
871 }
872
873 for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
874 p > cycle->conf_file.data;
875 p--)
876 {
877 if (ngx_path_separator(*p)) {
878 cycle->conf_prefix.len = p - ngx_cycle->conf_file.data + 1;
879 cycle->conf_prefix.data = ngx_cycle->conf_file.data;
880 break;
881 }
882 }
883
884 if (ngx_conf_params) {
885 cycle->conf_param.len = ngx_strlen(ngx_conf_params);
886 cycle->conf_param.data = ngx_conf_params;
887 }
888
889 if (ngx_test_config) {
890 cycle->log->log_level = NGX_LOG_INFO;
891 }
721 892
722 return NGX_OK; 893 return NGX_OK;
723 } 894 }
724 895
725 896