tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c
Extension
.c
Size
12714 bytes
Lines
726
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 (fd >= 0) {
			close(fd);
			_exit(9);
		}
		if (errno != ENOENT)
			_exit(10);

		/*
		 * Use the new mount API to create tmpfs and get a mount fd.
		 * We need the fd because after attaching the tmpfs on top of
		 * "/", path resolution of "/" still returns the process root
		 * (nullfs) without following the overmount.  The mount fd
		 * lets us fchdir + chroot into the tmpfs.
		 */
		fsfd = sys_fsopen("tmpfs", 0);
		if (fsfd < 0)
			_exit(11);

		if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "size", "1M", 0)) {
			close(fsfd);
			_exit(12);
		}

		if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) {
			close(fsfd);
			_exit(13);
		}

		mntfd = sys_fsmount(fsfd, 0, 0);
		close(fsfd);
		if (mntfd < 0)
			_exit(14);

		if (sys_move_mount(mntfd, "", AT_FDCWD, "/",
				   MOVE_MOUNT_F_EMPTY_PATH)) {
			close(mntfd);
			_exit(15);
		}

		if (count_mounts() != 2) {
			close(mntfd);
			_exit(16);
		}

		/* Enter the tmpfs via the mount fd */
		if (fchdir(mntfd)) {
			close(mntfd);
			_exit(17);
		}

		if (chroot(".")) {
			close(mntfd);
			_exit(18);
		}

		close(mntfd);

		/* Verify "/" now resolves to tmpfs */
		root_id = get_unique_mnt_id("/");
		if (!root_id)
			_exit(19);

		sm = statmount_alloc(root_id, 0, STATMOUNT_FS_TYPE, 0);
		if (!sm)
			_exit(20);

		if (!(sm->mask & STATMOUNT_FS_TYPE))
			_exit(21);

		if (strcmp(sm->str + sm->fs_type, "tmpfs") != 0)
			_exit(22);

		free(sm);

		/* Verify tmpfs is writable */
		fd = open("/testfile", O_CREAT | O_RDWR, 0644);
		if (fd < 0)
			_exit(23);

		if (write(fd, "test", 4) != 4) {
			close(fd);
			_exit(24);
		}

		close(fd);

		if (access("/testfile", F_OK))
			_exit(25);

		_exit(0);

Annotation

Implementation Notes