From e1daada73aafb2a802e21dce4c422fb89d88917b Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 26 Apr 2020 15:25:24 -0700 Subject: [PATCH] Add function to get all open files --- proc/proc-lib.pl | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/proc/proc-lib.pl b/proc/proc-lib.pl index 3db9d9229..6f5cb9ea1 100755 --- a/proc/proc-lib.pl +++ b/proc/proc-lib.pl @@ -452,16 +452,30 @@ my ($pid) = @_; return grep { $_->{'pid'} == $pid } &find_all_process_sockets(); } -# find_process_files(pid) +# find_all_process_files() +# Returns all files currently held open by all processes +sub find_all_process_files +{ +return &find_process_files(); +} + +# find_process_files([pid]) # Returns all files currently held open by some process sub find_process_files { +local ($pid) = @_; local @rv; if ($has_lsof_command) { - open(LSOF, "$has_lsof_command -p ".quotemeta($_[0])." |"); + if (defined($pid)) { + open(LSOF, "$has_lsof_command -p ".quotemeta($pid)." |"); + } + else { + open(LSOF, "$has_lsof_command |"); + } while() { if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(\d+)\s+(.*)/) { - push(@rv, { 'fd' => lc($4), + push(@rv, { 'pid' => $2, + 'fd' => lc($4), 'type' => lc($5), 'device' => [ $6, $7 ], 'size' => $8, @@ -469,7 +483,8 @@ if ($has_lsof_command) { 'file' => $10 }); } elsif (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(.*)/) { - push(@rv, { 'fd' => lc($4), + push(@rv, { 'pid' => $2, + 'fd' => lc($4), 'type' => lc($5), 'device' => [ $6, $7 ], 'inode' => $8,