[lxc-devel] [lxc/master] Better criu logging

tych0 on Github lxc-bot at linuxcontainers.org
Fri Mar 18 19:39:10 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 351 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160318/f2d0bde5/attachment.bin>
-------------- next part --------------
From cf4b07a5af0d27fb55f0084172d10e725f11e0bd Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Fri, 18 Mar 2016 10:19:36 -0600
Subject: [PATCH 1/2] c/r: log the exact command we exec

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 src/lxc/criu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/lxc/criu.c b/src/lxc/criu.c
index a745806..a0c6718 100644
--- a/src/lxc/criu.c
+++ b/src/lxc/criu.c
@@ -126,7 +126,7 @@ static void exec_criu(struct criu_opts *opts)
 	int netnr = 0;
 	struct lxc_list *it;
 
-	char buf[4096], tty_info[32];
+	char buf[4096], *pos, tty_info[32];
 
 	/* If we are currently in a cgroup /foo/bar, and the container is in a
 	 * cgroup /lxc/foo, lxcfs will give us an ENOENT if some task in the
@@ -356,6 +356,15 @@ static void exec_criu(struct criu_opts *opts)
 
 	argv[argc] = NULL;
 
+	buf[0] = 0;
+	pos = buf;
+	for (i = 0; argv[i]; i++) {
+		pos = strncat(buf, argv[i], buf + sizeof(buf) - pos);
+		pos = strncat(buf, " ", buf + sizeof(buf) - pos);
+	}
+
+	INFO("execing: %s", buf);
+
 #undef DECLARE_ARG
 	execv(argv[0], argv);
 err:

From 3d9a5c85fd79e5d564c024063f9396b92989961a Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Fri, 18 Mar 2016 13:13:17 -0600
Subject: [PATCH 2/2] c/r: print criu's stdout when it fails

In particular, when CRIU fails before it has its log completely initialized
(e.g. if the log directory doesn't exist, or if the argument parser fails),
it prints this to stdout. Let's log that.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 src/lxc/criu.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 11 deletions(-)

diff --git a/src/lxc/criu.c b/src/lxc/criu.c
index a0c6718..7b72c6e 100644
--- a/src/lxc/criu.c
+++ b/src/lxc/criu.c
@@ -530,12 +530,12 @@ static bool restore_net_info(struct lxc_container *c)
 
 // do_restore never returns, the calling process is used as the
 // monitor process. do_restore calls exit() if it fails.
-void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose)
+void do_restore(struct lxc_container *c, int status_pipe, char *directory, bool verbose)
 {
 	pid_t pid;
 	char pidfile[L_tmpnam];
 	struct lxc_handler *handler;
-	int status;
+	int status, pipes[2] = {-1, -1};
 
 	if (!tmpnam(pidfile))
 		goto out;
@@ -561,6 +561,11 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 
 	resolve_clone_flags(handler);
 
+	if (pipe(pipes) < 0) {
+		SYSERROR("pipe() failed");
+		goto out_fini_handler;
+	}
+
 	pid = fork();
 	if (pid < 0)
 		goto out_fini_handler;
@@ -570,8 +575,20 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 		struct lxc_rootfs *rootfs;
 		int flags;
 
-		close(pipe);
-		pipe = -1;
+		close(status_pipe);
+		status_pipe = -1;
+
+		close(pipes[0]);
+		pipes[0] = -1;
+		if (dup2(pipes[1], STDERR_FILENO) < 0) {
+			SYSERROR("dup2 failed");
+			goto out_fini_handler;
+		}
+
+		if (dup2(pipes[1], STDOUT_FILENO) < 0) {
+			SYSERROR("dup2 failed");
+			goto out_fini_handler;
+		}
 
 		if (unshare(CLONE_NEWNS))
 			goto out_fini_handler;
@@ -632,15 +649,18 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 		int ret;
 		char title[2048];
 
+		close(pipes[1]);
+		pipes[1] = -1;
+
 		pid_t w = waitpid(pid, &status, 0);
 		if (w == -1) {
 			SYSERROR("waitpid");
 			goto out_fini_handler;
 		}
 
-		ret = write(pipe, &status, sizeof(status));
-		close(pipe);
-		pipe = -1;
+		ret = write(status_pipe, &status, sizeof(status));
+		close(status_pipe);
+		status_pipe = -1;
 
 		if (sizeof(status) != ret) {
 			SYSERROR("failed to write all of status");
@@ -649,7 +669,18 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 
 		if (WIFEXITED(status)) {
 			if (WEXITSTATUS(status)) {
-				ERROR("criu process exited %d\n", WEXITSTATUS(status));
+				char buf[4096];
+				int n;
+
+				n = read(pipes[0], buf, sizeof(buf));
+				if (n < 0) {
+					SYSERROR("failed reading from criu stderr");
+					goto out_fini_handler;
+				}
+
+				buf[n] = 0;
+
+				ERROR("criu process exited %d, output:\n%s\n", WEXITSTATUS(status), buf);
 				goto out_fini_handler;
 			} else {
 				int ret;
@@ -679,6 +710,8 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 			goto out_fini_handler;
 		}
 
+		close(pipes[0]);
+
 		/*
 		 * See comment in lxcapi_start; we don't care if these
 		 * fail because it's just a beauty thing. We just
@@ -695,17 +728,22 @@ void do_restore(struct lxc_container *c, int pipe, char *directory, bool verbose
 	}
 
 out_fini_handler:
+	if (pipes[0] >= 0)
+		close(pipes[0]);
+	if (pipes[1] >= 0)
+		close(pipes[1]);
+
 	lxc_fini(c->name, handler);
 	if (unlink(pidfile) < 0 && errno != ENOENT)
 		SYSERROR("unlinking pidfile failed");
 
 out:
-	if (pipe >= 0) {
+	if (status_pipe >= 0) {
 		status = 1;
-		if (write(pipe, &status, sizeof(status)) != sizeof(status)) {
+		if (write(status_pipe, &status, sizeof(status)) != sizeof(status)) {
 			SYSERROR("writing status failed");
 		}
-		close(pipe);
+		close(status_pipe);
 	}
 
 	exit(1);


More information about the lxc-devel mailing list