[lxc-devel] [lxc/master] lxc-attach: error on -L/--pty-log + redirection

brauner on Github lxc-bot at linuxcontainers.org
Wed Apr 13 13:29:56 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 754 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160413/11ec8e6d/attachment.bin>
-------------- next part --------------
From c87524b77b3abf12f26c39d5b85d26ebc7aeaf97 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at mailbox.org>
Date: Wed, 13 Apr 2016 15:23:35 +0200
Subject: [PATCH] lxc-attach: error on -L/--pty-log + redirection

pty logging only works correctly when stdout and stderr refer to a pty. If they
do not, we do not dup2() them and lxc_console_cb_con() will never write to the
corresponding log file descriptor.

When redirection on stdout and stderr is used we can safely assume that the user
is already logging to a file or /dev/null and creating an additional pty log
doesn't seem to make sense.

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

diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c
index ed3ba0d..58f658b 100644
--- a/src/lxc/lxc_attach.c
+++ b/src/lxc/lxc_attach.c
@@ -21,7 +21,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#define _GNU_SOURCE
+#include "config.h"
+
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -38,7 +39,6 @@
 #include "attach.h"
 #include "arguments.h"
 #include "caps.h"
-#include "config.h"
 #include "confile.h"
 #include "console.h"
 #include "log.h"
@@ -254,7 +254,7 @@ static int get_pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *
 	INFO("Trying to allocate a pty on the host");
 
 	if (!isatty(args->ptyfd)) {
-		ERROR("stdin is not a tty");
+		ERROR("Standard file descriptor does not refer to a pty\n.");
 		return -1;
 	}
 
@@ -324,28 +324,40 @@ static int get_pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *
 	return ret;
 }
 
+static int stdfd_is_pty(void)
+{
+	if (isatty(STDIN_FILENO))
+		return STDIN_FILENO;
+	if (isatty(STDOUT_FILENO))
+		return STDOUT_FILENO;
+	if (isatty(STDERR_FILENO))
+		return STDERR_FILENO;
+
+	return -1;
+}
+
 int main(int argc, char *argv[])
 {
-	int ret = -1;
+	int ret = -1, r;
 	int wexit = 0;
 	pid_t pid;
 	lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
 	lxc_attach_command_t command = (lxc_attach_command_t){.program = NULL};
 
-	ret = lxc_caps_init();
-	if (ret)
+	r = lxc_caps_init();
+	if (r)
 		exit(EXIT_FAILURE);
 
-	ret = lxc_arguments_parse(&my_args, argc, argv);
-	if (ret)
+	r = lxc_arguments_parse(&my_args, argc, argv);
+	if (r)
 		exit(EXIT_FAILURE);
 
 	if (!my_args.log_file)
 		my_args.log_file = "none";
 
-	ret = lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
+	r = lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			   my_args.progname, my_args.quiet, my_args.lxcpath[0]);
-	if (ret)
+	if (r)
 		exit(EXIT_FAILURE);
 	lxc_log_options_no_override();
 
@@ -388,19 +400,23 @@ int main(int argc, char *argv[])
 		command.argv = (char**)my_args.argv;
 	}
 
-	if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO)) {
-		struct wrapargs wrap = (struct wrapargs){
-			.command = &command,
+	struct wrapargs wrap = (struct wrapargs){
+		.command = &command,
 			.options = &attach_options
-		};
-		if (isatty(STDIN_FILENO))
-			wrap.ptyfd = STDIN_FILENO;
-		else if (isatty(STDOUT_FILENO))
-			wrap.ptyfd = STDOUT_FILENO;
-		else if (isatty(STDERR_FILENO))
-			wrap.ptyfd = STDERR_FILENO;
+	};
+
+	wrap.ptyfd = stdfd_is_pty();
+	if (wrap.ptyfd >= 0) {
+		if ((!isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) && my_args.console_log) {
+			fprintf(stderr, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
+			goto out;
+		}
 		ret = get_pty_on_host(c, &wrap, &pid);
 	} else {
+		if (my_args.console_log) {
+			fprintf(stderr, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
+			goto out;
+		}
 		if (command.program)
 			ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid);
 		else


More information about the lxc-devel mailing list