From: Martin Lucina <martin@lucina.net>
To: xen-devel@lists.xenproject.org
Cc: Wei Liu <wei.liu2@citrix.com>, Martin Lucina <martin@lucina.net>,
Ian Jackson <ian.jackson@eu.citrix.com>,
Ian Campbell <ian.campbell@citrix.com>,
Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Subject: [PATCH] xenconsole: Allow non-interactive use
Date: Wed, 22 Jul 2015 19:08:08 +0200 [thread overview]
Message-ID: <1437584888-9074-1-git-send-email-martin@lucina.net> (raw)
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 <martin@lucina.net>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Cc: Ian Campbell <ian.campbell@citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>
---
tools/console/client/main.c | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/tools/console/client/main.c b/tools/console/client/main.c
index f4c783b..8a42101 100644
--- a/tools/console/client/main.c
+++ b/tools/console/client/main.c
@@ -168,7 +168,8 @@ 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;
@@ -176,8 +177,13 @@ static int console_loop(int fd, struct xs_handle *xs, char *pty_path)
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;
+ }
+ else {
+ max_fd = -1;
+ }
FD_SET(xs_fd, &fds);
if (xs_fd > max_fd) max_fd = xs_fd;
if (fd != -1) FD_SET(fd, &fds);
@@ -284,6 +290,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 +400,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
next reply other threads:[~2015-07-22 17:08 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-22 17:08 Martin Lucina [this message]
2015-07-23 8:48 ` [PATCH] xenconsole: Allow non-interactive use Ian Campbell
2015-07-23 10:53 ` Wei Liu
2015-07-23 15:09 ` Martin Lucina
2015-07-23 15:23 ` Ian Campbell
2015-07-24 11:30 ` [PATCH v2] " Martin Lucina
2015-07-24 13:13 ` Ian Campbell
2015-07-24 11:36 ` [PATCH] " Martin Lucina
2015-07-24 11:44 ` Ian Campbell
2015-07-24 12:51 ` [PATCH] xenconsole: Ensure exclusive access to console using locks Martin Lucina
2015-07-24 13:11 ` Ian Campbell
2015-07-24 13:35 ` Ian Jackson
2015-07-24 13:40 ` Martin Lucina
2015-07-24 13:54 ` Ian Campbell
2015-07-24 13:32 ` Ian Jackson
2015-07-24 13:42 ` Martin Lucina
2015-07-24 14:47 ` [PATCH v2] " Martin Lucina
2015-07-24 15:07 ` Ian Jackson
2015-07-24 15:29 ` [PATCH v3] " Martin Lucina
2015-07-24 16:01 ` Ian Jackson
2015-07-27 12:44 ` Martin Lucina
2015-07-27 13:29 ` Wei Liu
2015-07-27 15:14 ` Ian Campbell
2015-07-24 15:12 ` [PATCH v2] " Ian Campbell
2015-07-24 13:42 ` [PATCH] " Ian Campbell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1437584888-9074-1-git-send-email-martin@lucina.net \
--to=martin@lucina.net \
--cc=ian.campbell@citrix.com \
--cc=ian.jackson@eu.citrix.com \
--cc=stefano.stabellini@eu.citrix.com \
--cc=wei.liu2@citrix.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).