diff options
author | Valentin Bartenev <vbart@nginx.com> | 2018-05-28 20:55:23 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2018-05-28 20:55:23 +0300 |
commit | d7e6e2bd8cb81d47c9e7300937cb08ec35b6c21b (patch) | |
tree | 6472a83f7b6acc1962f0a7449d74ddc577306b85 | |
parent | 3befb08204f64983e684904ecd2cb20ff45dd3c2 (diff) | |
download | unit-d7e6e2bd8cb81d47c9e7300937cb08ec35b6c21b.tar.gz unit-d7e6e2bd8cb81d47c9e7300937cb08ec35b6c21b.tar.bz2 |
Configuration of environment variables for application processes.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_application.c | 47 | ||||
-rw-r--r-- | src/nxt_application.h | 14 | ||||
-rw-r--r-- | src/nxt_conf_validation.c | 44 | ||||
-rw-r--r-- | src/nxt_main_process.c | 6 |
4 files changed, 106 insertions, 5 deletions
diff --git a/src/nxt_application.c b/src/nxt_application.c index ef15ab2b..186614a0 100644 --- a/src/nxt_application.c +++ b/src/nxt_application.c @@ -32,6 +32,7 @@ static void nxt_discovery_quit(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); static nxt_app_module_t *nxt_app_module_load(nxt_task_t *task, const char *name); +static nxt_int_t nxt_app_set_environment(nxt_conf_value_t *environment); static void nxt_app_http_release(nxt_task_t *task, void *obj, void *data); @@ -344,6 +345,13 @@ nxt_app_start(nxt_task_t *task, void *data) } } + if (nxt_slow_path(nxt_app_set_environment(app_conf->environment) + != NXT_OK)) + { + nxt_alert(task, "failed to set environment"); + return NXT_ERROR; + } + if (nxt_slow_path(nxt_thread_mutex_create(&nxt_app_mutex) != NXT_OK)) { return NXT_ERROR; } @@ -382,6 +390,45 @@ nxt_app_module_load(nxt_task_t *task, const char *name) } +static nxt_int_t +nxt_app_set_environment(nxt_conf_value_t *environment) +{ + char *env, *p; + uint32_t next; + nxt_str_t name, value; + nxt_conf_value_t *value_obj; + + if (environment != NULL) { + next = 0; + + for ( ;; ) { + value_obj = nxt_conf_next_object_member(environment, &name, &next); + if (value_obj == NULL) { + break; + } + + nxt_conf_get_string(value_obj, &value); + + env = nxt_malloc(name.length + value.length + 2); + if (nxt_slow_path(env == NULL)) { + return NXT_ERROR; + } + + p = nxt_cpymem(env, name.start, name.length); + *p++ = '='; + p = nxt_cpymem(p, value.start, value.length); + *p = '\0'; + + if (nxt_slow_path(putenv(env) != 0)) { + return NXT_ERROR; + } + } + } + + return NXT_OK; +} + + void nxt_app_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { diff --git a/src/nxt_application.h b/src/nxt_application.h index 1f9a40e6..6ac7fbd3 100644 --- a/src/nxt_application.h +++ b/src/nxt_application.h @@ -9,6 +9,9 @@ #define _NXT_APPLICATION_H_INCLUDED_ +#include <nxt_conf.h> + + typedef enum { NXT_APP_PYTHON, NXT_APP_PHP, @@ -65,12 +68,13 @@ typedef struct { struct nxt_common_app_conf_s { - nxt_str_t name; - nxt_str_t type; - nxt_str_t user; - nxt_str_t group; + nxt_str_t name; + nxt_str_t type; + nxt_str_t user; + nxt_str_t group; - char *working_directory; + char *working_directory; + nxt_conf_value_t *environment; union { nxt_python_app_conf_t python; diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 62ca4b06..f31f3f15 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -62,6 +62,8 @@ static nxt_int_t nxt_conf_vldt_system(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); static nxt_int_t nxt_conf_vldt_user(nxt_conf_validation_t *vldt, char *name); static nxt_int_t nxt_conf_vldt_group(nxt_conf_validation_t *vldt, char *name); +static nxt_int_t nxt_conf_vldt_environment(nxt_conf_validation_t *vldt, + nxt_str_t *name, nxt_conf_value_t *value); static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] = { @@ -165,6 +167,11 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = { NULL, NULL }, + { nxt_string("environment"), + NXT_CONF_VLDT_OBJECT, + &nxt_conf_vldt_object_iterator, + (void *) &nxt_conf_vldt_environment }, + NXT_CONF_VLDT_END }; @@ -724,3 +731,40 @@ nxt_conf_vldt_group(nxt_conf_validation_t *vldt, char *group) return NXT_ERROR; } + + +static nxt_int_t +nxt_conf_vldt_environment(nxt_conf_validation_t *vldt, nxt_str_t *name, + nxt_conf_value_t *value) +{ + nxt_str_t str; + + if (name->length == 0) { + return nxt_conf_vldt_error(vldt, + "The environment name must not be empty."); + } + + if (nxt_memchr(name->start, '\0', name->length) != NULL) { + return nxt_conf_vldt_error(vldt, "The environment name must not " + "contain null character."); + } + + if (nxt_memchr(name->start, '=', name->length) != NULL) { + return nxt_conf_vldt_error(vldt, "The environment name must not " + "contain '=' character."); + } + + if (nxt_conf_type(value) != NXT_CONF_STRING) { + return nxt_conf_vldt_error(vldt, "The \"%V\" environment value must be " + "a string.", name); + } + + nxt_conf_get_string(value, &str); + + if (nxt_memchr(str.start, '\0', str.length) != NULL) { + return nxt_conf_vldt_error(vldt, "The \"%V\" environment value must " + "not contain null character.", name); + } + + return NXT_OK; +} diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index 46f49a72..f0a5903e 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -121,6 +121,12 @@ static nxt_conf_map_t nxt_common_app_conf[] = { NXT_CONF_MAP_CSTRZ, offsetof(nxt_common_app_conf_t, working_directory), }, + + { + nxt_string("environment"), + NXT_CONF_MAP_PTR, + offsetof(nxt_common_app_conf_t, environment), + }, }; |