From 6ce4598244119810565ef866087c88f2bd39e390 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 26 Apr 2020 10:30:43 -0700 Subject: [PATCH 1/3] Don't try to run lsof if we know it's not installed --- proc/proc-lib.pl | 112 +++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/proc/proc-lib.pl b/proc/proc-lib.pl index 925e892ef..c50fb4aa5 100755 --- a/proc/proc-lib.pl +++ b/proc/proc-lib.pl @@ -376,13 +376,15 @@ $has_lsof_command = &has_command("lsof"); sub find_socket_processes { local @rv; -open(LSOF, "lsof -i '$_[0]:$_[1]' |"); -while() { - if (/^(\S+)\s+(\d+)/) { - push(@rv, $2); +if ($has_lsof_command) { + open(LSOF, "$has_lsof_command -i ".quotemeta("$_[0]:$_[1]")." |"); + while() { + if (/^(\S+)\s+(\d+)/) { + push(@rv, $2); + } } + close(LSOF); } -close(LSOF); return @rv; } @@ -391,13 +393,15 @@ return @rv; sub find_ip_processes { local @rv; -open(LSOF, "lsof -i '\@$_[0]' |"); -while() { - if (/^(\S+)\s+(\d+)/) { - push(@rv, $2); +if ($has_lsof_command) { + open(LSOF, "$has_lsof_command -i ".quotemeta("\@$_[0]")." |"); + while() { + if (/^(\S+)\s+(\d+)/) { + push(@rv, $2); + } } + close(LSOF); } -close(LSOF); return @rv; } @@ -406,34 +410,36 @@ return @rv; sub find_process_sockets { local @rv; -open(LSOF, "lsof -i tcp -i udp -n |"); -while() { - if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+).*(TCP|UDP)\s+(.*)/ - && $2 eq $_[0]) { - local $n = { 'fd' => $4, - 'type' => $5, - 'proto' => $7 }; - local $m = $8; - if ($m =~ /^([^:\s]+):([^:\s]+)\s+\(listen\)/i) { - $n->{'lhost'} = $1; - $n->{'lport'} = $2; - $n->{'listen'} = 1; +if ($has_lsof_command) { + open(LSOF, "$has_lsof_command -i tcp -i udp -n |"); + while() { + if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+).*(TCP|UDP)\s+(.*)/ + && $2 eq $_[0]) { + local $n = { 'fd' => $4, + 'type' => $5, + 'proto' => $7 }; + local $m = $8; + if ($m =~ /^([^:\s]+):([^:\s]+)\s+\(listen\)/i) { + $n->{'lhost'} = $1; + $n->{'lport'} = $2; + $n->{'listen'} = 1; + } + elsif ($m =~ /^([^:\s]+):([^:\s]+)->([^:\s]+):([^:\s]+)\s+\((\S+)\)/) { + $n->{'lhost'} = $1; + $n->{'lport'} = $2; + $n->{'rhost'} = $3; + $n->{'rport'} = $4; + $n->{'state'} = $5; + } + elsif ($m =~ /^([^:\s]+):([^:\s]+)/) { + $n->{'lhost'} = $1; + $n->{'lport'} = $2; + } + push(@rv, $n); } - elsif ($m =~ /^([^:\s]+):([^:\s]+)->([^:\s]+):([^:\s]+)\s+\((\S+)\)/) { - $n->{'lhost'} = $1; - $n->{'lport'} = $2; - $n->{'rhost'} = $3; - $n->{'rport'} = $4; - $n->{'state'} = $5; - } - elsif ($m =~ /^([^:\s]+):([^:\s]+)/) { - $n->{'lhost'} = $1; - $n->{'lport'} = $2; - } - push(@rv, $n); } + close(LSOF); } -close(LSOF); return @rv; } @@ -442,25 +448,27 @@ return @rv; sub find_process_files { local @rv; -open(LSOF, "lsof -p '$_[0]' |"); -while() { - if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(\d+)\s+(.*)/) { - push(@rv, { 'fd' => lc($4), - 'type' => lc($5), - 'device' => [ $6, $7 ], - 'size' => $8, - 'inode' => $9, - 'file' => $10 }); - } - elsif (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(.*)/) { - push(@rv, { 'fd' => lc($4), - 'type' => lc($5), - 'device' => [ $6, $7 ], - 'inode' => $8, - 'file' => $9 }); +if ($has_lsof_command) { + open(LSOF, "$has_lsof_command -p ".quotemeta($_[0])." |"); + while() { + if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(\d+)\s+(.*)/) { + push(@rv, { 'fd' => lc($4), + 'type' => lc($5), + 'device' => [ $6, $7 ], + 'size' => $8, + 'inode' => $9, + 'file' => $10 }); + } + elsif (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+)\s+(.*)/) { + push(@rv, { 'fd' => lc($4), + 'type' => lc($5), + 'device' => [ $6, $7 ], + 'inode' => $8, + 'file' => $9 }); + } } + close(LSOF); } -close(LSOF); return @rv; } From eeba63c7ad0b072b81d9032d8a1ea5c0e832bfe4 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 26 Apr 2020 15:14:44 -0700 Subject: [PATCH 2/3] Add new function to get all the open sockets --- proc/proc-lib.pl | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/proc/proc-lib.pl b/proc/proc-lib.pl index c50fb4aa5..3db9d9229 100755 --- a/proc/proc-lib.pl +++ b/proc/proc-lib.pl @@ -405,17 +405,17 @@ if ($has_lsof_command) { return @rv; } -# find_process_sockets(pid) -# Returns all network connections made by some process -sub find_process_sockets +# find_all_process_sockets() +# Returns all network connections made by any process +sub find_all_process_sockets { local @rv; if ($has_lsof_command) { open(LSOF, "$has_lsof_command -i tcp -i udp -n |"); while() { - if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+).*(TCP|UDP)\s+(.*)/ - && $2 eq $_[0]) { - local $n = { 'fd' => $4, + if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+).*(TCP|UDP)\s+(.*)/) { + local $n = { 'pid' => $2, + 'fd' => $4, 'type' => $5, 'proto' => $7 }; local $m = $8; @@ -441,6 +441,15 @@ if ($has_lsof_command) { close(LSOF); } return @rv; + +} + +# find_process_sockets(pid) +# Returns all network connections made by some process +sub find_process_sockets +{ +my ($pid) = @_; +return grep { $_->{'pid'} == $pid } &find_all_process_sockets(); } # find_process_files(pid) From e1daada73aafb2a802e21dce4c422fb89d88917b Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 26 Apr 2020 15:25:24 -0700 Subject: [PATCH 3/3] 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,