[lxc-devel] [lxc/master] start: dup std{in,out,err} to pty slave

brauner on Github lxc-bot at linuxcontainers.org
Mon Jul 10 10:07:24 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 744 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170710/e01f1640/attachment.bin>
-------------- next part --------------
From c5b93afba1d79c6861a6f45db2943b6f3cfbdab4 Mon Sep 17 00:00:00 2001
From: Li Feng <lifeng68 at huawei.com>
Date: Mon, 10 Jul 2017 17:19:52 +0800
Subject: [PATCH 1/2] start: dup std{in,out,err} to pty slave

In the case the container has a console with a valid slave pty file descriptor
we duplicate std{in,out,err} to the slave file descriptor so console logging
works correctly. When the container does not have a valid slave pty file
descriptor for its console and is started daemonized we should dup to
/dev/null.

Closes #1646.

Signed-off-by: Li Feng <lifeng68 at huawei.com>
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/start.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/lxc/start.c b/src/lxc/start.c
index 9a7e462d0..481776186 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1063,8 +1063,13 @@ static int do_start(void *data)
 	 * setup on its console ie. the pty allocated in lxc_console_create() so
 	 * make sure that that pty is stdin,stdout,stderr.
 	 */
-	if (lxc_console_set_stdfds(handler->conf->console.slave) < 0)
-		goto out_warn_father;
+	 if (handler->conf->console.slave >= 0)
+		 if (set_stdfds(handler->conf->console.slave) < 0) {
+			ERROR("Failed to redirect std{in,out,err} to pty file "
+			      "descriptor %d",
+			      handler->conf->console.slave);
+			goto out_warn_father;
+		 }
 
 	/* If we mounted a temporary proc, then unmount it now. */
 	tmp_proc_unmount(handler->conf);
@@ -1142,8 +1147,12 @@ static int do_start(void *data)
 			goto out_warn_father;
 	}
 
-	if (handler->backgrounded && set_stdfds(devnull_fd))
-		goto out_warn_father;
+	if (handler->conf->console.slave < 0 && handler->backgrounded)
+		if (set_stdfds(devnull_fd) < 0) {
+			ERROR("Failed to redirect std{in,out,err} to "
+			      "\"/dev/null\"");
+			goto out_warn_father;
+		}
 
 	if (devnull_fd >= 0) {
 		close(devnull_fd);

From bbbf65ee7707028a65c075c7eb822d65e0c564eb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Mon, 10 Jul 2017 11:46:54 +0200
Subject: [PATCH 2/2] utils: set_stdfds()

non-functional changes

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/utils.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index f4fa0ab06..5b61cba00 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -1822,14 +1822,21 @@ int open_devnull(void)
 
 int set_stdfds(int fd)
 {
+	int ret;
+
 	if (fd < 0)
 		return -1;
 
-	if (dup2(fd, 0) < 0)
+	ret = dup2(fd, STDIN_FILENO);
+	if (ret < 0)
 		return -1;
-	if (dup2(fd, 1) < 0)
+
+	ret = dup2(fd, STDOUT_FILENO);
+	if (ret < 0)
 		return -1;
-	if (dup2(fd, 2) < 0)
+
+	ret = dup2(fd, STDERR_FILENO);
+	if (ret < 0)
 		return -1;
 
 	return 0;


More information about the lxc-devel mailing list