Re: Apache2::SubProcess sucks

Jeff McCarrell <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <C7A59308.C681%[email protected]>
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" <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.