reagrd exec/start-container success after fork/clone success

If fork/clone failed, return ERROR message to outside.
otherwise ragard exec/start-container success, add
container/exec to exec list, waitpid in init will get
the exec status.

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2016-05-17 15:33:01 +08:00
parent 783d711be1
commit 9615c24fca
3 changed files with 8 additions and 54 deletions
+3 -25
View File
@@ -402,9 +402,9 @@ static int container_setup_workdir(struct hyper_container *container)
return 0;
}
static int container_setup_tty(int fd, struct hyper_container *container)
static int container_setup_tty(struct hyper_container *container)
{
return hyper_dup_exec_tty(fd, &container->exec);
return hyper_dup_exec_tty(&container->exec);
}
static int hyper_rescan_scsi(void)
@@ -457,7 +457,6 @@ struct hyper_container_arg {
struct hyper_pod *pod;
int ipcns;
int utsns;
int pipe[2];
};
static int hyper_container_init(void *data)
@@ -617,7 +616,7 @@ static int hyper_container_init(void *data)
fflush(stdout);
if (container_setup_tty(arg->pipe[1], container) < 0) {
if (container_setup_tty(container) < 0) {
fprintf(stdout, "setup tty failed\n");
goto fail;
}
@@ -625,10 +624,7 @@ static int hyper_container_init(void *data)
execvp(container->exec.argv[0], container->exec.argv);
perror("exec container command failed");
_exit(-1);
fail:
hyper_send_type(arg->pipe[1], ERROR);
_exit(-1);
}
@@ -666,11 +662,9 @@ 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];
uint32_t type;
void *stack;
int pid;
@@ -685,11 +679,6 @@ 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_watch_exec_pty(&container->exec, pod) < 0) {
fprintf(stderr, "faile to watch container pty\n");
goto fail;
@@ -715,24 +704,13 @@ 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:
close(arg.pipe[0]);
close(arg.pipe[1]);
close(container->ns);
hyper_reset_event(&container->exec.stdinev);
hyper_reset_event(&container->exec.stdoutev);
+4 -28
View File
@@ -390,7 +390,7 @@ int hyper_setup_exec_tty(struct hyper_exec *e)
return 0;
}
int hyper_dup_exec_tty(int to, struct hyper_exec *e)
int hyper_dup_exec_tty(struct hyper_exec *e)
{
int ret = -1;
@@ -416,7 +416,6 @@ int hyper_dup_exec_tty(int to, struct hyper_exec *e)
}
fflush(stdout);
hyper_send_type(to, READY);
if (dup2(e->stdinfd, STDIN_FILENO) < 0) {
perror("dup tty device to stdin failed");
@@ -535,8 +534,7 @@ static int hyper_do_exec_cmd(void *data)
struct hyper_exec_arg *arg = data;
struct hyper_exec *exec = arg->exec;
struct hyper_pod *pod = arg->pod;
int pipe[2] = {-1, -1}, pid;
int ret = -1;
int pid, ret = -1;
if (exec->id) {
char path[512];
@@ -558,11 +556,6 @@ static int hyper_do_exec_cmd(void *data)
close(pidns);
}
if (pipe2(pipe, O_CLOEXEC) < 0) {
perror("create pipe in exec command failed");
goto out;
}
if (hyper_watch_exec_pty(exec, pod) < 0) {
fprintf(stderr, "add pts master event failed\n");
goto out;
@@ -573,14 +566,6 @@ static int hyper_do_exec_cmd(void *data)
perror("fail to fork");
goto out;
} else if (pid > 0) {
uint32_t type;
if (hyper_get_type(pipe[0], &type) < 0 || type != READY) {
fprintf(stderr, "hyper init doesn't get execcmd ready message\n");
goto out;
}
fprintf(stdout, "hyper init get ready message\n");
exec->pid = pid;
//TODO combin ref++ and add to list.
list_add_tail(&exec->list, &pod->exec_head);
@@ -600,25 +585,16 @@ static int hyper_do_exec_cmd(void *data)
goto exit;
}
if (hyper_dup_exec_tty(pipe[1], exec) < 0) {
if (hyper_dup_exec_tty(exec) < 0) {
fprintf(stderr, "dup pts to exec stdio failed\n");
goto exit;
}
if (execvp(exec->argv[0], exec->argv) < 0) {
if (execvp(exec->argv[0], exec->argv) < 0)
perror("exec failed");
goto exit;
}
ret = 0;
exit:
close(pipe[0]);
close(pipe[1]);
hyper_send_type(pipe[1], ERROR);
_exit(ret);
out:
close(pipe[0]);
close(pipe[1]);
hyper_send_type(arg->pipe[1], ret ? ERROR : READY);
_exit(ret);
}
+1 -1
View File
@@ -48,7 +48,7 @@ int hyper_exec_cmd(char *json, int length);
int hyper_release_exec(struct hyper_exec *, struct hyper_pod *);
int hyper_container_execcmd(struct hyper_pod *pod);
int hyper_setup_exec_tty(struct hyper_exec *e);
int hyper_dup_exec_tty(int fd, struct hyper_exec *e);
int hyper_dup_exec_tty(struct hyper_exec *e);
struct hyper_exec *hyper_find_exec_by_pid(struct list_head *head, int pid);
struct hyper_exec *hyper_find_exec_by_seq(struct hyper_pod *pod, uint64_t seq);
int hyper_setup_exec_user(struct hyper_exec *e);