From f0d9fc7bcec1fe43426101fdb72c1d8bd49e5483 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Fri, 25 Sep 2015 09:17:45 +0800 Subject: [PATCH] fix possible memory leak Signed-off-by: Gao feng --- src/exec.c | 15 +++++++------- src/init.c | 58 +++++++++++++++++++++++++++++++----------------------- src/util.c | 10 ++++++---- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/exec.c b/src/exec.c index e0a04f3..ae88c6e 100644 --- a/src/exec.c +++ b/src/exec.c @@ -369,8 +369,7 @@ int hyper_exec_cmd(char *json, int length) struct hyper_exec *exec; struct hyper_pod *pod = &global_pod; int stacksize = getpagesize() * 4; - void *stack = malloc(stacksize); - struct hyper_exec_arg arg = { + void *stack = NULL; struct hyper_exec_arg arg = { .pod = pod, .exec = NULL, .pipe = {-1, -1}, @@ -392,11 +391,6 @@ int hyper_exec_cmd(char *json, int length) goto out; } - if (stack == NULL) { - perror("fail to allocate stack for container init"); - goto out; - } - if (hyper_setup_exec_tty(exec) < 0) { fprintf(stderr, "setup exec tty failed\n"); goto out; @@ -408,6 +402,13 @@ int hyper_exec_cmd(char *json, int length) } arg.exec = exec; + + stack = malloc(stacksize); + if (stack == NULL) { + perror("fail to allocate stack for container init"); + goto out; + } + pid = clone(hyper_do_exec_cmd, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg); fprintf(stdout, "do_exec_cmd pid %d\n", pid); free(stack); diff --git a/src/init.c b/src/init.c index 7893c08..cdb793c 100644 --- a/src/init.c +++ b/src/init.c @@ -450,7 +450,7 @@ out: int hyper_start_containers(struct hyper_pod *pod) { int stacksize = getpagesize() * 4; - void *stack = malloc(stacksize); + void *stack = NULL; struct hyper_pod_arg arg = { .pod = pod, .ctl_pipe = {-1, -1}, @@ -458,16 +458,18 @@ int hyper_start_containers(struct hyper_pod *pod) int ret = -1, pid; uint32_t type; - if (stack == NULL) { - perror("fail to allocate stack for container init"); - goto out; - } if (pipe2(arg.ctl_pipe, O_CLOEXEC) < 0) { perror("create pipe between hyper init and pod init failed"); goto out; } + stack = malloc(stacksize); + if (stack == NULL) { + perror("fail to allocate stack for container init"); + goto out; + } + pid = clone(hyper_do_start_containers, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg); free(stack); if (pid < 0) { @@ -515,16 +517,17 @@ static int hyper_setup_container(struct hyper_pod *pod) uint32_t type; void *stack; + int ret = -1; if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, arg.ctl_pipe) < 0) { perror("create pipe between hyper init and pod init failed"); - return -1; + goto out; } stack = malloc(stacksize); if (stack == NULL) { perror("fail to allocate stack for container init"); - return -1; + goto out; } arg.pod = pod; @@ -533,41 +536,45 @@ static int hyper_setup_container(struct hyper_pod *pod) free(stack); if (pod->init_pid < 0) { perror("create container init process failed"); - return -1; + goto out; } fprintf(stdout, "pod init pid %d\n", pod->init_pid); - close(arg.ctl_pipe[1]); - ctl.ctl.fd = arg.ctl_pipe[0]; - /* Wait for container start */ - if (hyper_get_type_block(ctl.ctl.fd, &type) < 0) { + if (hyper_get_type_block(arg.ctl_pipe[0], &type) < 0) { perror("get container init ready message failed"); - return -1; + goto out; } if (type != READY) { fprintf(stderr, "get incorrect message type %d, expect READY\n", type); - return -1; + goto out; } if (hyper_start_containers(pod) < 0) { fprintf(stderr, "start containers failed\n"); - return -1; + goto out; } - if (hyper_setfd_cloexec(ctl.ctl.fd) < 0) { + if (hyper_setfd_cloexec(arg.ctl_pipe[0]) < 0) { perror("set ctl pipe fd FD_CLOEXEC failed"); - return -1; + 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) { - return -1; + goto out; } - return 0; + ret = 0; +out: + close(arg.ctl_pipe[1]); + if (ret < 0) { + close(arg.ctl_pipe[0]); + } + return ret; } #ifdef WITH_VBOX @@ -894,15 +901,10 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ .pipe = {-1, -1}, }; int stacksize = getpagesize() * 4; - void *stack = malloc(stacksize); + void *stack = NULL; int pid, ret = -1; uint32_t type; - if (stack == NULL) { - perror("fail to allocate stack for container init"); - goto out; - } - fprintf(stdout, "%s\n", __func__); memset(&reader, 0, sizeof(reader)); @@ -932,6 +934,12 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ arg.data = data; sprintf(arg.root, "/tmp/hyper/%s/root/%s/", c->id, c->rootfs); + stack = malloc(stacksize); + if (stack == NULL) { + perror("fail to allocate stack for container init"); + goto out; + } + pid = clone(hyper_do_cmd_read_file, stack + stacksize, CLONE_VM| SIGCHLD, &arg); free(stack); if (pid < 0) { diff --git a/src/util.c b/src/util.c index adfdd29..57c5ca1 100644 --- a/src/util.c +++ b/src/util.c @@ -57,6 +57,7 @@ int hyper_list_dir(char *path) for (i = 0; i < num; i++) { dir = list[i]; fprintf(stdout, "%s get %s\n", path, dir->d_name); + free(dir); } free(list); @@ -192,8 +193,7 @@ int hyper_insmod(char *module) ret = 0; out: close(fd); - if (buf) - free(buf); + free(buf); return ret; err: @@ -426,13 +426,15 @@ void hyper_kill_all(void) int hyper_send_finish(struct hyper_pod *pod) { - int i; + int i, ret; uint8_t *data = calloc(pod->c_num, 4); for (i = 0; i < pod->c_num; i++) hyper_set_be32(data + (i * 4), pod->c[i].exec.code); - return hyper_send_msg(ctl.chan.fd, FINISH, pod->c_num * 4, data); + ret = hyper_send_msg(ctl.chan.fd, FINISH, pod->c_num * 4, data); + free(data); + return ret; } void hyper_shutdown(struct hyper_pod *pod)