bschmer has asked for the wisdom of the Perl Monks concerning the following question:
The problem is this, I have a script that starts with some filehandles already open which seem to be inherited via fork and/or exec and these file handles point to files not on the local filesystem which causes some pain when the remote file system goes out to lunch or the local system is rebooted. The annoyance is that I don't use these files in my script at all, so I'd just like to close all files other than STDIN, STDOUT and STDERR. Here's the code that was my attempt:
This doesn't do the jobs since the 2nd opendir set prints out all of the same file descriptor numbers that exist at the beginning.opendir(DH, "/proc/$$/fd"); while (my $item = readdir DH){ next if ($item =~ /\.\.?/); my @sinfo = stat("/proc/$/fd/" . $item); my $mode = $sinfo[2]; printf("Item is $item: %o\n", $mode); my $fh = FileHandle::fdopen($item, O_RDWR) || warn "Could not dup +$item"; print Dumper($fh); close($fh); } closedir(DH); opendir(DH, "/proc/$$/fd"); while (my $item = readdir DH){ next if ($item =~ /\.\.?/); print "Item is $item\n"; } closedir(DH);
I'd be grateful for any suggestions.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Close all open file descriptors
by ysth (Canon) on Jul 28, 2004 at 13:49 UTC | |
by bschmer (Friar) on Jul 28, 2004 at 15:22 UTC | |
|
Re: Close all open file descriptors
by mifflin (Curate) on Jul 28, 2004 at 14:45 UTC | |
by belg4mit (Prior) on Jul 28, 2004 at 14:47 UTC | |
|
Re: Close all open file descriptors
by BrowserUk (Patriarch) on Jul 28, 2004 at 14:55 UTC | |
by Fletch (Bishop) on Jul 28, 2004 at 15:34 UTC | |
by BrowserUk (Patriarch) on Jul 28, 2004 at 15:38 UTC | |
by edan (Curate) on Jul 29, 2004 at 08:19 UTC | |
by ysth (Canon) on Jul 29, 2004 at 08:41 UTC | |
| |
|
Re: Close all open file descriptors
by belg4mit (Prior) on Jul 28, 2004 at 14:24 UTC | |
|
Re: Close all open file descriptors
by zentara (Cardinal) on Jul 28, 2004 at 14:41 UTC |