r/C_Programming • u/Materac_YT • Sep 25 '26
POSIX exec
hey guys i am trying to write POSIX compatible program and i need to close the fds before i exec, i cant guarante they all fd's are O_CLOEXEC becouse i use libraries
0
Upvotes
13
u/RealisticDuck1957 Sep 25 '26
A well behaved library needing a setup function should also have a matching shutdown. Unless the library has state shared between processes after a fork() and you only want to disconnect on one side of the fork..
4
u/_d17y Sep 25 '26
Would using LD_PRELOAD to inject a small interpose for
openand in your implementation ofopenyou explicitly addO_CLOEXECbe an option?3
u/EpochVanquisher Sep 25 '26
Rewrite the libraries so they all use O_CLOEXEC :-)
(If your libraries are opening file descriptors and not using O_CLOEXEC, honestly, this is a flaw in the library and should be fixed)
Otherwise, use Linux-specific close_range(), and on Darwin or BSD use… something else, I forget. You can find plenty of examples.
1
u/Materac_YT Sep 26 '26
For some context i am writing a POSIX 2024 compliant file manager with lua and ncurses and ofcourse libc i also use c23
1
u/Classic-Rate-5104 Sep 25 '26
Do you know the close_range() function?
1
u/simonask_ Sep 25 '26
Consider if it would work for you to spawn the processes you need early (before libraries open any files) and let the child processes wait on a work queue.
But yes, as others have said, `O_CLOEXEC` should be the norm for files, and it’s typically a bug when a library doesn’t use it.
1
u/flyingron Sep 25 '26 edited Sep 25 '26