diff --git a/src/container.c b/src/container.c index daa2a02..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); } @@ -491,8 +490,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 +504,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/exec.c b/src/exec.c index 72114e2..64e42a7 100644 --- a/src/exec.c +++ b/src/exec.c @@ -515,21 +515,24 @@ 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); + hyper_cleanup_pod(pod); + return 0; + } + /* should shutdown? */ 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 7740f5e..215eb72 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,13 +82,13 @@ 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); 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 c758bb8..8af778b 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) { @@ -178,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; @@ -257,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; @@ -276,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 || @@ -332,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); @@ -368,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; @@ -453,7 +393,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; @@ -521,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; @@ -581,23 +516,12 @@ 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; - } - - 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) { - 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; } @@ -987,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) { @@ -995,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"); @@ -1003,23 +934,18 @@ static void hyper_cleanup_shared(struct hyper_pod *pod) if (rmdir("/tmp/hyper/shared") < 0) perror("fail to delete /tmp/hyper/shared"); + + sync(); } -static int hyper_send_stoppod(int fd) +void hyper_cleanup_pod(struct hyper_pod *pod) { - 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; + 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) @@ -1031,21 +957,7 @@ 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_cleanup_exec(pod); - hyper_cleanup_container(pod); - hyper_cleanup_network(pod); - hyper_cleanup_dns(pod); - hyper_cleanup_shared(pod); - - free(pod->hostname); - - sync(); - /* Wait for pod init ack */ - close(ctl.ctl.fd); - hyper_reset_event(&ctl.ctl); + hyper_term_all(pod); return 0; } @@ -1160,7 +1072,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); diff --git a/src/net.c b/src/net.c index 4ece161..bd4a58a 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); @@ -866,10 +869,19 @@ 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/resolv.conf failed"); + perror("open /tmp/hyper/resolv.conf failed"); return; }