diff --git a/src/exec.c b/src/exec.c index b5cff2e..7d47eef 100644 --- a/src/exec.c +++ b/src/exec.c @@ -524,6 +524,29 @@ free_exec: goto out; } +static int hyper_send_pod_finished(struct hyper_pod *pod) +{ + int ret = -1; + struct hyper_container *c; + uint8_t *data = NULL, *new; + int c_num = 0; + + list_for_each_entry(c, &pod->containers, list) { + c_num++; + new = realloc(data, c_num * 4); + if (new == NULL) + goto out; + + hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code); + data = new; + } + + ret = hyper_send_msg_block(ctl.chan.fd, PODFINISHED, c_num * 4, data); +out: + free(data); + return ret; +} + int hyper_release_exec(struct hyper_exec *exec, struct hyper_pod *pod) { @@ -550,8 +573,11 @@ int hyper_release_exec(struct hyper_exec *exec, return 0; if (pod->type == STOPPOD) { - /* stop pod manually */ + /* stop pod manually, hyper doesn't care the pod finished codes */ hyper_send_msg_block(ctl.chan.fd, ACK, 0, NULL); + } else if (pod->type == DESTROYPOD) { + /* shutdown vm manually, hyper doesn't care the pod finished codes */ + hyper_shutdown(); } else { /* send out pod finish message, hyper will decide if restart pod or not */ hyper_send_pod_finished(pod); @@ -562,7 +588,6 @@ int hyper_release_exec(struct hyper_exec *exec, } hyper_free_exec(exec); - return 0; } diff --git a/src/init.c b/src/init.c index d12f0bf..fe784e2 100644 --- a/src/init.c +++ b/src/init.c @@ -669,6 +669,18 @@ static void hyper_print_uptime(void) close(fd); } +static int hyper_destroy_pod(struct hyper_pod *pod) +{ + if (pod->init_pid == 0) { + /* Pod stopped, just shutdown */ + hyper_shutdown(); + } else { + /* Kill pod */ + hyper_term_all(pod); + } + return 0; +} + static int hyper_start_pod(char *json, int length) { struct hyper_pod *pod = &global_pod; @@ -684,7 +696,7 @@ static int hyper_start_pod(char *json, int length) } if (hyper_setup_pod(pod) < 0) { - hyper_shutdown(pod); + hyper_destroy_pod(pod); return -1; } @@ -1151,7 +1163,7 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len) //break; case DESTROYPOD: fprintf(stdout, "get DESTROYPOD message\n"); - hyper_shutdown(pod); + hyper_destroy_pod(pod); return 0; case EXECCMD: ret = hyper_exec_cmd((char *)buf->data + 8, len - 8); diff --git a/src/util.c b/src/util.c index ea2f1a8..5af7e50 100644 --- a/src/util.c +++ b/src/util.c @@ -339,7 +339,7 @@ int hyper_socketpair(int domain, int type, int protocol, int sv[2]) return 0; } -void hyper_unmount_all(void) +static void hyper_unmount_all(void) { FILE *mtab; struct mntent *mnt; @@ -384,38 +384,9 @@ void hyper_unmount_all(void) sync(); } -int hyper_send_pod_finished(struct hyper_pod *pod) +void hyper_shutdown() { - int ret = -1; - struct hyper_container *c; - uint8_t *data = NULL, *new; - int c_num = 0; - - list_for_each_entry(c, &pod->containers, list) { - c_num++; - new = realloc(data, c_num * 4); - if (new == NULL) - goto out; - - hyper_set_be32(new + ((c_num - 1) * 4), c->exec.code); - data = new; - } - - ret = hyper_send_msg_block(ctl.chan.fd, PODFINISHED, c_num * 4, data); -out: - free(data); - return ret; -} - -void hyper_shutdown(struct hyper_pod *pod) -{ - hyper_send_pod_finished(pod); - /* vm will shutdown immediately after we call reboot, - * no chance to send out eof message in release exec. - * send it out by ourself */ - hyper_cleanup_exec(pod); - + hyper_send_msg_block(ctl.chan.fd, ACK, 0, NULL); hyper_unmount_all(); - reboot(LINUX_REBOOT_CMD_POWER_OFF); } diff --git a/src/util.h b/src/util.h index 5087d2f..dbaa352 100644 --- a/src/util.h +++ b/src/util.h @@ -24,8 +24,6 @@ int hyper_setfd_cloexec(int fd); int hyper_setfd_block(int fd); int hyper_setfd_nonblock(int fd); int hyper_socketpair(int domain, int type, int protocol, int sv[2]); -void hyper_shutdown(struct hyper_pod *pod); -int hyper_send_pod_finished(struct hyper_pod *pod); -void hyper_unmount_all(void); +void hyper_shutdown(void); int hyper_insmod(char *module); #endif