tools/testing/selftests/landlock/fs_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/landlock/fs_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/landlock/fs_test.c
Extension
.c
Size
275589 bytes
Lines
10193
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct mnt_opt {
	const char *const source;
	const char *const type;
	const unsigned long flags;
	const char *const data;
};

#define MNT_TMP_DATA "size=4m,mode=700"

static const struct mnt_opt mnt_tmp = {
	.type = "tmpfs",
	.data = MNT_TMP_DATA,
};

static int mount_opt(const struct mnt_opt *const mnt, const char *const target)
{
	return mount(mnt->source ?: mnt->type, target, mnt->type, mnt->flags,
		     mnt->data);
}

static void prepare_layout_opt(struct __test_metadata *const _metadata,
			       const struct mnt_opt *const mnt)
{
	disable_caps(_metadata);
	umask(0077);
	create_directory(_metadata, TMP_DIR);

	/*
	 * Do not pollute the rest of the system: creates a private mount point
	 * for tests relying on pivot_root(2) and move_mount(2).
	 */
	set_cap(_metadata, CAP_SYS_ADMIN);
	ASSERT_EQ(0, unshare(CLONE_NEWNS | CLONE_NEWCGROUP));
	ASSERT_EQ(0, mount_opt(mnt, TMP_DIR))
	{
		TH_LOG("Failed to mount the %s filesystem: %s", mnt->type,
		       strerror(errno));
		/*
		 * FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP()
		 * failed, so we need to explicitly do a minimal cleanup to
		 * avoid cascading errors with other tests that don't depend on
		 * the same filesystem.
		 */
		remove_path(TMP_DIR);
	}
	ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL));
	clear_cap(_metadata, CAP_SYS_ADMIN);
}

static void prepare_layout(struct __test_metadata *const _metadata)
{
	prepare_layout_opt(_metadata, &mnt_tmp);
}

static void cleanup_layout(struct __test_metadata *const _metadata)
{
	set_cap(_metadata, CAP_SYS_ADMIN);
	if (umount(TMP_DIR)) {
		/*
		 * According to the test environment, the mount point of the
		 * current directory may be shared or not, which changes the
		 * visibility of the nested TMP_DIR mount point for the test's
		 * parent process doing this cleanup.
		 */
		ASSERT_EQ(EINVAL, errno);
	}
	clear_cap(_metadata, CAP_SYS_ADMIN);
	EXPECT_EQ(0, remove_path(TMP_DIR));
}

/* clang-format off */
FIXTURE(layout0) {};
/* clang-format on */

FIXTURE_SETUP(layout0)
{
	prepare_layout(_metadata);
}

FIXTURE_TEARDOWN_PARENT(layout0)
{
	cleanup_layout(_metadata);
}

static void create_layout1(struct __test_metadata *const _metadata)
{
	create_file(_metadata, file1_s1d1);
	create_file(_metadata, file1_s1d2);
	create_file(_metadata, file1_s1d3);
	create_file(_metadata, file2_s1d1);

Annotation

Implementation Notes