1File::NFSLock(3)      User Contributed Perl Documentation     File::NFSLock(3)
2
3
4

NAME

6       File::NFSLock - perl module to do NFS (or not) locking
7

SYNOPSIS

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

DESCRIPTION

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
49       originally presented in Mail::Folder::Maildir).  Some routine flow is
50       taken from there -- particularly the idea of creating a random local
51       file, hard linking a common file to the local file, and then checking
52       the nlink status.  Some ideologies were not complete (uncache
53       mechanism, shared locking) and some coding was even incorrect (wrong
54       stat index).  File::NFSLock was written to be light, generic, and fast.
55

USAGE

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

PARAMETERS

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
105           blocking, or shared.  Blocking locks will wait until other locks
106           are 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
113           specified, File::NFSLock will block up to the number of seconds
114           specified 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

METHODS

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         $lock->unlock;
131
132       This method may be used to explicitly release a lock that is aquired.
133       In most cases, it is not necessary to call unlock directly since it
134       will implicitly be called when the object leaves whatever scope it is
135       in.
136
137   uncache
138         $lock->uncache;
139         $lock->uncache("otherfile1");
140         uncache("otherfile2");
141
142       This method is used to freshen up the contents of a file across NFS,
143       ignoring what is contained in the NFS client cache.  It is always
144       called from within the new constructor on the file that the lock is
145       being attempted.  uncache may be used as either an object method or as
146       a stand alone subroutine.
147
148   newpid
149         my $pid = fork;
150         if (defined $pid) {
151           # Fork Failed
152         } elsif ($pid) {
153           $lock->newpid; # Parent
154         } else {
155           $lock->newpid; # Child
156         }
157
158       If fork() is called after a lock has been aquired, then when the lock
159       object leaves scope in either the parent or child, it will be released.
160       This behavior may be inappropriate for your application.  To delegate
161       ownership of the lock from the parent to the child, both the parent and
162       child process must call the newpid() method after a successful fork()
163       call.  This will prevent the parent from releasing the lock when unlock
164       is called or when the lock object leaves scope.  This is also useful to
165       allow the parent to fail on subsequent lock attempts if the child lock
166       is still aquired.
167

FAILURE

169       On failure, a global variable, $File::NFSLock::errstr, should be set
170       and should contain the cause for the failure to get a lock.  Useful
171       primarily for debugging.
172

LOCK_EXTENSION

174       By default File::NFSLock will use a lock file extenstion of ".NFSLock".
175       This is in a global variable $File::NFSLock::LOCK_EXTENSION that may be
176       changed to suit other purposes (such as compatibility in mail systems).
177

BUGS

179       Notify paul@seamons.com or bbb@cpan.org if you spot anything.
180
181   FIFO
182       Locks are not necessarily obtained on a first come first serve basis.
183       Not only does this not seem fair to new processes trying to obtain a
184       lock, but it may cause a process starvation condition on heavily locked
185       files.
186
187   DIRECTORIES
188       Locks cannot be obtained on directory nodes, nor can a directory node
189       be uncached with the uncache routine because hard links do not work
190       with directory nodes.  Some other algorithm might be used to uncache a
191       directory, but I am unaware of the best way to do it.  The biggest use
192       I can see would be to avoid NFS cache of directory modified and last
193       accessed timestamps.
194

INSTALL

196       Download and extract tarball before running these commands in its base
197       directory:
198
199         perl Makefile.PL
200         make
201         make test
202         make install
203
204       For RPM installation, download tarball before running these commands in
205       your _topdir:
206
207         rpm -ta SOURCES/File-NFSLock-*.tar.gz
208         rpm -ih RPMS/noarch/perl-File-NFSLock-*.rpm
209

AUTHORS

211       Paul T Seamons (paul@seamons.com) - Performed majority of the
212       programming with copious amounts of input from Rob Brown.
213
214       Rob B Brown (bbb@cpan.org) - In addition to helping in the programming,
215       Rob Brown provided most of the core testing to make sure implementation
216       worked properly.  He is now the current maintainer.
217
218       Also Mark Overmeer (mark@overmeer.net) - Author of Mail::Box::Locker,
219       from which some key concepts for File::NFSLock were taken.
220
221       Also Kevin Johnson (kjj@pobox.com) - Author of Mail::Folder::Maildir,
222       from which Mark Overmeer based Mail::Box::Locker.
223
225         Copyright (C) 2001
226         Paul T Seamons
227         paul@seamons.com
228         http://seamons.com/
229
230         Copyright (C) 2002-2003,
231         Rob B Brown
232         bbb@cpan.org
233
234         This package may be distributed under the terms of either the
235         GNU General Public License
236           or the
237         Perl Artistic License
238
239         All rights reserved.
240

POD ERRORS

242       Hey! The above document had some coding errors, which are explained
243       below:
244
245       Around line 624:
246           You forgot a '=back' before '=head1'
247
248
249
250perl v5.12.0                      2003-05-13                  File::NFSLock(3)
Impressum