From 50db29c634cca99e89b6c5d93de9f828087fcbf8 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Wed, 9 Mar 2016 16:01:17 +0800 Subject: [PATCH] add stdinfd/stdoutfd and rename errfd to stderrfd Signed-off-by: Lai Jiangshan --- src/exec.c | 40 ++++++++++++++++++++++++---------------- src/exec.h | 4 +++- src/parse.c | 8 ++++++-- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/exec.c b/src/exec.c index 43f6cf0..7b85cca 100644 --- a/src/exec.c +++ b/src/exec.c @@ -188,7 +188,7 @@ int hyper_setup_exec_tty(struct hyper_exec *e) } hyper_setfd_nonblock(errpipe[0]); e->errev.fd = errpipe[0]; - e->errfd = errpipe[1]; + e->stderrfd = errpipe[1]; } if (!e->tty) { // don't use tty for stdio @@ -245,8 +245,10 @@ int hyper_setup_exec_tty(struct hyper_exec *e) fprintf(stdout, "get pty device for exec %s\n", ptmx); done: + e->stdinfd = e->ptyfd; + e->stdoutfd = e->ptyfd; if (e->errseq == 0) - e->errfd = e->ptyfd; + e->stderrfd = e->ptyfd; fprintf(stdout, "%s pts event %p, fd %d %d\n", __func__, &e->e, e->e.fd, e->ptyfd); return 0; @@ -254,14 +256,12 @@ done: int hyper_dup_exec_tty(int to, struct hyper_exec *e) { - int fd = -1, ret = -1; + int ret = -1; fprintf(stdout, "%s\n", __func__); setsid(); - fd = e->ptyfd; - - if (e->tty && (ioctl(fd, TIOCSCTTY, NULL) < 0)) { + if (e->tty && (ioctl(e->ptyfd, TIOCSCTTY, NULL) < 0)) { perror("ioctl pty device for execcmd failed"); goto out; } @@ -269,25 +269,23 @@ int hyper_dup_exec_tty(int to, struct hyper_exec *e) fflush(stdout); hyper_send_type(to, READY); - if (dup2(fd, STDIN_FILENO) < 0) { + if (dup2(e->stdinfd, STDIN_FILENO) < 0) { perror("dup tty device to stdin failed"); goto out; } - if (dup2(fd, STDOUT_FILENO) < 0) { + if (dup2(e->stdoutfd, STDOUT_FILENO) < 0) { perror("dup tty device to stdout failed"); goto out; } - if (dup2(e->errfd, STDERR_FILENO) < 0) { + if (dup2(e->stderrfd, STDERR_FILENO) < 0) { perror("dup err pipe to stderr failed"); goto out; } ret = 0; out: - close(fd); - return ret; } @@ -561,8 +559,12 @@ out: return ret; close_tty: close(exec->ptyfd); - if (exec->errfd != exec->ptyfd) - close(exec->errfd); + 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->e.fd); free_exec: hyper_free_exec(exec); @@ -688,9 +690,15 @@ int hyper_handle_exec_exit(struct hyper_pod *pod, int pid, uint8_t code) close(exec->ptyfd); exec->ptyfd = -1; - if (exec->errfd != exec->ptyfd) - close(exec->errfd); - exec->errfd = -1; + if (exec->stdinfd != exec->ptyfd) + close(exec->stdinfd); + exec->stdinfd = -1; + if (exec->stdoutfd != exec->ptyfd) + close(exec->stdoutfd); + exec->stdoutfd = -1; + if (exec->stderrfd != exec->ptyfd) + close(exec->stderrfd); + exec->stderrfd = -1; hyper_release_exec(exec, pod); diff --git a/src/exec.h b/src/exec.h index 97cb986..96aab1d 100644 --- a/src/exec.h +++ b/src/exec.h @@ -18,7 +18,9 @@ struct hyper_exec { int ptyno; int init; int ptyfd; - int errfd; + int stdinfd; + int stdoutfd; + int stderrfd; uint8_t code; uint8_t exit; uint8_t ref; diff --git a/src/parse.c b/src/parse.c index fc529f6..6ef73ee 100644 --- a/src/parse.c +++ b/src/parse.c @@ -472,7 +472,9 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container * c->exec.e.fd = -1; c->exec.errev.fd = -1; c->exec.ptyfd = -1; - c->exec.errfd = -1; + c->exec.stdinfd = -1; + c->exec.stdoutfd = -1; + c->exec.stderrfd = -1; c->ns = -1; INIT_LIST_HEAD(&c->list); @@ -1027,7 +1029,9 @@ realloc: } exec->ptyfd = -1; - exec->errfd = -1; + exec->stdinfd = -1; + exec->stdoutfd = -1; + exec->stderrfd = -1; exec->e.fd = -1; exec->errev.fd = -1; INIT_LIST_HEAD(&exec->list);