From mboxrd@z Thu Jan 1 00:00:00 1970 From: Martin Lucina Subject: [PATCH v2] xenconsole: Allow non-interactive use Date: Fri, 24 Jul 2015 13:30:48 +0200 Message-ID: <1437737448-10733-1-git-send-email-martin@lucina.net> References: <1437664991.24746.10.camel@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1ZIbBl-0004kr-Ko for xen-devel@lists.xenproject.org; Fri, 24 Jul 2015 11:30:53 +0000 In-Reply-To: <1437664991.24746.10.camel@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Wei Liu , Martin Lucina , Ian Jackson , Ian Campbell , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org If xenconsole is run with stdin closed or redirected to /dev/null, console_loop() will return immediately due to failure to read from STDIN_FILENO. This patch tests if stdin and stdout are both connected to a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes. Existing behaviour when xenconsole is run from a terminal does not change. This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command. Signed-off-by: Martin Lucina Cc: Ian Jackson Cc: Stefano Stabellini Cc: Ian Campbell Cc: Wei Liu --- tools/console/client/main.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tools/console/client/main.c b/tools/console/client/main.c index f4c783b..753b3aa 100644 --- a/tools/console/client/main.c +++ b/tools/console/client/main.c @@ -168,16 +168,19 @@ static void restore_term(int fd, struct termios *old) tcsetattr(fd, TCSANOW, old); } -static int console_loop(int fd, struct xs_handle *xs, char *pty_path) +static int console_loop(int fd, struct xs_handle *xs, char *pty_path, + bool interactive) { - int ret, xs_fd = xs_fileno(xs), max_fd; + int ret, xs_fd = xs_fileno(xs), max_fd = -1; do { fd_set fds; FD_ZERO(&fds); - FD_SET(STDIN_FILENO, &fds); - max_fd = STDIN_FILENO; + if (interactive) { + FD_SET(STDIN_FILENO, &fds); + max_fd = STDIN_FILENO; + } FD_SET(xs_fd, &fds); if (xs_fd > max_fd) max_fd = xs_fd; if (fd != -1) FD_SET(fd, &fds); @@ -284,6 +287,10 @@ int main(int argc, char **argv) struct xs_handle *xs; char *end; console_type type = CONSOLE_INVAL; + bool interactive = 0; + + if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) + interactive = 1; while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { switch(ch) { @@ -390,9 +397,11 @@ int main(int argc, char **argv) } init_term(spty, &attr); - init_term(STDIN_FILENO, &stdin_old_attr); - atexit(restore_term_stdin); /* if this fails, oh dear */ - console_loop(spty, xs, path); + if (interactive) { + init_term(STDIN_FILENO, &stdin_old_attr); + atexit(restore_term_stdin); /* if this fails, oh dear */ + } + console_loop(spty, xs, path, interactive); free(path); free(dom_path); -- 2.1.4