iSulad/0008-fixbug-add-hostname-env-for-container.patch
haozi007 1469344a51 sync from openEuler
1. add hostname env;
2. fix uwait use after free;

Signed-off-by: haozi007 <liuhao27@huawei.com>
2022-09-29 19:13:58 +08:00

136 lines
5.1 KiB
Diff

From 502cf70a63afa5b5a728d7813b1ff1481badf63a Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Thu, 8 Sep 2022 20:29:36 +0800
Subject: [PATCH 08/11] [fixbug] add hostname env for container
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
.../executor/container_cb/execution_network.c | 16 +++++++------
src/daemon/modules/spec/specs.c | 13 +++++-----
src/daemon/modules/spec/specs_extend.c | 24 ++++++++++++++-----
3 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/src/daemon/executor/container_cb/execution_network.c b/src/daemon/executor/container_cb/execution_network.c
index fa0ec612..bbc35e80 100644
--- a/src/daemon/executor/container_cb/execution_network.c
+++ b/src/daemon/executor/container_cb/execution_network.c
@@ -890,17 +890,19 @@ static int create_default_hostname(const char *id, const char *rootpath, bool sh
char hostname_content[MAX_HOST_NAME_LEN + 2] = { 0 };
if (v2_spec->config->hostname == NULL) {
+ char hostname[MAX_HOST_NAME_LEN] = { 0 };
if (share_host) {
- char hostname[MAX_HOST_NAME_LEN] = { 0x00 };
ret = gethostname(hostname, sizeof(hostname));
- if (ret != 0) {
- ERROR("Get hostname error");
- goto out;
- }
- v2_spec->config->hostname = util_strdup_s(hostname);
} else {
- v2_spec->config->hostname = util_strdup_s("localhost");
+ // max length of hostname from ID is 12 + '\0'
+ nret = snprintf(hostname, 13, "%s", id);
+ ret = nret < 0 ? 1 : 0;
+ }
+ if (ret != 0) {
+ ERROR("Create hostname error");
+ goto out;
}
+ v2_spec->config->hostname = util_strdup_s(hostname);
}
nret = snprintf(file_path, PATH_MAX, "%s/%s/%s", rootpath, id, "hostname");
diff --git a/src/daemon/modules/spec/specs.c b/src/daemon/modules/spec/specs.c
index 44e38674..ec6385a6 100644
--- a/src/daemon/modules/spec/specs.c
+++ b/src/daemon/modules/spec/specs.c
@@ -2112,6 +2112,13 @@ int merge_all_specs(host_config *host_spec, const char *real_rootfs, container_c
goto out;
}
+ // should before merge process env
+ ret = merge_hostname(oci_spec, host_spec, v2_spec->config);
+ if (ret != 0) {
+ ERROR("Failed to merge hostname");
+ goto out;
+ }
+
ret = merge_process_conf(oci_spec, host_spec, v2_spec->config);
if (ret != 0) {
goto out;
@@ -2138,12 +2145,6 @@ int merge_all_specs(host_config *host_spec, const char *real_rootfs, container_c
goto out;
}
- ret = merge_hostname(oci_spec, host_spec, v2_spec->config);
- if (ret != 0) {
- ERROR("Failed to merge hostname");
- goto out;
- }
-
ret = make_annotations(oci_spec, v2_spec->config, host_spec);
if (ret != 0) {
ret = -1;
diff --git a/src/daemon/modules/spec/specs_extend.c b/src/daemon/modules/spec/specs_extend.c
index 6276a586..c8faa102 100644
--- a/src/daemon/modules/spec/specs_extend.c
+++ b/src/daemon/modules/spec/specs_extend.c
@@ -415,13 +415,19 @@ out:
int merge_env(oci_runtime_spec *oci_spec, const char **env, size_t env_len)
{
int ret = 0;
+ int nret = 0;
size_t new_size = 0;
size_t old_size = 0;
- size_t i = 0;
+ size_t i;
char **temp = NULL;
+ // 10 is lenght of "HOSTNAME=" and '\0'
+ char host_name_env[MAX_HOST_NAME_LEN + 10] = { 0 };
- if (env_len == 0 || env == NULL) {
- return 0;
+ nret = snprintf(host_name_env, sizeof(host_name_env), "HOSTNAME=%s", oci_spec->hostname);
+ if (nret < 0 || (size_t)nret >= sizeof(host_name_env)) {
+ ret = -1;
+ ERROR("Sprint failed");
+ goto out;
}
ret = make_sure_oci_spec_process(oci_spec);
@@ -429,13 +435,14 @@ int merge_env(oci_runtime_spec *oci_spec, const char **env, size_t env_len)
goto out;
}
- if (env_len > LIST_ENV_SIZE_MAX - oci_spec->process->env_len) {
+ if (env_len > LIST_ENV_SIZE_MAX - oci_spec->process->env_len - 1) {
ERROR("The length of envionment variables is too long, the limit is %lld", LIST_ENV_SIZE_MAX);
isulad_set_error_message("The length of envionment variables is too long, the limit is %d", LIST_ENV_SIZE_MAX);
ret = -1;
goto out;
}
- new_size = (oci_spec->process->env_len + env_len) * sizeof(char *);
+ // add 1 for hostname env
+ new_size = (oci_spec->process->env_len + env_len + 1) * sizeof(char *);
old_size = oci_spec->process->env_len * sizeof(char *);
ret = util_mem_realloc((void **)&temp, new_size, oci_spec->process->env, old_size);
if (ret != 0) {
@@ -445,7 +452,12 @@ int merge_env(oci_runtime_spec *oci_spec, const char **env, size_t env_len)
}
oci_spec->process->env = temp;
- for (i = 0; i < env_len; i++) {
+
+ // append hostname env into default oci spec env list
+ oci_spec->process->env[oci_spec->process->env_len] = util_strdup_s(host_name_env);
+ oci_spec->process->env_len++;
+
+ for (i = 0; i < env_len && env != NULL; i++) {
oci_spec->process->env[oci_spec->process->env_len] = util_strdup_s(env[i]);
oci_spec->process->env_len++;
}
--
2.25.1