tools/testing/selftests/pid_namespace/pidns_init_via_setns.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/pid_namespace/pidns_init_via_setns.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/pid_namespace/pidns_init_via_setns.c- Extension
.c- Size
- 5501 bytes
- Lines
- 239
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
fcntl.hsched.hstdio.hsys/types.hunistd.hkselftest_harness.h../pidfd/pidfd.h
Detected Declarations
function unsharefunction pidns_init_via_setns_set_tid_grandchildfunction pidns_init_via_setns_set_tid_childfunction pidns_init_via_setns_set_tid_wrapper
Annotated Snippet
if (grandchild == 0) {
/* Should be init (PID 1) in the new namespace */
if (getpid() != 1)
_exit(1);
_exit(0);
}
ASSERT_EQ(0, wait_for_pid(grandchild));
_exit(0);
}
close(pipe_fd[0]);
ASSERT_EQ(0, unshare(CLONE_NEWPID));
/* Signal child that the new PID namespace is ready */
buf = 0;
ASSERT_EQ(1, write_nointr(pipe_fd[1], &buf, 1));
close(pipe_fd[1]);
ASSERT_EQ(0, wait_for_pid(child));
}
/*
* Similar to pidns_init_via_setns, but:
* 1. Parent enters a new PID namespace right from the start to be able to
* later freely use pid 1001 in it.
* 2. After forking child, parent also calls unshare(CLONE_NEWUSER)
* before unshare(CLONE_NEWPID) so that new old and new pid namespaces have
* different user namespace owners.
* 3. Child uses clone3() with set_tid={1, 1001} instead of fork() and
* grandchild checks that it gets desired pids .
*
* Flow:
* 1. Test process creates a new PID namespace and forks a wrapper
* (PID 1 in the outer namespace).
* 2. Wrapper forks a child.
* 3. Wrapper calls unshare(CLONE_NEWUSER) + unshare(CLONE_NEWPID)
* to create an inner PID namespace.
* 4. Wrapper signals the child via pipe.
* 5. Child opens wrapper's /proc/<pid>/ns/pid_for_children and calls
* setns(fd, CLONE_NEWPID) to join the inner namespace.
* 6. Child calls clone3() with set_tid={1, 1001}.
* 7. Grandchild verifies its NSpid ends with "1001 1".
*/
pid_t set_tid[] = {1, 1001};
static int pidns_init_via_setns_set_tid_grandchild(struct __test_metadata *_metadata)
{
char *line = NULL;
size_t len = 0;
int found = 0;
FILE *gf;
gf = fopen("/proc/self/status", "r");
ASSERT_NE(gf, NULL);
while (getline(&line, &len, gf) != -1) {
if (strncmp(line, "NSpid:", 6) != 0)
continue;
for (int i = 0; i < 2; i++) {
char *last = strrchr(line, '\t');
pid_t pid;
ASSERT_NE(last, NULL);
ASSERT_EQ(sscanf(last, "%d", &pid), 1);
ASSERT_EQ(pid, set_tid[i]);
*last = '\0';
}
found = true;
break;
}
free(line);
fclose(gf);
ASSERT_TRUE(found);
return 0;
}
static int pidns_init_via_setns_set_tid_child(struct __test_metadata *_metadata,
pid_t parent_pid, int pipe_fd[2])
{
struct __clone_args args = {
.exit_signal = SIGCHLD,
.set_tid = ptr_to_u64(set_tid),
.set_tid_size = 2,
};
Annotation
- Immediate include surface: `fcntl.h`, `sched.h`, `stdio.h`, `sys/types.h`, `unistd.h`, `kselftest_harness.h`, `../pidfd/pidfd.h`.
- Detected declarations: `function unshare`, `function pidns_init_via_setns_set_tid_grandchild`, `function pidns_init_via_setns_set_tid_child`, `function pidns_init_via_setns_set_tid_wrapper`.
- 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.