[lxc-devel] [lxc/master] fix logic for execute log file

tych0 on Github lxc-bot at linuxcontainers.org
Thu May 3 18:34:46 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 760 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180503/f4199661/attachment.bin>
-------------- next part --------------
From cd90db2c0ec7948a9a52dfc83d3a2b92ed522511 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho at tycho.ws>
Date: Thu, 3 May 2018 18:32:19 +0000
Subject: [PATCH] fix logic for execute log file

The problem here is that lxc-init runs *inside* the container. So if a
person has the log file set to /home/$USER/foo, lxc-init ends up making a
directory /home/$USER/foo inside the container to put the log file in. What
we really want are the logs to be propagated from inside the container to
the outside. We accomplish this by passing an fd without O_CLOEXEC, and
telling lxc-init to log to that file.

Signed-off-by: Tycho Andersen <tycho at tycho.ws>
---
 src/lxc/execute.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/lxc/execute.c b/src/lxc/execute.c
index c7320ab2d..9fe1af0eb 100644
--- a/src/lxc/execute.c
+++ b/src/lxc/execute.c
@@ -21,11 +21,13 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 #include "conf.h"
 #include "log.h"
@@ -36,9 +38,9 @@ lxc_log_define(lxc_execute, lxc_start);
 
 static int execute_start(struct lxc_handler *handler, void* data)
 {
-	int j, i = 0;
+	int j, i = 0, log = -1;
 	struct execute_args *my_args = data;
-	char **argv;
+	char **argv, *logfd;
 	int argc = 0, argc_add;
 
 	while (my_args->argv[argc++]);
@@ -69,9 +71,25 @@ static int execute_start(struct lxc_handler *handler, void* data)
 		argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
 	}
 
-	if (handler->conf->logfile) {
+	if (current_config->logfd != -1 || lxc_log_fd != -1) {
+		int to_dup = current_config->logfd;
+
+		if (current_config->logfd == -1)
+			to_dup = lxc_log_fd;
+
+		log = dup(to_dup);
+		if (log < 0) {
+			SYSERROR("dup of log fd failed");
+			goto out2;
+		}
+
+		if (asprintf(&logfd, "/proc/1/fd/%d", log) < 0) {
+			ERROR("Couldn't allocate memory for log string");
+			goto out3;
+		}
+
 		argv[i++] = "-o";
-		argv[i++] = (char *)handler->conf->logfile;
+		argv[i++] = logfd;
 	}
 
 	if (my_args->quiet)
@@ -92,6 +110,9 @@ static int execute_start(struct lxc_handler *handler, void* data)
 	execvp(argv[0], argv);
 	SYSERROR("Failed to exec %s", argv[0]);
 
+	free(logfd);
+out3:
+	close(log);
 out2:
 	free(argv);
 out1:


More information about the lxc-devel mailing list