re: close all open file descriptors portably

well, if one is on a POSIX system, _SC_OPEN_MAX gives the max integer.
Then just close them all.
Here's my usual recipe for this:

    # close all open file descriptors
    my $max_fd = POSIX::sysconf(&POSIX::_SC_OPEN_MAX);
    $max_fd = ((! defined $max_fd) || $max_fd < 0) ? 64 : $max_fd;
    while (--$max_fd >= 0) {
        next if ($max_fd == fileno($pid_fh));
        POSIX::close($max_fd);
    }

    # now reopen std{in,out,err} on /dev/null
    open(STDIN,  "+>/dev/null");
    open(STDOUT, "+>&STDIN");
    open(STDERR, "+>&STDIN");

Proc::Daemon does the same thing...

HTH,
-- jeff


On 2/20/10 1:10 PM, "Torsten Förtsch" <torsten.foert...@gmx.net> wrote:

> - is there a portable way to get all open file descriptors of the current
> process? Under Linux one can readdir(/proc/self/fd). On Darwin I once simply 

Reply via email to