| 1 | # See copyright, etc in below POD section. | ||||||||||||||||||||
| 2 | ###################################################################### | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package IPC::PidStat::PidServer; | ||||||||||||||||||||
| 5 | 3 | require 5.004; | |||||||||||||||||||
| 6 | 3 | require Exporter; | |||||||||||||||||||
| 7 | 3 | @ISA = qw(Exporter); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | ▶ | use IPC::Locker; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use IO::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 3 | ▶ | use vars qw($VERSION $Debug $Hostname); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | ###################################################################### | ||||||||||||||||||||
| 18 | #### Configuration Section | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Other configurable settings. | ||||||||||||||||||||
| 21 | 3 | $Debug = 0; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 3 | $VERSION = '1.500'; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 3 | $Hostname = IPC::Locker::hostfqdn(); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | ###################################################################### | ||||||||||||||||||||
| 28 | #### Creator | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub new { | ||||||||||||||||||||
| 31 | # Establish the server | ||||||||||||||||||||
| 32 | 0 | ▶ | @_ >= 1 or croak 'usage: IPC::PidStat::PidServer->new ({options})'; | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 0 | my $proto = shift; | |||||||||||||||||||
| 34 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $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 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
start_server: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # Open the socket | ||||||||||||||||||||
| 47 | 0 | ▶ | print "Listening on $self->{port}\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | my $server = IO::Socket::INET->new( Proto => 'udp', | ||||||||||||||||||||
| 49 | LocalPort => $self->{port}, | ||||||||||||||||||||
| 50 | 0 | ▶ | Reuse => 1) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | or die "$0: Error, socket: $!"; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | while (1) { | |||||||||||||||||||
| 54 | 0 | my $in_msg; | |||||||||||||||||||
| 55 | 0 | ▶ | next unless $server->recv($in_msg, 8192); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 0 | ▶ | print "Got msg $in_msg\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if ($cmd eq 'PIDR') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 0 | my $pid = $param[0]; | |||||||||||||||||||
| 64 | 0 | ▶ | my $host = $param[1] || $Hostname; # Loop the host through, as the machine may have multiple names | ||||||||||||||||||
|
Condition0%0/3
$param[1] || $IPC::PidStat::PidServer::Hostname
MC/DC0%0/2
$param[1] || $IPC::PidStat::PidServer::Hostname
$param[1]
$IPC::PidStat::PidServer::Hostname
Truth table0%0/3
A || B
A= $param[1]B= $IPC::PidStat::PidServer::Hostname
| |||||||||||||||||||||
| 65 | 0 | ▶ | my $which = $param[2] || 3; | ||||||||||||||||||
|
Condition0%0/2
$param[2] || 3
MC/DC0%0/1
$param[2] || 3
$param[2]
Truth table0%0/2
A
A= $param[2]
| |||||||||||||||||||||
| 66 | 0 | $! = undef; | |||||||||||||||||||
| 67 | 0 | my $exists = IPC::PidStat::local_pid_exists($pid); | |||||||||||||||||||
| 68 | 0 | ▶ | if ($exists) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 69 | 0 | ▶ | if ($which & 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | my $out_msg = "EXIS $pid $exists $host"; # PID response | |||||||||||||||||||
| 71 | 0 | ▶ | print " Send msg $out_msg\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | $server->send($out_msg); # or die... But we'll ignore errors | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } elsif (defined $exists) { # Known not to exist | ||||||||||||||||||||
| 75 | 0 | ▶ | if ($which & 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | my $out_msg = "EXIS $pid $exists $host"; # PID response | |||||||||||||||||||
| 77 | 0 | ▶ | print " Send msg $out_msg\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | if ($which & 4) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | my $out_msg = "UNKN $pid na $host"; # PID response | |||||||||||||||||||
| 83 | 0 | ▶ | print " Send msg $out_msg\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | $server->send($out_msg); # or die... But we'll ignore errors | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | ###################################################################### | ||||||||||||||||||||
| 92 | #### Package return | ||||||||||||||||||||
| 93 | 3 | 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<IPC::PidStat::PidServer> 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<https://www.veripool.org/ipc-locker>. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Copyright 2002-2019 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 <wsnyder@wsnyder.org> | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 SEE ALSO | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | L<IPC::Locker>, L<IPC::PidStat>, L<pidstatd> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | ###################################################################### | ||||||||||||||||||||