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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
errno.hsched.hsignal.hstdio.hstdlib.hstring.hsys/socket.hunistd.h../pidfd/pidfd.h../kselftest_harness.h
Detected Declarations
function setnsfunction create_child
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
- Immediate include surface: `errno.h`, `sched.h`, `signal.h`, `stdio.h`, `stdlib.h`, `string.h`, `sys/socket.h`, `unistd.h`.
- Detected declarations: `function setns`, `function create_child`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.