[CRIU] [PATCH 10/11] zdtm: Check that files from alien mnt ns are handled (v2)

Andrey Vagin avagin at openvz.org
Thu Jul 24 14:12:34 PDT 2014


Check that files opened before switching to new mount namespace
remain in it after restore. Right now this is not so :( Andrey is
fixing the issue.

Christopher, can you check whether the ns_child's call to system()
works in your minimal set-ups (it launches cat and awk). If not,
then I should rewrite this routine in pure C.

The first version was written by Pavel (xemul@).

v2: don't use test_init_ns
    don't call awk and cat

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 test/zdtm.sh                       |   4 +-
 test/zdtm/live/static/Makefile     |   1 +
 test/zdtm/live/static/mntns_open.c | 134 +++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 test/zdtm/live/static/mntns_open.c

diff --git a/test/zdtm.sh b/test/zdtm.sh
index 1c7bfef..23781ab 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -174,6 +174,7 @@ static/netns
 static/cgroup00
 static/cgroup01
 ns/static/clean_mntns
+ns/static/mntns_open
 "
 
 TEST_CR_KERNEL="
@@ -207,6 +208,7 @@ cgroup00
 cgroup01
 clean_mntns
 deleted_dev
+mntns_open
 "
 
 source $(readlink -f `dirname $0`/env.sh) || exit 1
@@ -389,7 +391,7 @@ start_test()
 			mkdir -p dump
 			ZDTM_ROOT=`mktemp -d /tmp/criu-root.XXXXXX`
 			ZDTM_ROOT=`readlink -f $ZDTM_ROOT`
-			mount --bind . $ZDTM_ROOT || return 1
+			mount --make-private --bind . $ZDTM_ROOT || return 1
 		fi
 		construct_root $ZDTM_ROOT $tdir/$tname || return 1
 		export ZDTM_NEWNS=1
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index d3d42a1..d9f97ca 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -164,6 +164,7 @@ TST_DIR		=				\
 		cgroup00			\
 		rmdir_open			\
 		cgroup01			\
+		mntns_open			\
 
 TST_DIR_FILE	=				\
 		chroot				\
diff --git a/test/zdtm/live/static/mntns_open.c b/test/zdtm/live/static/mntns_open.c
new file mode 100644
index 0000000..a40de96
--- /dev/null
+++ b/test/zdtm/live/static/mntns_open.c
@@ -0,0 +1,134 @@
+#define _GNU_SOURCE
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sched.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include "zdtmtst.h"
+
+#ifndef CLONE_NEWNS
+#define CLONE_NEWNS     0x00020000
+#endif
+
+const char *test_doc	= "Check that mnt_id is repsected";
+const char *test_author	= "Pavel Emelianov <xemul at parallels.com>";
+
+#define MPTS_FILE	"F"
+char *dirname;
+TEST_OPTION(dirname, string, "directory name", 1);
+char fpath[PATH_MAX];
+
+#define NS_STACK_SIZE	4096
+/* All arguments should be above stack, because it grows down */
+struct ns_exec_args {
+	char stack[NS_STACK_SIZE];
+	char stack_ptr[0];
+	int fd;
+};
+
+#define AWK_OK		13
+#define AWK_FAIL	42
+
+static int get_mntid(int fd)
+{
+	char str[256];
+	int mnt_id = -1;
+	FILE *f;
+
+	snprintf(str, sizeof(str), "/proc/self/fdinfo/%d", fd);
+	f = fopen(str, "r");
+	if (!f) {
+		err("Can't open %s to parse", str);
+		return -1;
+	}
+	while (fgets(str, sizeof(str), f)) {
+		if (sscanf(str, "mnt_id: %d", &mnt_id) == 1)
+			break;
+	}
+
+	fclose(f);
+	return mnt_id;
+}
+
+int ns_child(void *_arg)
+{
+	struct ns_exec_args *args = _arg;
+	int fd2;
+	int id1, id2;
+
+	fd2 = open(fpath, O_RDWR);
+	test_waitsig();
+
+	id1 = get_mntid(args->fd);
+	id2 = get_mntid(fd2);
+
+	test_msg("%d %d", id1, id2);
+
+	if (id1 <0 || id2 < 0)
+		exit(1);
+	if (id1 > 0 && id1 != id2)
+		exit(AWK_OK);
+	else
+		exit(AWK_FAIL);
+}
+
+int main(int argc, char **argv)
+{
+	struct ns_exec_args args;
+	pid_t pid = -1;
+
+	test_init(argc, argv);
+
+	snprintf(fpath, sizeof(fpath), "%s/%s", dirname, MPTS_FILE);
+	if (mkdir(dirname, 0600) < 0) {
+		fail("Can't make zdtm_sys");
+		return 1;
+	}
+
+	if (getenv("ZDTM_NOSUBNS") == NULL) {
+		args.fd = open(fpath, O_CREAT | O_RDWR, 0600);
+		if (args.fd < 0) {
+			fail("Can't open file");
+			return 1;
+		}
+
+		pid = clone(ns_child, args.stack_ptr, CLONE_NEWNS | SIGCHLD, &args);
+		if (pid < 0) {
+			err("Unable to fork child");
+			return 1;
+		}
+
+		close(args.fd);
+	}
+
+	test_daemon();
+	test_waitsig();
+
+
+	if (pid > 0) {
+		kill(pid, SIGTERM);
+		int status = 1;
+		wait(&status);
+		if (WIFEXITED(status)) {
+			if (WEXITSTATUS(status) == AWK_OK)
+				pass();
+			else if (WEXITSTATUS(status) == AWK_FAIL)
+				fail("Mount ID not restored");
+			else
+				fail("Failed to check mount IDs (%d)", WEXITSTATUS(status));
+		} else
+			fail("Test died");
+	}
+
+	unlink(fpath);
+	rmdir(dirname);
+	return 0;
+}
-- 
1.8.5.3



More information about the CRIU mailing list