tools/testing/selftests/filesystems/statmount/statmount_test_ns.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/filesystems/statmount/statmount_test_ns.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/statmount/statmount_test_ns.c
Extension
.c
Size
8317 bytes
Lines
362
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 (ret < 0) {
			ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
			return NSID_ERROR;
		}

		if (sm.mask != STATMOUNT_MNT_NS_ID) {
			ksft_print_msg("statmount mnt ns id unavailable\n");
			return NSID_SKIP;
		}

		if (sm.mnt_ns_id != mnt_ns_id) {
			ksft_print_msg("listmount gave us the wrong ns id: 0x%llx != 0x%llx\n",
				       (unsigned long long)sm.mnt_ns_id,
				       (unsigned long long)mnt_ns_id);
			return NSID_FAIL;
		}
	}

	return NSID_PASS;
}

static void test_listmount_ns(void)
{
	uint64_t nr_mounts;
	char pval;
	int child_ready_pipe[2];
	int parent_ready_pipe[2];
	pid_t pid;
	int ret, child_ret;

	if (pipe(child_ready_pipe) < 0)
		ksft_exit_fail_msg("failed to create the child pipe: %s\n",
				   strerror(errno));
	if (pipe(parent_ready_pipe) < 0)
		ksft_exit_fail_msg("failed to create the parent pipe: %s\n",
				   strerror(errno));

	pid = fork();
	if (pid < 0)
		ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));

	if (pid == 0) {
		char cval;
		uint64_t list[256];

		close(child_ready_pipe[0]);
		close(parent_ready_pipe[1]);

		ret = setup_namespace();
		if (ret != NSID_PASS)
			exit(ret);

		nr_mounts = listmount(LSMT_ROOT, 0, 0, list, 256, 0);
		if (nr_mounts == (uint64_t)-1) {
			ksft_print_msg("listmount: %s\n", strerror(errno));
			exit(NSID_FAIL);
		}

		/*
		 * Tell our parent how many mounts we have, and then wait for it
		 * to tell us we're done.
		 */
		if (write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts)) !=
					sizeof(nr_mounts))
			ret = NSID_ERROR;
		if (read(parent_ready_pipe[0], &cval, sizeof(cval)) != sizeof(cval))
			ret = NSID_ERROR;
		exit(NSID_PASS);
	}

	close(child_ready_pipe[1]);
	close(parent_ready_pipe[0]);

	/* Wait until the child has created everything. */
	if (read(child_ready_pipe[0], &nr_mounts, sizeof(nr_mounts)) !=
	    sizeof(nr_mounts))
		ret = NSID_ERROR;

	ret = validate_external_listmount(pid, nr_mounts);

	if (write(parent_ready_pipe[1], &pval, sizeof(pval)) != sizeof(pval))
		ret = NSID_ERROR;

	child_ret = wait_for_pid(pid);
	if (child_ret != NSID_PASS)
		ret = child_ret;
	handle_result(ret, "test listmount ns id");
}

int main(void)

Annotation

Implementation Notes