mirror of
https://github.com/clearlinux/hyperstart.git
synced 2026-09-03 20:21:47 +00:00
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:
+16
-3
@@ -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);
|
||||
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user