diff --git a/src/container.c b/src/container.c index 93cf3f7..619820d 100644 --- a/src/container.c +++ b/src/container.c @@ -430,11 +430,13 @@ int hyper_start_container(struct hyper_container *container, { int stacksize = getpagesize() * 4; struct hyper_container_arg arg = { - .c = container, - .utsns = utsns, - .ipcns = ipcns, + .c = container, + .utsns = utsns, + .ipcns = ipcns, + .pipe = {-1, -1}, }; int flags = CLONE_NEWNS | SIGCHLD; + char path[128]; uint32_t type; void *stack; int pid; @@ -467,7 +469,13 @@ int hyper_start_container(struct hyper_container *container, perror("create child process failed"); goto fail; } + sprintf(path, "/proc/%d/ns/mnt", pid); container->exec.pid = pid; + container->ns = open(path, O_RDONLY | O_CLOEXEC); + if (container->ns < 0) { + perror("open container mount ns failed"); + goto fail; + } /* wait for ready message */ if (hyper_get_type_block(arg.pipe[0], &type) < 0 || type != READY) { @@ -484,6 +492,10 @@ int hyper_start_container(struct hyper_container *container, fprintf(stdout, "container %s init pid is %d\n", container->id, pid); return 0; fail: + close(arg.pipe[0]); + close(arg.pipe[1]); + close(container->ns); + container->ns = -1; fprintf(stdout, "container %s init exit code %d\n", container->id, -1); container->exec.code = -1; return -1; @@ -552,6 +564,7 @@ void hyper_cleanup_container(struct hyper_pod *pod) free(map->path); } free(c->maps); + close(c->ns); } free(pod->c); diff --git a/src/container.h b/src/container.h index 8ab04fd..6b2065b 100644 --- a/src/container.h +++ b/src/container.h @@ -33,6 +33,7 @@ struct hyper_container { int vols_num; int envs_num; int maps_num; + int ns; uint32_t code; struct hyper_exec exec; }; diff --git a/src/exec.c b/src/exec.c index 00c9272..10a275b 100644 --- a/src/exec.c +++ b/src/exec.c @@ -204,22 +204,21 @@ int hyper_enter_container(struct hyper_pod *pod, return -1; } - sprintf(path, "/proc/%d/ns/uts", c->exec.pid); + sprintf(path, "/proc/%d/ns/uts", pod->init_pid); utsns = open(path, O_RDONLY| O_CLOEXEC); if (utsns < 0) { perror("fail to open utsns of pod init"); goto out; } - sprintf(path, "/proc/%d/ns/ipc", c->exec.pid); + sprintf(path, "/proc/%d/ns/ipc", pod->init_pid); ipcns = open(path, O_RDONLY| O_CLOEXEC); if (ipcns < 0) { perror("fail to open ipcns of pod init"); goto out; } - sprintf(path, "/proc/%d/ns/mnt", c->exec.pid); - mntns = open(path, O_RDONLY| O_CLOEXEC); + mntns = c->ns; if (mntns < 0) { perror("fail to open mntns of pod init"); goto out; @@ -248,7 +247,6 @@ int hyper_enter_container(struct hyper_pod *pod, out: close(ipcns); close(utsns); - close(mntns); return ret; } diff --git a/src/init.c b/src/init.c index dcde8ba..1aa063e 100644 --- a/src/init.c +++ b/src/init.c @@ -752,8 +752,7 @@ static int hyper_cmd_write_file(char *json, int length) goto out; } - sprintf(path, "/proc/%d/ns/mnt", c->exec.pid); - mntns = open(path, O_RDONLY); + mntns = c->ns; if (mntns < 0) { perror("fail to open mnt ns"); goto out; @@ -813,7 +812,6 @@ static int hyper_cmd_write_file(char *json, int length) out: close(pipe[0]); close(pipe[1]); - close(mntns); free(writter.id); free(writter.file); free(writter.data); @@ -901,7 +899,6 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ int stacksize = getpagesize() * 4; void *stack = malloc(stacksize); int pid, ret = -1; - char path[128]; uint32_t type; if (stack == NULL) { @@ -927,8 +924,7 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ goto out; } - sprintf(path, "/proc/%d/ns/mnt", c->exec.pid); - arg.mntns = open(path, O_RDONLY); + arg.mntns = c->ns; if (arg.mntns < 0) { perror("fail to open mnt ns"); goto out; @@ -955,7 +951,6 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ out: close(arg.pipe[0]); close(arg.pipe[1]); - close(arg.mntns); free(reader.id); free(reader.file); diff --git a/src/parse.c b/src/parse.c index cf843e6..bd61d70 100644 --- a/src/parse.c +++ b/src/parse.c @@ -199,6 +199,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * c->exec.init = 1; c->exec.code = -1; + c->ns = -1; next_container = toks[i].size; fprintf(stdout, "next container %d\n", next_container);