Closing file descriptors before fork
I need to close all file descriptors before forking, and I can't rely on FD_CLOEXEC as there may (will) be libraries I'm using that have opened some descriptors as well as me.
I've done a bit of Googling, and there doesn't seem to be a standard way, other than looping to a really high number and calling close. I'm in a highly threaded environment so can only make system calls (no library functions)
My intention is to call setrlimit at the start of the program, and setting the hard limit to something like 50. That way, after the fork I only have to loop to 50, and I can guarantee closing all the descriptors.
My question is whether there are any issues with this I'm not considering, or any reason why it won't work?