GNOME Bugzilla – Bug 469231
g_spawn optimization for setting all open fds to CLOEXEC
Last modified: 2007-10-16 05:29:17 UTC
g_spawn currently loops through all open and possibly open fds by using sysconf(_SC_OPEN_MAX). That sucks. Because that limit might be immensly high and on normal Linux is already at 1024 by default. The patch I attached changes this (on Linux at least) by iterating through /proc/self/fd/ and closing every fd listed there. This is measurably more efficient in most cases (i.e. when only very few fds are opened which is the case most of the time) If /proc is not accessible the code falls back to using the open fd limit. However I also added an optimization here. Instead of using the system limit of open fds we try to use the process limit RLIMIT_NOFILE. On Linux this is practically no improvement, but it might be on other OSes, and it is cleaner anyway.
Created attachment 94107 [details] [review] The patch
I guess we should do this.
2007-10-16 Matthias Clasen <mclasen@redhat.com> * configure.in: Check for sys/resource.h * glib/gspawn.c: Improve the fdwalk implementation on Linux to only walk over actually open file descriptors. (#469231, Lennart Poettering)