[CRIU] [PATCHv2 2/2] test: add mandatory-lock test

Dmitry Safonov dsafonov at virtuozzo.com
Tue Jul 12 08:16:40 PDT 2016


Cc: Andrey Vagin <avagin at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
v2: correct test messages

 test/zdtm/.gitignore               |   1 +
 test/zdtm/static/Makefile          |   3 +-
 test/zdtm/static/file_locks06.c    | 110 +++++++++++++++++++++++++++++++++++++
 test/zdtm/static/file_locks06.desc |   1 +
 test/zdtm/static/file_locks06.opts |   1 +
 5 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 test/zdtm/static/file_locks06.c
 create mode 100644 test/zdtm/static/file_locks06.desc
 create mode 100644 test/zdtm/static/file_locks06.opts

diff --git a/test/zdtm/.gitignore b/test/zdtm/.gitignore
index 85e39a2099e5..cd3dfb24069b 100644
--- a/test/zdtm/.gitignore
+++ b/test/zdtm/.gitignore
@@ -227,6 +227,7 @@
 /static/file_locks03
 /static/file_locks04
 /static/file_locks05
+/static/file_locks06
 /static/mlock_setuid
 /static/mntns_root_bind
 /static/mntns_root_bind02
diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index 407a80f56400..842329634469 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -224,7 +224,8 @@ TST_DIR		=				\
 		cgroup02			\
 		cgroup03			\
 		cgroup04			\
-		cgroup_stray		\
+		cgroup_stray			\
+		file_locks06			\
 		unlink_fstat04			\
 		mntns_open			\
 		mntns_link_remap		\
diff --git a/test/zdtm/static/file_locks06.c b/test/zdtm/static/file_locks06.c
new file mode 100644
index 000000000000..dcd320989228
--- /dev/null
+++ b/test/zdtm/static/file_locks06.c
@@ -0,0 +1,110 @@
+#include <fcntl.h>
+#include <errno.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Mandatory locking mounting with exclusive locks";
+const char *test_author	= "Dmitry Safonov <dsafonov at virtuozzo.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "tmpfs mountpoint directory", 1);
+const char *test_file	= "test";
+
+int main(int argc, char **argv)
+{
+	pid_t pid;
+	int fd, status, ret;
+
+	test_init(argc, argv);
+
+	if (mkdir(dirname, 0755) == -1 && errno != EEXIST) {
+		pr_perror("mkdir %s failed", dirname);
+		return 1;
+	}
+
+	if (mount("zdtm", dirname, "tmpfs", MS_MANDLOCK, 0)) {
+		pr_perror("can't mount tmpfs with mandatory locking at %s",
+				dirname);
+		ret = 1;
+		goto out_rmdir;
+	}
+
+	chdir(dirname);
+	fd = open(test_file, O_CREAT | O_RDWR | O_TRUNC, 02600);
+	if (fd < 0) {
+		pr_perror("failed to open test file at %s", dirname);
+		ret = 1;
+		goto out_umount;
+	}
+
+	if (lockf(fd, F_LOCK, 0) == -1) {
+		pr_perror("lockf %s/%s failed", dirname, test_file);
+		ret = 1;
+		goto out_close;
+	}
+
+	if (write(fd, test_file, strlen(test_file)) != strlen(test_file)) {
+		pr_perror("failed to write to test file %s/%s",
+				dirname, test_file);
+		ret = 1;
+		goto out_close;
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	pid = fork();
+	if (pid < 0) {
+		pr_perror("failed to fork");
+		ret = 1;
+		goto out_close;
+	}
+
+	if (pid == 0) {
+		errno = 0;
+		if (lockf(fd, F_TLOCK, 0) == -1) {
+			if (errno == EWOULDBLOCK || errno == EAGAIN)
+				return 0;
+
+			pr_perror("failed to lock %s/%s", dirname, test_file);
+			return 1;
+		}
+
+		fail("locked %s/%s, which is under exclusive lock\n",
+				dirname, test_file);
+		close(fd);
+		return 1;
+	}
+
+	status = -1;
+	if (waitpid(pid, &status, 0) != pid) {
+		pr_perror("waitpid(%d) failed", pid);
+		ret = 1;
+		goto out_close;
+	}
+	if (!WEXITSTATUS(status)) {
+		pass();
+	} else {
+		pr_perror("child returned non-zero code: 0x%x\n",
+				WEXITSTATUS(status));
+		ret = 1;
+	}
+
+out_close:
+	if (close(fd))
+		pr_perror("close on %d failed", fd);
+out_umount:
+	chdir("..");
+	if (umount(dirname))
+		pr_perror("umount on %s failed", dirname);
+out_rmdir:
+	if (rmdir(dirname))
+		pr_perror("rmdir on %s failed", dirname);
+
+	return ret;
+}
diff --git a/test/zdtm/static/file_locks06.desc b/test/zdtm/static/file_locks06.desc
new file mode 100644
index 000000000000..00e9ab1cfc4a
--- /dev/null
+++ b/test/zdtm/static/file_locks06.desc
@@ -0,0 +1 @@
+{'flags': 'suid', 'opts': '--file-locks'}
diff --git a/test/zdtm/static/file_locks06.opts b/test/zdtm/static/file_locks06.opts
new file mode 100644
index 000000000000..738083ade329
--- /dev/null
+++ b/test/zdtm/static/file_locks06.opts
@@ -0,0 +1 @@
+--file-locks
-- 
2.9.0



More information about the CRIU mailing list