Created
April 2, 2014 09:54
-
-
Save syohex/9931207 to your computer and use it in GitHub Desktop.
Convert file descriptor to file name in Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use File::Spec; | |
my @fhs; | |
opendir my $dh, '/etc'; | |
for my $entry (readdir $dh) { | |
my $file = File::Spec->catfile('/etc', $entry); | |
next unless -f $file; | |
open my $fh, '<', $file or next; | |
push @fhs, $fh; | |
last if @fhs > 20; | |
} | |
show_fd_names(); | |
closedir $dh; | |
sub show_fd_names { | |
opendir my $dh, "/proc/self/fd"; | |
for my $entry (readdir $dh) { | |
my $file = File::Spec->catfile('/proc/self/fd', $entry); | |
next unless -l $file; | |
my $opened_file = readlink $file; | |
print "fd=$entry => $opened_file\n"; | |
} | |
closedir $dh; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment