From 99ee15c7312636971de03eec53266c8352879273 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Sat, 24 Oct 2015 20:51:39 +0800 Subject: [PATCH] send eof message to hyper before shutdown vm When hyperstart received destory-vm message, some execs are still running, send eof message immediately. Signed-off-by: Gao feng --- src/exec.c | 14 ++++++++++---- src/net.c | 19 +++++-------------- src/util.c | 50 ++++++-------------------------------------------- 3 files changed, 21 insertions(+), 62 deletions(-) diff --git a/src/exec.c b/src/exec.c index 7d66172..84041c8 100644 --- a/src/exec.c +++ b/src/exec.c @@ -636,14 +636,20 @@ int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code) return 0; } -/* + void hyper_cleanup_exec(struct hyper_pod *pod) { struct hyper_exec *exec, *next; + uint8_t buf[12]; + if (hyper_setfd_block(ctl.tty.fd) < 0) + return; + + hyper_set_be32(buf + 8, 12); list_for_each_entry_safe(exec, next, &pod->exec_head, list) { - fprintf(stdout, "cleanup exec seq %" PRIu64 "\n", exec->seq); - hyper_release_exec(exec, pod); + fprintf(stdout, "send eof for exec seq %" PRIu64 "\n", exec->seq); + hyper_set_be64(buf, exec->seq); + if (hyper_send_data(ctl.tty.fd, buf, 12) < 0) + fprintf(stderr, "send eof failed\n"); } } -*/ diff --git a/src/net.c b/src/net.c index 822d18b..865af9f 100644 --- a/src/net.c +++ b/src/net.c @@ -10,6 +10,7 @@ #include #include "hyper.h" +#include "util.h" #include "../config.h" void hyper_set_be32(uint8_t *buf, uint32_t val) @@ -115,14 +116,9 @@ int hyper_get_type_block(int fd, uint32_t *type) { int ret = 0, flags; - flags = fcntl(fd, F_GETFL, 0); + flags = hyper_setfd_block(fd); if (flags < 0) { - fprintf(stderr, "%s get fd flag failed\n", __func__); - return -1; - } - - if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) { - perror("set fd BLOCK failed"); + fprintf(stderr, "%s fail to set fd block\n", __func__); return -1; } @@ -144,14 +140,9 @@ int hyper_send_type_block(int fd, uint32_t type, int need_ack) int ret = 0, flags; uint32_t t; - flags = fcntl(fd, F_GETFL, 0); + flags = hyper_setfd_block(fd); if (flags < 0) { - fprintf(stderr, "get fd flag failed\n"); - return -1; - } - - if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) { - perror("set fd BLOCK failed"); + fprintf(stderr, "%s fail to set fd block\n", __func__); return -1; } diff --git a/src/util.c b/src/util.c index 57c5ca1..1ff507d 100644 --- a/src/util.c +++ b/src/util.c @@ -304,7 +304,7 @@ int hyper_setfd_block(int fd) return -1; } - return 0; + return flags; } int hyper_setfd_nonblock(int fd) @@ -321,7 +321,7 @@ int hyper_setfd_nonblock(int fd) return -1; } - return 0; + return flags; } int hyper_socketpair(int domain, int type, int protocol, int sv[2]) @@ -384,46 +384,6 @@ void hyper_unmount_all(void) sync(); } -void hyper_kill_all(void) -{ - int npids = 0; - int index = 0; - int pid; - DIR *dp; - struct dirent *de; - pid_t *pids = NULL; - - dp = opendir("/proc"); - if (dp == NULL) - return; - - while ((de = readdir(dp)) && de != NULL) { - if (!isdigit(de->d_name[0])) - continue; - pid = atoi(de->d_name); - if (pid == 1) - continue; - if (index <= npids) { - pids = realloc(pids, npids + 16384); - if (pids == NULL) - return; - npids += 16384; - } - - pids[index++] = pid; - } - - fprintf(stdout, "Sending SIGTERM\n"); - - for (--index; index >= 0; --index) { - fprintf(stdout, "kill process %d\n", pids[index]); - kill(pids[index], SIGTERM); - } - - free(pids); - closedir(dp); -} - int hyper_send_finish(struct hyper_pod *pod) { int i, ret; @@ -440,8 +400,10 @@ int hyper_send_finish(struct hyper_pod *pod) void hyper_shutdown(struct hyper_pod *pod) { hyper_send_finish(pod); - - //hyper_kill_all(); + /* 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_unmount_all();