diff --git a/src/exec.c b/src/exec.c index ae88c6e..ea45fd0 100644 --- a/src/exec.c +++ b/src/exec.c @@ -132,7 +132,7 @@ int hyper_setup_exec_tty(struct hyper_exec *e) return -1; } - e->ptyfd = open(ptmx, O_RDWR | O_NOCTTY); + e->ptyfd = open(ptmx, O_RDWR | O_NOCTTY | O_CLOEXEC); fprintf(stdout, "get pty device for exec %s\n", ptmx); return 0; diff --git a/src/init.c b/src/init.c index aaf9c16..c758bb8 100644 --- a/src/init.c +++ b/src/init.c @@ -37,6 +37,7 @@ struct hyper_exec *global_exec; struct hyper_ctl ctl; +static struct hyper_event_ops hyper_signal_ops; static int hyper_handle_exit(struct hyper_pod *pod); static int hyper_set_win_size(char *json, int length) @@ -215,7 +216,7 @@ static int hyper_handle_exit(struct hyper_pod *pod) pid, WTERMSIG(status)); } - if (hyper_send_exec_eof(ctl.tty.fd, pod, pid, data[4]) < 0) + if (pod && hyper_send_exec_eof(ctl.tty.fd, pod, pid, data[4]) < 0) fprintf(stderr, "signal_loop send eof failed\n"); } @@ -283,6 +284,13 @@ static int pod_init_loop(struct hyper_pod *pod) return -1; } + fprintf(stdout, "hyper_init_event pod signal event %p, ops %p, fd %d\n", + &pod->sig, &hyper_signal_ops, pod->sig.fd); + if (hyper_init_event(&pod->sig, &hyper_signal_ops, NULL) < 0 || + hyper_add_event(pod->efd, &pod->sig, EPOLLIN) < 0) { + return -1; + } + events = calloc(MAXEVENTS, sizeof(*events)); while (1) { @@ -315,6 +323,7 @@ static int hyper_pod_init(void *data) { struct hyper_pod_arg *arg = data; struct hyper_pod *pod = arg->pod; + sigset_t mask; close(arg->ctl_pipe[0]); close(ctl.sig.fd); @@ -322,12 +331,27 @@ static int hyper_pod_init(void *data) close(ctl.chan.fd); close(ctl.tty.fd); + pod->sig.fd = -1; pod->ctl.fd = arg->ctl_pipe[1]; if (hyper_setfd_cloexec(pod->ctl.fd) < 0) { perror("set pod init ctl pipe fd FD_CLOEXEC failed"); goto fail; } + sigemptyset(&mask); + sigaddset(&mask, SIGCHLD); + + if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { + perror("sigprocmask SIGCHLD failed"); + goto fail; + } + + pod->sig.fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC); + if (pod->sig.fd < 0) { + perror("create pod signalfd failed"); + goto fail; + } + /* mount new proc directory */ if (umount("/proc") < 0) { perror("umount proc filesystem failed\n"); @@ -356,6 +380,7 @@ out: _exit(-1); fail: + close(pod->sig.fd); hyper_send_type(arg->ctl_pipe[1], ERROR); goto out; } @@ -508,7 +533,7 @@ static int hyper_setup_container(struct hyper_pod *pod) { int stacksize = getpagesize() * 4; int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | - CLONE_NEWUTS | CLONE_VM | SIGCHLD; + CLONE_NEWUTS; struct hyper_pod_arg arg = { .pod = NULL,