blib/lib/IPC/Locker.pm

filter: Statementstmt 41.8% 95 / 227 Branchbran 18.5% 29 / 156 Conditioncond 36.8% 21 / 57 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 56.2% 18 / 32 Podpod 71.4% 10 / 14 totaltotal 33.8% 176 / 520 SCAR 80.2
CC
99
Cov
33%
_request_attempt
640.6
_request
83.5
owner
42.0
CRAP 3052.8
« blib/lib/IPC/Locker/Server.pm ↑ Summary blib/lib/IPC/PidStat/PidServer.pm »
1 # See copyright, etc in below POD section.
2 ######################################################################
3  
4 =head1 NAME
5  
6 IPC::Locker - Distributed lock handler
7  
8 =head1 SYNOPSIS
9  
10 use IPC::Locker;
11  
12 my $lock = IPC::Locker->lock(lock=>'one_per_machine',
13 host=>'example.std.com',
14 port=>223);
15  
16 if ($lock->lock()) { something; }
17 if ($lock->locked()) { something; }
18  
19 $lock->unlock();
20  
21 =head1 DESCRIPTION
22  
23 L<IPC::Locker> will query a remote lockerd server to obtain a lock around a
24 critical section. When the critical section completes, the lock may be
25 returned.
26  
27 This is useful for distributed utilities which run on many machines, and
28 cannot use file locks or other such mechanisms due to NFS or lack of common
29 file systems.
30  
31 Multiple locks may be requested, in which case the first lock to be free
32 will be used. Lock requests are serviced in a first-in-first-out order,
33 and the locker can optionally free locks for any processes that cease to
34 exist.
35  
36 =over 4
37  
38 =item new ([parameter=>value ...]);
39  
40 Create a lock structure.
41  
42 =item lock ([parameter=>value ...]);
43  
44 Try to obtain the lock, return the lock object if successful, else undef.
45  
46 =item locked ()
47  
48 Return true if the lock has been obtained.
49  
50 =item lock_name ()
51  
52 Return the name of the lock.
53  
54 =item unlock ()
55  
56 Remove the given lock. This will be called automatically when the object
57 is destroyed.
58  
59 =item ping ()
60  
61 A simplified version of ping_status; polls the server to see if it is up.
62 Returns true if up, otherwise undef.
63  
64 =item ping_status ()
65  
66 Polls the server to see if it is up. Returns hash reference with {ok}
67 indicating if up, and {status} with status information. If called without
68 an object, defaults to call new() with connect_tries=>1, under the
69 assumption that a quick go/nogo response is desired.
70  
71 =item break_lock ()
72  
73 Remove current locker for the given lock.
74  
75 =item owner ([parameter=>value ...]);
76  
77 Returns a string of who has the lock or undef if not currently locked.
78 Note that this information is not atomic, and may change asynchronously; do
79 not use this to tell if the lock will be available, to do that, try to
80 obtain the lock and then release it if you got it.
81  
82 =back
83  
84 =head1 PARAMETERS
85  
86 =over 4
87  
88 =item block
89  
90 Boolean flag, true indicates wait for the lock when calling lock() and die
91 if an error occurs. False indicates to just return false. Defaults to
92 true.
93  
94 =item connect_tries
95  
96 If none of the lockerd hosts are available or other network errors are
97 encountered, perform this number of retries, with a random connect_delay to
98 connect_delay*2 interval between them before signalling an error.
99  
100 =item connect_delay
101  
102 The minimum seconds to wait between each of the connect_tries, and
103 one-half of the maximum random wait. Defaults to 30 seconds.
104  
105 =item destroy_unlock
106  
107 Boolean flag, true indicates destruction of the lock variable should unlock
108 the lock, only if the current process id matches the pid passed to the
109 constructor. Set to false if destruction should not close the lock, such
110 as when other children destroying the lock variable should not unlock the
111 lock.
112  
113 =item family
114  
115 The family of transport to use, either INET or UNIX. Defaults to INET.
116  
117 =item host
118  
119 The name of the host containing the lock server. It may also be an array
120 of hostnames, where if the first one is down, subsequent ones will be
121 tried. Defaults to value of IPCLOCKER_HOST or localhost.
122  
123 =item port
124  
125 The port number (INET) or name (UNIX) of the lock server. Defaults to
126 IPCLOCKER_PORT environment variable, else 'lockerd' looked up via
127 /etc/services, else 1751.
128  
129 =item lock
130  
131 The name of the lock. This may also be a reference to an array of lock names,
132 and the first free lock will be returned.
133  
134 =item lock_list
135  
136 Return a list of lock and lock owner pairs. (You can assign this to a hash
137 for easier parsing.)
138  
139 =item pid
140  
141 The process ID that owns the lock, defaults to the current process id.
142  
143 =item print_broke
144  
145 A function to print a message when the lock is broken. The only argument
146 is self. Defaults to print a message if verbose is set.
147  
148 =item print_down
149  
150 A function to print a message when the lock server is unavailable. The
151 first argument is self. Defaults to a croak message.
152  
153 =item print_obtained
154  
155 A function to print a message when the lock is obtained after a delay. The
156 only argument is self. Defaults to print a message if verbose is set.
157  
158 =item print_retry
159  
160 A function to print a message when the lock server is unavailable, and is
161 about to be retried. The first argument is self. Defaults to a print
162 message.
163  
164 =item print_waiting
165  
166 A function to print a message when the lock is busy and needs to be waited
167 for. The first argument is self, second the name of the lock. Defaults to
168 print a message if verbose is set.
169  
170 =item timeout
171  
172 The maximum time in seconds that the lock may be held before being forced
173 open, passed to the server when the lock is created. Thus if the requester
174 dies, the lock will be released after that amount of time. Zero disables
175 the timeout. Defaults to 30 minutes.
176  
177 =item user
178  
179 Name to request the lock under, defaults to host_pid_user
180  
181 =item autounlock
182  
183 True to cause the server to automatically timeout a lock if the locking
184 process has died. For the process to be detected, it must be on the same
185 host as either the locker client (the host making the lock call), or the
186 locker server. Defaults false.
187  
188 =item verbose
189  
190 True to print messages when waiting for locks. Defaults false.
191  
192 =back
193  
194 =head1 ENVIRONMENT
195  
196 =over 4
197  
198 =item IPCLOCKER_HOST
199  
200 Hostname of L<lockerd> server, or colon separated list including backup
201 servers. Defaults to localhost.
202  
203 =item IPCLOCKER_PORT
204  
205 The port number (INET) or name (UNIX) of the lock server. Defaults to
206 'lockerd' looked up via /etc/services, else 1751.
207  
208 =back
209  
210 =head1 DISTRIBUTION
211  
212 The latest version is available from CPAN and from L<https://www.veripool.org/ipc-locker>.
213  
214 Copyright 1999-2019 by Wilson Snyder. This package is free software; you
215 can redistribute it and/or modify it under the terms of either the GNU
216 Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.
217  
218 =head1 AUTHORS
219  
220 Wilson Snyder <wsnyder@wsnyder.org>
221  
222 =head1 SEE ALSO
223  
224 L<lockerd>, L<IPC::Locker::Server>
225  
226 L<IPC::PidStat>, L<pidstat>, L<pidstatd>, L<pidwatch>
227  
228 =cut
229  
230 ######################################################################
231  
232 package IPC::Locker;
233 24 require 5.004;
234 24 require Exporter;
235 24 @ISA = qw(Exporter);
236  
237 24 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 24 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
239 24 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 24 ▶use IPC::PidStat;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 24 ▶use vars qw($VERSION $Debug $Default_Port $Default_Family $Default_UNIX_port $Default_PidStat_Port);
Subroutine BEGIN: called CC=1 SCAR=0.0
244 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 ######################################################################
247 #### Configuration Section
248  
249 # Other configurable settings.
250 24 $Debug = 0;
251  
252 24 $VERSION = '1.500';
253  
254 ######################################################################
255 #### Useful Globals
256  
257 24 ▶$Default_Port = ($ENV{IPCLOCKER_PORT}||'lockerd'); # Number (1751) or name to lookup in /etc/services
Condition50%1/2
$ENV{'IPCLOCKER_PORT'} || 'lockerd'
l!l
024
MC/DC0%0/1
$ENV{'IPCLOCKER_PORT'} || 'lockerd'
$ENV{'IPCLOCKER_PORT'}
Truth table50%1/2
A
A= $ENV{'IPCLOCKER_PORT'}
Aresult
11
00
258 24 ▶$Default_Port = 1751 if ($Default_Port !~ /^\d+$/ && !getservbyname ($Default_Port,""));
Branch: 1 branch
BranchTrueFalseTotal
unless $IPC::Locker::Default_Port =~ /^\d+$/ or getservbyname $IPC::Locker::Default_Port, "" 24 0 24
Condition33%1/3
$IPC::Locker::Default_Port =~ /^\d+$/ or getservbyname $IPC::Locker::Default_Port, ""
l!l&&r!l&&!r
0024
MC/DC0%0/2
$IPC::Locker::Default_Port =~ /^\d+$/ or getservbyname $IPC::Locker::Default_Port, ""
$IPC::Locker::Default_Port =~ /^\d+$/ getservbyname $IPC::Locker::Default_Port, ""
Truth table33%1/3
A or B
A= $IPC::Locker::Default_Port =~ /^\d+$/B= getservbyname $IPC::Locker::Default_Port, ""
ABresult
1X1
011
000
259 24 $Default_PidStat_Port = 'pidstatd'; # Number (1752) or name to lookup in /etc/services
260 24 ▶$Default_PidStat_Port = 1752 if !getservbyname ($Default_PidStat_Port,"");
Branch: 1 branch
BranchTrueFalseTotal
unless getservbyname $IPC::Locker::Default_PidStat_Port, "" 24 0 24
261 24 $Default_Family = 'INET';
262 24 $Default_UNIX_port = '/var/locks/lockerd';
263  
264 ######################################################################
265 #### Creator
266  
267 sub new {
268 3 ▶ @_ >= 1 or croak 'usage: IPC::Locker->new ({options})';
Subroutine new: called CC=9 SCAR=31.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 or croak("usage: IPC::Locker->new ({options})") 0 3 3
269 3 my $proto = shift;
270 3 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
271 3 my $hostname = hostfqdn();
272 my $self = {
273 #Documented
274 host=>($ENV{IPCLOCKER_HOST}||'localhost'),
275 port=>$Default_Port,
276 lock=>['lock'],
277 timeout=>60*10, block=>1,
278 pid=>$$,
279 #user=> # below
280 hostname=>$hostname,
281 autounlock=>0,
282 destroy_unlock=>1,
283 verbose=>$Debug,
284 connect_tries=>3,
285 connect_delay=>30,
286 0 ▶ print_broke=>sub {my $self=shift; print "Broke lock from $_[0] at ".(scalar(localtime))."\n" if $self->{verbose};},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
287 0 ▶ print_obtained=>sub {my $self=shift; print "Obtained lock at ".(scalar(localtime))."\n" if $self->{verbose};},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
288 0 ▶ print_waiting=>sub {my $self=shift; print "Waiting for lock from $_[0] at ".(scalar(localtime))."\n" if $self->{verbose};},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
289 0 ▶ print_retry=>sub {my ($self,$sleep)=@_; print "Unable to connect to server, retrying connection in ${sleep} sec at ".(scalar(localtime))."\n" if $self->{verbose};},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
290 3 ▶ print_down=>undef,
Condition50%1/2
$ENV{'IPCLOCKER_HOST'} || 'localhost'
l!l
03
MC/DC0%0/1
$ENV{'IPCLOCKER_HOST'} || 'localhost'
$ENV{'IPCLOCKER_HOST'}
Truth table50%1/2
A
A= $ENV{'IPCLOCKER_HOST'}
Aresult
11
00
291 family=>$Default_Family,
292 #Internal
293 locked=>0,
294 @_,};
295 3 ▶ $self->{user} ||= hostfqdn() . "_".$self->{pid}."_" . ($ENV{USER} || "");
Condition50%2/4
$self->{"user"} ||= hostfqdn() . '_' . $self->{"pid"} . '_' . ($ENV{"USER"} || "")
l!l
03
$ENV{'USER'} || ''
l!l
03
MC/DC0%0/1
$self->{"user"} ||= hostfqdn() . '_' . $self->{"pid"} . '_' . ($ENV{"USER"} || "")
$self->{"user"}
MC/DC0%0/1
$ENV{'USER'} || ''
$ENV{'USER'}
Truth table50%1/2
A
A= $self->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'USER'}
Aresult
11
00
296 3 foreach (_array_or_one($self->{lock})) {
297 3 ▶ ($_ !~ /\s/) or carp "%Error: Lock names cannot contain whitespace: $_\n";
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\s/ and carp("%Error: Lock names cannot contain whitespace: $_\n") 0 3 3
298 }
299 3 bless $self, $class;
300 3 ▶ _timelog("Locker->new ",$self->lock_name_list,"\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 3 3
301 3 return $self;
302 }
303  
304 ######################################################################
305 #### Static Accessors
306  
307 sub hostfqdn {
308 24 ▶ return IPC::PidStat::hostfqdn();
Subroutine hostfqdn: called CC=1 SCAR=0.0
Pod undocumented
309 }
310  
311 ######################################################################
312 #### Accessors
313  
314 sub locked () {
315 3 ▶ my $self = shift; ($self && ref($self)) or croak 'usage: $self->locked()';
Subroutine locked: called CC=4 SCAR=15.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self and ref $self or croak("usage: \$self->locked()") 0 3 3
Condition33%1/3
$self and ref $self
!ll&&!rl&&r
003
MC/DC0%0/2
$self and ref $self
$self ref $self
Truth table33%1/3
A and B
A= $selfB= ref $self
ABresult
0X0
100
111
316 3 ▶ return $self if $self->{locked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"locked"} 0 3 3
317 3 return undef;
318 }
319  
320 sub ping {
321 0 ▶ my $self = shift;
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
322 0 my $res = $self->ping_status(@_);
323 0 ▶ if ($res->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'ok'}) { } 0 0 0
324 0 return $self;
325 } else {
326 0 return undef;
327 }
328 }
329  
330 sub ping_status {
331 0 ▶ my $self = shift;
Subroutine ping_status: not called CC=3 SCAR=24.8
Pod documented
332 # Return OK and status message, for nagios like checks
333 0 ▶ $self = $self->new(connect_tries=>1, @_) if (!ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
334 0 my $ok = 0;
335 0 my $start_time = [gettimeofday()];
336 0 eval {
337 0 $self->_request("");
338 0 $ok = 1;
339 };
340 0 my $elapsed = tv_interval ( $start_time, [gettimeofday]);
341  
342 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) { } 0 0 0
343 0 return ({ok=>undef,status=>"No response from lockerd on $self->{host}:$self->{port}"});
344 } else {
345 0 return ({ok=>1,status=>sprintf("%1.3f second response on $self->{host}:$self->{port}", $elapsed)});
346 }
347 }
348  
349 ######################################################################
350 #### Constructor
351  
352 sub lock {
353 2 ▶ my $self = shift;
Subroutine lock: called CC=5 SCAR=23.4
Pod documented
354 2 ▶ $self = $self->new(@_) if (!ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2 2
355 2 ▶ if (!$self->locked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->locked) 2 0 2
356 2 $self->_request("LOCK");
357 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
358 }
359 0 ▶ return ($self) if $self->{locked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"locked"} 0 0 0
360 0 return undef;
361 }
362  
363 ######################################################################
364 #### Destructor/Unlock
365  
366 sub DESTROY () {
367 3 ▶ my $self = shift; ($self && ref($self)) or croak 'usage: $self->DESTROY()';
Subroutine DESTROY: called CC=6 SCAR=19.3
Branch: 1 branch
BranchTrueFalseTotal
$self and ref $self or croak("usage: \$self->DESTROY()") 0 3 3
Condition33%1/3
$self and ref $self
!ll&&!rl&&r
003
MC/DC0%0/2
$self and ref $self
$self ref $self
Truth table33%1/3
A and B
A= $selfB= ref $self
ABresult
0X0
100
111
368 3 ▶ if ($self->{destroy_unlock} && $self->{pid} && $self->{pid}==$$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"destroy_unlock"} and $self->{"pid"} and $self->{"pid"} == $$) 1 2 3
Condition66%4/6
$self->{"destroy_unlock"} and $self->{"pid"} and $self->{"pid"} == $$
!ll&&!rl&&r
201
$self->{"destroy_unlock"} and $self->{"pid"}
!ll&&!rl&&r
201
MC/DC33%1/3
$self->{"destroy_unlock"} and $self->{"pid"} and $self->{"pid"} == $$
$self->{"destroy_unlock"} $self->{"pid"} $self->{"pid"} == $$
Truth table50%2/4
A and B and C
A= $self->{"destroy_unlock"}B= $self->{"pid"}C= $self->{"pid"} == $$
ABCresult
0XX0
10X0
1100
1111
369 1 $self->unlock();
370 }
371 }
372  
373 sub unlock {
374 1 ▶ my $self = shift; ($self && ref($self)) or croak 'usage: $self->unlock()';
Subroutine unlock: called CC=5 SCAR=21.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self and ref $self or croak("usage: \$self->unlock()") 0 1 1
Condition33%1/3
$self and ref $self
!ll&&!rl&&r
001
MC/DC0%0/2
$self and ref $self
$self ref $self
Truth table33%1/3
A and B
A= $selfB= ref $self
ABresult
0X0
100
111
375 1 ▶ if ($self->locked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->locked) 0 1 1
376 0 $self->_request("UNLOCK");
377 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
378 }
379 1 return ($self);
380 }
381  
382 sub break_lock {
383 0 ▶ my $self = shift; ($self) or croak 'usage: $self->break_lock()';
Subroutine break_lock: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self or croak("usage: \$self->break_lock()") 0 0 0
384 0 ▶ $self = $self->new(@_) if (!ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
385 0 $self->_request("BREAK_LOCK");
386 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
387 0 return ($self);
388 }
389  
390 sub dead_pid {
391 0 ▶ my $self = shift; (ref $self) or croak 'usage: $self->dead_pid()';
Subroutine dead_pid: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("usage: \$self->dead_pid()") 0 0 0
392 0 my %args = (host => hostfqdn(),
393 pid => -1,
394 @_);
395 # Used internally to indicate a pid is gone.
396 0 $self->_request("DEAD_PID $args{host} $args{pid}");
397 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
398 0 return ($self);
399 }
400  
401 ######################################################################
402 #### User utilities: owner
403  
404 sub owner {
405 0 ▶ my $self = shift; ($self) or croak 'usage: $self->status()';
Subroutine owner: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self or croak("usage: \$self->status()") 0 0 0
406 0 ▶ $self = $self->new(@_) if (!ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
407 0 $self->_request ("STATUS");
408 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
409 0 ▶ _timelog("Locker->owner = ",($self->{owner}||''),"\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
Condition0%0/2
$self->{'owner'} || ''
l!l
00
MC/DC0%0/1
$self->{'owner'} || ''
$self->{'owner'}
Truth table0%0/2
A
A= $self->{'owner'}
Aresult
11
00
410 0 return $self->{owner};
411 }
412  
413 sub lock_name {
414 0 ▶ my $self = shift; ($self) or croak 'usage: $self->lock_name()';
Subroutine lock_name: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self or croak("usage: \$self->lock_name()") 0 0 0
415 0 ▶ if (ref $self->{lock}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'lock'} and $#{$$self{'lock'};} < 1) { } 0 0 0
Condition0%0/3
ref $self->{"lock"} and $#{$$self{"lock"};} < 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"lock"} and $#{$$self{"lock"};} < 1
ref $self->{"lock"} $#{$$self{"lock"};} < 1
Truth table0%0/3
A and B
A= ref $self->{"lock"}B= $#{$$self{"lock"};} < 1
ABresult
0X0
100
111
416 0 && $#{$self->{lock}}<1) {
417 0 return $self->{lock}[0];
418 } else {
419 0 return $self->{lock};
420 }
421 }
422  
423 sub lock_list {
424 1 ▶ my $self = shift;
Subroutine lock_list: called CC=3 SCAR=16.0
Pod documented
425 1 ▶ $self = $self->new(@_) if (!ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
426 1 $self->_request("LOCK_LIST");
427 0 ▶ croak $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
428 0 return @{$self->{lock_list}};
429 }
430  
431 ######################################################################
432 ######################################################################
433 #### Guts: Sending and receiving messages
434  
435 sub _request {
436 3 ▶ my $self = shift;
Subroutine _request: called CC=15 SCAR=44.2
437 3 my $cmd = shift;
438  
439 3 my @hostlist = ('localhost');
440 3 ▶ if ($self->{family} eq 'INET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"family"} eq "INET") 3 0 3
441 3 @hostlist = ($self->{host});
442 3 ▶ @hostlist = split (':', $self->{host}) if (!ref($self->{host}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"host"} 3 0 3
443 3 ▶ @hostlist = @{$self->{host}} if (ref($self->{host}) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"host"} eq "ARRAY" 0 3 3
444 }
445  
446 3 my $ok;
447 try:
448 3 ▶ for (my $tries = 0; $tries < ($self->{connect_tries}||1); $tries++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries < ($self->{"connect_tries"} || 1)) 3 0 3
Condition50%1/2
$self->{'connect_tries'} || 1
l!l
30
MC/DC0%0/1
$self->{'connect_tries'} || 1
$self->{'connect_tries'}
Truth table50%1/2
A
A= $self->{'connect_tries'}
Aresult
11
00
449 3 ▶ if ($tries > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > 0) 0 3 3
450 0 my $sleep = $self->{connect_delay} + int(rand($self->{connect_delay}));
451 0 ▶ _timelog("Locker->connect_delay $sleep sec\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
452 0 &{$self->{print_retry}} ($self, $sleep);
453 0 sleep($sleep);
454 }
455 3 foreach my $host (@hostlist) {
456 3 $ok = $self->_request_attempt($cmd,$host);
457 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
458 0 ▶ if ($host ne $hostlist[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host ne $hostlist[0]) 0 0 0
459 # Reorganize host list so whoever responded is first
460 # This is so if we grab a lock we'll try to return it to the same host
461 0 $self->{host} = [$host, grep( ($_ ne $host), @hostlist)];
462 }
463 0 last try;
464 }
465 }
466 }
467  
468 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
469 0 ▶ if (defined $self->{print_down}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"print_down"}) 0 0 0
470 0 &{$self->{print_down}} ($self);
471 0 return;
472 }
473 croak "%Error: Can't locate lock server on "
474 0 ▶ . (($self->{family} eq 'INET') ? (join " or ", @hostlist) : "UNIX port")
Branch: 1 branch
BranchTrueFalseTotal
$self->{'family'} eq 'INET' ? : 0 0 0
475 ." $self->{port}\n"
476 . "\tYou probably need to run lockerd\n$self->_request(): Stopped";
477 }
478  
479 0 ▶ _timelog("Locker->DONE\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
480 }
481  
482 sub _request_attempt {
483 3 ▶ my $self = shift;
Subroutine _request_attempt: called CC=35 SCAR=64.6
484 3 my $cmd = shift;
485 3 my $host = shift;
486 # Return true if request was successful
487  
488 # IO::Socket::INET nastily undef's $@. Since this may get called
489 # in a destructor due to an error, that looses the error message.
490 # Workaround: save the error and restore at the end.
491 3 my $preerror = $@;
492  
493 retry:
494  
495 # If adding new features, only send the new feature to the server
496 # if the feature is on. This allows for newer clients that don't
497 # need to the new feature to still talk to older servers.
498 my $req = ("user $self->{user}\n"
499 3 ."locks ".join(' ',@{_array_or_one($self->{lock})})."\n");
500 $req.= ("block ".($self->{block}||0)."\n"
501 3 ▶ ."timeout ".($self->{timeout}||0)."\n") if $cmd ne 'UNLOCK';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd ne "UNLOCK" 3 0 3
Condition75%3/4
$self->{'block'} || 0
l!l
30
$self->{'timeout'} || 0
l!l
12
MC/DC0%0/1
$self->{'block'} || 0
$self->{'block'}
MC/DC100%1/1
$self->{'timeout'} || 0
$self->{'timeout'}
Truth table50%1/2
A
A= $self->{'block'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'timeout'}
Aresult
11
00
502 $req.= ("autounlock ".($self->{autounlock}||0)."\n"
503 ."pid ".($self->{pid}||$$)."\n"
504 ."hostname ".($self->{hostname})."\n"
505 3 ▶ ) if $self->{autounlock} && $cmd ne 'UNLOCK';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autounlock"} and $cmd ne "UNLOCK" 2 1 3
Condition50%4/8
$self->{"autounlock"} and $cmd ne "UNLOCK"
!ll&&!rl&&r
102
$self->{'autounlock'} || 0
l!l
20
$self->{'pid'} || $$
l!l&&r!l&&!r
200
MC/DC50%1/2
$self->{"autounlock"} and $cmd ne "UNLOCK"
$self->{"autounlock"} $cmd ne "UNLOCK"
MC/DC0%0/1
$self->{'autounlock'} || 0
$self->{'autounlock'}
MC/DC0%0/2
$self->{'pid'} || $$
$self->{'pid'} $$
Truth table66%2/3
A and B
A= $self->{"autounlock"}B= $cmd ne "UNLOCK"
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->{'autounlock'}
Aresult
11
00
Truth table33%1/3
A || B
A= $self->{'pid'}B= $$
ABresult
1X1
011
000
506 3 $req.= ("$cmd\n"
507 ."\n" # End of group. Some day we may not always send EOF immediately
508 ."EOF\n");
509 3 ▶ _timelog("Locker->REQ\nR ",join("\nR ",split(/\n/,$req)),"\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 3 3
510  
511 3 my $fh;
512 3 ▶ if ($self->{family} eq 'INET') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'family'} eq 'INET') { } 3 0 3
elsif ($self->{'family'} eq 'UNIX') { } 0 0 0
513 3 ▶ _timelog("Locker->Trying host $host $self->{port}\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 3 3
514 $fh = IO::Socket::INET->new( Proto => _tcp_proto(),
515 PeerAddr => $host,
516 3 PeerPort => $self->{port}, );
517 } elsif ($self->{family} eq 'UNIX') {
518 0 ▶ _timelog("Locker->Trying UNIX socket\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
519 0 $fh = IO::Socket::UNIX->new( Peer => $self->{port}, );
520 } else {
521 0 croak "IPC::Locker->_request(): No or wrong transport specified.";
522 }
523  
524 0 ▶ return undef if !$fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
525  
526 0 $self->{lock_list} = [];
527  
528 0 print $fh "$req\n";
529 0 ▶ while (defined (my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
530 0 chomp $line;
531 0 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
532 0 my @args = split /\s+/, $line;
533 0 my $cmd = shift @args;
534 0 ▶ _timelog("RESP $line\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
535 0 ▶ $self->{locked} = $args[0] if ($cmd eq "locked");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "locked" 0 0 0
536 0 ▶ $self->{owner} = $args[0] if ($cmd eq "owner");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "owner" 0 0 0
537 0 ▶ $self->{error} = $args[0] if ($cmd eq "error");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "error" 0 0 0
538 0 ▶ if ($cmd eq "lockname") { # LOCK request's reply
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "lockname") 0 0 0
539 0 $self->{lock} = [$args[0]];
540 0 ▶ $self->{lock} = $self->{lock}[0] if ($#{$self->{lock}}<1); # Back compatible
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self{"lock"};} < 1 0 0 0
541 }
542 0 ▶ if ($cmd eq 'lock' && @args == 2) { # LOCK_LIST request's reply
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "lock" and @args == 2) 0 0 0
Condition0%0/3
$cmd eq "lock" and @args == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$cmd eq "lock" and @args == 2
$cmd eq "lock" @args == 2
Truth table0%0/3
A and B
A= $cmd eq "lock"B= @args == 2
ABresult
0X0
100
111
543 0 push @{$self->{lock_list}}, @args;
544 }
545 0 ▶ if ($cmd eq "autounlock_check") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "autounlock_check") 0 0 0
546 # See if we can break the lock because the lock holder ran on this same machine.
547 0 my ($lname,$lhost,$lpid,$supports_dead) = @args;
548 0 ▶ if ($self->{hostname} eq $lhost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hostname"} eq $lhost) 0 0 0
549 0 ▶ if (IPC::PidStat::local_pid_doesnt_exist($lpid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IPC::PidStat::local_pid_doesnt_exist($lpid)) 0 0 0
550 0 ▶ _timelog("Autounlock_LOCAL $lname $lhost $lpid $supports_dead\n") if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $IPC::Locker::Debug 0 0 0
551 0 ▶ if ($supports_dead) { # 1.480 server and newer
Branch: 1 branch
BranchTrueFalseTotal
if ($supports_dead) { } 0 0 0
552 0 $self->dead_pid(host=>$lhost, pid=>$lpid);
553 } else { # This has a potential race case, which may kill the wrong lock
554 0 $self->break_lock(lock=>$self->{lock});
555 }
556 0 $fh->close();
557 0 goto retry;
558 }
559 }
560 }
561 0 ▶ &{$self->{print_obtained}} ($self,@args) if ($cmd eq "print_obtained");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "print_obtained" 0 0 0
562 0 ▶ &{$self->{print_waiting}} ($self,@args) if ($cmd eq "print_waiting");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "print_waiting" 0 0 0
563 0 ▶ &{$self->{print_broke}} ($self,@args) if ($cmd eq "print_broke");
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "print_broke" 0 0 0
564 0 ▶ print "$1\n" if ($line =~ /^ECHO\s+(.*)$/ && $self->{verbose}); #debugging
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^ECHO\s+(.*)$/ and $self->{"verbose"} 0 0 0
Condition0%0/3
$line =~ /^ECHO\s+(.*)$/ and $self->{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^ECHO\s+(.*)$/ and $self->{"verbose"}
$line =~ /^ECHO\s+(.*)$/ $self->{"verbose"}
Truth table0%0/3
A and B
A= $line =~ /^ECHO\s+(.*)$/B= $self->{"verbose"}
ABresult
0X0
100
111
565 }
566 # Note above break_lock also has prologue close
567 0 $fh->close();
568  
569 0 ▶ $@ = $preerror || $@; # User's error is more important than any we make
Condition0%0/3
$preerror || $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$preerror || $@
$preerror $@
Truth table0%0/3
A || B
A= $preerrorB= $@
ABresult
1X1
011
000
570 0 return 1;
571 }
572  
573 ######################################################################
574  
575 24 our $_Tcp_Proto;
576 sub _tcp_proto {
577 # We don't want creating a socket to have to keep reading /etc/services
578 # One would have thought IO::Socket etc kept this for us...
579 3 ▶ if (!defined $_Tcp_Proto) {
Subroutine _tcp_proto: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $IPC::Locker::_Tcp_Proto) 3 0 3
580 3 ▶ $_Tcp_Proto = getprotobyname("tcp")
Branch: 1 branch
BranchTrueFalseTotal
$IPC::Locker::_Tcp_Proto = getprotobyname "tcp" or die "Could not determine the protocol number for tcp" 3 0 3
581 or die "Could not determine the protocol number for tcp";
582 }
583 0 return $_Tcp_Proto;
584 }
585  
586 sub _array_or_one {
587 6 ▶ return [$_[0]] if !ref $_[0];
Subroutine _array_or_one: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 6 6
588 6 return $_[0];
589 }
590  
591 sub colon_joined_list {
592 0 ▶ my $item = shift;
Subroutine colon_joined_list: not called CC=2 SCAR=17.9
Pod undocumented
593 0 ▶ return $item if !ref $item;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item 0 0 0
594 0 return (join ":",@{$item});
595 }
596  
597 sub lock_name_list {
598 0 ▶ my $self = shift;
Subroutine lock_name_list: not called CC=1 SCAR=6.9
Pod undocumented
599 0 return colon_joined_list($self->{lock});
600 }
601  
602 ######################################################################
603 #### Logging
604  
605 sub _timelog {
606 0 ▶ my $msg = join('',@_);
Subroutine _timelog: not called CC=1 SCAR=6.9
607 0 my ($time, $time_usec) = Time::HiRes::gettimeofday();
608 0 my ($sec,$min,$hour,$mday,$mon) = localtime($time);
609 0 printf +("[%02d/%02d %02d:%02d:%02d.%06d] %s",
610 $mon+1, $mday, $hour, $min, $sec, $time_usec, $msg);
611 }
612  
613 sub _timelog_split {
614 0 ▶ my $first = shift;
Subroutine _timelog_split: not called CC=1 SCAR=6.9
615 0 my $prefix = shift;
616 0 my $text = shift;
617 0 my $msg = $first . join("\n$prefix", split(/\n+/, "\n$text")) . "\n";
618 0 _timelog($msg)
619 }
620  
621 ######################################################################
622 #### Package return
623 24 1;
« blib/lib/IPC/Locker/Server.pm ↑ Summary blib/lib/IPC/PidStat/PidServer.pm »