comparison src/core/nginx.c @ 278:704622b2528a NGINX_0_5_9

nginx 0.5.9 *) Change: now the ngx_http_memcached_module uses the $memcached_key variable value as a key. *) Feature: the $memcached_key variable. *) Feature: the "clean" parameter in the "client_body_in_file_only" directive. *) Feature: the "env" directive. *) Feature: the "sendfile" directive is available inside the "if" block. *) Feature: now on failure of the writing to access nginx logs a message to error_log, but not more often than once a minute. *) Bugfix: the "access_log off" directive did not always turn off the logging.
author Igor Sysoev <http://sysoev.ru>
date Thu, 25 Jan 2007 00:00:00 +0300
parents 6eb1e38f0f1f
children fa73fb2d9427
comparison
equal deleted inserted replaced
277:b3aec7787b8e 278:704622b2528a
14 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv); 14 static ngx_int_t ngx_getopt(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 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); 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); 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); 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);
19 static char *ngx_set_priority(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);
20 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, 21 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
21 void *conf); 22 void *conf);
22 23
23 24
124 { ngx_string("working_directory"), 125 { ngx_string("working_directory"),
125 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, 126 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
126 ngx_conf_set_str_slot, 127 ngx_conf_set_str_slot,
127 0, 128 0,
128 offsetof(ngx_core_conf_t, working_directory), 129 offsetof(ngx_core_conf_t, working_directory),
130 NULL },
131
132 { ngx_string("env"),
133 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
134 ngx_set_env,
135 0,
136 0,
129 NULL }, 137 NULL },
130 138
131 #if (NGX_THREADS) 139 #if (NGX_THREADS)
132 140
133 { ngx_string("worker_threads"), 141 { ngx_string("worker_threads"),
176 ngx_uint_t ngx_max_module; 184 ngx_uint_t ngx_max_module;
177 185
178 static ngx_uint_t ngx_show_version; 186 static ngx_uint_t ngx_show_version;
179 static ngx_uint_t ngx_show_configure; 187 static ngx_uint_t ngx_show_configure;
180 188
181 static char *ngx_null_environ = NULL; 189 static char **ngx_os_environ;
182 190
183 191
184 int ngx_cdecl 192 int ngx_cdecl
185 main(int argc, char *const *argv) 193 main(int argc, char *const *argv)
186 { 194 {
272 } 280 }
273 281
274 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) { 282 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
275 return 1; 283 return 1;
276 } 284 }
277
278 environ = &ngx_null_environ;
279 285
280 ngx_max_module = 0; 286 ngx_max_module = 0;
281 for (i = 0; ngx_modules[i]; i++) { 287 for (i = 0; ngx_modules[i]; i++) {
282 ngx_modules[i]->index = ngx_max_module++; 288 ngx_modules[i]->index = ngx_max_module++;
283 } 289 }
405 411
406 return ngx_set_inherited_sockets(cycle); 412 return ngx_set_inherited_sockets(cycle);
407 } 413 }
408 414
409 415
416 char **
417 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
418 {
419 char **p, **env;
420 ngx_str_t *var;
421 ngx_uint_t i, n;
422 ngx_core_conf_t *ccf;
423
424 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
425
426 if (last) {
427 n = *last;
428
429 } else {
430 if (ccf->environment) {
431 return ccf->environment;
432 }
433
434 n = 0;
435 }
436
437 var = ccf->env.elts;
438
439 for (i = 0; i < ccf->env.nelts; i++) {
440 if (ngx_strcmp(var[i].data, "TZ") == 0
441 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
442 {
443 goto tz_found;
444 }
445 }
446
447 var = ngx_array_push(&ccf->env);
448
449 var->len = 2;
450 var->data = (u_char *) "TZ";
451
452 var = ccf->env.elts;
453
454 tz_found:
455
456 for (i = 0; i < ccf->env.nelts; i++) {
457
458 if (var[i].data[var[i].len] == '=') {
459 n++;
460 continue;
461 }
462
463 for (p = ngx_os_environ; *p; p++) {
464
465 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
466 && (*p)[var[i].len] == '=')
467 {
468 n++;
469 break;
470 }
471 }
472 }
473
474 if (last) {
475 *last = n;
476 env = ngx_alloc((n + 1) * sizeof(char *), cycle->log);
477
478 } else {
479 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
480 }
481
482 if (env == NULL) {
483 return NULL;
484 }
485
486 n = 0;
487
488 for (i = 0; i < ccf->env.nelts; i++) {
489
490 if (var[i].data[var[i].len] == '=') {
491 env[n++] = (char *) var[i].data;
492 continue;
493 }
494
495 for (p = ngx_os_environ; *p; p++) {
496
497 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
498 && (*p)[var[i].len] == '=')
499 {
500 env[n++] = *p;
501 break;
502 }
503 }
504 }
505
506 env[n] = NULL;
507
508 if (last == NULL) {
509 ccf->environment = env;
510 environ = env;
511 }
512
513 return env;
514 }
515
516
410 ngx_pid_t 517 ngx_pid_t
411 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv) 518 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
412 { 519 {
413 char *env[3], *var; 520 char **env, *var;
414 u_char *p; 521 u_char *p;
415 ngx_uint_t i; 522 ngx_uint_t i, n;
416 ngx_pid_t pid; 523 ngx_pid_t pid;
417 ngx_exec_ctx_t ctx; 524 ngx_exec_ctx_t ctx;
418 ngx_core_conf_t *ccf; 525 ngx_core_conf_t *ccf;
419 ngx_listening_t *ls; 526 ngx_listening_t *ls;
420 527
421 ctx.path = argv[0]; 528 ctx.path = argv[0];
422 ctx.name = "new binary process"; 529 ctx.name = "new binary process";
423 ctx.argv = argv; 530 ctx.argv = argv;
424 531
532 n = 2;
533 env = ngx_set_environment(cycle, &n);
534 if (env == NULL) {
535 return NGX_INVALID_PID;
536 }
537
425 var = ngx_alloc(sizeof(NGINX_VAR) 538 var = ngx_alloc(sizeof(NGINX_VAR)
426 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2, 539 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
427 cycle->log); 540 cycle->log);
428 541
429 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR)); 542 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
433 p = ngx_sprintf(p, "%ud;", ls[i].fd); 546 p = ngx_sprintf(p, "%ud;", ls[i].fd);
434 } 547 }
435 548
436 *p = '\0'; 549 *p = '\0';
437 550
438 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var); 551 env[n++] = var;
439
440 env[0] = var;
441 552
442 #if (NGX_SETPROCTITLE_USES_ENV) 553 #if (NGX_SETPROCTITLE_USES_ENV)
443 554
444 /* allocate the spare 300 bytes for the new binary process title */ 555 /* allocate the spare 300 bytes for the new binary process title */
445 556
446 env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 557 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
447 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 558 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
448 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 559 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
449 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 560 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
450 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 561 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
451 562
452 env[2] = NULL; 563 #endif
453 564
454 #else 565 env[n] = NULL;
455 566
456 env[1] = NULL; 567 #if (NGX_DEBUG)
457 568 {
458 #endif 569 char **e;
459 570 for (e = env; *e; e++) {
460 ctx.envp = (char *const *) &env; 571 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
572 }
573 }
574 #endif
575
576 ctx.envp = (char *const *) env;
461 577
462 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 578 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
463 579
464 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) 580 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
465 != NGX_OK)
466 {
467 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 581 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
468 ngx_rename_file_n " %s to %s failed " 582 ngx_rename_file_n " %s to %s failed "
469 "before executing new binary process \"%s\"", 583 "before executing new binary process \"%s\"",
470 ccf->pid.data, ccf->oldpid.data, argv[0]); 584 ccf->pid.data, ccf->oldpid.data, argv[0]);
471 585
586 ngx_free(env);
472 ngx_free(var); 587 ngx_free(var);
473 588
474 return NGX_INVALID_PID; 589 return NGX_INVALID_PID;
475 } 590 }
476 591
477 pid = ngx_execute(cycle, &ctx); 592 pid = ngx_execute(cycle, &ctx);
478 593
479 if (pid == NGX_INVALID_PID) { 594 if (pid == NGX_INVALID_PID) {
480 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) 595 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
481 != NGX_OK)
482 {
483 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 596 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
484 ngx_rename_file_n " %s back to %s failed " 597 ngx_rename_file_n " %s back to %s failed after "
485 "after try to executing new binary process \"%s\"", 598 "the try to execute the new binary process \"%s\"",
486 ccf->oldpid.data, ccf->pid.data, argv[0]); 599 ccf->oldpid.data, ccf->pid.data, argv[0]);
487 } 600 }
488 } 601 }
489 602
603 ngx_free(env);
490 ngx_free(var); 604 ngx_free(var);
491 605
492 return pid; 606 return pid;
493 } 607 }
494 608
585 } 699 }
586 700
587 ngx_argv[i] = NULL; 701 ngx_argv[i] = NULL;
588 702
589 #endif 703 #endif
704
705 ngx_os_environ = environ;
590 706
591 return NGX_OK; 707 return NGX_OK;
592 } 708 }
593 709
594 710
629 #if (NGX_THREADS) 745 #if (NGX_THREADS)
630 ccf->worker_threads = NGX_CONF_UNSET; 746 ccf->worker_threads = NGX_CONF_UNSET;
631 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE; 747 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
632 #endif 748 #endif
633 749
750 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
751 != NGX_OK)
752 {
753 return NULL;
754 }
755
634 return ccf; 756 return ccf;
635 } 757 }
636 758
637 759
638 static char * 760 static char *
639 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf) 761 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
640 { 762 {
641 ngx_core_conf_t *ccf = conf; 763 ngx_core_conf_t *ccf = conf;
642 764
643 #if !(NGX_WIN32) 765 #if !(NGX_WIN32)
644 ngx_str_t lock_file; 766 ngx_str_t lock_file;
645 struct passwd *pwd; 767 struct group *grp;
646 struct group *grp; 768 struct passwd *pwd;
647 #endif 769 #endif
648 770
649 ngx_conf_init_value(ccf->daemon, 1); 771 ngx_conf_init_value(ccf->daemon, 1);
650 ngx_conf_init_value(ccf->master, 1); 772 ngx_conf_init_value(ccf->master, 1);
651 ngx_conf_init_msec_value(ccf->timer_resolution, 0); 773 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
828 #endif 950 #endif
829 } 951 }
830 952
831 953
832 static char * 954 static char *
955 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
956 {
957 ngx_core_conf_t *ccf = conf;
958
959 ngx_str_t *value, *var;
960 ngx_uint_t i;
961
962 var = ngx_array_push(&ccf->env);
963 if (var == NULL) {
964 return NGX_CONF_ERROR;
965 }
966
967 value = cf->args->elts;
968 *var = value[1];
969
970 for (i = 0; i < value[1].len; i++) {
971
972 if (value[1].data[i] == '=') {
973
974 var->len = i;
975
976 return NGX_CONF_OK;
977 }
978 }
979
980 return NGX_CONF_OK;
981 }
982
983
984 static char *
833 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 985 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
834 { 986 {
835 ngx_core_conf_t *ccf = conf; 987 ngx_core_conf_t *ccf = conf;
836 988
837 ngx_str_t *value; 989 ngx_str_t *value;