tools/testing/selftests/namespaces/nsid_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/namespaces/nsid_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/namespaces/nsid_test.c
Extension
.c
Size
22868 bytes
Lines
994
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) {
			/* Skip test if we don't have permission */
			if (errno == EPERM || errno == EACCES) {
				write(pipefd[1], "S", 1); /* Signal skip */
				_exit(0);
			}
			_exit(1);
		}

		/* Signal success */
		write(pipefd[1], "Y", 1);
		close(pipefd[1]);

		/* Keep namespace alive */
		pause();
		_exit(0);
	}

	/* Track child for cleanup */
	self->child_pid = pid;

	/* Parent process */
	close(pipefd[1]);

	char buf;
	ASSERT_EQ(read(pipefd[0], &buf, 1), 1);
	close(pipefd[0]);

	if (buf == 'S') {
		/* Child couldn't create namespace, skip test */
		close(fd_parent_mntns);
		SKIP(return, "No permission to create mount namespace");
	}

	ASSERT_EQ(buf, 'Y');

	/* Open child's mount namespace */
	char path[256];
	snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
	fd_child_mntns = open(path, O_RDONLY);
	ASSERT_GE(fd_child_mntns, 0);

	/* Get child's mount namespace ID */
	ret = ioctl(fd_child_mntns, NS_GET_ID, &child_mnt_ns_id);
	ASSERT_EQ(ret, 0);
	ASSERT_NE(child_mnt_ns_id, 0);

	/* Parent and child should have different mount namespace IDs */
	ASSERT_NE(parent_mnt_ns_id, child_mnt_ns_id);

	close(fd_parent_mntns);
	close(fd_child_mntns);
}

TEST(nsid_cgroupns_basic)
{
	__u64 cgroup_ns_id = 0;
	int fd_cgroupns;
	int ret;

	/* Open the current cgroup namespace */
	fd_cgroupns = open("/proc/self/ns/cgroup", O_RDONLY);
	ASSERT_GE(fd_cgroupns, 0);

	/* Get the cgroup namespace ID */
	ret = ioctl(fd_cgroupns, NS_GET_ID, &cgroup_ns_id);
	ASSERT_EQ(ret, 0);
	ASSERT_NE(cgroup_ns_id, 0);

	/* Verify we can get the same ID again */
	__u64 cgroup_ns_id2 = 0;
	ret = ioctl(fd_cgroupns, NS_GET_ID, &cgroup_ns_id2);
	ASSERT_EQ(ret, 0);
	ASSERT_EQ(cgroup_ns_id, cgroup_ns_id2);

	close(fd_cgroupns);
}

TEST_F(nsid, cgroupns_separate)
{
	__u64 parent_cgroup_ns_id = 0;
	__u64 child_cgroup_ns_id = 0;
	int fd_parent_cgroupns, fd_child_cgroupns;
	int ret;
	pid_t pid;
	int pipefd[2];

	/* Get parent's cgroup namespace ID */
	fd_parent_cgroupns = open("/proc/self/ns/cgroup", O_RDONLY);
	ASSERT_GE(fd_parent_cgroupns, 0);

Annotation

Implementation Notes