blib/lib/Lazy/Lockfile.pm

filter: Statementstmt 84.2% 80 / 95 Branchbran 52.3% 22 / 42 Conditioncond 44.4% 12 / 27 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 67.1% 129 / 192 CC 31 SCAR 29.5
CC
31
Cov
70%
new
30.4
DESTROY
18.1
delete_on_destroy
3.3
CRAP 19.1
↑ 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.21';
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 sub new {
116 3 ▶ my ( $class, $params ) = @_;
Subroutine new: called CC=18 SCAR=34.2
Pod documented
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 0 3 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
210
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
001
kill 0, $file_pid or $!{"EPERM"}
l!l&&r!l&&!r
100
MC/DC0%0/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 table16%1/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 seek( $lock, 0, 0 );
176 2 truncate( $lock, 0 );
177 2 ▶ if ( $params->{'no_pid'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'no_pid'}) { } 0 2 2
178 0 print $lock "0\n";
179 } else {
180 2 print $lock "$$\n";
181 }
182 2 flock( $lock, LOCK_UN );
183 2 close( $lock );
184 2 bless $self, $class;
185 2 $self->{'lockfile_location'} = $lockfile_location;
186  
187 2 ▶ if ( defined $params->{'delete_on_destroy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'delete_on_destroy'}) { } 0 2 2
188 0 ▶ $self->{'delete_on_destroy'} = $params->{'delete_on_destroy'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'delete_on_destroy'} ? : 0 0 0
189 } else {
190 2 $self->{'delete_on_destroy'} = 1;
191 }
192  
193 2 return $self;
194 }
195  
196 =head2 name
197  
198 Returns the file name of the lockfile.
199  
200 =cut
201  
202 sub name {
203 0 ▶ my ( $self ) = @_;
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
204 0 return $self->{'lockfile_location'};
205 }
206  
207 =head2 delete_on_destroy
208  
209 Gets or sets the "delete on destroy" flag.
210  
211 If called without a parameter (or with undef), delete_on_destroy will return
212 the current state of the "delete on destroy" flag. If called with a parameter,
213 this flag will be set.
214  
215 =cut
216  
217 sub delete_on_destroy {
218 1 ▶ my ( $self, $new_setting ) = @_;
Subroutine delete_on_destroy: called CC=3 SCAR=12.0
Pod documented
219  
220 1 ▶ if ( !defined $new_setting ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $new_setting) { } 0 1 1
221 0 return $self->{'delete_on_destroy'};
222 } else {
223 1 ▶ $self->{'delete_on_destroy'} = $new_setting ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$new_setting ? : 0 1 1
224 1 return;
225 }
226 }
227  
228 =head2 unlock
229  
230 Explicitly removes the lockfile, just as if the object were destroyed. Once
231 this has been called, delete_on_destroy will be set to false, since the lock
232 has already been deleted. Once this method is called, there is not much use
233 left for the object, so the user may as well delete it now.
234  
235 unlock should be used when the lockfile needs to be removed deterministically
236 while the program is running. If you simply remove all references to the
237 Lazy::Lockfile object, the lock will be freed when garbage collection is run,
238 which is not guaranteed to happen until the program exits (though it will
239 likely happen immediately).
240  
241 Returns a true value if the lockfile was found and removed, false otherwise.
242  
243 =cut
244  
245 sub unlock {
246 1 ▶ my ( $self ) = @_;
Subroutine unlock: called CC=1 SCAR=0.0
Pod documented
247 1 my $retval = $self->DESTROY;
248 1 $self->delete_on_destroy(0);
249 1 return $retval;
250 }
251  
252 # Make sure the lockfile contains our pid before we delete it...
253 # do we need this?
254 sub DESTROY {
255 3 ▶ my ( $self ) = @_;
Subroutine DESTROY: called CC=12 SCAR=28.9
256 3 my $retval = 0;
257 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
258 2 my ( $lock, $file_pid );
259 2 my $lock_tries = 0;
260 2 ▶ open( $lock, '<', $self->{'lockfile_location'} ) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open $lock, "<", $self->{"lockfile_location"} or return 0 0 2 2
261 2 ▶ while ( $lock_tries++ < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries++ < 5) 2 0 2
262 2 ▶ if ( flock( $lock, LOCK_NB | LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $lock, 6) 2 0 2
263 2 last;
264 }
265 0 sleep( 1 );
266 }
267 2 ▶ if ( $lock_tries > 5 ) { close( $lock ); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_tries > 5) 0 2 2
268 2 seek( $lock, 0, 0 );
269 2 $file_pid = <$lock>;
270 2 ▶ chomp( $file_pid ) if defined $file_pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file_pid 2 0 2
271 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
272 2 $retval = unlink $self->{'lockfile_location'};
273 }
274 2 close( $lock );
275 }
276 3 return $retval;
277 }
278  
279 =head1 CHANGES
280  
281 =head2 2014-09-14, 1.21 - jeagle
282  
283 Re-package to make it easier to convert to RPM, etc.
284  
285 =head2 2012-04-01, 1.20 - jeagle
286  
287 Updated documentation, thanks Alister W.
288  
289 =head2 2011-01-05, 1.19 - jeagle
290  
291 Change to unit tests to appease cpantesters.
292  
293 =head2 2011-01-04, 1.18 - jeagle
294  
295 Implement suggestion by srezic to check PIDs belonging to other users
296 (RT#69185).
297  
298 Clean up documentation.
299  
300 =head2 2010-06-22, 1.17 - jeagle
301  
302 Update L<unlock> to return a useful status.
303  
304 =head2 2010-06-22, 1.16 - jeagle
305  
306 Version bumps for migration to CPAN.
307  
308 =head2 2009-12-03, 1.14 - jeagle
309  
310 Fix a bug causing lockfiles with no_pid to not be deleted on destroy/unlink.
311  
312 =head2 2009-12-03, 1.13 - jeagle
313  
314 Add the unlock method, to allow for deterministic lockfile removal at runtime.
315  
316 =head2 2009-11-30, 1.12 - jeagle
317  
318 Update documentation to clarify delete_on_destroy parameter default setting.
319  
320 =head2 2009-07-06, 1.11 - jeagle
321  
322 Fix error thrown when running with taint checking enabled.
323  
324 =head2 2009-07-06, 1.10 - jeagle
325  
326 Fix a bug with lockfile location being overwritten with the default.
327  
328 =head2 2009-07-06, 1.9 - jeagle
329  
330 Add new parameter, no_pid, which disabled active lockfile checks.
331  
332 Allow constructor to accept multiple parameters via hashref.
333  
334 =head2 2009-06-10, 0.4 - jeagle
335  
336 Introduce the delete_on_destroy flag.
337  
338 =head2 2009-06-03, 0.3 - jeagle
339  
340 Open pid file with O_NOFOLLOW, to avoid symlink attacks.
341  
342 Change default pid file location from /var/tmp to /tmp.
343  
344 Correct dates in CHANGES section.
345  
346 Add useful error indicators, documentation on error detection.
347  
348 =head2 2009-04-27, 0.2 - jeagle
349  
350 Fix a bug with unspecified lockfile paths trying to create impossible file
351 names.
352  
353 =head2 2009-04-06, v0.1 - jeagle
354  
355 Initial release.
356  
357 =cut
358  
359 1 1;
↑ Summary