tools/testing/selftests/pidfd/pidfd_file_handle_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/pidfd/pidfd_file_handle_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/pidfd/pidfd_file_handle_test.c- Extension
.c- Size
- 14457 bytes
- Lines
- 564
- 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
errno.hfcntl.hlimits.hlinux/types.hpoll.hsched.hsignal.hstdio.hstdlib.hstring.hsyscall.hsys/prctl.hsys/wait.hunistd.hsys/socket.hlinux/kcmp.hsys/stat.hpidfd.hkselftest_harness.h
Detected Declarations
function ASSERT_LT
Annotated Snippet
if (pidfd >= 0) {
TH_LOG("Managed to open pidfd outside of the caller's pid namespace hierarchy");
_exit(1);
}
_exit(0);
}
ASSERT_EQ(wait_for_pid(pid), 0);
free(fh);
}
/*
* Test that we can decode a pidfs file handle of a process that has
* exited but not been reaped.
*/
TEST_F(file_handle, pid_has_exited)
{
int mnt_id, pidfd, child_pidfd3;
struct file_handle *fh;
struct stat st1, st2;
fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
ASSERT_NE(fh, NULL);
memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ);
fh->handle_bytes = MAX_HANDLE_SZ;
ASSERT_EQ(name_to_handle_at(self->child_pidfd3, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
ASSERT_EQ(fstat(self->child_pidfd3, &st1), 0);
pidfd = open_by_handle_at(self->pidfd, fh, 0);
ASSERT_GE(pidfd, 0);
ASSERT_EQ(fstat(pidfd, &st2), 0);
ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
ASSERT_EQ(close(pidfd), 0);
child_pidfd3 = self->child_pidfd3;
self->child_pidfd3 = -EBADF;
EXPECT_EQ(sys_pidfd_send_signal(child_pidfd3, SIGKILL, NULL, 0), 0);
EXPECT_EQ(close(child_pidfd3), 0);
EXPECT_EQ(sys_waitid(P_PID, self->child_pid3, NULL, WEXITED | WNOWAIT), 0);
pidfd = open_by_handle_at(self->pidfd, fh, 0);
ASSERT_GE(pidfd, 0);
EXPECT_EQ(sys_waitid(P_PID, self->child_pid3, NULL, WEXITED), 0);
}
/*
* Test that we fail to decode a pidfs file handle of a process that has
* already been reaped.
*/
TEST_F(file_handle, pid_has_been_reaped)
{
int mnt_id, pidfd, child_pidfd3;
struct file_handle *fh;
struct stat st1, st2;
fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
ASSERT_NE(fh, NULL);
memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ);
fh->handle_bytes = MAX_HANDLE_SZ;
ASSERT_EQ(name_to_handle_at(self->child_pidfd3, "", fh, &mnt_id, AT_EMPTY_PATH), 0);
ASSERT_EQ(fstat(self->child_pidfd3, &st1), 0);
pidfd = open_by_handle_at(self->pidfd, fh, 0);
ASSERT_GE(pidfd, 0);
ASSERT_EQ(fstat(pidfd, &st2), 0);
ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
ASSERT_EQ(close(pidfd), 0);
child_pidfd3 = self->child_pidfd3;
self->child_pidfd3 = -EBADF;
EXPECT_EQ(sys_pidfd_send_signal(child_pidfd3, SIGKILL, NULL, 0), 0);
EXPECT_EQ(close(child_pidfd3), 0);
EXPECT_EQ(sys_waitid(P_PID, self->child_pid3, NULL, WEXITED), 0);
pidfd = open_by_handle_at(self->pidfd, fh, 0);
ASSERT_LT(pidfd, 0);
}
/*
* Test valid flags to open a pidfd file handle. Note, that
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `limits.h`, `linux/types.h`, `poll.h`, `sched.h`, `signal.h`, `stdio.h`.
- Detected declarations: `function ASSERT_LT`.
- 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.