[lxc-devel] [lxc/master] [attach]Set the containers's environment variables

lifeng68 on Github lxc-bot at linuxcontainers.org
Wed Feb 7 04:14:27 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 433 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180207/cf049d5c/attachment.bin>
-------------- next part --------------
From 42ae4b4a875e0c59733089a41e782616a82c3ae4 Mon Sep 17 00:00:00 2001
From: l00415420 <lifeng68 at huawei.com>
Date: Wed, 7 Feb 2018 07:39:28 -0500
Subject: [PATCH] [attach]Set the containers's environment variables

Set the same environment variables with container when attach
to the contaienr.

Signed-off-by: l00415420 <lifeng68 at huawei.com>
---
 src/lxc/attach.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index 6d3377f3e..ac8d9b2d5 100644
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -322,9 +322,12 @@ static int lxc_attach_drop_privs(struct lxc_proc_context_info *ctx)
 	return 0;
 }
 
-static int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy,
+static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx,
+				      enum lxc_attach_env_policy_t policy,
 				      char **extra_env, char **extra_keep)
 {
+	struct lxc_list *iterator;
+
 	if (policy == LXC_ATTACH_CLEAR_ENV) {
 		int path_kept = 0;
 		char **extra_keep_store = NULL;
@@ -400,6 +403,23 @@ static int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy,
 		return -1;
 	}
 
+	/* Set container environment variables.*/
+	if (init_ctx && init_ctx->container && init_ctx->container->lxc_conf) {
+		lxc_list_for_each(iterator, &init_ctx->container->lxc_conf->environment) {
+			char *env_tmp = strdup((char *)iterator->elem);
+			if (!env_tmp) {
+				SYSERROR("Failed to allocate memory for container environment "
+				         "variables.");
+				return -1;
+			}
+
+			if (putenv(env_tmp)) {
+				SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem);
+				return -1;
+			}
+		}
+	}
+
 	/* Set extra environment variables. */
 	if (extra_env) {
 		for (; *extra_env; extra_env++) {
@@ -798,7 +818,8 @@ static int attach_child_main(struct attach_clone_payload *payload)
 	/* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL)
 	 * if you want this to be a no-op).
 	 */
-	ret = lxc_attach_set_environment(options->env_policy,
+	ret = lxc_attach_set_environment(init_ctx,
+					 options->env_policy,
 					 options->extra_env_vars,
 					 options->extra_keep_env);
 	if (ret < 0)


More information about the lxc-devel mailing list