blib/lib/Qmail/Control/Lock.pm

filter: Statementstmt 31.8% 14 / 44 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 4 / 10 Podpod 100% 5 / 5 totaltotal 29.8% 23 / 77 CC 10 SCAR 28.7
CC
10
Cov
23%
relock
42.0
lock_shared
12.0
lock_exclusive
12.0
CRAP 17.6
« blib/lib/Qmail/Control.pm ↑ Summary Control/Lock/test.pl »
1 ## This file is Copyright (C) 2002, Paul Prince <princep@charter.net>.
2 ## It is licensed and distributed under the terms of Perl itself.
3  
4 package Qmail::Control::Lock;
5  
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.01';
11  
12 2 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 require Exporter;
15  
16 =head1 NAME
17  
18 Qmail::Control::Lock - Perl extension for locking Qmail's control file
19 subsystem.
20  
21 =head1 SYNOPSIS
22  
23 use Qmail::Control::Lock;
24  
25 my $lock = Qmail::Control::Lock->new();
26  
27 # Get a shared lock on the control subsystem.
28 $lock->lock_shared() or die "Couldn't get sh lock: $!\n";
29  
30 # Get an exclusive lock on the control subsystem.
31 $lock2->lock_exclusive() or die "Couldn't get ex lock: $!\n";
32  
33 # Change a shared lock to an exclusive lock.
34 $lock->relock('exclusive') or die "Couldn't change sh to ex lock: $!\n";
35  
36 # Unlock the control subsystem.
37 $lock->unlock();
38 $lock2->unlock();
39  
40 =head1 DESCRIPTION
41  
42 Qmail::Control::Lock provides and interface for locking Qmail's control
43 file subsystem.
44  
45 Dan Bernstein does not endorse this module or this
46 locking method, and as far as I know, only Qmail::Control::Lock uses it.
47  
48 =head1 EXPORTS
49  
50 None by default.
51  
52 =cut
53  
54 ######################
55 # METHODS START HERE #
56 ######################
57  
58 =head1 METHODS
59  
60 =over 30
61  
62 =item Qmail::Control::Lock->new()
63  
64 Creates a new Qmail::Control::Lock object.
65  
66 Returns a reference to the newly created object.
67  
68 Takes no arguments, currently. This may change, but the argumentless
69 form will always exist.
70  
71 =cut
72  
73 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
74 # Do I need this stuff??
75 # my $invocant = shift;
76 # my $class = ref($invocant) || $invocant;
77 # bless($class);
78  
79 # Create a new Qmail::Control::Lock object.
80 0 my $self = { };
81 0 bless($self);
82  
83 # Locking is achieved by calling flock() on QMAILHOME/control/.lock .
84 # This is ususally /var/qmail/control/.lock , which is what we
85 # assume.
86  
87 # Open the lockfile for reading.
88 0 my $lockfile_handle;
89 0 open($lockfile_handle, '<', '/var/qmail/control/.lock');
90  
91 # Put a reference to the filehandle into the hash which represents the
92 # object.
93 0 $self->{'lockfile_handle'} = $lockfile_handle;
94  
95 # Return a reference to the newly created object.
96 0 return $self;
97 }
98  
99 =item $lock->lock_shared();
100  
101 Gets a shared lock on the Qmail control file subsystem.
102  
103 Returns true on success or undef on a serious error.
104  
105 Takes no arguments.
106  
107 =cut
108  
109 0 ▶sub lock_shared {
Subroutine lock_shared: not called CC=3 SCAR=24.8
Pod documented
110 0 my $self = shift;
111  
112 # Confirm that there is a filehandle in $self.
113 0 ▶ exists $self->{'lockfile_handle'} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"lockfile_handle"} or return undef 0 0 0
114  
115 # Lock that filehandle.
116 0 ▶ flock ($self->{'lockfile_handle'}, LOCK_SH) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"lockfile_handle"}, 1 or return undef 0 0 0
117
118 # Let the object know that it is locked, and how.
119 0 $self->{'locked'} = 'shared';
120  
121 # Return true.
122 0 return 1;
123 }
124  
125 =item $lock->lock_exclusive();
126  
127 Gets an exclusive lock on the Qmail control file subsystem.
128  
129 Returns true on success or undef on a serious error.
130  
131 Takes no arguments.
132  
133 =cut
134  
135 0 ▶sub lock_exclusive {
Subroutine lock_exclusive: not called CC=3 SCAR=24.8
Pod documented
136 0 my $self = shift;
137  
138 # Confirm that there is a filehandle in $self.
139 0 ▶ exists $self->{'lockfile_handle'} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"lockfile_handle"} or return undef 0 0 0
140  
141 # Lock that filehandle.
142 0 ▶ flock ($self->{'lockfile_handle'}, LOCK_EX) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"lockfile_handle"}, 2 or return undef 0 0 0
143
144 # Let the object know that it is locked, and how.
145 0 $self->{'locked'} = 'exclusive';
146  
147 # Return true.
148 0 return 1;
149 }
150  
151 =item $lock->relock();
152  
153 Changes one type of lock (either shared or exclusive) into another.
154  
155 Returns true on success or undef on a serious error.
156  
157 Takes a single argument, either 'shared' or 'exclusive', which indicates
158 which type of lock to engage. If you pass 'shared', and the lock is
159 already 'shared', this is a no-op.
160  
161 =cut
162  
163 0 ▶sub relock {
Subroutine relock: not called CC=6 SCAR=37.4
Pod documented
164 0 my $self = shift;
165  
166 # Confirm that there is a filehandle in $self.
167 0 ▶ exists $self->{'lockfile_handle'} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"lockfile_handle"} or return undef 0 0 0
168  
169 # # Unlock that filehandle.
170 # flock ($self->{'lockfile_handle'}, LOCK_UN) or return undef;
171  
172 # If we got 'shared', lock 'shared'.
173 0 ▶ if ($_[0] eq 'shared') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq 'shared') { } 0 0 0
elsif ($_[0] eq 'exclusive') { } 0 0 0
174 0 ▶ flock ($self->{'lockfile_handle'}, LOCK_SH) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"lockfile_handle"}, 1 or return undef 0 0 0
175 }
176 # Else, if we got 'exclusive', lock 'exclusive'.
177 elsif ($_[0] eq 'exclusive') {
178 0 ▶ flock ($self->{'lockfile_handle'}, LOCK_EX) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"lockfile_handle"}, 2 or return undef 0 0 0
179 }
180 # Else, we got an invalud param, return undef.
181 else {
182 0 return undef;
183 }
184  
185 # Let the object know that it is locked, and how.
186 0 $self->{'locked'} = $_[0];
187  
188 # Return true.
189 0 return 1;
190 }
191  
192 =item $lock->unlock();
193  
194 Unlocks the Qmail control file subsystem.
195  
196 Returns nothing.
197  
198 Takes no arguments.
199  
200 =cut
201  
202 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204  
205 # Unlock that filehandle.
206 0 flock ($self->{'lockfile_handle'}, LOCK_UN);
207
208 # Let the object know that it is not locked.
209 0 delete $self->{'locked'};
210  
211 # Return true.
212 0 return 1;
213 }
214  
215 =pod
216  
217 =back
218  
219 =head1 AUTHOR
220  
221 Paul Prince, E<lt>princep@charter.netE<gt>
222  
223 =head1 SEE ALSO
224  
225 L<perl>.
226 L<Qmail::Control::Lock>.
227  
228 =cut
229  
230 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
231 0 my $self = shift;
232 0 close $self->{'lockfile_handle'};
233 }
234  
235 # End the package.
236 2 1;
« blib/lib/Qmail/Control.pm ↑ Summary Control/Lock/test.pl »