linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrei Vagin <avagin@openvz.org>
To: David Howells <dhowells@redhat.com>
Cc: linux-fsdevel@vger.kernel.org, Andrei Vagin <avagin@gmail.com>
Subject: [PATCH dhowells/mount-api 2/2] selftests: implement a test for a new mount API
Date: Fri, 10 Aug 2018 15:00:27 -0700	[thread overview]
Message-ID: <20180810220027.2735-2-avagin@openvz.org> (raw)
In-Reply-To: <20180810220027.2735-1-avagin@openvz.org>

From: Andrei Vagin <avagin@gmail.com>

Currently, a reconfigure call-back is implemented only for the proc file
system. This test creates a new mount and pid namespace and then we can
create a new proc mount instance and be sure that a host configuration
will not be affected.

Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
 tools/testing/selftests/fsopen/Makefile |   5 +
 tools/testing/selftests/fsopen/config   |   2 +
 tools/testing/selftests/fsopen/fsopen.c | 120 ++++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tools/testing/selftests/fsopen/Makefile
 create mode 100644 tools/testing/selftests/fsopen/config
 create mode 100644 tools/testing/selftests/fsopen/fsopen.c

diff --git a/tools/testing/selftests/fsopen/Makefile b/tools/testing/selftests/fsopen/Makefile
new file mode 100644
index 000000000000..bfb1dd015e37
--- /dev/null
+++ b/tools/testing/selftests/fsopen/Makefile
@@ -0,0 +1,5 @@
+CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS)
+
+TEST_GEN_PROGS := fsopen
+
+include ../lib.mk
diff --git a/tools/testing/selftests/fsopen/config b/tools/testing/selftests/fsopen/config
new file mode 100644
index 000000000000..d2ced6498f5a
--- /dev/null
+++ b/tools/testing/selftests/fsopen/config
@@ -0,0 +1,2 @@
+CONFIG_PROC_FS=y
+CONFIG_PID_NS=y
diff --git a/tools/testing/selftests/fsopen/fsopen.c b/tools/testing/selftests/fsopen/fsopen.c
new file mode 100644
index 000000000000..c7cbfcda40ad
--- /dev/null
+++ b/tools/testing/selftests/fsopen/fsopen.c
@@ -0,0 +1,120 @@
+#define _GNU_SOURCE         /* See feature_test_macros(7) */
+#include <unistd.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sched.h>
+#include <linux/fs.h>
+#include <sys/syscall.h>
+#include <sys/wait.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+static int sys_mount(char *dev_name, char *dir_name, char *type,
+				unsigned long flags, void *data)
+{
+	return syscall(__NR_mount, dev_name, dir_name, type, flags, data);
+}
+
+static int sys_fsopen(const char *fs_name, unsigned int flags)
+{
+	return syscall(__NR_fsopen, fs_name, flags);
+}
+
+static int sys_fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
+{
+	return syscall(__NR_fsmount, fsfd, flags, ms_flags);
+}
+
+static int sys_fspick(unsigned int dirfd, const char *path, unsigned int flags)
+{
+	return syscall(__NR_fspick, dirfd, path, flags);
+}
+
+static int sys_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux)
+{
+	return syscall(__NR_fsconfig, fd, cmd, key, value, aux);
+}
+
+static int move_mount(int from_dfd, char *from_pathname,
+		int to_dfd, char *to_pathname, unsigned int flags)
+{
+	return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, to_pathname, flags);
+}
+
+#define pr_err(fmt, ...) \
+                ({ \
+                        fprintf(stderr, "%s:%d:" fmt ": %m\n", \
+                                __func__, __LINE__, ##__VA_ARGS__); \
+                        1; \
+                })
+
+static const char dev[] = "s test_fsopen_123";
+//static const char opts[] = "rw";
+static int test()
+{
+	int fsfd, fsfd2, dfd;
+
+	fsfd = sys_fsopen("proc", 0);
+	if (fsfd < 0)
+		return pr_err("sys_fsopen");
+
+	if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "gid", "0", 0))
+		return pr_err("sys_fsconfig sets gid=5");
+
+	if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", dev, 0) < 0)
+		return pr_err("fsconfig_set_string sets source = %s", dev);
+
+	if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
+		return pr_err("fsconfig_cmd_create");
+
+	dfd = sys_fsmount(fsfd, 0, 0);
+	if (dfd < 0)
+		return pr_err("sys_fsmount");
+
+	if (move_mount(dfd, ".", AT_FDCWD, "/proc", 0))
+		return pr_err("move_mount");
+
+	fsfd2 = sys_fspick(dfd, ".", 0);
+	if (fsfd2 < 0)
+		return pr_err("sys_fspick");
+
+	if (sys_fsconfig(fsfd2, FSCONFIG_SET_STRING, "gid", "5", 0))
+		return pr_err("fsconfig_set_string sets gid=5");
+
+	if (sys_fsconfig(fsfd2, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0))
+		return pr_err("fsconfig_cmd_reconfigure");
+
+	if (close(dfd) < 0)
+		return pr_err("close(dfd)");
+	if (close(fsfd) < 0)
+		return pr_err("close(fsfd)");
+	if (close(fsfd2) < 0)
+		return pr_err("close(fsfd2)");
+
+	return 0;
+}
+
+int main()
+{
+	pid_t pid;
+	int status;
+
+	if (unshare(CLONE_NEWNS | CLONE_NEWPID))
+		return pr_err("unshare");
+
+	if (sys_mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL))
+		return pr_err("mount");
+
+	pid = fork();
+	if (pid < 0)
+		return pr_err("fork");
+	if (pid == 0)
+		return test();
+	if (waitpid(pid, &status, 0) != pid)
+		return pr_err("waitpid");
+	if (status)
+		return 1;
+	return 0;
+}
-- 
2.17.1

  reply	other threads:[~2018-08-11  0:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-10 22:00 [PATCH dhowells/mount-api 1/2] fs/fsconfig: handle FSCONFIG_CMD_RECONFIGURE Andrei Vagin
2018-08-10 22:00 ` Andrei Vagin [this message]
2018-08-20 23:47   ` [PATCH dhowells/mount-api 2/2] selftests: implement a test for a new mount API Dave Chinner
2018-08-10 22:52 ` [PATCH dhowells/mount-api 1/2] fs/fsconfig: handle FSCONFIG_CMD_RECONFIGURE David Howells
2018-08-10 22:55 ` [PATCH dhowells/mount-api 2/2] selftests: implement a test for a new mount API David Howells
2018-08-20 23:29   ` Andrei Vagin
2018-08-21  9:19   ` David Howells

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180810220027.2735-2-avagin@openvz.org \
    --to=avagin@openvz.org \
    --cc=avagin@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).