podinit: wait until container environment is ready

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2016-05-18 13:17:15 +08:00
parent 72fc9ade82
commit 9ce56b757e
+20
View File
@@ -457,6 +457,7 @@ struct hyper_container_arg {
struct hyper_pod *pod;
int ipcns;
int utsns;
int pipe[2];
};
static int hyper_container_init(void *data)
@@ -614,6 +615,7 @@ static int hyper_container_init(void *data)
goto fail;
}
hyper_send_type(arg->pipe[1], READY);
fflush(stdout);
if (container_setup_tty(container) < 0) {
@@ -632,6 +634,7 @@ static int hyper_container_init(void *data)
_exit(126);
fail:
hyper_send_type(arg->pipe[1], ERROR);
_exit(125);
}
@@ -669,10 +672,12 @@ int hyper_start_container(struct hyper_container *container,
.pod = pod,
.utsns = utsns,
.ipcns = ipcns,
.pipe = {-1, -1},
};
int flags = CLONE_NEWNS | SIGCHLD;
char path[128];
void *stack;
uint32_t type;
int pid;
if (container->image == NULL || container->exec.argv == NULL) {
@@ -681,6 +686,11 @@ int hyper_start_container(struct hyper_container *container,
goto fail;
}
if (pipe2(arg.pipe, O_CLOEXEC) < 0) {
perror("create pipe between pod init execcmd failed");
goto fail;
}
if (hyper_setup_pty(container) < 0) {
fprintf(stderr, "setup pty device for container failed\n");
goto fail;
@@ -711,10 +721,18 @@ int hyper_start_container(struct hyper_container *container,
goto fail;
}
/* wait for ready message */
if (hyper_get_type(arg.pipe[0], &type) < 0 || type != READY) {
fprintf(stderr, "wait for container started failed\n");
goto fail;
}
container->exec.pid = pid;
list_add_tail(&container->exec.list, &pod->exec_head);
container->exec.ref++;
close(arg.pipe[0]);
close(arg.pipe[1]);
fprintf(stdout, "container %s,init pid %d,ref %d\n", container->id, pid, container->exec.ref);
return 0;
fail:
@@ -727,6 +745,8 @@ fail:
container->exec.code = -1;
container->exec.seq = 0;
container->exec.ref = 0;
close(arg.pipe[0]);
close(arg.pipe[1]);
return -1;
}