From 8553460afe2e27d11ae691f3496ec62f594f8586 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 10 Oct 2015 13:50:52 +0800 Subject: [PATCH 1/5] return fail if start container failed Signed-off-by: Gao feng --- src/container.c | 5 ++++- src/init.c | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/container.c b/src/container.c index daa2a02..343583a 100644 --- a/src/container.c +++ b/src/container.c @@ -491,8 +491,10 @@ int hyper_start_container(struct hyper_container *container, close(arg.pipe[0]); close(arg.pipe[1]); - if (hyper_watch_exec_pty(&container->exec, pod) < 0) + if (hyper_watch_exec_pty(&container->exec, pod) < 0) { fprintf(stderr, "faile to watch container pty\n"); + goto fail; + } fprintf(stdout, "container %s init pid is %d\n", container->id, pid); return 0; @@ -503,6 +505,7 @@ fail: container->ns = -1; fprintf(stdout, "container %s init exit code %d\n", container->id, -1); container->exec.code = -1; + container->exec.seq = 0; return -1; } diff --git a/src/init.c b/src/init.c index c758bb8..0a91ebb 100644 --- a/src/init.c +++ b/src/init.c @@ -39,6 +39,7 @@ struct hyper_ctl ctl; static struct hyper_event_ops hyper_signal_ops; static int hyper_handle_exit(struct hyper_pod *pod); +static int hyper_stop_pod(struct hyper_pod *pod); static int hyper_set_win_size(char *json, int length) { @@ -453,7 +454,10 @@ static int hyper_do_start_containers(void *data) for (i = 0; i < pod->c_num; i++) { c = &pod->c[i]; list_add_tail(&c->exec.list, &pod->exec_head); - hyper_start_container(c, utsns, ipcns, pod); + if (hyper_start_container(c, utsns, ipcns, pod) < 0) { + fprintf(stderr, "fail to start container\n"); + goto out; + } } ret = 0; @@ -555,6 +559,7 @@ static int hyper_setup_container(struct hyper_pod *pod) goto out; } + ctl.ctl.fd = arg.ctl_pipe[0]; arg.pod = pod; pod->init_pid = clone(hyper_pod_init, stack + stacksize, flags, &arg); @@ -586,7 +591,6 @@ static int hyper_setup_container(struct hyper_pod *pod) goto out; } - ctl.ctl.fd = arg.ctl_pipe[0]; fprintf(stdout, "hyper_init_event hyper ctl pipe fd %d\n", ctl.ctl.fd); if (hyper_init_event(&ctl.ctl, &hyper_ctl_pipe_ops, pod) < 0 || hyper_add_event(ctl.efd, &ctl.ctl, EPOLLIN) < 0) { @@ -598,6 +602,7 @@ out: close(arg.ctl_pipe[1]); if (ret < 0) { close(arg.ctl_pipe[0]); + hyper_stop_pod(pod); } return ret; } From b14f68378ddfbbb491f6ca0fceac9e805bcc608c Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 10 Oct 2015 18:00:55 +0800 Subject: [PATCH 2/5] fix incorrect path of link file Signed-off-by: Gao feng --- src/net.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/net.c b/src/net.c index 4ece161..378f59e 100644 --- a/src/net.c +++ b/src/net.c @@ -335,14 +335,17 @@ static int hyper_remove_nic(char *device) { char path[256], real[128]; int fd; + ssize_t size; sprintf(path, "/sys/class/net/%s", device); - if (readlink(path, real, 128) < 0) { + size = readlink(path, real, 128); + if (size < 0 || size > 127) { perror("fail to read link directory"); return -1; } + real[size] = '\0'; sprintf(path, "/sys/%s/../../../remove", real + 5); fprintf(stdout, "get net sys path %s\n", path); @@ -869,7 +872,7 @@ void hyper_cleanup_dns(struct hyper_pod *pod) int fd = open("/tmp/hyper/resolv.conf", O_WRONLY| O_TRUNC); if (fd < 0) { - perror("open /tmp/resolv.conf failed"); + perror("open /tmp/hyper/resolv.conf failed"); return; } From 555bc46d1423685e04081e09ef76fcd3ad2a726b Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 10 Oct 2015 18:01:40 +0800 Subject: [PATCH 3/5] remove pipe between pod and hyper init Get rid of pipe by stopping pod in hyper init. Signed-off-by: Gao feng --- src/exec.c | 10 +++-- src/hyper.h | 2 - src/init.c | 120 +++++----------------------------------------------- 3 files changed, 16 insertions(+), 116 deletions(-) diff --git a/src/exec.c b/src/exec.c index 72114e2..c48e1df 100644 --- a/src/exec.c +++ b/src/exec.c @@ -515,16 +515,18 @@ int hyper_release_exec(struct hyper_exec *exec, fprintf(stdout, "%s container init exited, type %d, remains %d, policy %d\n", __func__, pod->type, pod->remains, pod->policy); - /* stop pod, should not restart container */ - if (pod->type == STOPPOD) - return 0; - if (exec->code) pod->code = exec->code; if (--pod->remains > 0) return 0; + /* stop pod, should not restart container */ + if (pod->type == STOPPOD) { + hyper_send_type(ctl.chan.fd, ACK); + return 0; + } + /* should shutdown? */ if (pod->policy == POLICY_NEVER || ((pod->policy == POLICY_ONFAILURE) && pod->code == 0)) { diff --git a/src/hyper.h b/src/hyper.h index 7740f5e..3dd7301 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -56,7 +56,6 @@ struct hyper_pod { uint8_t policy; int efd; struct hyper_event sig; - struct hyper_event ctl; }; struct hyper_win_size { @@ -83,7 +82,6 @@ struct hyper_ctl { struct hyper_event sig; struct hyper_event tty; struct hyper_event chan; - struct hyper_event ctl; }; int hyper_mkdir(char *hyper_path); diff --git a/src/init.c b/src/init.c index 0a91ebb..e65a4ab 100644 --- a/src/init.c +++ b/src/init.c @@ -179,25 +179,6 @@ static void hyper_term_all(struct hyper_pod *pod) } } -static int pod_ctl_pipe_handle(struct hyper_event *de, uint32_t len) -{ - struct hyper_buf *buf = &de->rbuf; - - fprintf(stdout, "%s\n", __func__); - - if (hyper_get_be32(buf->data) == STOPPOD) { - struct hyper_pod *pod = de->ptr; - - fprintf(stdout, "pod init get type STOPPOD, exit\n"); - hyper_term_all(pod); - hyper_reset_event(&pod->ctl); - hyper_unmount_all(); - _exit(0); - } - - return 0; -} - static int hyper_handle_exit(struct hyper_pod *pod) { int pid, status; @@ -258,14 +239,6 @@ static int hyper_signal_loop(struct hyper_event *de) return 0; } -static struct hyper_event_ops pod_ctl_pipe_ops = { - .read = hyper_event_read, - .handle = pod_ctl_pipe_handle, - .hup = hyper_event_hup, - .rbuf_size = 8, - .len_offset = 4, -}; - static int pod_init_loop(struct hyper_pod *pod) { int i, n; @@ -277,14 +250,6 @@ static int pod_init_loop(struct hyper_pod *pod) return -1; } - fprintf(stdout, "hyper_init_event pod ctl pipe event %p, ops %p, fd %d\n", - &pod->ctl, &pod_ctl_pipe_ops, pod->ctl.fd); - if (hyper_init_event(&pod->ctl, &pod_ctl_pipe_ops, pod) < 0 || - hyper_add_event(pod->efd, &pod->ctl, EPOLLIN) < 0) { - fprintf(stderr, "hyper add pod ctl pipe event failed\n"); - return -1; - } - fprintf(stdout, "hyper_init_event pod signal event %p, ops %p, fd %d\n", &pod->sig, &hyper_signal_ops, pod->sig.fd); if (hyper_init_event(&pod->sig, &hyper_signal_ops, NULL) < 0 || @@ -333,11 +298,6 @@ static int hyper_pod_init(void *data) close(ctl.tty.fd); pod->sig.fd = -1; - pod->ctl.fd = arg->ctl_pipe[1]; - if (hyper_setfd_cloexec(pod->ctl.fd) < 0) { - perror("set pod init ctl pipe fd FD_CLOEXEC failed"); - goto fail; - } sigemptyset(&mask); sigaddset(&mask, SIGCHLD); @@ -369,47 +329,26 @@ static int hyper_pod_init(void *data) goto fail; } - fprintf(stdout, "pod ctl_pipe %d\n", arg->ctl_pipe[1]); if (hyper_send_type(arg->ctl_pipe[1], READY) < 0) { fprintf(stderr, "container init send ready message failed\n"); goto fail; } + close(arg->ctl_pipe[1]); + pod_init_loop(pod); fprintf(stdout, "pod init exit\n"); out: _exit(-1); fail: - close(pod->sig.fd); hyper_send_type(arg->ctl_pipe[1], ERROR); + close(arg->ctl_pipe[1]); + close(pod->sig.fd); + goto out; } -static int hyper_ctl_pipe_handle(struct hyper_event *de, uint32_t len) -{ - struct hyper_buf *buf = &de->rbuf; - uint32_t type; - - /* container exec finish message */ - fprintf(stdout, "%s\n", __func__); - - type = hyper_get_be32(buf->data); - - switch (type) { - case ACK: - /* ACK only being sent on stoppod, in this case, ctl pipe - * fd is block, and ACK is the last message, exit loop. */ - fprintf(stdout, "hyper_ctl_pipe_loop get ack\n"); - return 1; - default: - fprintf(stdout, "get unknown type %" PRIu32"\n", type); - break; - } - - return 0; -} - static int hyper_do_start_containers(void *data) { int i, pidns, ipcns, utsns, ret; @@ -525,14 +464,6 @@ out: return ret; } -static struct hyper_event_ops hyper_ctl_pipe_ops = { - .read = hyper_event_read, - .handle = hyper_ctl_pipe_handle, - .hup = hyper_event_hup, - .rbuf_size = 256, - .len_offset = 4, -}; - static int hyper_setup_container(struct hyper_pod *pod) { int stacksize = getpagesize() * 4; @@ -559,7 +490,6 @@ static int hyper_setup_container(struct hyper_pod *pod) goto out; } - ctl.ctl.fd = arg.ctl_pipe[0]; arg.pod = pod; pod->init_pid = clone(hyper_pod_init, stack + stacksize, flags, &arg); @@ -586,22 +516,11 @@ static int hyper_setup_container(struct hyper_pod *pod) goto out; } - if (hyper_setfd_cloexec(arg.ctl_pipe[0]) < 0) { - perror("set ctl pipe fd FD_CLOEXEC failed"); - goto out; - } - - fprintf(stdout, "hyper_init_event hyper ctl pipe fd %d\n", ctl.ctl.fd); - if (hyper_init_event(&ctl.ctl, &hyper_ctl_pipe_ops, pod) < 0 || - hyper_add_event(ctl.efd, &ctl.ctl, EPOLLIN) < 0) { - goto out; - } - ret = 0; out: close(arg.ctl_pipe[1]); + close(arg.ctl_pipe[0]); if (ret < 0) { - close(arg.ctl_pipe[0]); hyper_stop_pod(pod); } return ret; @@ -1008,23 +927,8 @@ static void hyper_cleanup_shared(struct hyper_pod *pod) if (rmdir("/tmp/hyper/shared") < 0) perror("fail to delete /tmp/hyper/shared"); -} -static int hyper_send_stoppod(int fd) -{ - if (hyper_setfd_block(fd) < 0) { - perror("set fd BLOCK failed"); - return -1; - } - - if (hyper_send_type(fd, STOPPOD) < 0) { - fprintf(stderr, "send STOPPOD message failed\n"); - return -1; - } - - hyper_event_read(&ctl.ctl); - - return 0; + sync(); } static int hyper_stop_pod(struct hyper_pod *pod) @@ -1036,9 +940,8 @@ static int hyper_stop_pod(struct hyper_pod *pod) } pod->init_pid = 0; - /* Make hyper ctl_pipe blocked */ - hyper_send_stoppod(ctl.ctl.fd); + hyper_term_all(pod); hyper_cleanup_exec(pod); hyper_cleanup_container(pod); hyper_cleanup_network(pod); @@ -1047,10 +950,6 @@ static int hyper_stop_pod(struct hyper_pod *pod) free(pod->hostname); - sync(); - /* Wait for pod init ack */ - close(ctl.ctl.fd); - hyper_reset_event(&ctl.ctl); return 0; } @@ -1165,7 +1064,8 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) break; case STOPPOD: ret = hyper_stop_pod(pod); - break; + return 0; + //break; case DESTROYPOD: fprintf(stdout, "get DESTROYPOD message\n"); hyper_shutdown(pod); From f3f78303b15bec6c81a12d2e2e7021200688e66b Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 10 Oct 2015 20:49:20 +0800 Subject: [PATCH 4/5] cleanup pod after nobody use pod data Signed-off-by: Gao feng --- src/exec.c | 3 ++- src/hyper.h | 1 + src/init.c | 26 +++++++++++++++++--------- src/net.c | 11 ++++++++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/exec.c b/src/exec.c index c48e1df..64e42a7 100644 --- a/src/exec.c +++ b/src/exec.c @@ -524,6 +524,7 @@ int hyper_release_exec(struct hyper_exec *exec, /* stop pod, should not restart container */ if (pod->type == STOPPOD) { hyper_send_type(ctl.chan.fd, ACK); + hyper_cleanup_pod(pod); return 0; } @@ -531,7 +532,7 @@ int hyper_release_exec(struct hyper_exec *exec, if (pod->policy == POLICY_NEVER || ((pod->policy == POLICY_ONFAILURE) && pod->code == 0)) { hyper_send_finish(pod); - //hyper_shutdown(pod); + hyper_cleanup_pod(pod); return 0; } diff --git a/src/hyper.h b/src/hyper.h index 3dd7301..215eb72 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -88,6 +88,7 @@ int hyper_mkdir(char *hyper_path); int hyper_open_serial(char *tty); struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id); int hyper_start_containers(struct hyper_pod *pod); +void hyper_cleanup_pod(struct hyper_pod *pod); extern struct hyper_pod global_pod; extern struct hyper_ctl ctl; diff --git a/src/init.c b/src/init.c index e65a4ab..8af778b 100644 --- a/src/init.c +++ b/src/init.c @@ -911,6 +911,12 @@ out: return ret; } +static void hyper_cleanup_hostname(struct hyper_pod *pod) +{ + free(pod->hostname); + pod->hostname = NULL; +} + static void hyper_cleanup_shared(struct hyper_pod *pod) { if (pod->tag == NULL) { @@ -919,6 +925,7 @@ static void hyper_cleanup_shared(struct hyper_pod *pod) } free(pod->tag); + pod->tag = NULL; if (umount("/tmp/hyper/shared") < 0 && umount2("/tmp/hyper/shared", MNT_DETACH)) { perror("fail to umount 9p dir"); @@ -931,6 +938,16 @@ static void hyper_cleanup_shared(struct hyper_pod *pod) sync(); } +void hyper_cleanup_pod(struct hyper_pod *pod) +{ + hyper_cleanup_exec(pod); + hyper_cleanup_container(pod); + hyper_cleanup_network(pod); + hyper_cleanup_shared(pod); + hyper_cleanup_dns(pod); + hyper_cleanup_hostname(pod); +} + static int hyper_stop_pod(struct hyper_pod *pod) { fprintf(stdout, "hyper_stop_pod init_pid %d\n", pod->init_pid); @@ -940,16 +957,7 @@ static int hyper_stop_pod(struct hyper_pod *pod) } pod->init_pid = 0; - hyper_term_all(pod); - hyper_cleanup_exec(pod); - hyper_cleanup_container(pod); - hyper_cleanup_network(pod); - hyper_cleanup_dns(pod); - hyper_cleanup_shared(pod); - - free(pod->hostname); - return 0; } diff --git a/src/net.c b/src/net.c index 378f59e..bd4a58a 100644 --- a/src/net.c +++ b/src/net.c @@ -869,8 +869,17 @@ out: void hyper_cleanup_dns(struct hyper_pod *pod) { - int fd = open("/tmp/hyper/resolv.conf", O_WRONLY| O_TRUNC); + int fd, i; + for (i = 0; i < pod->d_num; i++) { + free(pod->dns[i]); + } + + free(pod->dns); + pod->dns = NULL; + pod->d_num = 0; + + fd = open("/tmp/hyper/resolv.conf", O_WRONLY| O_TRUNC); if (fd < 0) { perror("open /tmp/hyper/resolv.conf failed"); return; From 08fcf381e848c555e3fb74a919867a1043393468 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 10 Oct 2015 23:20:26 +0800 Subject: [PATCH 5/5] remove useless code Signed-off-by: Gao feng --- src/container.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/container.c b/src/container.c index 343583a..42d2160 100644 --- a/src/container.c +++ b/src/container.c @@ -400,7 +400,6 @@ static int hyper_container_init(void *data) _exit(-1); fail: - container->exec.code = -1; hyper_send_type(arg->pipe[1], ERROR); _exit(-1); }