Match/Command.pm

filter: Statementstmt 92.0% 46 / 50 Branchbran 77.2% 17 / 22 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 84.0% 79 / 94 CC 16 SCAR 20.7
CC
16
Cov
85%
match
10.6
new
7.2
BEGIN
1.0
CRAP 8.0
« Match/Swapped.pm ↑ Summary Match/KernProc.pm »
1 package Proc::ProcessTable::Match::Command;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Proc::ProcessTable::Match::Command - Check if the fname or cmndline of a process matches via regexp.
10  
11 =head1 VERSION
12  
13 Version 0.1.0
14  
15 =cut
16  
17 2 our $VERSION = '0.1.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Proc::ProcessTable::Match::Command;
23
24 my %args=(
25 commands=>[
26 'syslogd',
27 'kernel',
28 ],
29 );
30
31 my $checker=Proc::ProcessTable::Match::Command->new( \%args );
32
33 if ( $checker->match( $proc ) ){
34 print "It matches.\n";
35 }
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 This intiates the object.
42  
43 It takes a hash reference with one key. One key is required and
44 that is 'commands', which is a array of commands to match.
45  
46 The matching is done via regexp.
47  
48 Atleast one value must be specified.
49  
50 If the new method fails, it dies.
51  
52 my %args=(
53 commands=>[
54 'syslogd',
55 'kernel',
56 ],
57 );
58
59 my $checker=Proc::ProcessTable::Match::Command->new( \%args );
60  
61 =cut
62  
63 5 ▶sub new{
Subroutine new: called CC=7 SCAR=19.7
Pod documented
64 5 my %args;
65 5 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 5 0 5
66 5 %args= %{$_[1]};
67 };
68  
69 # run some basic checks to make sure we have the minimum stuff required to work
70 5 ▶ if ( ! defined( $args{commands} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"commands"}) 1 4 5
71 1 die ('No commands key specified in the argument hash');
72 }
73 4 ▶ if ( ref( $args{commands} ) ne 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"commands"} ne "ARRAY") 0 4 4
74 0 die ('The commands key is not a array');
75 }
76 4 ▶ if ( ! defined $args{commands}[0] ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"commands"}[0]) 0 4 4
77 0 die ('Nothing defined in the commands array');
78 }
79  
80 # make sure each specified value compiles as a regex
81 4 my $regex_int=0;
82 4 ▶ while ( defined( $args{commands}[$regex_int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"commands"}[$regex_int]) 4 3 7
83 4 ▶ if ( ! eval{ qr/$args{commands}[$regex_int]/ } ){
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { qr/$args{"commands"}[$regex_int]/ } }) 1 3 4
84 1 die (q{The value "}.$args{commands}[$regex_int].q{" in the commands array does not compile as a regex... }.$@);
85 }
86 3 $regex_int++;
87 }
88  
89 3 my $class=$_[0];
90 my $self = {
91 commands=>$args{commands},
92 3 };
93 3 bless $self, $class;
94  
95 3 return $self;
96 }
97  
98 =head2 match
99  
100 Checks if a single Proc::ProcessTable::Process object matches the stack.
101  
102 One argument is taken and that is a Proc::ProcessTable::Process object.
103  
104 The returned value is a boolean.
105  
106 if ( $checker->match( $proc ) ){
107 print "The process matches.\n";
108 }
109  
110 =cut
111  
112 4 ▶sub match{
Subroutine match: called CC=10 SCAR=23.6
Pod documented
113 4 my $self=$_[0];
114 4 my $object=$_[1];
115  
116 4 ▶ if ( !defined( $object ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 0 4 4
117 0 return 0;
118 }
119  
120 4 ▶ if ( ref( $object ) ne 'Proc::ProcessTable::Process' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Proc::ProcessTable::Process") 0 4 4
121 0 return 0;
122 }
123  
124 4 my $proc_fname;
125 my $proc_cmndline;
126 4 eval{
127 4 $proc_fname=$object->fname;
128 };
129 4 eval{
130 4 $proc_cmndline=$object->cmndline;
131 };
132  
133 # don't bother proceeding, the object won't match ever
134 # as it does not have a fname
135 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc_fname or defined $proc_cmndline) 1 3 4
Condition66%2/3
defined $proc_fname or defined $proc_cmndline
l!l&&r!l&&!r
301
MC/DC50%1/2
defined $proc_fname or defined $proc_cmndline
defined $proc_fname defined $proc_cmndline
Truth table66%2/3
A or B
A= defined $proc_fnameB= defined $proc_cmndline
ABresult
1X1
011
000
136 ( ! defined( $proc_fname ) ) &&
137 ( ! defined( $proc_cmndline) )
138 ){
139 1 return 0;
140 }
141  
142 3 foreach my $command ( @{ $self->{commands} } ){
143 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc_fname and $proc_fname =~ /$command/) 1 2 3
Condition66%2/3
defined $proc_fname and $proc_fname =~ /$command/
!ll&&!rl&&r
021
MC/DC50%1/2
defined $proc_fname and $proc_fname =~ /$command/
defined $proc_fname $proc_fname =~ /$command/
Truth table66%2/3
A and B
A= defined $proc_fnameB= $proc_fname =~ /$command/
ABresult
0X0
100
111
144 ( defined( $proc_fname ) ) &&
145 ( $proc_fname =~ /$command/ )
146 ){
147 1 return 1;
148 }
149 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc_cmndline and $proc_cmndline =~ /$command/) 1 1 2
Condition66%2/3
defined $proc_cmndline and $proc_cmndline =~ /$command/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $proc_cmndline and $proc_cmndline =~ /$command/
defined $proc_cmndline $proc_cmndline =~ /$command/
Truth table66%2/3
A and B
A= defined $proc_cmndlineB= $proc_cmndline =~ /$command/
ABresult
0X0
100
111
150 ( defined( $proc_cmndline ) ) &&
151 ( $proc_cmndline =~ /$command/ )
152 ){
153 1 return 1;
154 }
155 }
156  
157 1 return 0;
158 }
159  
160 =head1 AUTHOR
161  
162 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
163  
164 =head1 BUGS
165  
166 Please report any bugs or feature requests to C<bug-Proc-ProcessTable-Match at rt.cpan.org>, or through
167 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-Match>. I will be notified, and then you'll
168 automatically be notified of progress on your bug as I make changes.
169  
170  
171  
172  
173 =head1 SUPPORT
174  
175 You can find documentation for this module with the perldoc command.
176  
177 perldoc Proc::ProcessTable::Match
178  
179  
180 You can also look for information at:
181  
182 =over 4
183  
184 =item * RT: CPAN's request tracker (report bugs here)
185  
186 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-Match>
187  
188 =item * Search CPAN
189  
190 L<https://metacpan.org/release/Proc-ProcessTable-Match>
191  
192 =back
193  
194  
195 =head1 ACKNOWLEDGEMENTS
196  
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 Copyright 2019 Zane C. Bowers-Hadley.
201  
202 This program is free software; you can redistribute it and/or modify it
203 under the terms of the the Artistic License (2.0). You may obtain a
204 copy of the full license at:
205  
206 L<http://www.perlfoundation.org/artistic_license_2_0>
207  
208 Any use, modification, and distribution of the Standard or Modified
209 Versions is governed by this Artistic License. By using, modifying or
210 distributing the Package, you accept this license. Do not use, modify,
211 or distribute the Package, if you do not accept this license.
212  
213 If your Modified Version has been derived from a Modified Version made
214 by someone other than you, you are nevertheless required to ensure that
215 your Modified Version complies with the requirements of this license.
216  
217 This license does not grant you the right to use any trademark, service
218 mark, tradename, or logo of the Copyright Holder.
219  
220 This license includes the non-exclusive, worldwide, free-of-charge
221 patent license to make, have made, use, offer to sell, sell, import and
222 otherwise transfer the Package with respect to any patent claims
223 licensable by the Copyright Holder that are necessarily infringed by the
224 Package. If you institute patent litigation (including a cross-claim or
225 counterclaim) against any party alleging that the Package constitutes
226 direct or contributory patent infringement, then this Artistic License
227 to you shall terminate on the date that such litigation is filed.
228  
229 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
230 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
231 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
232 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
233 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
234 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
235 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
236 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237  
238  
239 =cut
240  
241 2 1; # End of Proc::ProcessTable::Match
« Match/Swapped.pm ↑ Summary Match/KernProc.pm »