File Coverage

blib/lib/Proc/ProcessTable/Match/Command.pm
Criterion Covered Total %
statement 8 41 19.5
branch 0 18 0.0
condition 0 9 0.0
subroutine 3 5 60.0
pod 2 2 100.0
total 13 75 17.3


line stmt bran cond sub pod time code
1             package Proc::ProcessTable::Match::Command;
2              
3 1     1   52025 use 5.006;
  1         10  
4 1     1   4 use strict;
  1         2  
  1         25  
5 1     1   5 use warnings;
  1         1  
  1         287  
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.0.0
14              
15             =cut
16              
17             our $VERSION = '0.0.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             sub new{
64 0     0 1   my %args;
65 0 0         if(defined($_[1])){
66 0           %args= %{$_[1]};
  0            
67             };
68              
69             # run some basic checks to make sure we have the minimum stuff required to work
70 0 0         if ( ! defined( $args{commands} ) ){
71 0           die ('No commands key specified in the argument hash');
72             }
73 0 0         if ( ref( \$args{commands} ) eq 'ARRAY' ){
74 0           die ('The commands key is not a array');
75             }
76 0 0         if ( ! defined $args{commands}[0] ){
77 0           die ('Nothing defined in the commands array');
78             }
79              
80             my $self = {
81             commands=>$args{commands},
82 0           };
83 0           bless $self;
84              
85 0           return $self;
86             }
87              
88             =head2 match
89              
90             Checks if a single Proc::ProcessTable::Process object matches the stack.
91              
92             One argument is taken and that is a Proc::ProcessTable::Process object.
93              
94             The returned value is a boolean.
95              
96             if ( $checker->match( $proc ) ){
97             print "The connection matches.\n";
98             }
99              
100             =cut
101              
102             sub match{
103 0     0 1   my $self=$_[0];
104 0           my $object=$_[1];
105              
106 0 0         if ( !defined( $object ) ){
107 0           return 0;
108             }
109              
110 0 0         if ( ref( $object ) ne 'Proc::ProcessTable::Process' ){
111 0           return 0;
112             }
113              
114 0           my $proc_fname;
115             my $proc_cmndline;
116 0           eval{
117 0           $proc_fname=$object->fname;
118             };
119 0           eval{
120 0           $proc_fname=$object->cmndline;
121             };
122              
123             # don't bother proceeding, the object won't match ever
124             # as it does not have a fname
125 0 0 0       if (
126             ( ! defined( $proc_fname ) ) &&
127             ( ! defined( $proc_cmndline) )
128             ){
129 0           return 0;
130             }
131              
132 0           foreach my $command ( @{ $self->{commands} } ){
  0            
133 0 0 0       if (
134             ( defined( $proc_fname ) ) &&
135             ( $proc_fname =~ /$command/ )
136             ){
137 0           return 1;
138             }
139 0 0 0       if (
140             ( defined( $proc_cmndline ) ) &&
141             ( $proc_cmndline =~ /$command/ )
142             ){
143 0           return 1;
144             }
145             }
146              
147 0           return 0;
148             }
149              
150             =head1 AUTHOR
151              
152             Zane C. Bowers-Hadley, C<< >>
153              
154             =head1 BUGS
155              
156             Please report any bugs or feature requests to C, or through
157             the web interface at L. I will be notified, and then you'll
158             automatically be notified of progress on your bug as I make changes.
159              
160              
161              
162              
163             =head1 SUPPORT
164              
165             You can find documentation for this module with the perldoc command.
166              
167             perldoc Proc::ProcessTable::Match
168              
169              
170             You can also look for information at:
171              
172             =over 4
173              
174             =item * RT: CPAN's request tracker (report bugs here)
175              
176             L
177              
178             =item * AnnoCPAN: Annotated CPAN documentation
179              
180             L
181              
182             =item * CPAN Ratings
183              
184             L
185              
186             =item * Search CPAN
187              
188             L
189              
190             =back
191              
192              
193             =head1 ACKNOWLEDGEMENTS
194              
195              
196             =head1 LICENSE AND COPYRIGHT
197              
198             Copyright 2019 Zane C. Bowers-Hadley.
199              
200             This program is free software; you can redistribute it and/or modify it
201             under the terms of the the Artistic License (2.0). You may obtain a
202             copy of the full license at:
203              
204             L
205              
206             Any use, modification, and distribution of the Standard or Modified
207             Versions is governed by this Artistic License. By using, modifying or
208             distributing the Package, you accept this license. Do not use, modify,
209             or distribute the Package, if you do not accept this license.
210              
211             If your Modified Version has been derived from a Modified Version made
212             by someone other than you, you are nevertheless required to ensure that
213             your Modified Version complies with the requirements of this license.
214              
215             This license does not grant you the right to use any trademark, service
216             mark, tradename, or logo of the Copyright Holder.
217              
218             This license includes the non-exclusive, worldwide, free-of-charge
219             patent license to make, have made, use, offer to sell, sell, import and
220             otherwise transfer the Package with respect to any patent claims
221             licensable by the Copyright Holder that are necessarily infringed by the
222             Package. If you institute patent litigation (including a cross-claim or
223             counterclaim) against any party alleging that the Package constitutes
224             direct or contributory patent infringement, then this Artistic License
225             to you shall terminate on the date that such litigation is filed.
226              
227             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
228             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
229             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
230             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
231             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
232             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
233             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
234             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
235              
236              
237             =cut
238              
239             1; # End of Proc::ProcessTable::Match