enter container namespace with opened ns fd

After process exit,the path of pid ns will disappear,
open it first to prohibit ns being released.

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-09-21 10:52:58 +08:00
parent 27f4b56e71
commit 330c46cdb2
5 changed files with 23 additions and 15 deletions
+16 -3
View File
@@ -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);
+1
View File
@@ -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;
};
+3 -5
View File
@@ -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;
}
+2 -7
View File
@@ -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);
+1
View File
@@ -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);