Klaus Demo nginx / 8885f87
env Igor Sysoev 15 years ago
4 changed file(s) with 220 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
1515 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
1616 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
1717 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);
1819 static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
1920 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
2021 void *conf);
125126 ngx_conf_set_str_slot,
126127 0,
127128 offsetof(ngx_core_conf_t, working_directory),
129 NULL },
130
131 { ngx_string("env"),
132 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
133 ngx_set_env,
134 0,
135 0,
128136 NULL },
129137
130138 #if (NGX_THREADS)
177185 static ngx_uint_t ngx_show_version;
178186 static ngx_uint_t ngx_show_configure;
179187
180 static char *ngx_null_environ = NULL;
188 static char **ngx_os_environ;
181189
182190
183191 int ngx_cdecl
273281 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
274282 return 1;
275283 }
276
277 environ = &ngx_null_environ;
278284
279285 ngx_max_module = 0;
280286 for (i = 0; ngx_modules[i]; i++) {
406412 }
407413
408414
415 char **
416 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
417 {
418 char **p, **env;
419 ngx_str_t *var;
420 ngx_uint_t i, n;
421 ngx_core_conf_t *ccf;
422
423 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
424
425 if (last) {
426 n = *last;
427
428 } else {
429 if (ccf->environment) {
430 return ccf->environment;
431 }
432
433 n = 0;
434 }
435
436 var = ccf->env.elts;
437
438 for (i = 0; i < ccf->env.nelts; i++) {
439 if (ngx_strcmp(var[i].data, "TZ") == 0
440 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
441 {
442 goto tz_found;
443 }
444 }
445
446 var = ngx_array_push(&ccf->env);
447
448 var->len = 2;
449 var->data = (u_char *) "TZ";
450
451 var = ccf->env.elts;
452
453 tz_found:
454
455 for (i = 0; i < ccf->env.nelts; i++) {
456
457 if (var[i].data[var[i].len] == '=') {
458 n++;
459 continue;
460 }
461
462 for (p = ngx_os_environ; *p; p++) {
463
464 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
465 && (*p)[var[i].len] == '=')
466 {
467 n++;
468 break;
469 }
470 }
471 }
472
473 if (last) {
474 *last = n;
475 env = ngx_alloc((n + 1) * sizeof(char *), cycle->log);
476
477 } else {
478 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
479 }
480
481 if (env == NULL) {
482 return NULL;
483 }
484
485 n = 0;
486
487 for (i = 0; i < ccf->env.nelts; i++) {
488
489 if (var[i].data[var[i].len] == '=') {
490 env[n++] = (char *) var[i].data;
491 continue;
492 }
493
494 for (p = ngx_os_environ; *p; p++) {
495
496 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
497 && (*p)[var[i].len] == '=')
498 {
499 env[n++] = *p;
500 break;
501 }
502 }
503 }
504
505 env[n] = NULL;
506
507 if (last == NULL) {
508 ccf->environment = env;
509 environ = env;
510 }
511
512 return env;
513 }
514
515
409516 ngx_pid_t
410517 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
411518 {
412 char *env[3], *var;
413 u_char *p;
414 ngx_uint_t i;
415 ngx_pid_t pid;
416 ngx_exec_ctx_t ctx;
417 ngx_core_conf_t *ccf;
418 ngx_listening_t *ls;
519 char **env, *var;
520 u_char *p;
521 ngx_uint_t i, n;
522 ngx_pid_t pid;
523 ngx_exec_ctx_t ctx;
524 ngx_core_conf_t *ccf;
525 ngx_listening_t *ls;
419526
420527 ctx.path = argv[0];
421528 ctx.name = "new binary process";
422529 ctx.argv = argv;
423530
531 n = 2;
532 env = ngx_set_environment(cycle, &n);
533 if (env == NULL) {
534 return NGX_INVALID_PID;
535 }
536
424537 var = ngx_alloc(sizeof(NGINX_VAR)
425538 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
426539 cycle->log);
434547
435548 *p = '\0';
436549
437 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var);
438
439 env[0] = var;
550 env[n++] = var;
440551
441552 #if (NGX_SETPROCTITLE_USES_ENV)
442553
443554 /* allocate the spare 300 bytes for the new binary process title */
444555
445 env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
446 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
447 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
448 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
449 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
450
451 env[2] = NULL;
452
453 #else
454
455 env[1] = NULL;
456
457 #endif
458
459 ctx.envp = (char *const *) &env;
556 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
557 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
558 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
559 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
560 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
561
562 #endif
563
564 env[n] = NULL;
565
566 #if (NGX_DEBUG)
567 {
568 char **e;
569 for (e = env; *e; e++) {
570 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
571 }
572 }
573 #endif
574
575 ctx.envp = (char *const *) env;
460576
461577 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
462578
463 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data)
464 != NGX_OK)
465 {
579 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
466580 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
467581 ngx_rename_file_n " %s to %s failed "
468582 "before executing new binary process \"%s\"",
469583 ccf->pid.data, ccf->oldpid.data, argv[0]);
470584
585 ngx_free(env);
471586 ngx_free(var);
472587
473588 return NGX_INVALID_PID;
476591 pid = ngx_execute(cycle, &ctx);
477592
478593 if (pid == NGX_INVALID_PID) {
479 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data)
480 != NGX_OK)
481 {
594 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
482595 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
483 ngx_rename_file_n " %s back to %s failed "
484 "after try to executing new binary process \"%s\"",
596 ngx_rename_file_n " %s back to %s failed after "
597 "the try to execute the new binary process \"%s\"",
485598 ccf->oldpid.data, ccf->pid.data, argv[0]);
486599 }
487600 }
488601
602 ngx_free(env);
489603 ngx_free(var);
490604
491605 return pid;
586700 ngx_argv[i] = NULL;
587701
588702 #endif
703
704 ngx_os_environ = environ;
589705
590706 return NGX_OK;
591707 }
630746 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
631747 #endif
632748
749 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
750 != NGX_OK)
751 {
752 return NULL;
753 }
754
633755 return ccf;
634756 }
635757
640762 ngx_core_conf_t *ccf = conf;
641763
642764 #if !(NGX_WIN32)
643 ngx_str_t lock_file;
644 struct passwd *pwd;
645 struct group *grp;
765 ngx_str_t lock_file;
766 struct group *grp;
767 struct passwd *pwd;
646768 #endif
647769
648770 ngx_conf_init_value(ccf->daemon, 1);
829951
830952
831953 static char *
954 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
955 {
956 ngx_core_conf_t *ccf = conf;
957
958 ngx_str_t *value, *var;
959 ngx_uint_t i;
960
961 var = ngx_array_push(&ccf->env);
962 if (var == NULL) {
963 return NGX_CONF_ERROR;
964 }
965
966 value = cf->args->elts;
967 *var = value[1];
968
969 for (i = 0; i < value[1].len; i++) {
970
971 if (value[1].data[i] == '=') {
972
973 var->len = i;
974
975 return NGX_CONF_OK;
976 }
977 }
978
979 return NGX_CONF_OK;
980 }
981
982
983 static char *
832984 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
833985 {
834986 ngx_core_conf_t *ccf = conf;
9292 ngx_str_t pid;
9393 ngx_str_t oldpid;
9494
95 ngx_array_t env;
96 char **environment;
97
9598 #if (NGX_THREADS)
9699 ngx_int_t worker_threads;
97100 size_t thread_stack_size;
112115 ngx_int_t ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log);
113116 void ngx_delete_pidfile(ngx_cycle_t *cycle);
114117 void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user);
118 char **ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last);
115119 ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv);
116120 u_long ngx_get_cpu_affinity(ngx_uint_t n);
117121 ngx_shm_zone_t *ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name,
4343 static void ngx_http_perl_sleep_handler(ngx_http_request_t *r);
4444 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf,
4545 ngx_http_perl_main_conf_t *pmcf);
46 static PerlInterpreter *
47 ngx_http_perl_create_interpreter(ngx_http_perl_main_conf_t *pmcf,
48 ngx_log_t *log);
46 static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf,
47 ngx_http_perl_main_conf_t *pmcf);
4948 static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires,
5049 ngx_log_t *log);
5150 static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r,
474473
475474 PERL_SYS_INIT(&ngx_argc, &ngx_argv);
476475
477 pmcf->perl = ngx_http_perl_create_interpreter(pmcf, cf->log);
476 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf);
478477
479478 if (pmcf->perl == NULL) {
480479 PERL_SYS_TERM();
499498
500499
501500 static PerlInterpreter *
502 ngx_http_perl_create_interpreter(ngx_http_perl_main_conf_t *pmcf,
503 ngx_log_t *log)
501 ngx_http_perl_create_interpreter(ngx_conf_t *cf,
502 ngx_http_perl_main_conf_t *pmcf)
504503 {
505504 int n;
506505 STRLEN len;
508507 char *ver, *embedding[6];
509508 PerlInterpreter *perl;
510509
511 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "create perl interpreter");
510 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter");
511
512 if (ngx_set_environment(cf->cycle, NULL) == NULL) {
513 return NULL;
514 }
512515
513516 perl = perl_alloc();
514517 if (perl == NULL) {
515 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_alloc() failed");
518 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed");
516519 return NULL;
517520 }
518521
545548 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL);
546549
547550 if (n != 0) {
548 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_parse() failed: %d", n);
551 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n);
549552 goto fail;
550553 }
551554
553556 ver = SvPV(sv, len);
554557
555558 if (ngx_strcmp(ver, NGINX_VERSION) != 0) {
556 ngx_log_error(NGX_LOG_ALERT, log, 0,
559 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
557560 "version " NGINX_VERSION " of nginx.pm is required, "
558561 "but %s was found", ver);
559562 goto fail;
560563 }
561564
562 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, log) != NGX_OK) {
565 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) {
563566 goto fail;
564567 }
565568
756756 static void
757757 ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
758758 {
759 sigset_t set;
760 ngx_int_t n;
761 ngx_uint_t i;
762 struct rlimit rlmt;
763 ngx_core_conf_t *ccf;
764 ngx_listening_t *ls;
759 sigset_t set;
760 ngx_int_t n;
761 ngx_uint_t i;
762 struct rlimit rlmt;
763 ngx_core_conf_t *ccf;
764 ngx_listening_t *ls;
765765
766766 ngx_process = NGX_PROCESS_WORKER;
767
768 if (ngx_set_environment(cycle, NULL) == NULL) {
769 /* fatal */
770 exit(2);
771 }
767772
768773 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
769774