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

name

6       File::FcntlLock - File locking with fcntl(2)
7

SYNOPSIS

9         use File::FcntlLock;
10
11         my $fs = new File::FcntlLock;
12         $fs->l_type( F_RDLCK );
13         $fs->l_whence( SEEK_CUR );
14         $fs->l_start( 100 );
15         $fs->l_len( 123 );
16
17         open my $fh, '<', 'file_name' or die "Can't open file: $!\n";
18         $fs->lock( $fh, F_SETLK )
19             or print "Locking failed: " . $fs->error . "\n";
20         $fs->l_type( F_UNLCK );
21         $fs->lock( $fh, F_SETLK )
22             or print "Unlocking failed: " . $fs->error . "\n";
23

DESCRIPTION

25       File locking in Perl is usually done using the flock() function.
26       Unfortunately, this only allows locks on whole files and is often
27       implemented in terms of flock(2), which has some shortcomings.
28
29       Using this module file locking via fcntl(2) can be done (obviously,
30       this restricts the use of the module to systems that have a fcntl(2)
31       system call). Before a file (or parts of a file) can be locked, an
32       object simulating a flock structure must be created and its properties
33       set. Afterwards, by calling the "lock()" method a lock can be set or it
34       can be determined if and which process currently holds the lock.
35
36       To create a new object representing a flock structure call "new()":
37
38         $fs = new File::FcntlLock;
39
40       You also can pass the "new()" method a set of key-value pairs to
41       initialize the objects properties, e.g. use
42
43         $fs = new File::FcntlLock l_type   => F_WRLCK,
44                                   l_whence => SEEK_SET,
45                                   l_start  => 0,
46                                   l_len    => 100;
47
48       if you plan to obtain a write lock for the first 100 bytes of a file.
49
50       Once you have created the object simulating the flock structure the
51       following methods allow to query and in most cases also to modify the
52       properties of the object.
53
54       "l_type()"
55           If called without an argument returns the current setting of the
56           lock type, otherwise the lock type is set to the argument, which
57           must be either "F_RDLCK", "F_WRLCK" or "F_UNLCK" (for read lock,
58           write lock or unlock).
59
60       "l_whence()"
61           Queries or sets the "l_whence" property of the flock object,
62           determining if the "l_start" value is relative to the start of the
63           file, to the current position in the file or to the end of the
64           file. The corresponding values are "SEEK_SET", "SEEK_CUR" and
65           "SEEK_END". See also the man page for lseek(2).
66
67       "l_start()"
68           Queries or sets the start position (offset) of the lock in the file
69           according to the mode selected by the "l_whence" member.  See also
70           the man page for lseek(2).
71
72       "l_len()"
73           Queries or sets the length of the region (in bytes) in the file to
74           be locked. A value of 0 is interpreted as to mean a lock (starting
75           at "l_start") up to the very end of the file.
76
77           According to SUSv3 negative values for "l_start" are allowed
78           (resulting in a lock ranging from "l_start + l_len" to "l_start -
79           1") Unfortunately, not all systems allow negative arguments and
80           will return an error when you try to obtain the lock, so please
81           read the fcntl(2) man page of your system carefully for details.
82
83       "l_pid()"
84           This method allows retrieving the PID of a process currently
85           holding the lock after a call of "lock()" with "F_SETLK" indicated
86           that another process is holding the lock. A call to "lock()" with
87           "F_GETLK" will fill in this value so "l_pid()" can be called.
88
89       When not initialized the flock objects "l_type" property is set to
90       "F_RDLCK" by default, "l_whence" to "SEEK_SET", and both "l_start" and
91       "l_len" to 0, i.e. the settings for a read lock on the whole file.
92
93       After having set up the object representing a flock structure you can
94       determine the current holder of a lock or try to obtain a lock by
95       invoking the "lock()" method with two arguments, a file handle (or a
96       file descriptor, the module figures out automatically what it got) and
97       a flag indicating the action to be taken, e.g.
98
99         $fs->lock( $fh, F_SETLK );
100
101       There are three values that can be used as the second argument:
102
103       "F_GETLK"
104           For "F_GETLK" the "lock()" method determines if and who currently
105           is holding the lock.  If no other process is holding the lock the
106           "l_type" field is set to "F_UNLCK". Otherwise the flock structure
107           object is set to the values that prevent us from obtaining a lock.
108           There may be multiple such blocking processes, including some that
109           are themselves blocked waiting to obtain a lock. "F_GETLK" will
110           only make details of one of these visible, and one has no control
111           over which process this is.
112
113       "F_SETLK"
114           For "F_SETLK" the "lock()" method tries to obtain the lock (when
115           "l_type" is set to either "F_WRLCK" or "F_RDLCK") or releases the
116           lock (if "l_type" is set to "F_UNLCK"). If a lock is held by some
117           other proces the method call returns "undef" and errno is set to
118           "EACCESS" or "EAGAIN" (please see the the man page for fcntl(2) for
119           the details).
120
121       "F_SETLKW"
122           is similar to "F_SETLK" but instead of returning an error if the
123           lock can't be obtained immediately it blocks until the lock is
124           obtained. If a signal is received while waiting for the lock the
125           method returns "undef" and errno is set to "EINTR".
126
127       On success the method returns the string "0 but true". If the method
128       fails (as indicated by an "undef" return value) you can either
129       immediately evaluate the error number (usingf $!, $ERRNO or $OS_ERROR)
130       or check for it at some later time via the methods discussed below.
131
132       There are three methods for obtaining information about the reason the
133       the last call of "lock()" for the object failed:
134
135       "lock_errno()"
136           Returns the error number from the latest call of "lock()". If the
137           last call did not result in an error the method returns "undef".
138
139       "error()"
140           Returns a short description of the error that happened during the
141           latest call of "lock()" with the object. Please take the messages
142           with a grain of salt, they represent what SUSv3 (IEEE 1003.1-2001)
143           and the Linux, TRUE64, OpenBSD3 and Solaris8 man pages tell what
144           the error numbers mean, there could be differences (and additional
145           error numbers) on other systems. If there was no error the method
146           returns "undef".
147
148       "system_error()"
149           While the previous method, "error()", tries to return a string with
150           some relevance to the locking operation (i.e. "File or segment
151           already locked by other process(es)" instead of "Permission
152           denied") this method returns the "normal" system error message
153           associated with errno. The method returns "undef" if there was no
154           error.
155
156   EXPORT
157       F_GETLK F_SETLK F_SETLKW F_RDLCK F_WRLCK F_UNLCK SEEK_SET SEEK_CUR
158       SEEK_END
159

CREDITS

161       Thanks to Mark Jason Dominus (MJD) and Benjamin Goldberg (GOLDBB) for
162       helpful discussions, code examples and encouragement. Glenn Herteg
163       pointed out several problems and also helped improve the documentation.
164

AUTHOR

166       Jens Thoms Toerring <jt@toerring.de>
167

SEE ALSO

169       perl(1), fcntl(2), lseek(2).
170
171
172
173perl v5.12.1                      2009-10-08                File::FcntlLock(3)
Impressum