r/C_Programming • u/icy-mist-01 • 23d ago
Article Wrote a lightweight Linux dynamic file descriptor leak detector project in C using LD_PRELOAD. Looking for critiques and reviews.
I've written an LD_PRELOAD file descriptor leak tracker in C.
https://github.com/anu-icon/libfdhunt
Apart from memory leaks which get the most spotlight, file descriptor leaks is a long-standing problem in production environments, often causing service crashes.
While Valgrind has --track-fds=yes, running full binary translation can be overkill or too slow if you're working with timing-sensitive code.
Polling lsof or /proc/$PID/fd also misses transient descriptors that open and vanish.
This is highly useful since you don't need advanced tools and extra permissions, don't have to recompile or review entire large code bases to find the flaw.
Just LD_PRELOAD=./build/libfdhunt.so /path/to/your/program [args]
How it works under the hood:
For anyone interested in the implementation:
Symbol Interposition: It uses dlsym(RTLD_NEXT, ...) to wrap standard libc syscall wrappers (open, close, pipe2, socket, dup2, epoll_create1, etc.).
O(1) Flat Array Registry: Instead of using linked lists or calling malloc/free inside hot syscall paths (which invites lock contention and heap re-entrancy issues), it uses a flat array indexed directly by descriptor number (table[fd]). It sizes the table on demand using sysconf(_SC_OPEN_MAX) and guards slot flips with a single mutex.
Exit Hook: Leak reporting is wired into GCC's __attribute__((destructor)). When the binary exits normally, the library sweeps the table, skips standard streams (0, 1, 2), and reports active descriptors.
Design constraints and comparison & prior art is in the readme
Looking forward to critique.
AI usage declaration: This is not vibe-coded. It evolved over a month starting from using linked lists to tackle a similar problem.
AI was used for cross checking, calibrating, testing, scaffolding and some documentation
•
u/AutoModerator 23d ago
Hi /u/icy-mist-01,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at How do I post my Project?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/didntplaymysummercar 23d ago
What about parsing output of strace? Doing it on your own child requires nothing special iirc.
2
u/pjf_cpp 19d ago
I haven't tried it but the idea sounds nice.
Valgrind has a few nice features. One issue is where you have code that does something like the following sequence
module1: fd1 = open() ... use fd1 ... close(fd1)
module2: fd2 = open() [recycles same fd number as module1 fd]
module1: use fd1 which refers to the wrong file
then there is an option which causes Valgrind to try to avoid recycling fds (which is the POSIX behaviour).
Also I guess that you don't handle the complex sycalls like fcntl and ioctl which can create fds.
1
u/limitless_grow 23d ago
How about adding just something like this at the end of your main function:
That's a trivial way to get a warning about open file descriptors at program termination.
Beside of that may cppcheck help to spot some leaks in code that's rarely executed.