[CRIU] [PATCH 1/3] ns: Add ns_get_userns() feature

Kirill Tkhai ktkhai at virtuozzo.com
Wed May 17 05:19:27 PDT 2017


Check for NS_GET_USERNS nsfs ioctl().

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/cr-check.c        |   13 +++++++++++++
 criu/include/kerndat.h |    2 ++
 criu/kerndat.c         |   22 ++++++++++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/criu/cr-check.c b/criu/cr-check.c
index 0fcb28547..4ef2a46ae 100644
--- a/criu/cr-check.c
+++ b/criu/cr-check.c
@@ -1113,6 +1113,17 @@ static int check_ns_pid(void)
 	return 0;
 }
 
+static int check_ns_get_userns(void)
+{
+	if (kerndat_has_ns_get_userns() < 0)
+		return -1;
+
+	if (!kdat.has_ns_get_userns)
+		return -1;
+
+	return 0;
+}
+
 static int (*chk_feature)(void);
 
 /*
@@ -1218,6 +1229,7 @@ int cr_check(void)
 		ret |= check_loginuid();
 		ret |= check_sk_netns();
 		ret |= check_ns_pid();
+		ret |= check_ns_get_userns();
 	}
 
 	/*
@@ -1299,6 +1311,7 @@ static struct feature_list feature_list[] = {
 	{ "nsid", check_nsid },
 	{ "link_nsid", check_link_nsid},
 	{ "ns_pid", check_ns_pid},
+	{ "ns_get_userns", check_ns_get_userns },
 	{ NULL, NULL },
 };
 
diff --git a/criu/include/kerndat.h b/criu/include/kerndat.h
index 5be04c61b..bc6a9f1d4 100644
--- a/criu/include/kerndat.h
+++ b/criu/include/kerndat.h
@@ -50,6 +50,7 @@ struct kerndat_s {
 	bool has_nsid;
 	bool has_link_nsid;
 	bool has_nspid;
+	bool has_ns_get_userns;
 };
 
 extern struct kerndat_s kdat;
@@ -73,5 +74,6 @@ extern int kerndat_fs_virtualized(unsigned int which, u32 kdev);
 extern int kerndat_tcp_repair();
 extern int kerndat_uffd(void);
 extern int kerndat_has_nspid(void);
+extern int kerndat_has_ns_get_userns(void);
 
 #endif /* __CR_KERNDAT_H__ */
diff --git a/criu/kerndat.c b/criu/kerndat.c
index 61b271b34..770c6568e 100644
--- a/criu/kerndat.c
+++ b/criu/kerndat.c
@@ -654,6 +654,26 @@ int kerndat_has_nspid(void)
 	return ret;
 }
 
+int kerndat_has_ns_get_userns(void)
+{
+	int pid_fd, user_fd;
+
+	pid_fd = open("/proc/self/ns/pid", O_RDONLY);
+	if (pid_fd < 0) {
+		perror("Can't open pid ns");
+		return -1;
+	}
+
+	user_fd = ioctl(pid_fd, NS_GET_USERNS);
+	if (user_fd >= 0) {
+		kdat.has_ns_get_userns = true;
+		close(user_fd);
+	}
+
+	close(pid_fd);
+	return 0;
+}
+
 #define KERNDAT_CACHE_FILE	KDAT_RUNDIR"/criu.kdat"
 #define KERNDAT_CACHE_FILE_TMP	KDAT_RUNDIR"/.criu.kdat"
 
@@ -775,6 +795,8 @@ int kerndat_init(void)
 		ret = kerndat_has_memfd_create();
 	if (!ret)
 		ret = kerndat_uffd();
+	if (!ret)
+		ret = kerndat_has_ns_get_userns();
 
 	kerndat_lsm();
 	kerndat_mmap_min_addr();



More information about the CRIU mailing list