1 .\" $NetBSD: lockf.3,v 1.12 2011/10/15 21:35:49 rmind Exp $
3 .\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Klaus Klein and S.P. Zeidler.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd record locking on files
41 .Fn lockf "int filedes" "int function" "off_t size"
45 function allows sections of a file to be locked with advisory-mode locks.
48 from other processes which attempt to lock the locked file section will
49 either return an error value or block until the section becomes unlocked.
50 All the locks for a process are removed when the process terminates.
54 is an open file descriptor.
55 The file descriptor must have been opened either for write-only
63 argument is a control value which specifies the action to be taken.
64 The permissible values for
68 .Bl -tag -width F_ULOCKXX -compact -offset indent
72 unlock locked sections
74 lock a section for exclusive use
76 test and lock a section for exclusive use
78 test a section for locks by other processes
82 removes locks from a section of the file;
86 both lock a section of a file if the section is available;
88 detects if a lock by another process is present on the specified section.
92 argument is the number of contiguous bytes to be locked or
94 The section to be locked or unlocked starts at the current
95 offset in the file and extends forward for a positive size or backward
96 for a negative size (the preceding bytes up to but not including the
98 However, it is not permitted to lock a section that
99 starts or extends before the beginning of the file.
102 is 0, the section from the current offset through the largest possible
103 file offset is locked (that is, from the current offset through the
104 present or any future end-of-file).
106 The sections locked with
110 may, in whole or in part, contain or be contained by a previously
111 locked section for the same process.
112 When this occurs, or if adjacent
113 locked sections would occur, the sections are combined into a single
115 If the request would cause the number of locks to
116 exceed a system-imposed limit, the request will fail.
121 requests differ only by the action taken if the section is not
124 blocks the calling process until the section is available.
126 makes the function fail if the section is already locked by another
129 File locks are released on first close by the locking process of any
130 file descriptor for the file.
133 requests release (wholly or in part) one or more locked sections
134 controlled by the process.
135 Locked sections will be unlocked starting
136 at the current file offset through
138 bytes or to the end of file if size is 0.
139 When all of a locked section
140 is not released (that is, when the beginning or end of the area to be
141 unlocked falls within a locked section), the remaining portions of
142 that section are still locked by the process.
144 portion of a locked section will cause the remaining locked beginning
145 and end portions to become two separate locked sections.
147 request would cause the number of locks in the system to exceed a
148 system-imposed limit, the request will fail.
152 request in which size is non-zero and the offset of the last byte of
153 the requested section is the maximum value for an object of type
154 off_t, when the process has an existing lock in which size is 0 and
155 which includes the last byte of the requested section, will be treated
156 as a request to unlock from the start of the requested section with a
160 request will attempt to unlock only the requested section.
162 A potential for deadlock occurs if a process controlling a locked
163 region is put to sleep by attempting to lock the locked region of
165 This implementation detects that sleeping until a
166 locked region is unlocked would cause a deadlock and fails with an
174 locks may be safely used concurrently.
176 Blocking on a section is interrupted by any signal.
181 Otherwise, it returns \-1, sets
183 to indicate an error, and existing locks are not changed.
195 and the section is already locked by another process.
199 is not a valid open file descriptor.
209 is not a valid file descriptor open for writing.
215 and a deadlock is detected.
222 was interrupted by the delivery of a signal.
235 refers to a file that does not support locking.
244 and satisfying the lock or unlock request would result in the number
245 of locked regions in the system exceeding a system-imposed limit.
261 function first appeared in