πŸ”₯ Let’s roll out your next vault weapon:


🧠 02_fd_leak_visualizer

πŸ’£ FD Leak Visualizer

See what happens when FDs are opened without being closed.
Track the growth. Watch it bleed.
Expose one of the most common bugs in C: unclosed FDs.


🎯 GOAL:

  • Open a bunch of FDs without closing them.

  • Monitor what happens over time.

  • See how ulimit -n sets your per-process FD ceiling.

  • Print the currently open FDs by inspecting /proc/self/fd/


πŸ“„ 02_fd_leak_visualizer.c βœ… (Single-file only – perfectly scoped)

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
 
#define MAX_LEAK 4096
#define SLEEP_INTERVAL 50000 // microseconds
 
void	print_open_fds(void)
{
	DIR *dir = opendir("/proc/self/fd");
	if (!dir)
	{
		perror("opendir");
		return;
	}
 
	struct dirent *entry;
	printf("πŸ” Open file descriptors:\n");
	while ((entry = readdir(dir)) != NULL)
	{
		printf("  FD: %s\n", entry->d_name);
	}
	closedir(dir);
}
 
int	main(void)
{
	int	leaked_fds[MAX_LEAK];
	int	i = 0;
 
	printf("πŸ’£ Starting FD leak...\n");
 
	while (i < MAX_LEAK)
	{
		leaked_fds[i] = open("/dev/null", O_RDONLY);
		if (leaked_fds[i] == -1)
		{
			perror("open");
			break;
		}
		if (i % 100 == 0)
		{
			printf("πŸ” Leaked %d file descriptors\n", i);
			usleep(SLEEP_INTERVAL);
			print_open_fds();
			printf("-----------------------------\n");
		}
		i++;
	}
	printf("🧨 Final leaked FD count: %d\n", i);
	return (0);
}

πŸ“„ README.md

# πŸ’£ 02_fd_leak_visualizer
 
## πŸ”Ž What it does:
- Opens `/dev/null` up to MAX_LEAK times
- Does **not** close the file descriptors
- Every 100 iterations:
  - Logs current count
  - Lists all open FDs using `/proc/self/fd/`
 
## βœ… What you’ll learn:
- How leaking FDs looks at runtime
- How `open()` fails when `EMFILE` (Too many open files)
- How to visualize runtime resource usage using `procfs`
 
## πŸ“€ Sample output:

πŸ” Leaked 300 file descriptors FD: 0 FD: 1 FD: 2 FD: 3 …


## 🚨 Warnings:
- May hit per-process FD ceiling (check `ulimit -n`)
- If you don’t clean up, your process will leak FDs until it breaks

πŸ§ͺ Run Instructions

ulimit -n 1024         # Optional: reduce max FDs for testing
gcc 02_fd_leak_visualizer.c -o fd_leak
./fd_leak

🧠 Why This Project Matters

What it showsWhy it’s powerful
FD exhaustionCommon bug in prod systems
/proc/self/fdReal-time debugging tool
Silent resource leaksCan crash long-running daemons
Visual FD trackingReinforces kernel FD table intuition

πŸ” Ready for 03_read_closed_fd_trap/ next?

This one reads from an already closed FD and traps the result.
Let me know β€” we’ll keep assembling the vault πŸ’£πŸ§ πŸ“‚