From 0ebf7d8f1fb29b9155c1cbd0cb909a33a3b803cd Mon Sep 17 00:00:00 2001 From: Wang Xu Date: Thu, 23 Mar 2017 09:19:03 +0800 Subject: [PATCH] setup default PATH if it is not in the specified env list for the issue hyperhq/hyperd#566 Signed-off-by: Wang Xu --- src/exec.c | 4 +++- src/util.c | 10 +++++++++- src/util.h | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/exec.c b/src/exec.c index d1f9e425..bec6ed2f 100644 --- a/src/exec.c +++ b/src/exec.c @@ -534,6 +534,7 @@ static int hyper_do_exec_cmd(struct hyper_exec *exec, int pipe, struct stdio_con // do the exec, no return static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) { + bool defaultPath = false; if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) { perror("sigprocmask restore mask failed"); goto exit; @@ -550,7 +551,8 @@ static void hyper_exec_process(struct hyper_exec *exec, struct stdio_config *io) } // set the process env - if (hyper_setup_env(exec->envs, exec->envs_num) < 0) { + defaultPath = (exec->argv[0][0] != '/') && (strcmp(exec->container_id, HYPERSTART_EXEC_CONTAINER) != 0); + if (hyper_setup_env(exec->envs, exec->envs_num, defaultPath) < 0) { fprintf(stderr, "setup env failed\n"); goto exit; } diff --git a/src/util.c b/src/util.c index dcc62702..7eec1ae5 100644 --- a/src/util.c +++ b/src/util.c @@ -30,16 +30,24 @@ char *read_cmdline(void) return NULL; } -int hyper_setup_env(struct env *envs, int num) +int hyper_setup_env(struct env *envs, int num, bool setPATH) { int i, ret = 0; struct env *env; + if (setPATH) { + ret = setenv("PATH", "/usr/local/bin:/usr/bin:/bin:.", 1); + if (ret < 0) { + perror("fail to setup default PATH"); + ret = -1; + } + } for (i = 0; i < num; i++) { env = &envs[i]; if (setenv(env->env, env->value, 1) < 0) { perror("fail to setup env"); ret = -1; + continue; } } diff --git a/src/util.h b/src/util.h index 163d70ea..29047186 100644 --- a/src/util.h +++ b/src/util.h @@ -18,7 +18,7 @@ struct env; #endif char *read_cmdline(void); -int hyper_setup_env(struct env *envs, int num); +int hyper_setup_env(struct env *envs, int num, bool setPATH); int hyper_find_sd(char *addr, char **dev); int hyper_list_dir(char *path); int hyper_copy_dir(char *src, char *dst);