1File::NFSLock(3) User Contributed Perl Documentation File::NFSLock(3)
2
3
4
6 File::NFSLock - perl module to do NFS (or not) locking
7
9 use File::NFSLock qw(uncache);
10 use Fcntl qw(LOCK_EX LOCK_NB);
11
12 my $file = "somefile";
13
14 ### set up a lock - lasts until object looses scope
15 if (my $lock = new File::NFSLock {
16 file => $file,
17 lock_type => LOCK_EX⎪LOCK_NB,
18 blocking_timeout => 10, # 10 sec
19 stale_lock_timeout => 30 * 60, # 30 min
20 }) {
21
22 ### OR
23 ### my $lock = File::NFSLock->new($file,LOCK_EX⎪LOCK_NB,10,30*60);
24
25 ### do write protected stuff on $file
26 ### at this point $file is uncached from NFS (most recent)
27 open(FILE, "+<$file") ⎪⎪ die $!;
28
29 ### or open it any way you like
30 ### my $fh = IO::File->open( $file, 'w' ) ⎪⎪ die $!
31
32 ### update (uncache across NFS) other files
33 uncache("someotherfile1");
34 uncache("someotherfile2");
35 # open(FILE2,"someotherfile1");
36
37 ### unlock it
38 $lock->unlock();
39 ### OR
40 ### undef $lock;
41 ### OR let $lock go out of scope
42 }else{
43 die "I couldn't lock the file [$File::NFSLock::errstr]";
44 }
45
47 Program based of concept of hard linking of files being atomic across
48 NFS. This concept was mentioned in Mail::Box::Locker (which was origi‐
49 nally presented in Mail::Folder::Maildir). Some routine flow is taken
50 from there -- particularly the idea of creating a random local file,
51 hard linking a common file to the local file, and then checking the
52 nlink status. Some ideologies were not complete (uncache mechanism,
53 shared locking) and some coding was even incorrect (wrong stat index).
54 File::NFSLock was written to be light, generic, and fast.
55
57 Locking occurs by creating a File::NFSLock object. If the object is
58 created successfully, a lock is currently in place and remains in place
59 until the lock object goes out of scope (or calls the unlock method).
60
61 A lock object is created by calling the new method and passing two to
62 four parameters in the following manner:
63
64 my $lock = File::NFSLock->new($file,
65 $lock_type,
66 $blocking_timeout,
67 $stale_lock_timeout,
68 );
69
70 Additionally, parameters may be passed as a hashref:
71
72 my $lock = File::NFSLock->new({
73 file => $file,
74 lock_type => $lock_type,
75 blocking_timeout => $blocking_timeout,
76 stale_lock_timeout => $stale_lock_timeout,
77 });
78
80 Parameter 1: file
81 Filename of the file upon which it is anticipated that a write will
82 happen to. Locking will provide the most recent version (uncached)
83 of this file upon a successful file lock. It is not necessary for
84 this file to exist.
85
86 Parameter 2: lock_type
87 Lock type must be one of the following:
88
89 BLOCKING
90 BL
91 EXCLUSIVE (BLOCKING)
92 EX
93 NONBLOCKING
94 NB
95 SHARED
96 SH
97
98 Or else one or more of the following joined with '⎪':
99
100 Fcntl::LOCK_EX() (BLOCKING)
101 Fcntl::LOCK_NB() (NONBLOCKING)
102 Fcntl::LOCK_SH() (SHARED)
103
104 Lock type determines whether the lock will be blocking, non block‐
105 ing, or shared. Blocking locks will wait until other locks are
106 removed before the process continues. Non blocking locks will
107 return undef if another process currently has the lock. Shared
108 will allow other process to do a shared lock at the same time as
109 long as there is not already an exclusive lock obtained.
110
111 Parameter 3: blocking_timeout (optional)
112 Timeout is used in conjunction with a blocking timeout. If speci‐
113 fied, File::NFSLock will block up to the number of seconds speci‐
114 fied in timeout before returning undef (could not get a lock).
115
116 Parameter 4: stale_lock_timeout (optional)
117 Timeout is used to see if an existing lock file is older than the
118 stale lock timeout. If do_lock fails to get a lock, the modified
119 time is checked and do_lock is attempted again. If the
120 stale_lock_timeout is set to low, a recursion load could exist so
121 do_lock will only recurse 10 times (this is only a problem if the
122 stale_lock_timeout is set too low -- on the order of one or two
123 seconds).
124
126 After the $lock object is instantiated with new, as outlined above,
127 some methods may be used for additional functionality.
128
129 unlock
130
131 $lock->unlock;
132
133 This method may be used to explicitly release a lock that is aquired.
134 In most cases, it is not necessary to call unlock directly since it
135 will implicitly be called when the object leaves whatever scope it is
136 in.
137
138 uncache
139
140 $lock->uncache;
141 $lock->uncache("otherfile1");
142 uncache("otherfile2");
143
144 This method is used to freshen up the contents of a file across NFS,
145 ignoring what is contained in the NFS client cache. It is always
146 called from within the new constructor on the file that the lock is
147 being attempted. uncache may be used as either an object method or as
148 a stand alone subroutine.
149
150 newpid
151
152 my $pid = fork;
153 if (defined $pid) {
154 # Fork Failed
155 } elsif ($pid) {
156 $lock->newpid; # Parent
157 } else {
158 $lock->newpid; # Child
159 }
160
161 If fork() is called after a lock has been aquired, then when the lock
162 object leaves scope in either the parent or child, it will be released.
163 This behavior may be inappropriate for your application. To delegate
164 ownership of the lock from the parent to the child, both the parent and
165 child process must call the newpid() method after a successful fork()
166 call. This will prevent the parent from releasing the lock when unlock
167 is called or when the lock object leaves scope. This is also useful to
168 allow the parent to fail on subsequent lock attempts if the child lock
169 is still aquired.
170
172 On failure, a global variable, $File::NFSLock::errstr, should be set
173 and should contain the cause for the failure to get a lock. Useful
174 primarily for debugging.
175
177 By default File::NFSLock will use a lock file extenstion of ".NFSLock".
178 This is in a global variable $File::NFSLock::LOCK_EXTENSION that may be
179 changed to suit other purposes (such as compatibility in mail systems).
180
182 Notify paul@seamons.com or bbb@cpan.org if you spot anything.
183
184 FIFO
185
186 Locks are not necessarily obtained on a first come first serve basis.
187 Not only does this not seem fair to new processes trying to obtain a
188 lock, but it may cause a process starvation condition on heavily locked
189 files.
190
191 DIRECTORIES
192
193 Locks cannot be obtained on directory nodes, nor can a directory node
194 be uncached with the uncache routine because hard links do not work
195 with directory nodes. Some other algorithm might be used to uncache a
196 directory, but I am unaware of the best way to do it. The biggest use
197 I can see would be to avoid NFS cache of directory modified and last
198 accessed timestamps.
199
201 Download and extract tarball before running these commands in its base
202 directory:
203
204 perl Makefile.PL
205 make
206 make test
207 make install
208
209 For RPM installation, download tarball before running these commands in
210 your _topdir:
211
212 rpm -ta SOURCES/File-NFSLock-*.tar.gz
213 rpm -ih RPMS/noarch/perl-File-NFSLock-*.rpm
214
216 Paul T Seamons (paul@seamons.com) - Performed majority of the program‐
217 ming with copious amounts of input from Rob Brown.
218
219 Rob B Brown (bbb@cpan.org) - In addition to helping in the programming,
220 Rob Brown provided most of the core testing to make sure implementation
221 worked properly. He is now the current maintainer.
222
223 Also Mark Overmeer (mark@overmeer.net) - Author of Mail::Box::Locker,
224 from which some key concepts for File::NFSLock were taken.
225
226 Also Kevin Johnson (kjj@pobox.com) - Author of Mail::Folder::Maildir,
227 from which Mark Overmeer based Mail::Box::Locker.
228
230 Copyright (C) 2001
231 Paul T Seamons
232 paul@seamons.com
233 http://seamons.com/
234
235 Copyright (C) 2002-2003,
236 Rob B Brown
237 bbb@cpan.org
238
239 This package may be distributed under the terms of either the
240 GNU General Public License
241 or the
242 Perl Artistic License
243
244 All rights reserved.
245
246
247
248perl v5.8.8 2003-05-13 File::NFSLock(3)