[lxc-devel] [lxc/master] lxc-attach: fix redirection - stderr

brauner on Github lxc-bot at linuxcontainers.org
Fri Mar 4 19:33:16 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 808 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160304/c9cad69d/attachment.bin>
-------------- next part --------------
From 64d54127e5f9a445222780bad785c850b6e84260 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at mailbox.org>
Date: Fri, 4 Mar 2016 19:32:00 +0100
Subject: [PATCH] lxc-attach: fix redirection - stderr

So that we can do things like:

	lxc-attach -n a -- sh -c 'echo ERR >&2' > /dev/null

There seems to be no easy way to discern when we need to write to stderr
instead of stdout when we receive an event on the master fd of an allocated
pty. So we're using a "trick"/"hack". We write to STDOUT_FILENO if it refers to
a pty. If STDOUT_FILENO does not refer to a pty we check whether STDERR_FILENO
refers to a pty and if so write to it.

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
---
 src/lxc/lxc_attach.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c
index 348014d..df4fe79 100644
--- a/src/lxc/lxc_attach.c
+++ b/src/lxc/lxc_attach.c
@@ -273,7 +273,12 @@ static int get_pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *
 	if (lxc_console_create(conf) < 0)
 		return -1;
 	ts = conf->console.tty_state;
-	ts->stdoutfd = STDOUT_FILENO;
+	/* If stdout is not a tty but stderr is, we write to stderr. */
+	if (!isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
+		ts->stdoutfd = STDERR_FILENO;
+	else
+		ts->stdoutfd = STDOUT_FILENO;
+
 	conf->console.descr = &descr;
 
 	/* Shift ttys to container. */


More information about the lxc-devel mailing list