blib/lib/Lazy/Lockfile.pm

filter: Statementstmt 81.3% 83 / 102 Branchbran 52.2% 23 / 44 Conditioncond 43.3% 13 / 30 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 65.5% 135 / 206 CC 33 SCAR 31.8
CC
33
Cov
68%
new
39.8
DESTROY
18.1
delete_on_destroy
3.3
CRAP 24.0
↑ Summary
1 package Lazy::Lockfile;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Fcntl qw/ :DEFAULT :flock /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX qw/ :errno_h /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ( $VERSION ) = '1.23';
10  
11 =head1 NAME
12  
13 Lazy::Lockfile - File based locking for the lazy.
14  
15 =head1 SYNOPSIS
16  
17 use Lazy::Lockfile;
18  
19 my $lockfile = Lazy::Lockfile->new() || die "Couldn't get lock!";
20 ...
21 # Lock is released when $lockfile goes out of scope or your program exits.
22  
23 =head1 DESCRIPTION
24  
25 Lazy::Lockfile is a module designed for simple locking through the use of
26 lockfiles, requiring very little effort on the part of the developer. Once the
27 object is instanced, the lock will be held as long as object exists. When the
28 object is destroyed, the lock is released.
29  
30 Locks are based around the existence of a named file, not around the use of
31 L<flock> (though flock is used to synchronize access to the lock file).
32 Lazy::Lockfile is (usually) smart enough to detect stale lockfiles from PIDs no
33 longer running by placing the PID of the process holding the lock inside the
34 lockfile.
35  
36 =head1 NOTES
37  
38 Lazy::Lockfile is not safe for use on NFS volumes.
39  
40 Lazy::Lockfile is not tested to interact correctly with other file locking
41 systems when used on the same lockfile.
42  
43 Lazy::Lockfile uses kill (with signal zero) to determine if the lockfile is
44 stale. This works on most systems running as most users but there are likely
45 instances where this will fail. If this applies to your system, you can use the
46 L<no_pid> option to disable the check.
47  
48 If Lazy::Lockfile encounters a malformed lockfile (empty, containing other
49 text, etc), it will treat it as a corrupt file and overwrite it, assuming the
50 lock. The author believes this behavior should be changed (and malformed files
51 should be left untouched), but has kept this behavior for backwards
52 compatibility.
53  
54 =head1 USAGE
55  
56 All of the magic in Lazy::Lockfile is done through the constructor and
57 destructor.
58  
59 =head1 METHODS
60  
61 =head2 new
62  
63 Constructor for Lazy::Lockfile.
64  
65 =head3 Parameters
66  
67 Accepts a single optional parameter, a hashref containing the following
68 options:
69  
70 =head4 location
71  
72 Specifies the full path to the location of the lockfile. Defaults to:
73  
74 '/tmp/' . (fileparse($0))[0] . '.pid'
75  
76 i.e., the name of the program being run, with a ".pid" extension, in /tmp/.
77  
78 =head4 no_pid
79  
80 If true, instead of writing the PID file, a value of "0" is written instead.
81 When read by another instance of Lazy::Lockfile attempting to acquire the lock,
82 no PID check will be performed and the lock will be assumed to be active as
83 long as the file exists. Defaults to false.
84  
85 =head4 delete_on_destroy
86  
87 If true, sets the "delete on destroy" flag. This flag defaults to true, which
88 causes the lockfile to be removed when the object is destroyed. Generally,
89 this is the desired behavior. When set to false, this flag prevents the
90 lockfile from being removed automatically when the object is destroyed. See
91 also C<delete_on_destroy>.
92  
93 =head3 Compatibility
94  
95 For compatibility with older versions of Lazy::Lockfile (pre-1.0), a single
96 optional parameter is accepted, the path to the lockfile. This parameter
97 functions the same as the 'location' parameter described above.
98  
99 As stated above, malformed lockfiles will be overwritten, though this may be
100 subject to change in a future version.
101  
102 =head3 Return value
103  
104 If the lock can not be obtained, undef is returned (and $! will contain useful
105 information). Otherwise, the lock is exclusive to this process, as long as the
106 object exists.
107  
108 =head3 Example
109  
110 my $lockfile = Lazy::Lockfile->new( { location => "/var/lock", no_pid => 1 } )
111 || die "Couldn't get lock!";
112  
113 =cut
114  
115 3 ▶sub new {
Subroutine new: called CC=20 SCAR=36.8
Pod documented
116 3 my ( $class, $params ) = @_;
117 3 my $self = {};
118 3 my $lockfile_location;
119  
120 # Yargh, backwards compatibility ahoy!
121 3 ▶ if ( ref $params ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params ne 'HASH') { } 3 0 3
122 3 $lockfile_location = $params;
123 3 $params = {};
124 } else {
125 0 ▶ if ( !defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params) 0 0 0
126 0 $params = {};
127 }
128 0 $lockfile_location = $params->{'location'};
129 }
130  
131 3 ▶ if ( ( !defined $lockfile_location ) || ( $lockfile_location eq '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lockfile_location or $lockfile_location eq "") 3 0 3
Condition33%1/3
not defined $lockfile_location or $lockfile_location eq ""
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $lockfile_location or $lockfile_location eq ""
not defined $lockfile_location $lockfile_location eq ""
Truth table33%1/3
not(A) or B
A= not defined $lockfile_locationB= $lockfile_location eq ""
ABresult
1X1
011
000
132 3 $lockfile_location = '/tmp/' . (fileparse($0))[0] . '.pid';
133 }
134  
135 3 my $lock_tries = 0;
136 3 my ( $lock, $file_pid );
137  
138 # If we return here, sysopen will set $! for us.
139 3 ▶ sysopen( $lock, $lockfile_location, O_RDWR | O_CREAT | O_NOFOLLOW, 0644 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
sysopen $lock, $lockfile_location, 131138, 420 or return 3 0 3
140 3 ▶ while ( $lock_tries++ < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries++ < 5) 3 0 3
141 3 ▶ if ( flock( $lock, LOCK_NB | LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $lock, 6) 3 0 3
142 3 last;
143 }
144 0 sleep( 1 );
145 }
146 3 ▶ if ( $lock_tries > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries > 5) 0 3 3
147 0 close( $lock );
148 0 $! = EWOULDBLOCK;
149 0 return;
150 }
151 3 seek( $lock, 0, 0 );
152 3 $file_pid = <$lock>;
153  
154 3 ▶ if ( defined $file_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_pid) 1 2 3
155 1 ( $file_pid ) = $file_pid =~ /^(\d+)/;
156 }
157 # Would it be better to detect the broken file and return a different error?
158 # if ( ( !defined $file_pid ) && ( $file_pid eq '' ) )
159 # flock( $lock, LOCK_UN );
160 # close( $lock );
161 # $! = EFTYPE;
162 # return;
163 # }
164 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_pid and $file_pid ne "" and $file_pid == 0 || (kill(0, $file_pid) || $!{"EPERM"})) 1 2 3
Condition50%6/12
defined $file_pid and $file_pid ne "" and $file_pid == 0 || (kill(0, $file_pid) || $!{"EPERM"})
!ll&&!rl&&r
201
defined $file_pid and $file_pid ne ""
!ll&&!rl&&r
201
$file_pid == 0 or (kill 0, $file_pid or $!{"EPERM"})
l!l&&r!l&&!r
010
kill 0, $file_pid or $!{"EPERM"}
l!l&&r!l&&!r
100
MC/DC20%1/5
defined $file_pid and $file_pid ne "" and $file_pid == 0 || (kill(0, $file_pid) || $!{"EPERM"})
defined $file_pid $file_pid ne "" $file_pid == 0 kill 0, $file_pid $!{"EPERM"}
Truth table33%2/6
A and B and C or D or E
A= defined $file_pidB= $file_pid ne ""C= $file_pid == 0D= kill 0, $file_pidE= $!{"EPERM"}
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
165 ( ( defined $file_pid ) && ( $file_pid ne '' ) )
166 &&
167 1 ▶ ( ( $file_pid == 0 ) || ( kill( 0, $file_pid ) || $!{EPERM} ) )
Subroutine BEGIN: called CC=1 SCAR=0.0
168 ) {
169 1 flock( $lock, LOCK_UN );
170 1 close( $lock );
171 1 $! = EEXIST;
172 1 return;
173 }
174  
175 2 my $lockfile_contents;
176 2 ▶ if ( $params->{'no_pid'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'no_pid'}) { } 0 2 2
177 0 $lockfile_contents = "0\n";
178 } else {
179 2 $lockfile_contents = "$$\n";
180 }
181  
182 2 seek( $lock, 0, 0 );
183 2 truncate( $lock, length( $lockfile_contents ) );
184 2 my $r = syswrite( $lock, $lockfile_contents );
185  
186 # If there's a filesystem problem (full? corrupt?) and we can't write
187 # a PID, bail and error.
188 2 ▶ if ( !defined $r || $r != length( $lockfile_contents ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $r or $r != length $lockfile_contents) 0 2 2
Condition33%1/3
not defined $r or $r != length $lockfile_contents
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $r or $r != length $lockfile_contents
not defined $r $r != length $lockfile_contents
Truth table33%1/3
not(A) or B
A= not defined $rB= $r != length $lockfile_contents
ABresult
1X1
011
000
189 0 unlink( $lockfile_location );
190 0 flock( $lock, LOCK_UN );
191 0 close( $lock );
192 0 return;
193 }
194  
195 2 flock( $lock, LOCK_UN );
196 2 close( $lock );
197 2 bless $self, $class;
198 2 $self->{'lockfile_location'} = $lockfile_location;
199  
200 2 ▶ if ( defined $params->{'delete_on_destroy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'delete_on_destroy'}) { } 0 2 2
201 0 ▶ $self->{'delete_on_destroy'} = $params->{'delete_on_destroy'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'delete_on_destroy'} ? : 0 0 0
202 } else {
203 2 $self->{'delete_on_destroy'} = 1;
204 }
205  
206 2 return $self;
207 }
208  
209 =head2 name
210  
211 Returns the file name of the lockfile.
212  
213 =cut
214  
215 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
216 0 my ( $self ) = @_;
217 0 return $self->{'lockfile_location'};
218 }
219  
220 =head2 delete_on_destroy
221  
222 Gets or sets the "delete on destroy" flag.
223  
224 If called without a parameter (or with undef), delete_on_destroy will return
225 the current state of the "delete on destroy" flag. If called with a parameter,
226 this flag will be set.
227  
228 =cut
229  
230 1 ▶sub delete_on_destroy {
Subroutine delete_on_destroy: called CC=3 SCAR=12.0
Pod documented
231 1 my ( $self, $new_setting ) = @_;
232  
233 1 ▶ if ( !defined $new_setting ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $new_setting) { } 0 1 1
234 0 return $self->{'delete_on_destroy'};
235 } else {
236 1 ▶ $self->{'delete_on_destroy'} = $new_setting ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$new_setting ? : 0 1 1
237 1 return;
238 }
239 }
240  
241 =head2 unlock
242  
243 Explicitly removes the lockfile, just as if the object were destroyed. Once
244 this has been called, delete_on_destroy will be set to false, since the lock
245 has already been deleted. Once this method is called, there is not much use
246 left for the object, so the user may as well delete it now.
247  
248 unlock should be used when the lockfile needs to be removed deterministically
249 while the program is running. If you simply remove all references to the
250 Lazy::Lockfile object, the lock will be freed when garbage collection is run,
251 which is not guaranteed to happen until the program exits (though it will
252 likely happen immediately).
253  
254 Returns a true value if the lockfile was found and removed, false otherwise.
255  
256 =cut
257  
258 1 ▶sub unlock {
Subroutine unlock: called CC=1 SCAR=0.0
Pod documented
259 1 my ( $self ) = @_;
260 1 my $retval = $self->DESTROY;
261 1 $self->delete_on_destroy(0);
262 1 return $retval;
263 }
264  
265 # Make sure the lockfile contains our pid before we delete it...
266 # do we need this?
267 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=12 SCAR=28.9
268 3 my ( $self ) = @_;
269 3 my $retval = 0;
270 3 ▶ if ( ( $self ) && ( $self->{'lockfile_location'} ) && ( $self->{'delete_on_destroy'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $self->{"lockfile_location"} and $self->{"delete_on_destroy"}) 2 1 3
Condition50%3/6
$self and $self->{"lockfile_location"} and $self->{"delete_on_destroy"}
!ll&&!rl&&r
012
$self and $self->{"lockfile_location"}
!ll&&!rl&&r
003
MC/DC33%1/3
$self and $self->{"lockfile_location"} and $self->{"delete_on_destroy"}
$self $self->{"lockfile_location"} $self->{"delete_on_destroy"}
Truth table50%2/4
A and B and C
A= $selfB= $self->{"lockfile_location"}C= $self->{"delete_on_destroy"}
ABCresult
0XX0
10X0
1100
1111
271 2 my ( $lock, $file_pid );
272 2 my $lock_tries = 0;
273 2 ▶ open( $lock, '<', $self->{'lockfile_location'} ) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open $lock, "<", $self->{"lockfile_location"} or return 0 2 0 2
274 2 ▶ while ( $lock_tries++ < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries++ < 5) 2 0 2
275 2 ▶ if ( flock( $lock, LOCK_NB | LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $lock, 6) 2 0 2
276 2 last;
277 }
278 0 sleep( 1 );
279 }
280 0 ▶ if ( $lock_tries > 5 ) { close( $lock ); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries > 5) 0 2 2
281 2 seek( $lock, 0, 0 );
282 2 $file_pid = <$lock>;
283 2 ▶ chomp( $file_pid ) if defined $file_pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file_pid 2 0 2
284 2 ▶ if ( ( defined $file_pid ) && ( ( $file_pid == 0 ) || ( $file_pid == $$ ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_pid and $file_pid == 0 || $file_pid == $$) 2 0 2
Condition33%2/6
defined $file_pid and $file_pid == 0 || $file_pid == $$
!ll&&!rl&&r
002
$file_pid == 0 or $file_pid == $$
l!l&&r!l&&!r
020
MC/DC0%0/3
defined $file_pid and $file_pid == 0 || $file_pid == $$
defined $file_pid $file_pid == 0 $file_pid == $$
Truth table25%1/4
A and B or C
A= defined $file_pidB= $file_pid == 0C= $file_pid == $$
ABCresult
0XX0
1000
11X1
1011
285 2 $retval = unlink $self->{'lockfile_location'};
286 }
287 2 close( $lock );
288 }
289 3 return $retval;
290 }
291  
292 =head1 CHANGES
293  
294 =head2 2017-05-29, 1.23 - jeagle
295  
296 Detect and bail out on errors when writing to the lockfile (RT#121894).
297  
298 Thanks MRDVT.
299  
300 =head2 2014-10-30, 1.22 - jeagle
301  
302 Add missing dependency.
303  
304 =head2 2014-09-14, 1.21 - jeagle
305  
306 Re-package to make it easier to convert to RPM, etc.
307  
308 =head2 2012-04-01, 1.20 - jeagle
309  
310 Updated documentation, thanks Alister W.
311  
312 =head2 2011-01-05, 1.19 - jeagle
313  
314 Change to unit tests to appease cpantesters.
315  
316 =head2 2011-01-04, 1.18 - jeagle
317  
318 Implement suggestion by srezic to check PIDs belonging to other users
319 (RT#69185).
320  
321 Clean up documentation.
322  
323 =head2 2010-06-22, 1.17 - jeagle
324  
325 Update L<unlock> to return a useful status.
326  
327 =head2 2010-06-22, 1.16 - jeagle
328  
329 Version bumps for migration to CPAN.
330  
331 =head2 2009-12-03, 1.14 - jeagle
332  
333 Fix a bug causing lockfiles with no_pid to not be deleted on destroy/unlink.
334  
335 =head2 2009-12-03, 1.13 - jeagle
336  
337 Add the unlock method, to allow for deterministic lockfile removal at runtime.
338  
339 =head2 2009-11-30, 1.12 - jeagle
340  
341 Update documentation to clarify delete_on_destroy parameter default setting.
342  
343 =head2 2009-07-06, 1.11 - jeagle
344  
345 Fix error thrown when running with taint checking enabled.
346  
347 =head2 2009-07-06, 1.10 - jeagle
348  
349 Fix a bug with lockfile location being overwritten with the default.
350  
351 =head2 2009-07-06, 1.9 - jeagle
352  
353 Add new parameter, no_pid, which disabled active lockfile checks.
354  
355 Allow constructor to accept multiple parameters via hashref.
356  
357 =head2 2009-06-10, 0.4 - jeagle
358  
359 Introduce the delete_on_destroy flag.
360  
361 =head2 2009-06-03, 0.3 - jeagle
362  
363 Open pid file with O_NOFOLLOW, to avoid symlink attacks.
364  
365 Change default pid file location from /var/tmp to /tmp.
366  
367 Correct dates in CHANGES section.
368  
369 Add useful error indicators, documentation on error detection.
370  
371 =head2 2009-04-27, 0.2 - jeagle
372  
373 Fix a bug with unspecified lockfile paths trying to create impossible file
374 names.
375  
376 =head2 2009-04-06, v0.1 - jeagle
377  
378 Initial release.
379  
380 =cut
381  
382 1 1;
↑ Summary