From e4b9466cec0eabcaa29fecc0f81d4c9c745facd2 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Tue, 10 Jan 2017 13:39:55 +0800 Subject: [PATCH] do not cleanup resources of pod when all of the containers are exited Signed-off-by: Gao feng --- src/container.c | 10 -- src/container.h | 1 - src/exec.c | 7 +- src/hyper.h | 1 - src/init.c | 41 ------- src/net.c | 287 ---------------------------------------------- src/net.h | 2 - src/portmapping.c | 100 ---------------- src/portmapping.h | 1 - 9 files changed, 1 insertion(+), 449 deletions(-) diff --git a/src/container.c b/src/container.c index 10dd1210..4f9ea37a 100644 --- a/src/container.c +++ b/src/container.c @@ -778,13 +778,3 @@ void hyper_cleanup_container(struct hyper_container *c, struct hyper_pod *pod) hyper_cleanup_container_portmapping(c, pod); hyper_free_container(c); } - -void hyper_cleanup_containers(struct hyper_pod *pod) -{ - struct hyper_container *c, *n; - - list_for_each_entry_safe(c, n, &pod->containers, list) - hyper_cleanup_container(c, pod); - - pod->remains = 0; -} diff --git a/src/container.h b/src/container.h index bcfc948e..eaf6531b 100644 --- a/src/container.h +++ b/src/container.h @@ -59,7 +59,6 @@ struct hyper_pod; int hyper_setup_container(struct hyper_container *container, struct hyper_pod *pod); struct hyper_container *hyper_find_container(struct hyper_pod *pod, const char *id); void hyper_cleanup_container(struct hyper_container *container, struct hyper_pod *pod); -void hyper_cleanup_containers(struct hyper_pod *pod); void hyper_free_container(struct hyper_container *c); static inline int hyper_has_container(struct hyper_pod *pod, const char *id) { diff --git a/src/exec.c b/src/exec.c index 9c36b103..17559430 100644 --- a/src/exec.c +++ b/src/exec.c @@ -715,16 +715,11 @@ static int hyper_release_exec(struct hyper_exec *exec) fprintf(stdout, "%s container init exited %s, remains %d\n", __func__, exec->pod->req_destroy?"manually":"automatically", exec->pod->remains); - // TODO send finish of this container and full cleanup - if (--exec->pod->remains > 0) - return 0; - - if (exec->pod->req_destroy) { + if (--exec->pod->remains == 0 && exec->pod->req_destroy) { /* shutdown vm manually, hyper doesn't care the pod finished codes */ hyper_pod_destroyed(0); } - hyper_cleanup_pod(exec->pod); return 0; } diff --git a/src/hyper.h b/src/hyper.h index c6e38f96..be2b6875 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -82,7 +82,6 @@ static inline int hyper_create(char *hyper_path) } int hyper_open_serial(char *tty); -void hyper_cleanup_pod(struct hyper_pod *pod); int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe); void hyper_pod_destroyed(int failed); int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len); diff --git a/src/init.c b/src/init.c index f5e3787e..ef5c356c 100644 --- a/src/init.c +++ b/src/init.c @@ -887,47 +887,6 @@ static void hyper_cmd_online_cpu_mem() } } -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->share_tag == NULL) { - fprintf(stdout, "no shared directory\n"); - return; - } - - free(pod->share_tag); - pod->share_tag = NULL; - if (umount(SHARED_DIR) < 0 && - umount2(SHARED_DIR, MNT_DETACH)) { - perror("fail to umount shared dir"); - return; - } - - if (rmdir(SHARED_DIR) < 0) - perror("fail to delete " SHARED_DIR); - - sync(); -} - -void hyper_cleanup_pod(struct hyper_pod *pod) -{ - if (pod->init_pid) { - hyper_kill_process(pod->init_pid); - pod->init_pid = 0; - } - hyper_cleanup_containers(pod); - hyper_cleanup_network(pod); - hyper_cleanup_shared(pod); - hyper_cleanup_dns(pod); - hyper_cleanup_portmapping(pod); - hyper_cleanup_hostname(pod); -} - static int hyper_setup_ctl_channel(char *name) { uint8_t buf[8]; diff --git a/src/net.c b/src/net.c index e896523f..f119de77 100644 --- a/src/net.c +++ b/src/net.c @@ -267,64 +267,6 @@ static int hyper_up_nic(struct rtnl_handle *rth, int ifindex) return 0; } -static int hyper_remove_nic(char *device) -{ - char path[256], real[128]; - int fd; - ssize_t size; - - sprintf(path, "/sys/class/net/%s", device); - - 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); - - fd = open(path, O_WRONLY); - if (fd < 0) { - perror("open file failed"); - return -1; - } - - if (write(fd, "1\n", 2) < 0) { - perror("write 1 to file failed"); - close(fd); - return 1; - } - - close(fd); - return 0; -} - -static int hyper_down_nic(struct rtnl_handle *rth, int ifindex) -{ - struct { - struct nlmsghdr n; - struct ifinfomsg i; - char buf[1024]; - } req; - - memset(&req, 0, sizeof(req)); - req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); - req.n.nlmsg_flags = NLM_F_REQUEST; - req.n.nlmsg_type = RTM_NEWLINK; - req.i.ifi_family = AF_UNSPEC; - req.i.ifi_change |= IFF_UP; - req.i.ifi_flags &= ~IFF_UP; - req.i.ifi_index = ifindex; - - if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) - return -1; - - return 0; -} - static int mask2bits(uint32_t netmask) { unsigned bits = 0; @@ -463,94 +405,6 @@ static int hyper_setup_route(struct rtnl_handle *rth, return 0; } -static int hyper_cleanup_route(struct rtnl_handle *rth, struct hyper_route *rt) -{ - uint32_t data; - struct { - struct nlmsghdr n; - struct rtmsg r; - char buf[1024]; - } req; - - if (!rt->dst) { - fprintf(stderr, "route dest is null\n"); - return -1; - } - - memset(&req, 0, sizeof(req)); - req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); - req.n.nlmsg_flags = NLM_F_REQUEST; - req.n.nlmsg_type = RTM_DELROUTE; - - req.r.rtm_family = AF_INET; - req.r.rtm_table = RT_TABLE_MAIN; - req.r.rtm_scope = RT_SCOPE_UNIVERSE; - req.r.rtm_type = RTN_UNICAST; - req.r.rtm_protocol = RTPROT_BOOT; - req.r.rtm_dst_len = 0; - - if (rt->gw) { - if (get_addr_ipv4((uint8_t *)&data, rt->gw) <= 0) { - fprintf(stderr, "get gw failed\n"); - return -1; - } - - if (addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &data, 4)) { - fprintf(stderr, "setup gateway attr failed\n"); - return -1; - } - } - - if (rt->device) { - int ifindex = hyper_get_ifindex(rt->device); - if (ifindex < 0) { - fprintf(stderr, "failed to get the ifindix of %s\n", rt->device); - return -1; - } - - if (addattr_l(&req.n, sizeof(req), RTA_OIF, &ifindex, 4)) { - fprintf(stderr, "setup oif attr failed\n"); - return -1; - } - } - - if (strcmp(rt->dst, "default") && strcmp(rt->dst, "any") && strcmp(rt->dst, "all")) { - unsigned mask; - char *slash = strchr(rt->dst, '/'); - - req.r.rtm_dst_len = 32; - - if (slash) - *slash = 0; - - if (get_addr_ipv4((uint8_t *)&data, rt->dst) <= 0) { - fprintf(stderr, "get dst failed\n"); - return -1; - } - - if (addattr_l(&req.n, sizeof(req), RTA_DST, &data, 4)) { - fprintf(stderr, "setup gateway attr failed\n"); - return -1; - } - - if (slash) { - if (get_netmask(&mask, slash + 1) < 0) { - fprintf(stderr, "get netmask failed\n"); - return -1; - } - req.r.rtm_dst_len = mask; - *slash = '/'; - } - } - - if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) { - fprintf(stderr, "rtnl talk failed\n"); - return -1; - } - - return 0; -} - static int hyper_set_interface_name(struct rtnl_handle *rth, int ifindex, char *new_device_name) @@ -659,81 +513,6 @@ static int hyper_setup_interface(struct rtnl_handle *rth, return 0; } -static int hyper_cleanup_interface(struct rtnl_handle *rth, - struct hyper_interface *iface) -{ - uint8_t data[4]; - unsigned mask; - struct { - struct nlmsghdr n; - struct ifaddrmsg ifa; - char buf[256]; - } req; - int ifindex; - struct hyper_ipaddress *ip; - - if (!iface->device || list_empty(&iface->ipaddresses)) { - fprintf(stderr, "interface information incorrect\n"); - return -1; - } - - memset(&req, 0, sizeof(req)); - req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); - req.n.nlmsg_flags = NLM_F_REQUEST; - req.n.nlmsg_type = RTM_DELADDR; - req.ifa.ifa_family = AF_INET; - - ifindex = hyper_get_ifindex(iface->device); - if (ifindex < 0) { - fprintf(stderr, "failed to get the ifindix of %s\n", iface->device); - return -1; - } - - req.ifa.ifa_index = ifindex; - req.ifa.ifa_scope = 0; - - list_for_each_entry(ip, &iface->ipaddresses, list) { - if (get_addr_ipv4((uint8_t *)&data, ip->addr) <= 0) { - fprintf(stderr, "get addr failed\n"); - return -1; - } - - if (addattr_l(&req.n, sizeof(req), IFA_LOCAL, &data, 4)) { - fprintf(stderr, "setup attr failed\n"); - return -1; - } - - if (get_netmask(&mask, ip->mask) < 0) { - fprintf(stderr, "get netamsk failed\n"); - return -1; - } - - req.ifa.ifa_prefixlen = mask; - fprintf(stdout, "interface get netamsk %d %s\n", req.ifa.ifa_prefixlen, ip->mask); - if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) { - perror("rtnl_talk failed"); - return -1; - } - } - - /* Don't down&remove lo device */ - if (strcmp(iface->device, "lo") == 0) { - return 0; - } - - if (hyper_down_nic(rth, ifindex) < 0) { - fprintf(stderr, "up device %d failed\n", ifindex); - return -1; - } - - if (hyper_remove_nic(iface->device) < 0) { - fprintf(stderr, "remove device %s failed\n", iface->device); - return -1; - } - - return 0; -} - int hyper_rescan(void) { int fd = open("/sys/bus/pci/rescan", O_WRONLY); @@ -797,48 +576,6 @@ int hyper_setup_network(struct hyper_pod *pod) return ret; } -void hyper_cleanup_network(struct hyper_pod *pod) -{ - int i; - struct rtnl_handle rth; - struct hyper_interface *iface; - struct hyper_route *rt; - - if (netlink_open(&rth) < 0) { - fprintf(stdout, "open netlink failed\n"); - return; - } - - for (i = 0; i < pod->r_num; i++) { - rt = &pod->rt[i]; - - if (hyper_cleanup_route(&rth, rt) < 0) - fprintf(stderr, "cleanup route failed\n"); - - free(rt->dst); - free(rt->gw); - free(rt->device); - } - - free(pod->rt); - pod->rt = NULL; - pod->r_num = 0; - - for (i = 0; i < pod->i_num; i++) { - iface = &pod->iface[i]; - - if (hyper_cleanup_interface(&rth, iface) < 0) - fprintf(stderr, "link down device %s failed\n", iface->device); - - hyper_free_interface(iface); - } - - free(pod->iface); - pod->iface = NULL; - pod->i_num = 0; - netlink_close(&rth); -} - int hyper_cmd_setup_interface(char *json, int length) { int ret = -1; @@ -939,27 +676,3 @@ int hyper_setup_dns(struct hyper_pod *pod) close(fd); return ret; } - -void hyper_cleanup_dns(struct hyper_pod *pod) -{ - int fd, i; - - if (pod->dns == NULL) - return; - - 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; - } - - close(fd); -} diff --git a/src/net.h b/src/net.h index afd63e55..fb1f4079 100644 --- a/src/net.h +++ b/src/net.h @@ -47,9 +47,7 @@ uint64_t hyper_get_be64(uint8_t *buf); int hyper_setup_network(struct hyper_pod *pod); int hyper_cmd_setup_interface(char *json, int length); int hyper_cmd_setup_route(char *json, int length); -void hyper_cleanup_network(struct hyper_pod *pod); int hyper_setup_dns(struct hyper_pod *pod); -void hyper_cleanup_dns(struct hyper_pod *pod); int hyper_get_type(int fd, uint32_t *type); int hyper_send_type(int fd, uint32_t type); int hyper_send_data_block(int fd, uint8_t *data, uint32_t len); diff --git a/src/portmapping.c b/src/portmapping.c index 48c5f6e2..e5b688fd 100644 --- a/src/portmapping.c +++ b/src/portmapping.c @@ -164,106 +164,6 @@ int hyper_setup_portmapping(struct hyper_pod *pod) return 0; } -void hyper_cleanup_portmapping(struct hyper_pod *pod) -{ - if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 && - pod->portmap_white_lists->e_num == 0)) { - return; - } - - // iptables -t filter -D hyperstart-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT - // iptables -t filter -D hyperstart-INPUT -p icmp -j ACCEPT - // iptables -t filter -D hyperstart-INPUT -i lo -j ACCEPT - // iptables -t filter -D hyperstart-INPUT -j DROP - // iptables -t filter -D INPUT -j hyperstart-DNPUT - // iptables -t nat -D hyperstart-PREROUTING -j RETURN - // iptables -t nat -D PREROUTING -j hyperstart-PREROUTING - // iptables -t filter -F hyperstart-INPUT - // iptables -t nat -F hyperstart-PREROUTING - // iptables -t filter -X hyperstart-INPUT - // iptables -t nat -X hyperstart-PREROUTING - const struct ipt_rule rules[] = { - { - .table = "filter", - .op = "-D", - .chain = "hyperstart-INPUT", - .rule = "-m state --state RELATED,ESTABLISHED -j ACCEPT", - }, - { - .table = "filter", - .op = "-D", - .chain = "hyperstart-INPUT", - .rule = "-p icmp -j ACCEPT", - }, - { - .table = "filter", - .op = "-D", - .chain = "hyperstart-INPUT", - .rule = "-i lo -j ACCEPT", - }, - { - .table = "filter", - .op = "-D", - .chain = "hyperstart-INPUT", - .rule = "-j DROP", - }, - { - .table = "nat", - .op = "-D", - .chain = "hyperstart-PREROUTING", - .rule = "-j RETURN", - }, - { - .table = "nat", - .op = "-D", - .chain = "PREROUTING", - .rule = "-j hyperstart-PREROUTING", - }, - { - .table = "filter", - .op = "-D", - .chain = "INPUT", - .rule = "-j hyperstart-INPUT", - }, - { - .table = "nat", - .op = "-F", - .chain = "hyperstart-PREROUTING", - .rule = NULL, - }, - { - .table = "nat", - .op = "-X", - .chain = "hyperstart-PREROUTING", - .rule = NULL, - }, - { - .table = "filter", - .op = "-F", - .chain = "hyperstart-INPUT", - .rule = NULL, - }, - { - .table = "filter", - .op = "-X", - .chain = "hyperstart-INPUT", - .rule = NULL, - }, - }; - - int i = 0; - for(i=0; i< sizeof(rules)/sizeof(struct ipt_rule); i++) { - if (hyper_setup_iptables_rule(rules[i])<0) { - return; - } - } - - free(pod->portmap_white_lists->internal_networks); - free(pod->portmap_white_lists->external_networks); - free(pod->portmap_white_lists); - pod->portmap_white_lists = NULL; -} - int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod) { if (pod->portmap_white_lists == NULL || (pod->portmap_white_lists->i_num == 0 && diff --git a/src/portmapping.h b/src/portmapping.h index 453605ae..4162618e 100644 --- a/src/portmapping.h +++ b/src/portmapping.h @@ -16,7 +16,6 @@ struct ipt_rule { struct hyper_pod; struct hyper_container; int hyper_setup_portmapping(struct hyper_pod *pod); -void hyper_cleanup_portmapping(struct hyper_pod *pod); int hyper_setup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod); void hyper_cleanup_container_portmapping(struct hyper_container *c, struct hyper_pod *pod);