From 5f9a7ee0a270008de0315c5bca2a880874e5b049 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sat, 2 Apr 2016 11:10:48 +0800 Subject: [PATCH 1/2] reopen slave ptyfd for correcting the symlink path of the /dev/fd/1 before patch: root@ubuntu-4562002641:/# readlink /dev/fd/1 /tmp/hyper/656f914cd9c030214bf95aff8f3bf418c1c277f5b82a661a27a174c9c8ce901b/devpts/3 after patch: root@ubuntu-5512501784:/# readlink /dev/fd/1 /dev/pts/1 Signed-off-by: Lai Jiangshan --- src/exec.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/exec.c b/src/exec.c index 55cff3b..38986ac 100644 --- a/src/exec.c +++ b/src/exec.c @@ -290,12 +290,8 @@ int hyper_setup_exec_tty(struct hyper_exec *e) e->stdinev.fd = ptymaster; e->stdoutev.fd = dup(ptymaster); done: - - e->stdinfd = e->ptyfd; - e->stdoutfd = e->ptyfd; if (e->errseq == 0) { e->stderrev.fd = dup(e->stdoutev.fd); - e->stderrfd = e->ptyfd; } fprintf(stdout, "%s pts event %p, fd %d %d\n", __func__, &e->stdinev, ptymaster, e->ptyfd); @@ -309,9 +305,19 @@ int hyper_dup_exec_tty(int to, struct hyper_exec *e) fprintf(stdout, "%s\n", __func__); setsid(); - if (e->tty && (ioctl(e->ptyfd, TIOCSCTTY, NULL) < 0)) { - perror("ioctl pty device for execcmd failed"); - goto out; + if (e->tty) { + char ptmx[512]; + sprintf(ptmx, "/dev/pts/%d", e->ptyno); + // reopen slave ptyfd for correcting the symlink path of the /dev/fd/1 + e->ptyfd = open(ptmx, O_RDWR | O_CLOEXEC); + if (e->ptyfd < 0 || ioctl(e->ptyfd, TIOCSCTTY, NULL) < 0) { + perror("ioctl pty device for execcmd failed"); + goto out; + } + e->stdinfd = e->ptyfd; + e->stdoutfd = e->ptyfd; + if (e->errseq == 0) + e->stderrfd = e->ptyfd; } fflush(stdout); @@ -611,12 +617,9 @@ out: return ret; close_tty: close(exec->ptyfd); - if (exec->stdinfd != exec->ptyfd) - close(exec->stdinfd); - if (exec->stdoutfd != exec->ptyfd) - close(exec->stdoutfd); - if (exec->stderrfd != exec->ptyfd) - close(exec->stderrfd); + close(exec->stdinfd); + close(exec->stdoutfd); + close(exec->stderrfd); close(exec->stdinev.fd); close(exec->stdoutev.fd); close(exec->stderrev.fd); @@ -747,14 +750,11 @@ int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code) close(exec->ptyfd); exec->ptyfd = -1; - if (exec->stdinfd != exec->ptyfd) - close(exec->stdinfd); + close(exec->stdinfd); exec->stdinfd = -1; - if (exec->stdoutfd != exec->ptyfd) - close(exec->stdoutfd); + close(exec->stdoutfd); exec->stdoutfd = -1; - if (exec->stderrfd != exec->ptyfd) - close(exec->stderrfd); + close(exec->stderrfd); exec->stderrfd = -1; hyper_release_exec(exec, pod); From 1ad3ffcf63a509a4b21f31bcfbd21070d150bafc Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Sat, 2 Apr 2016 11:29:15 +0800 Subject: [PATCH 2/2] close ptymaster before exec ptymaster is dup-ed without close-on-exec, we need to colse it. Signed-off-by: Lai Jiangshan --- src/exec.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/exec.c b/src/exec.c index 38986ac..c1a2544 100644 --- a/src/exec.c +++ b/src/exec.c @@ -318,6 +318,9 @@ int hyper_dup_exec_tty(int to, struct hyper_exec *e) e->stdoutfd = e->ptyfd; if (e->errseq == 0) e->stderrfd = e->ptyfd; + close(e->stdinev.fd); + close(e->stdoutev.fd); + close(e->stderrev.fd); } fflush(stdout);