From 2fb02c57b624187fe9c22b6f80cb962a902794a4 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Wed, 16 Sep 2015 23:39:01 +0800 Subject: [PATCH] set cloexec flag on socket pair Signed-off-by: Gao feng --- src/container.c | 5 +---- src/exec.c | 9 ++------- src/init.c | 8 ++++---- src/util.c | 16 ++++++++++++++++ src/util.h | 1 + 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/container.c b/src/container.c index bf3dbc3..93cf3f7 100644 --- a/src/container.c +++ b/src/container.c @@ -389,9 +389,6 @@ static int hyper_container_init(void *data) goto fail; } - close(arg->pipe[0]); - close(arg->pipe[1]); - execvp(container->exec.argv[0], container->exec.argv); perror("exec container command failed"); @@ -453,7 +450,7 @@ int hyper_start_container(struct hyper_container *container, goto fail; } - if (socketpair(PF_UNIX, SOCK_STREAM, 0, arg.pipe) < 0) { + if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, arg.pipe) < 0) { perror("create pipe between pod init execcmd failed"); goto fail; } diff --git a/src/exec.c b/src/exec.c index 424fd8b..00c9272 100644 --- a/src/exec.c +++ b/src/exec.c @@ -285,7 +285,7 @@ static int hyper_do_exec_cmd(void *data) } } - if (socketpair(PF_UNIX, SOCK_STREAM, 0, pipe) < 0) { + if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, pipe) < 0) { perror("create pipe in exec command failed"); _exit(-1); } @@ -333,11 +333,6 @@ static int hyper_do_exec_cmd(void *data) _exit(-1); } - close(pipe[0]); - close(pipe[1]); - close(arg->pipe[0]); - close(arg->pipe[1]); - if (execvp(exec->argv[0], exec->argv) < 0) { perror("exec failed"); _exit(-1); @@ -384,7 +379,7 @@ int hyper_exec_cmd(char *json, int length) goto out; } - if (socketpair(PF_UNIX, SOCK_STREAM, 0, arg.pipe) < 0) { + if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, arg.pipe) < 0) { perror("create pipe between pod init execcmd failed"); goto out; } diff --git a/src/init.c b/src/init.c index ede12c0..2bb7b66 100644 --- a/src/init.c +++ b/src/init.c @@ -388,7 +388,7 @@ static int hyper_ctl_pipe_handle(struct hyper_event *de, uint32_t len) return 0; } -static int hyper_enter_container_pidns(void *data) +static int hyper_do_start_containers(void *data) { int i, pidns, ipcns, utsns, ret; struct hyper_container *c; @@ -467,12 +467,12 @@ int hyper_start_containers(struct hyper_pod *pod) goto out; } - if (socketpair(PF_UNIX, SOCK_STREAM, 0, arg.ctl_pipe) < 0) { + if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, arg.ctl_pipe) < 0) { perror("create pipe between hyper init and pod init failed"); goto out; } - pid = clone(hyper_enter_container_pidns, stack + stacksize, CLONE_VM| CLONE_FILES, &arg); + pid = clone(hyper_do_start_containers, stack + stacksize, CLONE_VM| CLONE_FILES, &arg); free(stack); if (pid < 0) { perror("enter container pid ns failed"); @@ -520,7 +520,7 @@ static int hyper_setup_container(struct hyper_pod *pod) uint32_t type; void *stack; - if (socketpair(PF_UNIX, SOCK_STREAM, 0, arg.ctl_pipe) < 0) { + if (hyper_socketpair(PF_UNIX, SOCK_STREAM, 0, arg.ctl_pipe) < 0) { perror("create pipe between hyper init and pod init failed"); return -1; } diff --git a/src/util.c b/src/util.c index 1d541fd..adfdd29 100644 --- a/src/util.c +++ b/src/util.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -323,6 +324,21 @@ int hyper_setfd_nonblock(int fd) return 0; } +int hyper_socketpair(int domain, int type, int protocol, int sv[2]) +{ + if (socketpair(domain, type, protocol, sv) < 0) { + perror("socketpair failed"); + return -1; + } + + if (hyper_setfd_cloexec(sv[0]) < 0 || + hyper_setfd_cloexec(sv[1]) < 0) { + return -1; + } + + return 0; +} + void hyper_unmount_all(void) { FILE *mtab; diff --git a/src/util.h b/src/util.h index 9471f5c..c285a9d 100644 --- a/src/util.h +++ b/src/util.h @@ -23,6 +23,7 @@ int hyper_open_serial_dev(char *tty); 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_finish(struct hyper_pod *pod); void hyper_unmount_all(void);