tools/testing/selftests/net/reuseaddr_ports_exhausted.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/reuseaddr_ports_exhausted.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/reuseaddr_ports_exhausted.c- Extension
.c- Size
- 4066 bytes
- Lines
- 163
- 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
arpa/inet.hnetinet/in.hsys/socket.hsys/types.hunistd.hkselftest_harness.h
Detected Declarations
struct reuse_optsfunction bind_port
Annotated Snippet
struct reuse_opts {
int reuseaddr[2];
int reuseport[2];
};
struct reuse_opts unreusable_opts[12] = {
{{0, 0}, {0, 0}},
{{0, 0}, {0, 1}},
{{0, 0}, {1, 0}},
{{0, 0}, {1, 1}},
{{0, 1}, {0, 0}},
{{0, 1}, {0, 1}},
{{0, 1}, {1, 0}},
{{0, 1}, {1, 1}},
{{1, 0}, {0, 0}},
{{1, 0}, {0, 1}},
{{1, 0}, {1, 0}},
{{1, 0}, {1, 1}},
};
struct reuse_opts reusable_opts[4] = {
{{1, 1}, {0, 0}},
{{1, 1}, {0, 1}},
{{1, 1}, {1, 0}},
{{1, 1}, {1, 1}},
};
int bind_port(struct __test_metadata *_metadata, int reuseaddr, int reuseport)
{
struct sockaddr_in local_addr;
int len = sizeof(local_addr);
int fd, ret;
fd = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_NE(-1, fd) TH_LOG("failed to open socket.");
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int));
ASSERT_EQ(0, ret) TH_LOG("failed to setsockopt: SO_REUSEADDR.");
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(int));
ASSERT_EQ(0, ret) TH_LOG("failed to setsockopt: SO_REUSEPORT.");
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
local_addr.sin_port = 0;
if (bind(fd, (struct sockaddr *)&local_addr, len) == -1) {
close(fd);
return -1;
}
return fd;
}
TEST(reuseaddr_ports_exhausted_unreusable)
{
struct reuse_opts *opts;
int i, j, fd[2];
for (i = 0; i < 12; i++) {
opts = &unreusable_opts[i];
for (j = 0; j < 2; j++)
fd[j] = bind_port(_metadata, opts->reuseaddr[j], opts->reuseport[j]);
ASSERT_NE(-1, fd[0]) TH_LOG("failed to bind.");
EXPECT_EQ(-1, fd[1]) TH_LOG("should fail to bind.");
for (j = 0; j < 2; j++)
if (fd[j] != -1)
close(fd[j]);
}
}
TEST(reuseaddr_ports_exhausted_reusable_same_euid)
{
struct reuse_opts *opts;
int i, j, fd[2];
for (i = 0; i < 4; i++) {
opts = &reusable_opts[i];
for (j = 0; j < 2; j++)
fd[j] = bind_port(_metadata, opts->reuseaddr[j], opts->reuseport[j]);
ASSERT_NE(-1, fd[0]) TH_LOG("failed to bind.");
if (opts->reuseport[0] && opts->reuseport[1]) {
EXPECT_EQ(-1, fd[1]) TH_LOG("should fail to bind because both sockets successfully listened.");
} else {
Annotation
- Immediate include surface: `arpa/inet.h`, `netinet/in.h`, `sys/socket.h`, `sys/types.h`, `unistd.h`, `kselftest_harness.h`.
- Detected declarations: `struct reuse_opts`, `function bind_port`.
- 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.