1 .\" Copyright 1997 Nicolás Lichtmaier <nick@debian.org>
2 .\" Created Thu Aug 7 00:44:00 ART 1997
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
25 .\" Added section stuff, aeb, 2002-04-22.
26 .\" Corrected include file, drepper, 2003-06-15.
28 .TH LOCKF 3 2017-09-15 "GNU" "Linux Programmer's Manual"
30 lockf \- apply, test or remove a POSIX lock on an open file
32 .B #include <unistd.h>
34 .BI "int lockf(int " fd ", int " cmd ", off_t " len );
37 Feature Test Macro Requirements for glibc (see
38 .BR feature_test_macros (7)):
44 _XOPEN_SOURCE\ >=\ 500
45 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
46 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
47 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
51 Apply, test or remove a POSIX lock on a section of an open file.
52 The file is specified by
54 a file descriptor open for writing, the action by
56 and the section consists of byte positions
57 .IR pos .. pos + len \-1
61 .IR pos \- len .. pos \-1
66 is the current file position, and if
68 is zero, the section extends from the current file position to
69 infinity, encompassing the present and future end-of-file positions.
70 In all cases, the section may extend past current end-of-file.
74 is just an interface on top of
77 Many other systems implement
79 in this way, but note that POSIX.1 leaves the relationship between
84 A portable application should probably avoid mixing calls
87 Valid operations are given below:
90 Set an exclusive lock on the specified section of the file.
91 If (part of) this section is already locked, the call
92 blocks until the previous lock is released.
93 If this section overlaps an earlier locked section,
95 File locks are released as soon as the process holding the locks
96 closes some file descriptor for the file.
97 A child process does not inherit these locks.
102 but the call never blocks and returns an error instead if the file is
106 Unlock the indicated section of the file.
107 This may cause a locked section to be split into two locked sections.
110 Test the lock: return 0 if the specified section
111 is unlocked or locked by this process; return \-1, set
116 on some other systems),
117 if another process holds a lock.
119 On success, zero is returned.
120 On error, \-1 is returned, and
122 is set appropriately.
125 .BR EACCES " or " EAGAIN
126 The file is locked and
130 was specified, or the operation is prohibited because the file has
131 been memory-mapped by another process.
135 is not an open file descriptor; or
143 is not a writable file descriptor.
148 and this lock operation would cause a deadlock.
151 An invalid operation was specified in
155 Too many segment locks open, lock table is full.
157 For an explanation of the terms used in this section, see
163 Interface Attribute Value
166 T} Thread safety MT-Safe
169 POSIX.1-2001, POSIX.1-2008, SVr4.
176 .I mandatory-locking.txt
177 in the Linux kernel source directory
178 .IR Documentation/filesystems
179 (on older kernels, these files are directly under the
182 .I mandatory-locking.txt