tools/testing/selftests/landlock/fs_bench.c

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/landlock/fs_bench.c
Extension
.c
Size
5288 bytes
Lines
215
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

if (use_landlock) {
			struct landlock_path_beneath_attr attr = {
				.allowed_access = LANDLOCK_ACCESS_FS_IOCTL_DEV,
				.parent_fd = curr,
			};
			if (landlock_add_rule(ruleset_fd,
					      LANDLOCK_RULE_PATH_BENEATH, &attr,
					      0) < 0)
				err(1, "landlock_add_rule");
		}

		if (mkdirat(curr, path, 0700) < 0)
			err(1, "mkdirat(%s)", path);

		prev = curr;
		curr = openat(curr, path, O_PATH);
		if (curr < 0)
			err(1, "openat(%s)", path);

		close(prev);
	}

	if (use_landlock) {
		if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
			err(1, "prctl");

		if (landlock_restrict_self(ruleset_fd, 0) < 0)
			err(1, "landlock_restrict_self");
	}

	close(ruleset_fd);
	return curr;
}

static void remove_recursively(const size_t depth)
{
	const char *path = "d"; /* directory name */

	int fd = openat(AT_FDCWD, ".", O_PATH);

	if (fd < 0)
		err(1, "openat(.)");

	for (size_t i = 0; i < depth - 1; i++) {
		int oldfd = fd;

		fd = openat(fd, path, O_PATH);
		if (fd < 0)
			err(1, "openat(%s)", path);
		close(oldfd);
	}

	for (size_t i = 0; i < depth; i++) {
		if (unlinkat(fd, path, AT_REMOVEDIR) < 0)
			err(1, "unlinkat(%s)", path);
		int newfd = openat(fd, "..", O_PATH);

		close(fd);
		fd = newfd;
	}
	close(fd);
}

int main(int argc, char *argv[])
{
	bool use_landlock = true;
	size_t num_iterations = 100000;
	size_t num_subdirs = 10000;
	int c, curr, fd;
	struct tms start_time, end_time;

	setbuf(stdout, NULL);
	while ((c = getopt(argc, argv, "hLd:n:")) != -1) {
		switch (c) {
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'L':
			use_landlock = false;
			break;
		case 'd':
			num_subdirs = atoi(optarg);
			break;
		case 'n':
			num_iterations = atoi(optarg);
			break;
		default:
			usage(argv[0]);
			return EXIT_FAILURE;
		}

Annotation

Implementation Notes