2

I have a large program. It very likes to open files (works with them, amount can be giant - more then 10k ). At some point I want to born a subprocess which will live its own life ( out-of-proc web browser ). I don't control who and how creates files in my main program by reason of its size ( > 10GB of code ) and third-party (plug-ins as well) dependencies. I'd want either:

  • O_CLOEXEC / FD_CLOEXEC by default. I suspect there is no such ability. Or
  • a way to enumerate all file (socket,pipe so on) descriptors in order to be able to close them after fork. Operation systems of interest: Mac OS X and Linux.
dev_null
  • 1,907
  • 1
  • 16
  • 25
  • "I don't control who and how creates files in my main program". Can you explain it a bit more clearly ? – Sanketh Apr 12 '14 at 16:51
  • Time to time just crappy code, several domains like network and disk. Big set of libraries, some of them third party. That is if I open a file with help of some library, I don't control how file is opened inside it and therefore can't set FD_CLOEXEC flag. – dev_null Apr 12 '14 at 16:59
  • Let your program maintain a small list of fds you **do** want to keep open, and close all fds, except the ones in this list. – wildplasser Apr 12 '14 at 17:32

2 Answers2

4

File descriptors are all small positive integers, so you can enumerate all of them and close them. Closing one that isn't open will cause an error, but you can ignore that:

#include <sys/resource.h>
struct rlimit lim;
getrlimit(RLIMIT_NOFILE, &lim);
for (int i = 3; i < lim.rlim_cur; i++)
    close(i);   // close all file descriptors other that stdin/stdout/stderr
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

Setting close-on-exec by default is not possible.(Related: how to set close-on-exec by default).

Enumerating all file descriptors within a process, you can look up /proc/PID/fd/ in Linux. In Mac OS X however, I don't have any programatic solution except using lsof(8) utility. lsof(8) is also available on Linux.

EDIT: Or, you can override open(2) call to specify O_CLOSEXEC by default and run your program using LD_PRELOAD on Linux. For Mac, follow What is the exact equivalent to LD_PRELOAD on OSX?.

Community
  • 1
  • 1
Younggun Kim
  • 938
  • 10
  • 26
  • launching external lsof seems a bit wrong... thank you for linux option. – dev_null Apr 12 '14 at 17:12
  • @dev_null How about override open(2) call? – Younggun Kim Apr 12 '14 at 17:26
  • It's possible and I even did similar things before, but not everything is opened via open. So there should be a set of functions and tasks quickly becomes complex. more over on mac I should take care about 2-level namespaces if I want to handle all cases ( http://stackoverflow.com/questions/20387225/how-libgmalloc-work-with-two-level-namespaces ) – dev_null Apr 13 '14 at 10:09