[CRIU] [PATCH 4/4] test: mount -- Add bind-mount-unix

Cyrill Gorcunov gorcunov at openvz.org
Tue Aug 8 14:25:10 MSK 2017


To test a case where unix socket is bind mounted to somewhere
so restore may fail if socket has not been created.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 test/zdtm/static/Makefile             |   1 +
 test/zdtm/static/bind-mount-unix.c    | 106 ++++++++++++++++++++++++++++++++++
 test/zdtm/static/bind-mount-unix.desc |   1 +
 3 files changed, 108 insertions(+)
 create mode 100644 test/zdtm/static/bind-mount-unix.c
 create mode 100644 test/zdtm/static/bind-mount-unix.desc

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index b3b5fae26585..579916257f88 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -267,6 +267,7 @@ TST_DIR		=				\
 		mnt_ro_bind			\
 		mount_paths			\
 		bind-mount			\
+		bind-mount-unix			\
 		inotify00			\
 		inotify01			\
 		inotify02			\
diff --git a/test/zdtm/static/bind-mount-unix.c b/test/zdtm/static/bind-mount-unix.c
new file mode 100644
index 000000000000..c3441a08abde
--- /dev/null
+++ b/test/zdtm/static/bind-mount-unix.c
@@ -0,0 +1,106 @@
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <linux/limits.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Check bind-mounts with unix socket";
+const char *test_author	= "Cyrill Gorcunov <gorcunov at virtuozzo.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "directory name", 1);
+
+int main(int argc, char **argv)
+{
+	char path_unix[PATH_MAX], path_bind[PATH_MAX];
+	char unix_name[] = "criu-log";
+	char bind_name[] = "criu-bind-log";
+	int sk = -1, ret = 1, fd;
+	struct sockaddr_un addr;
+	unsigned int addrlen;
+	struct stat st;
+
+	test_init(argc, argv);
+
+	mkdir(dirname, 0700);
+	if (mount("none", dirname, "tmpfs", 0, NULL)) {
+		pr_perror("Unable to mount %s", dirname);
+		return 1;
+	}
+
+	snprintf(path_bind, sizeof(path_bind), "%s/%s", dirname, bind_name);
+	snprintf(path_unix, sizeof(path_unix), "%s/%s", dirname, unix_name);
+
+	unlink(path_bind);
+	unlink(path_unix);
+
+	fd = open(path_bind, O_RDONLY | O_CREAT);
+	if (fd < 0) {
+		pr_perror("Can't open %s", path_bind);
+		goto err;
+	}
+	close(fd);
+
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, path_unix, sizeof(addr.sun_path));
+	addrlen = sizeof(addr.sun_family) + strlen(path_unix);
+
+	sk = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (sk < 0) {
+		pr_perror("Can't create socket %s", path_unix);
+		goto err;
+	}
+
+	ret = bind(sk, &addr, addrlen);
+	if (ret) {
+		pr_perror("Can't bind socket %s", path_unix);
+		goto err;
+	}
+
+	if (stat(path_unix, &st) == 0) {
+		test_msg("path %s st.st_ino %#lx st.st_mode 0%o (sock %d)\n",
+			 path_unix, (unsigned long)st.st_ino,
+			 (int)st.st_mode, !!S_ISSOCK(st.st_mode));
+	} else
+		pr_perror("Can't stat on %s", path_unix);
+
+	if (mount(path_unix, path_bind, NULL, MS_BIND | MS_REC, NULL)) {
+		pr_perror("Unable to bindmount %s -> %s", path_unix, path_bind);
+		goto err;
+	}
+
+	if (stat(path_unix, &st) == 0) {
+		test_msg("path %s st.st_dev %#x st.st_rdev %#x st.st_ino %#lx st.st_mode 0%o (sock %d)\n",
+			 path_unix, (int)st.st_dev, (int)st.st_rdev, (unsigned long)st.st_ino,
+			 (int)st.st_mode, !!S_ISSOCK(st.st_mode));
+	} else
+		pr_perror("Can't stat on %s", path_unix);
+
+	if (stat(path_bind, &st) == 0) {
+		test_msg("path %s st.st_dev %#x st.st_rdev %#x st.st_ino %#lx st.st_mode 0%o (sock %d)\n",
+			 path_bind, (int)st.st_dev, (int)st.st_rdev, (unsigned long)st.st_ino,
+			 (int)st.st_mode, !!S_ISSOCK(st.st_mode));
+	} else
+		pr_perror("Can't stat on %s", path_bind);
+
+	test_daemon();
+	test_waitsig();
+
+	pass();
+	ret = 0;
+
+err:
+	umount2(path_bind, MNT_DETACH);
+	umount2(dirname, MNT_DETACH);
+	unlink(path_bind);
+	unlink(path_unix);
+	close(sk);
+	return ret;
+}
diff --git a/test/zdtm/static/bind-mount-unix.desc b/test/zdtm/static/bind-mount-unix.desc
new file mode 100644
index 000000000000..3fd8e03f7ed7
--- /dev/null
+++ b/test/zdtm/static/bind-mount-unix.desc
@@ -0,0 +1 @@
+{'flavor': 'ns', 'flags': 'suid', 'feature': 'mnt_id'}
-- 
2.7.5



More information about the CRIU mailing list