Configuration of environment variables for application processes.

This commit is contained in:
Valentin Bartenev
2018-05-28 20:55:23 +03:00
parent 3befb08204
commit d7e6e2bd8c
4 changed files with 106 additions and 5 deletions

View File

@@ -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)
{

View File

@@ -9,6 +9,9 @@
#define _NXT_APPLICATION_H_INCLUDED_
#include <nxt_conf.h>
typedef enum {
NXT_APP_PYTHON,
NXT_APP_PHP,
@@ -71,6 +74,7 @@ struct nxt_common_app_conf_s {
nxt_str_t group;
char *working_directory;
nxt_conf_value_t *environment;
union {
nxt_python_app_conf_t python;

View File

@@ -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;
}

View File

@@ -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),
},
};