mirror of
https://github.com/clearlinux/hyperstart.git
synced 2026-08-19 04:17:40 +00:00
Merge pull request #69 from laijs/fix-exec-part1-cleanup
Fix exec part1 cleanup
This commit is contained in:
+8
-8
@@ -197,6 +197,11 @@ static int container_setup_mount(struct hyper_container *container)
|
||||
if (symlink("/dev/pts/ptmx", "./dev/ptmx") < 0)
|
||||
perror("link /dev/pts/ptmx to /dev/ptmx failed");
|
||||
|
||||
symlink("/proc/self/fd", "./dev/fd");
|
||||
symlink("/proc/self/fd/0", "./dev/stdin");
|
||||
symlink("/proc/self/fd/1", "./dev/stdout");
|
||||
symlink("/proc/self/fd/2", "./dev/stderr");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -331,10 +336,10 @@ static int container_setup_workdir(struct hyper_container *container)
|
||||
{
|
||||
if (container->initialize) {
|
||||
// create workdir
|
||||
hyper_mkdir(container->workdir);
|
||||
hyper_mkdir(container->exec.workdir);
|
||||
}
|
||||
|
||||
if (container->workdir && chdir(container->workdir) < 0) {
|
||||
if (container->exec.workdir && chdir(container->exec.workdir) < 0) {
|
||||
perror("change work directory failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -435,7 +440,7 @@ static int hyper_container_init(void *data)
|
||||
else
|
||||
unsetenv("TERM");
|
||||
|
||||
if (hyper_setup_env(container->envs, container->envs_num) < 0) {
|
||||
if (hyper_setup_env(container->exec.envs, container->exec.envs_num) < 0) {
|
||||
fprintf(stdout, "setup env failed\n");
|
||||
goto fail;
|
||||
}
|
||||
@@ -541,11 +546,6 @@ static int hyper_container_init(void *data)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
symlink("/proc/self/fd", "/dev/fd");
|
||||
symlink("/proc/self/fd/0", "/dev/stdin");
|
||||
symlink("/proc/self/fd/1", "/dev/stdout");
|
||||
symlink("/proc/self/fd/2", "/dev/stderr");
|
||||
|
||||
execvp(container->exec.argv[0], container->exec.argv);
|
||||
perror("exec container command failed");
|
||||
|
||||
|
||||
@@ -3,11 +3,6 @@
|
||||
|
||||
#include "exec.h"
|
||||
|
||||
struct env {
|
||||
char *env;
|
||||
char *value;
|
||||
};
|
||||
|
||||
struct volume {
|
||||
char *device;
|
||||
char *scsiaddr;
|
||||
@@ -34,14 +29,11 @@ struct hyper_container {
|
||||
char *rootfs;
|
||||
char *image;
|
||||
char *scsiaddr;
|
||||
char *workdir;
|
||||
char *fstype;
|
||||
struct volume *vols;
|
||||
struct env *envs;
|
||||
struct fsmap *maps;
|
||||
struct sysctl *sys;
|
||||
int vols_num;
|
||||
int envs_num;
|
||||
int maps_num;
|
||||
int sys_num;
|
||||
int ns;
|
||||
|
||||
+5
-4
@@ -227,8 +227,8 @@ int hyper_setup_exec_tty(struct hyper_exec *e)
|
||||
char ptmx[512], path[512];
|
||||
|
||||
if (e->seq == 0) {
|
||||
e->ptyfd = open("/dev/null", O_RDWR | O_NOCTTY | O_CLOEXEC);
|
||||
goto done;
|
||||
fprintf(stderr, "e->seq should be set\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!e->tty) { // don't use tty for stdio
|
||||
@@ -289,7 +289,6 @@ int hyper_setup_exec_tty(struct hyper_exec *e)
|
||||
|
||||
e->stdinev.fd = ptymaster;
|
||||
e->stdoutev.fd = dup(ptymaster);
|
||||
done:
|
||||
if (e->errseq == 0) {
|
||||
e->stderrev.fd = dup(e->stdoutev.fd);
|
||||
}
|
||||
@@ -422,7 +421,9 @@ int hyper_enter_container(struct hyper_pod *pod,
|
||||
/* TODO: wait for container finishing setup root */
|
||||
chdir("/");
|
||||
|
||||
ret = hyper_setup_env(c->envs, c->envs_num);
|
||||
if (hyper_setup_env(c->exec.envs, c->exec.envs_num) < 0)
|
||||
goto out;
|
||||
ret = hyper_setup_env(exec->envs, exec->envs_num);
|
||||
out:
|
||||
close(ipcns);
|
||||
close(utsns);
|
||||
|
||||
+16
-6
@@ -4,17 +4,16 @@
|
||||
#include "list.h"
|
||||
#include "event.h"
|
||||
|
||||
struct env {
|
||||
char *env;
|
||||
char *value;
|
||||
};
|
||||
|
||||
struct hyper_exec {
|
||||
struct list_head list;
|
||||
struct hyper_event stdinev;
|
||||
struct hyper_event stdoutev;
|
||||
struct hyper_event stderrev;
|
||||
char *id;
|
||||
char **argv;
|
||||
int argc;
|
||||
int tty; // use tty or not
|
||||
uint64_t seq;
|
||||
uint64_t errseq;
|
||||
int pid;
|
||||
int ptyno;
|
||||
int init;
|
||||
@@ -26,6 +25,17 @@ struct hyper_exec {
|
||||
uint8_t code;
|
||||
uint8_t exit;
|
||||
uint8_t ref;
|
||||
|
||||
// configs
|
||||
char *id;
|
||||
struct env *envs;
|
||||
int envs_num;
|
||||
char **argv;
|
||||
int argc;
|
||||
int tty; // use tty or not
|
||||
uint64_t seq;
|
||||
uint64_t errseq;
|
||||
char *workdir;
|
||||
};
|
||||
|
||||
struct hyper_pod;
|
||||
|
||||
+3
-4
@@ -143,7 +143,7 @@ static void hyper_term_all(struct hyper_pod *pod)
|
||||
DIR *dp;
|
||||
struct dirent *de;
|
||||
pid_t *pids = NULL;
|
||||
struct hyper_container *c;
|
||||
struct hyper_exec *e;
|
||||
|
||||
dp = opendir("/proc");
|
||||
if (dp == NULL)
|
||||
@@ -175,8 +175,8 @@ static void hyper_term_all(struct hyper_pod *pod)
|
||||
free(pids);
|
||||
closedir(dp);
|
||||
|
||||
list_for_each_entry(c, &pod->containers, list)
|
||||
hyper_kill_process(c->exec.pid);
|
||||
list_for_each_entry(e, &pod->exec_head, list)
|
||||
hyper_kill_process(e->pid);
|
||||
}
|
||||
|
||||
static int hyper_handle_exit(struct hyper_pod *pod)
|
||||
@@ -692,7 +692,6 @@ static int hyper_cmd_write_file(char *json, int length)
|
||||
struct hyper_pod *pod = &global_pod;
|
||||
int pipe[2] = {-1, -1};
|
||||
int pid, mntns = -1, fd;
|
||||
char path[512];
|
||||
int len = 0, size, ret = -1;
|
||||
|
||||
fprintf(stdout, "%s\n", __func__);
|
||||
|
||||
+36
-47
@@ -157,17 +157,32 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t
|
||||
return i;
|
||||
}
|
||||
|
||||
static void container_free_cmd(struct hyper_container *c)
|
||||
static void container_cleanup_exec(struct hyper_exec *exec)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < c->exec.argc; i++) {
|
||||
free(c->exec.argv[i]);
|
||||
free(exec->id);
|
||||
exec->id = NULL;
|
||||
|
||||
free(exec->workdir);
|
||||
exec->workdir = NULL;
|
||||
|
||||
for (i = 0; i < exec->envs_num; i++) {
|
||||
free(exec->envs[i].env);
|
||||
free(exec->envs[i].value);
|
||||
}
|
||||
|
||||
free(c->exec.argv);
|
||||
c->exec.argv = NULL;
|
||||
c->exec.argc = 0;
|
||||
free(exec->envs);
|
||||
exec->envs = NULL;
|
||||
exec->envs_num = 0;
|
||||
|
||||
for (i = 0; i < exec->argc; i++) {
|
||||
free(exec->argv[i]);
|
||||
}
|
||||
|
||||
free(exec->argv);
|
||||
exec->argv = NULL;
|
||||
exec->argc = 0;
|
||||
}
|
||||
|
||||
static void container_free_volumes(struct hyper_container *c)
|
||||
@@ -317,21 +332,7 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
|
||||
return i;
|
||||
}
|
||||
|
||||
static void container_free_envs(struct hyper_container *c)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < c->envs_num; i++) {
|
||||
free(c->envs[i].env);
|
||||
free(c->envs[i].value);
|
||||
}
|
||||
|
||||
free(c->envs);
|
||||
c->envs = NULL;
|
||||
c->envs_num = 0;
|
||||
}
|
||||
|
||||
static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t *toks)
|
||||
static int container_parse_envs(struct hyper_exec *exec, char *json, jsmntok_t *toks)
|
||||
{
|
||||
int i = 0, j;
|
||||
|
||||
@@ -340,17 +341,17 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
|
||||
return -1;
|
||||
}
|
||||
|
||||
c->envs = calloc(toks[i].size, sizeof(*c->envs));
|
||||
if (c->envs == NULL) {
|
||||
exec->envs = calloc(toks[i].size, sizeof(*exec->envs));
|
||||
if (exec->envs == NULL) {
|
||||
fprintf(stderr, "allocate memory for env failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
c->envs_num = toks[i].size;
|
||||
fprintf(stdout, "envs num %d\n", c->envs_num);
|
||||
exec->envs_num = toks[i].size;
|
||||
fprintf(stdout, "envs num %d\n", exec->envs_num);
|
||||
|
||||
i++;
|
||||
for (j = 0; j < c->envs_num; j++) {
|
||||
for (j = 0; j < exec->envs_num; j++) {
|
||||
int i_env, next_env;
|
||||
|
||||
if (toks[i].type != JSMN_OBJECT) {
|
||||
@@ -361,13 +362,13 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
|
||||
i++;
|
||||
for (i_env = 0; i_env < next_env; i_env++, i++) {
|
||||
if (json_token_streq(json, &toks[i], "env")) {
|
||||
c->envs[j].env =
|
||||
exec->envs[j].env =
|
||||
(json_token_str(json, &toks[++i]));
|
||||
fprintf(stdout, "envs %d env %s\n", j, c->envs[j].env);
|
||||
fprintf(stdout, "envs %d env %s\n", j, exec->envs[j].env);
|
||||
} else if (json_token_streq(json, &toks[i], "value")) {
|
||||
c->envs[j].value =
|
||||
exec->envs[j].value =
|
||||
(json_token_str(json, &toks[++i]));
|
||||
fprintf(stdout, "envs %d value %s\n", j, c->envs[j].value);
|
||||
fprintf(stdout, "envs %d value %s\n", j, exec->envs[j].value);
|
||||
} else {
|
||||
fprintf(stdout, "get unknown section %s in envs\n",
|
||||
json_token_str(json, &toks[i]));
|
||||
@@ -438,20 +439,13 @@ void hyper_free_container(struct hyper_container *c)
|
||||
free(c->scsiaddr);
|
||||
c->scsiaddr = NULL;
|
||||
|
||||
free(c->workdir);
|
||||
c->workdir = NULL;
|
||||
|
||||
free(c->fstype);
|
||||
c->fstype = NULL;
|
||||
|
||||
free(c->exec.id);
|
||||
c->exec.id = NULL;
|
||||
|
||||
container_free_volumes(c);
|
||||
container_free_envs(c);
|
||||
container_free_sysctl(c);
|
||||
container_free_fsmap(c);
|
||||
container_free_cmd(c);
|
||||
container_cleanup_exec(&c->exec);
|
||||
|
||||
list_del_init(&c->list);
|
||||
free(c);
|
||||
@@ -524,8 +518,8 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
|
||||
fprintf(stdout, "container stderr seq %" PRIu64 "\n", c->exec.errseq);
|
||||
i++;
|
||||
} else if (json_token_streq(json, t, "workdir") && t->size == 1) {
|
||||
c->workdir = (json_token_str(json, &toks[++i]));
|
||||
fprintf(stdout, "container workdir %s\n", c->workdir);
|
||||
c->exec.workdir = (json_token_str(json, &toks[++i]));
|
||||
fprintf(stdout, "container workdir %s\n", c->exec.workdir);
|
||||
i++;
|
||||
} else if (json_token_streq(json, t, "image") && t->size == 1) {
|
||||
c->image = (json_token_str(json, &toks[++i]));
|
||||
@@ -550,7 +544,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
|
||||
goto fail;
|
||||
i += next;
|
||||
} else if (json_token_streq(json, t, "envs") && t->size == 1) {
|
||||
next = container_parse_envs(c, json, &toks[++i]);
|
||||
next = container_parse_envs(&c->exec, json, &toks[++i]);
|
||||
if (next < 0)
|
||||
goto fail;
|
||||
i += next;
|
||||
@@ -1098,13 +1092,8 @@ out:
|
||||
free(toks);
|
||||
return exec;
|
||||
fail:
|
||||
free(exec->id);
|
||||
for (i = 0; i < exec->argc; i++)
|
||||
free(exec->argv[i]);
|
||||
|
||||
free(exec->argv);
|
||||
container_cleanup_exec(exec);
|
||||
free(exec);
|
||||
|
||||
exec = NULL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user