blib/lib/Proc/Find.pm

filter: Statementstmt 87.6% 106 / 121 Branchbran 71.4% 70 / 98 Conditioncond 57.6% 15 / 26 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 78.5% 11 / 14 Podpod 100% 4 / 4 totaltotal 75.3% 211 / 280 CC 65 SCAR 34.4
CC
65
Cov
78%
find_proc
52.5
_table
12.0
find_any_proc
11.0
CRAP 31.1
↑ Summary
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
BranchTrueFalseTotal
unless ($Proc::Find::CACHE and $_table_res) 0 0 0
Condition0%0/3
$Proc::Find::CACHE and $_table_res
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (ref $cond eq 'Regexp') { } 8 106 114
elsif (ref $cond eq 'ARRAY') { } 54 52 106
38 8 ▶ return 0 unless $target =~ $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless $target =~ /$cond/ 6 2 8
39 } elsif (ref $cond eq 'ARRAY') {
40 54 ▶ return 0 unless grep { $is_numeric ? $target == $_ : $target eq $_ } @$cond;
Branch: 2 branches
BranchTrueFalseTotal
unless grep {$is_numeric ? $target == $_ : $target eq $_;} @$cond 32 22 54
$is_numeric ? : 58 0 58
41 } else {
42 52 ▶ return 0 unless $is_numeric ? $target == $cond : $target eq $cond;
Branch: 2 branches
BranchTrueFalseTotal
unless $is_numeric ? $target == $cond : $target eq $cond 44 8 52
$is_numeric ? : 8 44 52
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
BranchTrueFalseTotal
if @unknown_args 0 33 33
58 if @unknown_args;
59  
60 33 ▶ my $table = $args{table} // _table();
Condition33%1/3
$args{"table"} // _table()
l!l&&r!l&&!r
3300
MC/DC0%0/2
$args{"table"} // _table()
$args{"table"} _table()
Truth table33%1/3
A // B
A= $args{"table"}B= _table()
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (defined $args{"filter"}) 8 122 130
75 8 local $_ = $p;
76 8 ▶ last COND unless $args{filter}->($p);
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"filter"}->($p) 4 4 8
77 }
78 126 ▶ if (defined $args{pid}) { last COND unless _match($p->{pid} , $args{pid} , 1) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{"pid"}) 8 118 126
unless _match($p->{"pid"}, $args{"pid"}, 1) 7 1 8
79 119 ▶ if (defined $args{name}) { last COND unless _match($p->{name} , $args{name} ) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{"name"}) 12 107 119
unless _match($p->{"name"}, $args{"name"}) 10 2 12
80 109 ▶ if (defined $args{cmndline}) { last COND unless _match($p->{cmndline}, $args{cmndline}) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{"cmndline"}) 12 97 109
unless _match($p->{"cmndline"}, $args{"cmndline"}) 10 2 12
81 99 ▶ if (defined $args{exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"exec"}) 28 71 99
82 28 ▶ my $exec = $p->{exec} // '';
Condition100%2/2
$p->{"exec"} // ""
l!l
217
MC/DC100%1/1
$p->{"exec"} // ""
$p->{"exec"}
Truth table100%2/2
A
A= $p->{"exec"}
Aresult
11
00
83 28 ▶ unless ($args{exec} =~ m!/!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"exec"} =~ m[/]) 20 8 28
84 20 $exec =~ s!.+/!!;
85 }
86 28 ▶ last COND unless _match($exec, $args{exec});
Branch: 1 branch
BranchTrueFalseTotal
unless _match($exec, $args{"exec"}) 23 5 28
87 }
88 76 ▶ if (defined($args{user}) || defined($args{uid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"user"} or defined $args{"uid"}) 26 50 76
Condition100%3/3
defined $args{"user"} or defined $args{"uid"}
l!l&&r!l&&!r
18850
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"}
ABresult
1X1
011
000
89 26 ▶ my $cond = $args{user} // $args{uid};
Condition66%2/3
$args{"user"} // $args{"uid"}
l!l&&r!l&&!r
1880
MC/DC0%0/2
$args{"user"} // $args{"uid"}
$args{"user"} $args{"uid"}
Truth table66%2/3
A // B
A= $args{"user"}B= $args{"uid"}
ABresult
1X1
011
000
90 26 ▶ if ($cond eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond eq 'Regexp') { } 0 26 26
91 0 ▶ last COND unless _match($p->{uid}, $cond, 1); # XXX allow matching against username?
Branch: 1 branch
BranchTrueFalseTotal
unless _match($p->{"uid"}, $cond, 1) 0 0 0
92 } else {
93 26 my @uids;
94 26 ▶ for my $val (ref $cond eq 'ARRAY' ? @$cond : $cond) {
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'ARRAY' ? : 4 22 26
95 30 ▶ if ($val =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\A\d+\z/) { } 30 0 30
96 30 push @uids, $val;
97 } else {
98 0 my @pw = getpwnam($val);
99 0 ▶ push @uids, @pw ? $pw[2] : -1;
Branch: 1 branch
BranchTrueFalseTotal
@pw ? : 0 0 0
100 }
101 }
102 26 ▶ last COND unless _match($p->{uid}, \@uids, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _match($p->{"uid"}, \@uids, 1) 15 11 26
103 }
104 }
105 61 ▶ if (defined($args{euser}) || defined($args{euid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"euser"} or defined $args{"euid"}) 28 33 61
Condition100%3/3
defined $args{"euser"} or defined $args{"euid"}
l!l&&r!l&&!r
82033
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"}
ABresult
1X1
011
000
106 28 ▶ my $cond = $args{euser} // $args{euid};
Condition66%2/3
$args{"euser"} // $args{"euid"}
l!l&&r!l&&!r
8200
MC/DC0%0/2
$args{"euser"} // $args{"euid"}
$args{"euser"} $args{"euid"}
Truth table66%2/3
A // B
A= $args{"euser"}B= $args{"euid"}
ABresult
1X1
011
000
107 28 ▶ if ($cond eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond eq 'Regexp') { } 0 28 28
108 0 ▶ last COND unless _match($p->{euid}, $cond, 1); # XXX allow matching against username?
Branch: 1 branch
BranchTrueFalseTotal
unless _match($p->{"euid"}, $cond, 1) 0 0 0
109 } else {
110 28 my @uids;
111 28 ▶ for my $val (ref $cond eq 'ARRAY' ? @$cond : $cond) {
Branch: 1 branch
BranchTrueFalseTotal
ref $cond eq 'ARRAY' ? : 0 28 28
112 28 ▶ if ($val =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\A\d+\z/) { } 28 0 28
113 28 push @uids, $val;
114 } else {
115 0 my @pw = getpwnam($val);
116 0 ▶ push @uids, @pw ? $pw[2] : -1;
Branch: 1 branch
BranchTrueFalseTotal
@pw ? : 0 0 0
117 }
118 }
119 28 ▶ last COND unless _match($p->{euid}, \@uids, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _match($p->{"euid"}, \@uids, 1) 17 11 28
120 }
121 }
122  
123 44 $cond = 1;
124 }
125  
126 130 ▶ $cond = !$cond if $args{inverse};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"inverse"} 0 130 130
127 130 ▶ next unless $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless $cond 86 44 130
128  
129 44 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 4 40 44
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
BranchTrueFalseTotal
if (defined $args{"result_max"}) 1 43 44
136 1 ▶ last if @res >= $args{result_max};
Branch: 1 branch
BranchTrueFalseTotal
if @res >= $args{"result_max"} 1 0 1
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
BranchTrueFalseTotal
@{find_proc(@_, 'result_max', 1);} > 0 ? : 1 1 2
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
BranchTrueFalseTotal
unless @_ 0 1 1
149  
150 1 my $detail = $_[0]->{detail};
151 1 ▶ my $table = $_[0]->{table} // _table();
Condition33%1/3
$_[0]{"table"} // _table()
l!l&&r!l&&!r
100
MC/DC0%0/2
$_[0]{"table"} // _table()
$_[0]{"table"} _table()
Truth table33%1/3
A // B
A= $_[0]{"table"}B= _table()
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($detail) { } 0 2 2
160 0 ▶ next ITEM if first {$_->{pid} == $res->{pid}} @allres;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $_->{"pid"} == $res->{"pid"}; } , @allres) 0 0 0
161 } else {
162 2 ▶ next ITEM if first {$_ == $res} @allres;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { $_ == $res; } , @allres) 0 2 2
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
BranchTrueFalseTotal
unless @_ 0 2 2
173  
174 2 my $detail = $_[0]->{detail};
175 2 ▶ my $table = $_[0]->{table} // _table();
Condition33%1/3
$_[0]{"table"} // _table()
l!l&&r!l&&!r
200
MC/DC0%0/2
$_[0]{"table"} // _table()
$_[0]{"table"} _table()
Truth table33%1/3
A // B
A= $_[0]{"table"}B= _table()
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless (@allres) 2 2 4
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
BranchTrueFalseTotal
$detail ? : 0 4 4
&first(sub { $p->{'pid'} == $_->{'pid'}; } , @$res) ? : 0 0 0
&first(sub { $p == $_; } , @$res) ? : 1 3 4
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__
↑ Summary