tools/testing/selftests/core/close_range_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/core/close_range_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/core/close_range_test.c- Extension
.c- Size
- 15054 bytes
- Lines
- 667
- 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.hlinux/kernel.hlimits.hstdbool.hstdio.hstdlib.hstring.hsyscall.hunistd.hsys/resource.hlinux/close_range.hkselftest_harness.h../clone3/clone3_selftests.h
Detected Declarations
function sys_close_rangefunction ASSERT_GEfunction EXPECT_EQfunction ASSERT_GEfunction ASSERT_GEfunction ASSERT_GEfunction ASSERT_GEfunction ASSERT_GE
Annotated Snippet
ASSERT_GE(fd, 0) {
if (errno == ENOENT)
SKIP(return, "Skipping test since /dev/null does not exist");
}
open_fds[i] = fd;
}
EXPECT_EQ(-1, sys_close_range(open_fds[0], open_fds[100], -1)) {
if (errno == ENOSYS)
SKIP(return, "close_range() syscall not supported");
}
for (i = 0; i < 100; i++) {
ret = fcntl(open_fds[i], F_DUPFD_QUERY, open_fds[i + 1]);
if (ret < 0) {
EXPECT_EQ(errno, EINVAL);
} else {
EXPECT_EQ(ret, 0);
}
}
EXPECT_EQ(0, sys_close_range(open_fds[0], open_fds[50], 0));
for (i = 0; i <= 50; i++)
EXPECT_EQ(-1, fcntl(open_fds[i], F_GETFL));
for (i = 51; i <= 100; i++)
EXPECT_GT(fcntl(open_fds[i], F_GETFL), -1);
/* create a couple of gaps */
close(57);
close(78);
close(81);
close(82);
close(84);
close(90);
EXPECT_EQ(0, sys_close_range(open_fds[51], open_fds[92], 0));
for (i = 51; i <= 92; i++)
EXPECT_EQ(-1, fcntl(open_fds[i], F_GETFL));
for (i = 93; i <= 100; i++)
EXPECT_GT(fcntl(open_fds[i], F_GETFL), -1);
/* test that the kernel caps and still closes all fds */
EXPECT_EQ(0, sys_close_range(open_fds[93], open_fds[99], 0));
for (i = 93; i <= 99; i++)
EXPECT_EQ(-1, fcntl(open_fds[i], F_GETFL));
EXPECT_GT(fcntl(open_fds[i], F_GETFL), -1);
EXPECT_EQ(0, sys_close_range(open_fds[100], open_fds[100], 0));
EXPECT_EQ(-1, fcntl(open_fds[100], F_GETFL));
}
TEST(close_range_unshare)
{
int i, ret, status;
pid_t pid;
int open_fds[101];
struct __clone_args args = {
.flags = CLONE_FILES,
.exit_signal = SIGCHLD,
};
for (i = 0; i < ARRAY_SIZE(open_fds); i++) {
int fd;
fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
ASSERT_GE(fd, 0) {
if (errno == ENOENT)
SKIP(return, "Skipping test since /dev/null does not exist");
}
open_fds[i] = fd;
}
pid = sys_clone3(&args, sizeof(args));
ASSERT_GE(pid, 0);
if (pid == 0) {
ret = sys_close_range(open_fds[0], open_fds[50],
CLOSE_RANGE_UNSHARE);
if (ret)
exit(EXIT_FAILURE);
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `linux/kernel.h`, `limits.h`, `stdbool.h`, `stdio.h`, `stdlib.h`, `string.h`.
- Detected declarations: `function sys_close_range`, `function ASSERT_GE`, `function EXPECT_EQ`, `function ASSERT_GE`, `function ASSERT_GE`, `function ASSERT_GE`, `function ASSERT_GE`, `function ASSERT_GE`.
- 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.