[CRIU] [PATCH] env: Introduce logging of runtime environment

Cyrill Gorcunov gorcunov at openvz.org
Wed Sep 17 00:22:23 PDT 2014


This kind of information is very usefull for debug.
It's gonna be extended but at moment command line
only.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile.crtools |  1 +
 crtools.c        |  5 +++++
 env.c            | 42 ++++++++++++++++++++++++++++++++++++++++++
 include/env.h    | 13 +++++++++++++
 4 files changed, 61 insertions(+)
 create mode 100644 env.c
 create mode 100644 include/env.h

diff --git a/Makefile.crtools b/Makefile.crtools
index 2a6d4d2583cb..c23662793759 100644
--- a/Makefile.crtools
+++ b/Makefile.crtools
@@ -50,6 +50,7 @@ obj-y	+= pstree.o
 obj-y	+= protobuf.o
 obj-y	+= protobuf-desc.o
 obj-y	+= tty.o
+obj-y	+= env.o
 obj-y	+= cr-exec.o
 obj-y	+= file-lock.o
 obj-y	+= page-pipe.o
diff --git a/crtools.c b/crtools.c
index b8f73060222f..7980053f2aff 100644
--- a/crtools.c
+++ b/crtools.c
@@ -36,6 +36,7 @@
 #include "plugin.h"
 #include "mount.h"
 #include "cgroup.h"
+#include "env.h"
 #include "action-scripts.h"
 
 #include "setproctitle.h"
@@ -182,6 +183,8 @@ int main(int argc, char *argv[], char *envp[])
 
 	BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
 
+	fill_rt_env(argc, argv);
+
 	cr_pb_init();
 	if (restrict_uid(getuid(), getgid()))
 		return 1;
@@ -459,6 +462,8 @@ int main(int argc, char *argv[], char *envp[])
 	if (log_init(opts.output))
 		return 1;
 
+	log_rt_env(LOG_INFO);
+
 	if (opts.img_parent)
 		pr_info("Will do snapshot from %s\n", opts.img_parent);
 
diff --git a/env.c b/env.c
new file mode 100644
index 000000000000..5dc01e6eec27
--- /dev/null
+++ b/env.c
@@ -0,0 +1,42 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "env.h"
+#include "log.h"
+
+static rt_env_t rt_env;
+
+void log_rt_env(unsigned int loglevel)
+{
+	if (pr_quelled(loglevel))
+		return;
+
+	print_on_level(loglevel, "Run time environment info\n");
+	print_on_level(loglevel, "  Command line: %s\n", rt_env.cmdline);
+}
+
+int fill_rt_env(int argc, char *argv[])
+{
+	size_t i, j;
+
+	/*
+	 * The kernel itself allocates cmdline in one
+	 * memory slab but before main executed() there
+	 * might be a bunch of calls from system libs so
+	 * we can't rely on cmdline memory continuity :(
+	 */
+	for (i = 0, j = 0; i < argc; i++) {
+		size_t len = strlen(argv[i]);
+
+		if (len + j + 1 > sizeof(rt_env.cmdline))
+			break;
+		strcpy(&rt_env.cmdline[j], argv[i]);
+		rt_env.cmdline[j + len] = ' ';
+		j += len + 1;
+	}
+	rt_env.cmdline[j ? j - 1 : 0] = '\0';
+
+	return 0;
+}
diff --git a/include/env.h b/include/env.h
new file mode 100644
index 000000000000..a0d4214c6923
--- /dev/null
+++ b/include/env.h
@@ -0,0 +1,13 @@
+#ifndef __CR_ENV_H__
+#define __CR_ENV_H__
+
+#include "asm-generic/page.h"
+
+typedef struct {
+	char		cmdline[PAGE_SIZE];
+} rt_env_t;
+
+extern int fill_rt_env(int argc, char *argv[]);
+extern void log_rt_env(unsigned int loglevel);
+
+#endif /* __CR_ENV_H__ */
-- 
1.9.3



More information about the CRIU mailing list