| 1 | package Proc::Find; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $DATE = '2019-11-23'; # DATE | |||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.051'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 13 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 15 | find_proc | ||||||||||||||||||||||||||||
| 16 | find_any_proc | ||||||||||||||||||||||||||||
| 17 | find_all_proc | ||||||||||||||||||||||||||||
| 18 | proc_exists | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | our $CACHE = 0; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | my $_table_res; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub _table { | ||||||||||||||||||||||||||
|
Subroutine
_table: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 25 | 0 | state $pt = do { | |||||||||||||||||||||||||||
| 26 | 0 | require Proc::ProcessTable; | |||||||||||||||||||||||||||
| 27 | 0 | Proc::ProcessTable->new; | |||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if (!$CACHE || !$_table_res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Proc::Find::CACHE and $_table_res
MC/DC0%0/2
$Proc::Find::CACHE and $_table_res
$Proc::Find::CACHE
$_table_res
Truth table0%0/3
A and B
A= $Proc::Find::CACHEB= $_table_res
| |||||||||||||||||||||||||||||
| 30 | 0 | $_table_res = $pt->table; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | $_table_res; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 114 | ▶ | sub _match { | ||||||||||||||||||||||||||
|
Subroutine
_match: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 36 | 114 | my ($target, $cond, $is_numeric) = @_; | |||||||||||||||||||||||||||
| 37 | 114 | ▶ | if (ref $cond eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | return 0 unless $target =~ $cond; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } elsif (ref $cond eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 40 | 54 | ▶ | return 0 unless grep { $is_numeric ? $target == $_ : $target eq $_ } @$cond; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 52 | ▶ | return 0 unless $is_numeric ? $target == $cond : $target eq $cond; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 32 | 1; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 33 | ▶ | sub find_proc { | ||||||||||||||||||||||||||
|
Subroutine
find_proc: called CC=41 SCAR=39.6
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 33 | my %args = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 33 | my @unknown_args = grep {!/\A( | |||||||||||||||||||||||||||
| 51 | filter| | ||||||||||||||||||||||||||||
| 52 | pid|name|cmndline|exec| | ||||||||||||||||||||||||||||
| 53 | user|uid|euser|euid| | ||||||||||||||||||||||||||||
| 54 | table|detail| | ||||||||||||||||||||||||||||
| 55 | result_max | ||||||||||||||||||||||||||||
| 56 | )\z/x} keys %args; | ||||||||||||||||||||||||||||
| 57 | 33 | ▶ | die "Unknown arguments to find_proc(): ".join(", ", @unknown_args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | if @unknown_args; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 33 | ▶ | my $table = $args{table} // _table(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"table"} // _table()
MC/DC0%0/2
$args{"table"} // _table()
$args{"table"}
_table()
Truth table33%1/3
A // B
A= $args{"table"}B= _table()
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 33 | my ($arg_uid, $arg_euid); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 33 | my @res; | |||||||||||||||||||||||||||
| 65 | 33 | for my $p (@$table) { | |||||||||||||||||||||||||||
| 66 | # create extra fields | ||||||||||||||||||||||||||||
| 67 | 130 | $p->{name} = $p->{cmndline}; | |||||||||||||||||||||||||||
| 68 | 130 | $p->{name} =~ s/\s.*//; | |||||||||||||||||||||||||||
| 69 | 130 | $p->{name} =~ s!.+/!!; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 130 | my $cond = 0; | |||||||||||||||||||||||||||
| 72 | COND: | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 130 | ▶ | if (defined $args{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 8 | local $_ = $p; | |||||||||||||||||||||||||||
| 76 | 8 | ▶ | last COND unless $args{filter}->($p); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 126 | ▶ | if (defined $args{pid}) { last COND unless _match($p->{pid} , $args{pid} , 1) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | 119 | ▶ | if (defined $args{name}) { last COND unless _match($p->{name} , $args{name} ) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | 109 | ▶ | if (defined $args{cmndline}) { last COND unless _match($p->{cmndline}, $args{cmndline}) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | 99 | ▶ | if (defined $args{exec}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 28 | ▶ | my $exec = $p->{exec} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$p->{"exec"} // ""
MC/DC100%1/1
$p->{"exec"} // ""
$p->{"exec"}
Truth table100%2/2
A
A= $p->{"exec"}
| |||||||||||||||||||||||||||||
| 83 | 28 | ▶ | unless ($args{exec} =~ m!/!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 20 | $exec =~ s!.+/!!; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 28 | ▶ | last COND unless _match($exec, $args{exec}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 76 | ▶ | if (defined($args{user}) || defined($args{uid})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"user"} or defined $args{"uid"}
MC/DC100%2/2
defined $args{"user"} or defined $args{"uid"}
defined $args{"user"}
defined $args{"uid"}
Truth table100%3/3
A or B
A= defined $args{"user"}B= defined $args{"uid"}
| |||||||||||||||||||||||||||||
| 89 | 26 | ▶ | my $cond = $args{user} // $args{uid}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{"user"} // $args{"uid"}
MC/DC0%0/2
$args{"user"} // $args{"uid"}
$args{"user"}
$args{"uid"}
Truth table66%2/3
A // B
A= $args{"user"}B= $args{"uid"}
| |||||||||||||||||||||||||||||
| 90 | 26 | ▶ | if ($cond eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | last COND unless _match($p->{uid}, $cond, 1); # XXX allow matching against username? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 26 | my @uids; | |||||||||||||||||||||||||||
| 94 | 26 | ▶ | for my $val (ref $cond eq 'ARRAY' ? @$cond : $cond) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 30 | ▶ | if ($val =~ /\A\d+\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 30 | push @uids, $val; | |||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | my @pw = getpwnam($val); | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | push @uids, @pw ? $pw[2] : -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 26 | ▶ | last COND unless _match($p->{uid}, \@uids, 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 61 | ▶ | if (defined($args{euser}) || defined($args{euid})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"euser"} or defined $args{"euid"}
MC/DC100%2/2
defined $args{"euser"} or defined $args{"euid"}
defined $args{"euser"}
defined $args{"euid"}
Truth table100%3/3
A or B
A= defined $args{"euser"}B= defined $args{"euid"}
| |||||||||||||||||||||||||||||
| 106 | 28 | ▶ | my $cond = $args{euser} // $args{euid}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{"euser"} // $args{"euid"}
MC/DC0%0/2
$args{"euser"} // $args{"euid"}
$args{"euser"}
$args{"euid"}
Truth table66%2/3
A // B
A= $args{"euser"}B= $args{"euid"}
| |||||||||||||||||||||||||||||
| 107 | 28 | ▶ | if ($cond eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | last COND unless _match($p->{euid}, $cond, 1); # XXX allow matching against username? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 28 | my @uids; | |||||||||||||||||||||||||||
| 111 | 28 | ▶ | for my $val (ref $cond eq 'ARRAY' ? @$cond : $cond) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 28 | ▶ | if ($val =~ /\A\d+\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 28 | push @uids, $val; | |||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | 0 | my @pw = getpwnam($val); | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | push @uids, @pw ? $pw[2] : -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 28 | ▶ | last COND unless _match($p->{euid}, \@uids, 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 44 | $cond = 1; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 130 | ▶ | $cond = !$cond if $args{inverse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 130 | ▶ | next unless $cond; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 44 | ▶ | if ($args{detail}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 4 | push @res, { %$p }; # unbless | |||||||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||||||||
| 132 | 40 | push @res, $p->{pid}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 44 | ▶ | if (defined $args{result_max}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | last if @res >= $args{result_max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 33 | \@res; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | sub proc_exists { | ||||||||||||||||||||||||||
|
Subroutine
proc_exists: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | @{ find_proc(@_, result_max=>1) } > 0 ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub find_any_proc { | ||||||||||||||||||||||||||
|
Subroutine
find_any_proc: called CC=8 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 1 | ▶ | return [] unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | my $detail = $_[0]->{detail}; | |||||||||||||||||||||||||||
| 151 | 1 | ▶ | my $table = $_[0]->{table} // _table(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$_[0]{"table"} // _table()
MC/DC0%0/2
$_[0]{"table"} // _table()
$_[0]{"table"}
_table()
Truth table33%1/3
A // B
A= $_[0]{"table"}B= _table()
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | my @allres; | |||||||||||||||||||||||||||
| 154 | 1 | for my $crit (@_) { | |||||||||||||||||||||||||||
| 155 | 2 | my $res = find_proc(%$crit, table=>$table, detail=>$detail); | |||||||||||||||||||||||||||
| 156 | ITEM: | ||||||||||||||||||||||||||||
| 157 | 2 | for my $item (@$res) { | |||||||||||||||||||||||||||
| 158 | # skip duplicate process | ||||||||||||||||||||||||||||
| 159 | 2 | ▶ | if ($detail) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | next ITEM if first {$_->{pid} == $res->{pid}} @allres; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 2 | ▶ | next ITEM if first {$_ == $res} @allres; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 2 | push @allres, @$res; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | \@allres; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | ▶ | sub find_all_proc { | ||||||||||||||||||||||||||
|
Subroutine
find_all_proc: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 2 | ▶ | return [] unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2 | my $detail = $_[0]->{detail}; | |||||||||||||||||||||||||||
| 175 | 2 | ▶ | my $table = $_[0]->{table} // _table(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$_[0]{"table"} // _table()
MC/DC0%0/2
$_[0]{"table"} // _table()
$_[0]{"table"}
_table()
Truth table33%1/3
A // B
A= $_[0]{"table"}B= _table()
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | my @allres; | |||||||||||||||||||||||||||
| 178 | CRIT: | ||||||||||||||||||||||||||||
| 179 | 2 | for my $crit (@_) { | |||||||||||||||||||||||||||
| 180 | 4 | my $res = find_proc(%$crit, table=>$table); | |||||||||||||||||||||||||||
| 181 | 4 | ▶ | if (!@allres) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 2 | push @allres, @$res; | |||||||||||||||||||||||||||
| 183 | 2 | next CRIT; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | @allres = grep { | ||||||||||||||||||||||||||||
| 186 | 2 | my $p = $_; | |||||||||||||||||||||||||||
| 187 | $detail ? | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | ((first {$p->{pid} == $_->{pid}} @$res) ? 1:0) : | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 189 | 4 | ▶ | ((first {$p == $_} @$res) ? 1:0) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.5
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 190 | } @allres; | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 2 | \@allres; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | 1; | |||||||||||||||||||||||||||
| 197 | # ABSTRACT: Find processes by name, PID, or some other attributes | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||