πŸ’£πŸ’€ 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 catch EMFILE or ENFILE?


🧬 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

ConceptValue
RLIMIT_NOFILEControls how many FDs your process can open
errno == EMFILEπŸ’€ Per-process FD limit reached
errno == ENFILE☠️ System-wide FD limit reached (rare)
Leak testGreat to simulate long-running daemons
/dev/nullIdeal 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 πŸ§ πŸ’£πŸ’Ύ