π£π SYSTEM MELTDOWN SIMULATOR COMING RIGHT UP
π§ 05_fd_exhaustion_test
π₯ FD Exhaustion Test
Letβs see what happens when you open as many file descriptors as your system allows.
Can you detect the hard stop?
Can you catchEMFILE
orENFILE
?
𧬠Purpose:
-
Discover the maximum number of open FDs
-
Watch the system reject you with
errno = EMFILE
-
Validate
ulimit -n
-
Catch your own failure gracefully π₯
π 05_fd_exhaustion_test.c
(single-file testbed)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/resource.h>
#define MAX_FDS 65536
int main(void)
{
int fd_list[MAX_FDS];
int i = 0;
char path[] = "/dev/null";
struct rlimit lim;
if (getrlimit(RLIMIT_NOFILE, &lim) == 0)
{
printf("π’ Soft FD limit: %lu\n", lim.rlim_cur);
printf("π’ Hard FD limit: %lu\n\n", lim.rlim_max);
}
printf("π₯ Attempting to open FDs until exhaustion...\n");
while (i < MAX_FDS)
{
int fd = open(path, O_RDONLY);
if (fd == -1)
{
perror("β open");
printf("𧨠Stopped at FD #%d β errno = %d (%s)\n", i, errno, strerror(errno));
break;
}
fd_list[i] = fd;
if (i % 100 == 0)
printf("π FD %d opened\n", i);
i++;
}
printf("π Reached limit: %d open FDs\n", i);
while (--i >= 0)
close(fd_list[i]);
return (0);
}
β Sample Output
π’ Soft FD limit: 1024
π’ Hard FD limit: 1048576
π₯ Attempting to open FDs until exhaustion...
π FD 0 opened
π FD 100 opened
π FD 200 opened
...
β open: Too many open files
𧨠Stopped at FD #1024 β errno = 24 (Too many open files)
π Reached limit: 1024 open FDs
π Learn This Deep
Concept | Value |
---|---|
RLIMIT_NOFILE | Controls how many FDs your process can open |
errno == EMFILE | π Per-process FD limit reached |
errno == ENFILE | β οΈ System-wide FD limit reached (rare) |
Leak test | Great to simulate long-running daemons |
/dev/null | Ideal for safe, non-blocking test targets |
π§ͺ Run This With:
gcc 05_fd_exhaustion_test.c -o fdburn
ulimit -n 1024 # Try limiting if you're too powerful
./fdburn
π Want Even More?
You can follow up with:
-
[[06_malloc_after_fork_glitch/]]
β simulate a fork-time memory inconsistency -
[[07_shared_mmap_allocator/]]
β your own mmap-backed allocator -
[[zombie_maker/]]
β test if zombie + FD exhaustion creates kernel instability
Say go and weβll build the next overload vector π§ π£πΎ