blib/lib/IPC/PidStat.pm

filter: Statementstmt 87.3% 110 / 126 Branchbran 61.2% 38 / 62 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 20 / 22 Podpod 60.0% 6 / 10 totaltotal 76.0% 175 / 230 SCAR 38.8
CC
34
Cov
77%
local_pid_exists
11.6
recv_stat
9.9
pid_request
9.1
CRAP 48.4
« blib/lib/IPC/PidStat/PidServer.pm ↑ Summary script/pidstat »
1 # See copyright, etc in below POD section.
2 ######################################################################
3  
4 package IPC::PidStat;
5 24 require 5.004;
6  
7 24 ▶use IPC::Locker;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Net::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use vars qw($VERSION $Debug $Stat_Of_Pid_Supported %Local_Hostnames);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 24 our @_Local_Responses;
20  
21 ######################################################################
22 #### Configuration Section
23  
24 # Other configurable settings.
25 24 $Debug = 0;
26  
27 24 $VERSION = '1.500';
28  
29 # True if pid existance can be detected by looking at /proc filesystem
30 24 $Stat_Of_Pid_Supported = -e "/proc/1";
31  
32 24 %Local_Hostnames = ('localhost' => 1,
33 hostname() => 1,
34 hostfqdn() => 1);
35  
36 ######################################################################
37 #### Creator
38  
39 sub new {
40 # Establish the server
41 7 ▶ @_ >= 1 or croak 'usage: IPC::PidStat->new ({options})';
Subroutine new: called CC=3 SCAR=11.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 or croak("usage: IPC::PidStat->new ({options})") 0 7 7
42 7 my $proto = shift;
43 7 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
44 7 my $self = {
45 socket=>undef, # IO::Socket handle of open socket
46 tries=>5,
47 # Documented
48 port=>$IPC::Locker::Default_PidStat_Port,
49 # Internal
50 _host_ips => {}, # Resolved IP address of hosts
51 @_,};
52 7 bless $self, $class;
53 7 return $self;
54 }
55  
56 sub open_socket {
57 13 ▶ my $self = shift;
Subroutine open_socket: called CC=3 SCAR=11.1
Pod undocumented
58 # Open the socket
59 13 ▶ return if $self->{_socket_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_socket_fh"} 9 4 13
60 4 ▶ $self->{_socket_fh} = IO::Socket::INET->new( Proto => 'udp')
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_socket_fh"} = "IO::Socket::INET"->new("Proto", "udp") or die "$0: %Error, socket: $!" 0 4 4
61 or die "$0: %Error, socket: $!";
62 }
63  
64 sub fh {
65 6 ▶ my $self = shift;
Subroutine fh: called CC=1 SCAR=0.0
Pod undocumented
66 # Return socket file handle, for external select loops
67 6 $self->open_socket(); #open if not already
68 6 return $self->{_socket_fh};
69 }
70  
71 sub pid_request {
72 7 ▶ my $self = shift;
Subroutine pid_request: called CC=9 SCAR=22.0
Pod documented
73 7 my %params = (host=>'localhost',
74 pid=>$$,
75 return_exist=>1,
76 return_doesnt=>1,
77 return_unknown=>1,
78 @_);
79  
80 7 $self->open_socket(); #open if not already
81  
82 7 my $res;
83 7 ▶ if ($Local_Hostnames{$params{host}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::PidStat::Local_Hostnames{$params{"host"}}) 5 2 7
84 # No need to go via server, instead check locally
85 5 my $res = $self->_local_response($params{pid}, $params{host});
86 5 ▶ push @_Local_Responses, $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 5 0 5
87 # If unknown (undef response), forward to the server
88 }
89  
90 7 ▶ if (!defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $res) 7 0 7
91 my $reqval = (($params{return_exist}?1:0)
92 | ($params{return_doesnt}?2:0)
93 7 ▶ | ($params{return_unknown}?4:0));
Branch: 3 branches
BranchTrueFalseTotal
$params{'return_exist'} ? : 4 3 7
$params{'return_doesnt'} ? : 7 0 7
$params{'return_unknown'} ? : 4 3 7
94 7 my $out_msg = "PIDR $params{pid} $params{host} $reqval\n";
95  
96 7 my $ipnum = $self->{_host_ips}->{$params{host}};
97 7 ▶ if (!$ipnum) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ipnum) 5 2 7
98 # inet_aton("name") calls gethostbyname(), which chats with the
99 # NS cache socket and NIS server. Too costly in a polling loop.
100 $ipnum = inet_aton($params{host})
101 5 ▶ or die "%Error: Can't find host $params{host}\n";
Branch: 1 branch
BranchTrueFalseTotal
$ipnum = inet_aton($params{"host"}) or die qq[%Error: Can't find host $params{"host"}\n] 3 2 5
102 2 $self->{_host_ips}->{$params{host}} = $ipnum;
103 }
104 4 my $dest = sockaddr_in($self->{port}, $ipnum);
105 4 $self->fh->send($out_msg,0,$dest);
106 }
107 }
108  
109 sub recv_stat {
110 4 ▶ my $self = shift;
Subroutine recv_stat: called CC=8 SCAR=22.9
Pod documented
111  
112 4 my $in_msg;
113 4 ▶ if ($#_Local_Responses >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#IPC::PidStat::_Local_Responses >= 0) { } 2 2 4
114 2 $in_msg = shift @_Local_Responses;
115 2 ▶ print "Got local response $in_msg\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::PidStat::Debug 2 0 2
116 } else {
117 2 ▶ $self->fh->recv($in_msg, 8192)
Branch: 1 branch
BranchTrueFalseTotal
$self->fh->recv($in_msg, 8192) or return undef 0 2 2
118 or return undef;
119 2 ▶ print "Got server response $in_msg\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::PidStat::Debug 2 0 2
120 }
121 4 ▶ if ($in_msg =~ /^EXIS (\d+) (\d+) (\S+)/) { # PID server response
Branch: 2 branches
BranchTrueFalseTotal
if ($in_msg =~ /^EXIS (\d+) (\d+) (\S+)/) { } 4 0 4
elsif ($in_msg =~ /^UNKN (\d+) (\s+) (\S+)/) { } 0 0 0
122 4 my $pid=$1; my $exists = $2; my $hostname = $3;
123 4 ▶ print " Pid $pid Exists on $hostname? $exists\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::PidStat::Debug 4 0 4
124 4 return ($pid, $exists, $hostname);
125 } elsif ($in_msg =~ /^UNKN (\d+) (\s+) (\S+)/) { # PID not determinate
126 0 return undef;
127 }
128 0 return undef;
129 }
130  
131 sub pid_request_recv {
132 4 ▶ my $self = shift;
Subroutine pid_request_recv: called CC=4 SCAR=14.5
Pod documented
133 4 my @params = @_;
134 4 ▶ for (my $try=0; $try<$self->{tries}; $try++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try < $self->{"tries"}) 4 0 4
135 4 $self->pid_request(@params);
136 4 my @recved;
137 4 eval {
138 4 ▶ local $SIG{ALRM} = sub { die "Timeout\n"; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
139 4 alarm(1);
140 4 @recved = $self->recv_stat();
141 4 alarm(0);
142 };
143 4 ▶ alarm(0) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
144 4 ▶ return @recved if defined $recved[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $recved[0] 4 0 4
145 }
146 0 return undef;
147 }
148  
149 ######################################################################
150 #### Status checking
151  
152 sub ping_status {
153 0 ▶ my $self = shift;
Subroutine ping_status: not called CC=2 SCAR=17.9
Pod undocumented
154 my %params = (pid => 1, # Init.
155 host => $self->{host},
156 0 @_,
157 );
158 # Return OK and status message, for nagios like checks
159 0 my $start_time = [gettimeofday()];
160 0 my ($epid, $eexists, $ehostname) = eval {
161 0 return $self->pid_request_recv(%params);
162 };
163 0 my $elapsed = tv_interval ( $start_time, [gettimeofday]);
164  
165 0 ▶ if (!$eexists) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $eexists) { } 0 0 0
166 0 return ({ok=>undef,status=>"No response from pidstatd on $self->{host}:$self->{port}"});
167 } else {
168 0 return ({ok=>1,status=>sprintf("%1.3f second response on $self->{host}:$self->{port}", $elapsed)});
169 }
170 }
171  
172 ######################################################################
173 #### Local messages
174  
175 sub _local_response {
176 5 ▶ my $self = shift;
Subroutine _local_response: called CC=3 SCAR=11.1
177 5 my $pid = shift;
178 5 my $host = shift;
179  
180 5 my $exists = IPC::PidStat::local_pid_exists($pid);
181 5 ▶ if ($exists) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exists) { } 3 2 5
elsif (defined $exists) { } 2 0 2
182 3 return "EXIS $pid $exists $host"; # PID response
183 } elsif (defined $exists) { # Known not to exist
184 2 return "EXIS $pid $exists $host"; # PID response
185 } else { # Perhaps we're not running as root?
186 0 return undef;
187 }
188 }
189  
190 ######################################################################
191 #### Static Accessors
192  
193 24 our $_Hostfqdn;
194 sub hostfqdn {
195 # Return hostname() including domain name
196 48 ▶ $_Hostfqdn = Net::Domain::hostfqdn() if !defined $_Hostfqdn;
Subroutine hostfqdn: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $IPC::PidStat::_Hostfqdn 24 24 48
197 48 return $_Hostfqdn;
198 }
199  
200 ######################################################################
201 #### Utilities
202  
203 sub local_pid_doesnt_exist {
204 4 ▶ my $result = local_pid_exists(@_);
Subroutine local_pid_doesnt_exist: called CC=2 SCAR=7.1
Pod documented
205 # Return 0 if a pid exists, 1 if not, undef (or second argument) if unknown
206 4 ▶ return undef if !defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 4 4
207 4 return !$result;
208 }
209  
210 sub local_pid_exists {
211 10 ▶ my $pid = shift;
Subroutine local_pid_exists: called CC=7 SCAR=24.5
Pod documented
212 # Return 1 if a pid exists, 0 if not, undef (or second argument) if unknown
213 # We can't just call kill, because if there's a different user running the
214 # process, we'll get an error instead of a result.
215 10 $! = undef;
216 10 ▶ my $exists = (kill (0,$pid))?1:0;
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $pid) ? : 8 2 10
217 10 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 2 8 10
218 2 ▶ if ($! == POSIX::ESRCH) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 3) { } 2 0 2
elsif ($! == 1 and $IPC::PidStat::Stat_Of_Pid_Supported) { } 0 0 0
219 2 $exists = 0;
220 ▶ } elsif ($! == POSIX::EPERM # Sigh, different user?
Condition0%0/3
$! == 1 and $IPC::PidStat::Stat_Of_Pid_Supported
!ll&&!rl&&r
000
MC/DC0%0/2
$! == 1 and $IPC::PidStat::Stat_Of_Pid_Supported
$! == 1 $IPC::PidStat::Stat_Of_Pid_Supported
Truth table0%0/3
A and B
A= $! == 1B= $IPC::PidStat::Stat_Of_Pid_Supported
ABresult
0X0
100
111
221 && $Stat_Of_Pid_Supported ) { # This system supports /proc
222 0 ▶ $exists = (-e "/proc/$pid") ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
-e "/proc/$pid" ? : 0 0 0
223 } else {
224 0 $exists = undef; # Unknown reason
225 }
226 }
227 10 return $exists;
228 }
229  
230 ######################################################################
231 #### Package return
232 24 1;
233 =pod
234  
235 =head1 NAME
236  
237 IPC::PidStat - Process ID existence test
238  
239 =head1 SYNOPSIS
240  
241 use IPC::PidStat;
242  
243 my $exister = new IPC::PidStat(
244 port=>1234,
245 );
246 $exister->pid_request(host=>'foo', pid=>$pid)
247 while (1) { # Poll receiving callbacks
248 my ($epid, $eexists, $ehostname) = $exister->recv_stat();
249 print "Pid $epid ",($eexists?'exists':'dead'),"\n" if $ehostname;
250 }
251  
252 =head1 DESCRIPTION
253  
254 L<IPC::PidStat> allows remote requests to be made to the
255 L<pidstatd>, to determine if a PID is running on the daemon's machine.
256  
257 PidStat uses UDP, and as such results are fast but may be unreliable.
258 Furthermore, the pidstatd may not even be running on the remote machine,
259 so responses should never be required before an application program makes
260 progress.
261  
262 =head1 METHODS
263  
264 =over 4
265  
266 =item new ([parameter=>value ...]);
267  
268 Creates a new object for later use. See the PARAMETERS section.
269  
270 =item pid_request (host=>$host, pid=>$pid);
271  
272 Sends a request to the specified host's server to see if the specified PID
273 exists.
274  
275 The optional parameters return_exist=>0, return_doesnt=>0 and
276 return_unknown=>0 improve performance by suppressing return messages if the
277 specified pid exists, doesn't exist, or has unknown state respectively.
278 Pidstatd versions before 1.480 ignore this flag, so the return code from
279 recv_stat should not assume the undesired return types will be suppressed.
280  
281 =item pid_request_recv (host=>$host, pid=>$pid);
282  
283 Calls pid_request and returns the recv_stat reply. If the response fails
284 to return in one second, it is retried up to 5 times, then undef is
285 returned.
286  
287 =item recv_stat()
288  
289 Blocks waiting for any return from the server. Returns undef if none is
290 found, or a 2 element array with the PID and existence flag. Generally
291 this would be called inside a IO::Poll loop.
292  
293 =back
294  
295 =head1 STATIC METHODS
296  
297 =over 4
298  
299 =item local_pid_doesnt_exist(<pid>)
300  
301 Static call, not a method call. Return 0 if a pid exists, 1 if not.
302 Return undef if it can't be determined.
303  
304 =item local_pid_exists(<pid>)
305  
306 Static call, not a method call. Return 1 if a pid exists, 0 if not.
307 Return undef if it can't be determined.
308  
309 =back
310  
311 =head1 PARAMETERS
312  
313 =over 4
314  
315 =item port
316  
317 The port number (INET) of the pidstatd server. Defaults to 'pidstatd'
318 looked up via /etc/services, else 1752.
319  
320 =back
321  
322 =head1 DISTRIBUTION
323  
324 The latest version is available from CPAN and from L<https://www.veripool.org/ipc-locker>.
325  
326 Copyright 2002-2019 by Wilson Snyder. This package is free software; you
327 can redistribute it and/or modify it under the terms of either the GNU
328 Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.
329  
330 =head1 AUTHORS
331  
332 Wilson Snyder <wsnyder@wsnyder.org>
333  
334 =head1 SEE ALSO
335  
336 L<IPC::Locker>, L<pidstat>, L<pidstatd>, L<pidwatch>
337  
338 L<IPC::PidStat::Server>
339  
340 =cut
341 ######################################################################
« blib/lib/IPC/PidStat/PidServer.pm ↑ Summary script/pidstat »