[rsyslog] Closing all file descriptors in a process

Dražen Kačar dave at fly.srk.fer.hr
Fri Dec 10 15:48:44 CET 2010


There is a comment in omprog.c:

   /* we close all file handles as we fork soon
    * Is there a better way to do this? - mail me!  * rgerhards at adiscon.com
    */

   for(i = 3 ; i <= 65535 ; ++i)
	close(i);

The better, not-portable, way is to open /proc/self/fd directory, read all
the entries and close exactly those file descriptors. Configure script should
check if /proc/self/fd exists and has the right contents. If it doesn't
exist, then check for /dev/fd directory.

Perhaps the check needs to be done in run-time, as well. IIRC, FreeBSD
doesn't eneble procfs by default, but it can be mounted by administrators
who wish to do so. I don't know if FreeBSD has /dev/fd.

If the above directory is not available, use getrlimit(RLIMIT_NOFILE) to
get the upper bound for the file descriptor numbers (it will be in the
soft limit field) and close them all in a loop. The limit will usually be
much lower than 65536.

-- 
 .-.   .-.    Yes, I am an agent of Satan, but my duties are largely
(_  \ /  _)   ceremonial.
     |
     |        dave at fly.srk.fer.hr


More information about the rsyslog mailing list