From 56198c3acd064da2244a9d36a57ced4efdf7bc07 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Sep 2016 22:44:53 +0800 Subject: [PATCH 1/5] save ptymaster on ptyfd rather than pty slave Signed-off-by: Lai Jiangshan --- src/exec.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/exec.c b/src/exec.c index ace5ba0..b3ccaff 100644 --- a/src/exec.c +++ b/src/exec.c @@ -394,21 +394,15 @@ static int hyper_setup_exec_tty(struct hyper_exec *e) return -1; } - if (sprintf(ptmx, "%s/%d", path, e->ptyno) < 0) { - fprintf(stderr, "get ptmx path failed\n"); - return -1; - } + e->ptyfd = ptymaster; - e->ptyfd = open(ptmx, O_RDWR | O_NOCTTY | O_CLOEXEC); - fprintf(stdout, "get pty device for exec %s\n", ptmx); - - e->stdinev.fd = ptymaster; + e->stdinev.fd = dup(ptymaster); e->stdoutev.fd = dup(ptymaster); if (e->errseq == 0) { e->stderrev.fd = dup(e->stdoutev.fd); } fprintf(stdout, "%s pts event %p, fd %d %d\n", - __func__, &e->stdinev, ptymaster, e->ptyfd); + __func__, &e->stdinev, e->stdinev.fd, e->ptyfd); return 0; } From da72799c96d209324e4d20e9889b9bf1a3322a39 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Sep 2016 22:49:30 +0800 Subject: [PATCH 2/5] close ptymaster when failed Signed-off-by: Lai Jiangshan --- src/exec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/exec.c b/src/exec.c index b3ccaff..f563914 100644 --- a/src/exec.c +++ b/src/exec.c @@ -386,11 +386,13 @@ static int hyper_setup_exec_tty(struct hyper_exec *e) if (ioctl(ptymaster, TIOCSPTLCK, &unlock) < 0) { perror("ioctl unlock ptmx device failed"); + close(ptymaster); return -1; } if (ioctl(ptymaster, TIOCGPTN, &e->ptyno) < 0) { perror("ioctl get execcmd pty device failed"); + close(ptymaster); return -1; } From 2360c4dc3f78f1c9ef91d9ebcb13f33adb0117f0 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Sep 2016 23:07:07 +0800 Subject: [PATCH 3/5] use ptyslave instead of e->ptyfd to make code clean Signed-off-by: Lai Jiangshan --- src/exec.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/exec.c b/src/exec.c index f563914..2388272 100644 --- a/src/exec.c +++ b/src/exec.c @@ -417,17 +417,18 @@ static int hyper_dup_exec_tty(struct hyper_exec *e) if (e->tty) { char ptmx[512]; + int ptyslave; + 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) { + ptyslave = open(ptmx, O_RDWR | O_CLOEXEC); + if (ptyslave < 0 || ioctl(ptyslave, TIOCSCTTY, NULL) < 0) { perror("ioctl pty device for execcmd failed"); goto out; } - e->stdinfd = e->ptyfd; - e->stdoutfd = e->ptyfd; + e->stdinfd = ptyslave; + e->stdoutfd = ptyslave; if (e->errseq == 0) - e->stderrfd = e->ptyfd; + e->stderrfd = ptyslave; close(e->stdinev.fd); close(e->stdoutev.fd); close(e->stderrev.fd); @@ -450,6 +451,10 @@ static int hyper_dup_exec_tty(struct hyper_exec *e) goto out; } + /* + * we are going to execvp(), all of the e->stdinfd, e->stdoutfd and + * e->stderrfd are O_CLOEXEC, we don't need to close them explicitly + */ ret = 0; out: return ret; From 064879ff0a98ce7378f8c356f6d97350471ef7bb Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Sep 2016 23:16:12 +0800 Subject: [PATCH 4/5] rename hyper_dup_exec_tty() to hyper_install_process_stdio() Signed-off-by: Lai Jiangshan --- src/exec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/exec.c b/src/exec.c index 2388272..c652b7c 100644 --- a/src/exec.c +++ b/src/exec.c @@ -408,7 +408,7 @@ static int hyper_setup_exec_tty(struct hyper_exec *e) return 0; } -static int hyper_dup_exec_tty(struct hyper_exec *e) +static int hyper_install_process_stdio(struct hyper_exec *e) { int ret = -1; @@ -557,7 +557,7 @@ static void hyper_exec_process(struct hyper_exec *exec) goto exit; } - if (hyper_dup_exec_tty(exec) < 0) { + if (hyper_install_process_stdio(exec) < 0) { fprintf(stderr, "dup pts to exec stdio failed\n"); goto exit; } From 5ed9195bf8a0ec0b1ebf778cd747a48e520be9be Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Sep 2016 23:18:46 +0800 Subject: [PATCH 5/5] move setsid() out from hyper_install_process_stdio() Signed-off-by: Lai Jiangshan --- src/exec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/exec.c b/src/exec.c index c652b7c..b2323c5 100644 --- a/src/exec.c +++ b/src/exec.c @@ -413,7 +413,6 @@ static int hyper_install_process_stdio(struct hyper_exec *e) int ret = -1; fprintf(stdout, "%s\n", __func__); - setsid(); if (e->tty) { char ptmx[512]; @@ -557,6 +556,8 @@ static void hyper_exec_process(struct hyper_exec *exec) goto exit; } + setsid(); + if (hyper_install_process_stdio(exec) < 0) { fprintf(stderr, "dup pts to exec stdio failed\n"); goto exit;