File Coverage

blib/lib/IPC/PidStat/PidServer.pm
Criterion Covered Total %
statement 18 51 35.2
branch 0 28 0.0
condition 0 8 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 26 97 26.8


line stmt bran cond sub pod time code
1             # See copyright, etc in below POD section.
2             ######################################################################
3              
4             package IPC::PidStat::PidServer;
5             require 5.004;
6             require Exporter;
7             @ISA = qw(Exporter);
8              
9 1     1   421 use IPC::Locker;
  1         2  
  1         32  
10 1     1   4 use Socket;
  1         1  
  1         361  
11 1     1   5 use IO::Socket;
  1         1  
  1         5  
12              
13 1     1   669 use strict;
  1         1  
  1         29  
14 1     1   5 use vars qw($VERSION $Debug $Hostname);
  1         1  
  1         38  
15 1     1   4 use Carp;
  1         1  
  1         429  
16              
17             ######################################################################
18             #### Configuration Section
19              
20             # Other configurable settings.
21             $Debug = 0;
22              
23             $VERSION = '1.502';
24              
25             $Hostname = IPC::Locker::hostfqdn();
26              
27             ######################################################################
28             #### Creator
29              
30             sub new {
31             # Establish the server
32 0 0   0 1   @_ >= 1 or croak 'usage: IPC::PidStat::PidServer->new ({options})';
33 0           my $proto = shift;
34 0   0       my $class = ref($proto) || $proto;
35 0           my $self = {
36             #Documented
37             port=>$IPC::Locker::Default_PidStat_Port,
38             @_,};
39 0           bless $self, $class;
40 0           return $self;
41             }
42              
43             sub start_server {
44 0     0 1   my $self = shift;
45              
46             # Open the socket
47 0 0         print "Listening on $self->{port}\n" if $Debug;
48             my $server = IO::Socket::INET->new( Proto => 'udp',
49             LocalPort => $self->{port},
50 0 0         Reuse => 1)
51             or die "$0: Error, socket: $!";
52              
53 0           while (1) {
54 0           my $in_msg;
55 0 0         next unless $server->recv($in_msg, 8192);
56 0 0         print "Got msg $in_msg\n" if $Debug;
57 0           my ($cmd,@param) = split /\s+/, $in_msg; # We rely on the newline to terminate the split
58             # We ignore unknown parameters for forward compatibility
59             # PIDR (\d+) (\S+) ([0-7]) # PID request, format after 1.480
60             # PIDR (\d+) (\S+) # PID request, format after 1.461
61             # PIDR (\d+) # PID request, format before 1.461
62 0 0         if ($cmd eq 'PIDR') {
63 0           my $pid = $param[0];
64 0   0       my $host = $param[1] || $Hostname; # Loop the host through, as the machine may have multiple names
65 0   0       my $which = $param[2] || 3;
66 0           $! = undef;
67 0           my $exists = IPC::PidStat::local_pid_exists($pid);
68 0 0         if ($exists) {
    0          
69 0 0         if ($which & 1) {
70 0           my $out_msg = "EXIS $pid $exists $host"; # PID response
71 0 0         print " Send msg $out_msg\n" if $Debug;
72 0           $server->send($out_msg); # or die... But we'll ignore errors
73             }
74             } elsif (defined $exists) { # Known not to exist
75 0 0         if ($which & 2) {
76 0           my $out_msg = "EXIS $pid $exists $host"; # PID response
77 0 0         print " Send msg $out_msg\n" if $Debug;
78 0           $server->send($out_msg); # or die... But we'll ignore errors
79             }
80             } else { # Perhaps we're not running as root?
81 0 0         if ($which & 4) {
82 0           my $out_msg = "UNKN $pid na $host"; # PID response
83 0 0         print " Send msg $out_msg\n" if $Debug;
84 0           $server->send($out_msg); # or die... But we'll ignore errors
85             }
86             }
87             }
88             }
89             }
90              
91             ######################################################################
92             #### Package return
93             1;
94             =pod
95              
96             =head1 NAME
97              
98             IPC::PidStat::PidServer - Process ID existence server
99              
100             =head1 SYNOPSIS
101              
102             use IPC::PidStat::PidServer;
103              
104             IPC::PidStat::PidServer->new(port=>1234)->start_server;
105              
106             # Or more typically via the command line
107             pidstatd
108              
109             =head1 DESCRIPTION
110              
111             L responds to UDP requests that contain a PID with
112             a packet indicating the PID and if the PID currently exists.
113              
114             The Perl IPC::Locker package optionally uses this daemon to break locks
115             for PIDs that no longer exists.
116              
117             =over 4
118              
119             =item new ([parameter=>value ...]);
120              
121             Creates a server object.
122              
123             =item start_server ([parameter=>value ...]);
124              
125             Starts the server. Does not return.
126              
127             =back
128              
129             =head1 PARAMETERS
130              
131             =over 4
132              
133             =item port
134              
135             The port number (INET) or name (UNIX) of the lock server. Defaults to
136             'pidstatd' looked up via /etc/services, else 1752.
137              
138             =back
139              
140             =head1 DISTRIBUTION
141              
142             The latest version is available from CPAN and from L.
143              
144             Copyright 2002-2022 by Wilson Snyder. This package is free software; you
145             can redistribute it and/or modify it under the terms of either the GNU
146             Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.
147              
148             =head1 AUTHORS
149              
150             Wilson Snyder
151              
152             =head1 SEE ALSO
153              
154             L, L, L
155              
156             =cut
157              
158             ######################################################################