From 21d63485b1ba745d07f70b88e9f26d01583d1880 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Thu, 10 Mar 2016 00:14:03 +0800 Subject: [PATCH] use pipes for stdio when non tty Signed-off-by: Lai Jiangshan --- src/event.c | 4 +++- src/exec.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/event.c b/src/event.c index 92aa03b..1e06d78 100644 --- a/src/event.c +++ b/src/event.c @@ -252,7 +252,9 @@ int hyper_handle_event(int efd, struct epoll_event *event) if (event->events & EPOLLERR) { fprintf(stderr, "get epoll err of not epool in event\n"); - return -1; + if (de->ops->hup) + de->ops->hup(de, efd); + return 0; } return 0; diff --git a/src/exec.c b/src/exec.c index 3d14d52..aabbf0e 100644 --- a/src/exec.c +++ b/src/exec.c @@ -174,6 +174,41 @@ struct hyper_event_ops err_ops = { /* don't need write buff, the stderr data is one way */ }; +static int hyper_setup_exec_notty(struct hyper_exec *e) +{ + if (e->errseq == 0) + return -1; + + int inpipe[2]; + if (pipe2(inpipe, O_CLOEXEC) < 0) { + fprintf(stderr, "creating stderr pipe failed\n"); + return -1; + } + hyper_setfd_nonblock(inpipe[1]); + e->stdinev.fd = inpipe[1]; + e->stdinfd = inpipe[0]; + + int outpipe[2]; + if (pipe2(outpipe, O_CLOEXEC) < 0) { + fprintf(stderr, "creating stderr pipe failed\n"); + return -1; + } + hyper_setfd_nonblock(outpipe[0]); + e->stdoutev.fd = outpipe[0]; + e->stdoutfd = outpipe[1]; + + int errpipe[2]; + if (pipe2(errpipe, O_CLOEXEC) < 0) { + fprintf(stderr, "creating stderr pipe failed\n"); + return -1; + } + hyper_setfd_nonblock(errpipe[0]); + e->stderrev.fd = errpipe[0]; + e->stderrfd = errpipe[1]; + + return 0; +} + int hyper_setup_exec_tty(struct hyper_exec *e) { int unlock = 0; @@ -185,6 +220,10 @@ int hyper_setup_exec_tty(struct hyper_exec *e) goto done; } + if (!e->tty) { // don't use tty for stdio + return hyper_setup_exec_notty(e); + } + if (e->errseq > 0) { int errpipe[2]; if (pipe2(errpipe, O_CLOEXEC) < 0) { @@ -196,19 +235,6 @@ int hyper_setup_exec_tty(struct hyper_exec *e) e->stderrfd = errpipe[1]; } - if (!e->tty) { // don't use tty for stdio - int iopair[2]; - if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, iopair) < 0) { - fprintf(stderr, "creating stdio pair failed\n"); - return -1; - } - hyper_setfd_nonblock(iopair[0]); - e->stdinev.fd = iopair[0]; - e->stdoutev.fd = dup(iopair[0]); - e->ptyfd = iopair[1]; - goto done; - } - if (e->id) { if (sprintf(path, "/tmp/hyper/%s/devpts/", e->id) < 0) { fprintf(stderr, "get ptmx path failed\n");