tools/testing/selftests/namespaces/regression_pidfd_setns_test.c

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/namespaces/regression_pidfd_setns_test.c
Extension
.c
Size
2883 bytes
Lines
114
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 (unshare(CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWUSER) < 0) {
			close(sv[1]);
			_exit(1);
		}

		/* Signal parent that namespaces are ready */
		if (write_nointr(sv[1], "1", 1) < 0) {
			close(sv[1]);
			_exit(1);
		}

		close(sv[1]);
		_exit(0);
	}
	ASSERT_GE(pidfd, 0);
	EXPECT_EQ(close(sv[1]), 0);

	ret = read_nointr(sv[0], &c, 1);
	ASSERT_EQ(ret, 1);
	EXPECT_EQ(close(sv[0]), 0);

	/* Set to child's namespaces via pidfd */
	ret = setns(pidfd, CLONE_NEWUTS | CLONE_NEWIPC);
	TH_LOG("setns() returned %d", ret);
	close(pidfd);
}

/*
 * Simple pidfd setns test using create_child().
 *
 * This variation uses create_child() with namespace flags directly.
 * Namespaces are created immediately at clone time.
 */
TEST(simple_pidfd_setns_clone)
{
	pid_t child_pid;
	int pidfd = -1;
	int ret;

	/* Ignore SIGCHLD for autoreap */
	ASSERT_NE(signal(SIGCHLD, SIG_IGN), SIG_ERR);

	/* Create a child process with new namespaces using create_child() */
	child_pid = create_child(&pidfd, CLONE_NEWUSER | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET);
	ASSERT_GE(child_pid, 0);

	if (child_pid == 0) {
		/* Child: sleep for a while so parent can setns to us */
		sleep(2);
		_exit(0);
	}

	/* Parent: pidfd was already created by create_child() */
	ASSERT_GE(pidfd, 0);

	/* Set to child's namespaces via pidfd */
	ret = setns(pidfd, CLONE_NEWUTS | CLONE_NEWIPC);
	close(pidfd);
	TH_LOG("setns() returned %d", ret);
}

TEST_HARNESS_MAIN

Annotation

Implementation Notes