xref: /freebsd/lib/libsys/lockf.3 (revision 1edb7116)
1.\" $NetBSD: lockf.3,v 1.10 2008/04/30 13:10:50 martin Exp $
2.\"
3.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Klaus Klein and S.P. Zeidler.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\"
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.
29.\"
30.Dd September 11, 2013
31.Dt LOCKF 3
32.Os
33.Sh NAME
34.Nm lockf
35.Nd record locking on files
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In unistd.h
40.Ft int
41.Fn lockf "int fd" "int function" "off_t size"
42.Sh DESCRIPTION
43The
44.Fn lockf
45function allows sections of a file to be locked with advisory-mode locks.
46Calls to
47.Fn lockf
48from other processes which attempt to lock the locked file section will
49either return an error value or block until the section becomes unlocked.
50All the locks for a process are removed when the process terminates.
51.Pp
52The argument
53.Fa fd
54is an open file descriptor.
55The file descriptor must have been opened either for write-only
56.Dv ( O_WRONLY )
57or read/write
58.Dv ( O_RDWR )
59operation.
60.Pp
61The
62.Fa function
63argument is a control value which specifies the action to be taken.
64The permissible values for
65.Fa function
66are as follows:
67.Bl -tag -width F_ULOCKXX -compact -offset indent
68.It Sy Function
69.Sy Description
70.It Dv F_ULOCK
71unlock locked sections
72.It Dv F_LOCK
73lock a section for exclusive use
74.It Dv F_TLOCK
75test and lock a section for exclusive use
76.It Dv F_TEST
77test a section for locks by other processes
78.El
79.Pp
80.Dv F_ULOCK
81removes locks from a section of the file;
82.Dv F_LOCK
83and
84.Dv F_TLOCK
85both lock a section of a file if the section is available;
86.Dv F_TEST
87detects if a lock by another process is present on the specified section.
88.Pp
89The
90.Fa size
91argument is the number of contiguous bytes to be locked or
92unlocked.
93The section to be locked or unlocked starts at the current
94offset in the file and extends forward for a positive size or backward
95for a negative size (the preceding bytes up to but not including the
96current offset).
97However, it is not permitted to lock a section that
98starts or extends before the beginning of the file.
99If
100.Fa size
101is 0, the section from the current offset through the largest possible
102file offset is locked (that is, from the current offset through the
103present or any future end-of-file).
104.Pp
105The sections locked with
106.Dv F_LOCK
107or
108.Dv F_TLOCK
109may, in whole or in part, contain or be contained by a previously
110locked section for the same process.
111When this occurs, or if adjacent
112locked sections would occur, the sections are combined into a single
113locked section.
114If the request would cause the number of locks to
115exceed a system-imposed limit, the request will fail.
116.Pp
117.Dv F_LOCK
118and
119.Dv F_TLOCK
120requests differ only by the action taken if the section is not
121available.
122.Dv F_LOCK
123blocks the calling process until the section is available.
124.Dv F_TLOCK
125makes the function fail if the section is already locked by another
126process.
127.Pp
128File locks are released on first close by the locking process of any
129file descriptor for the file.
130.Pp
131.Dv F_ULOCK
132requests release (wholly or in part) one or more locked sections
133controlled by the process.
134Locked sections will be unlocked starting
135at the current file offset through
136.Fa size
137bytes or to the end of file if size is 0.
138When all of a locked section
139is not released (that is, when the beginning or end of the area to be
140unlocked falls within a locked section), the remaining portions of
141that section are still locked by the process.
142Releasing the center
143portion of a locked section will cause the remaining locked beginning
144and end portions to become two separate locked sections.
145If the
146request would cause the number of locks in the system to exceed a
147system-imposed limit, the request will fail.
148.Pp
149An
150.Dv F_ULOCK
151request in which size is non-zero and the offset of the last byte of
152the requested section is the maximum value for an object of type
153off_t, when the process has an existing lock in which size is 0 and
154which includes the last byte of the requested section, will be treated
155as a request to unlock from the start of the requested section with a
156size equal to 0.
157Otherwise an
158.Dv F_ULOCK
159request will attempt to unlock only the requested section.
160.Pp
161A potential for deadlock occurs if a process controlling a locked
162region is put to sleep by attempting to lock the locked region of
163another process.
164This implementation detects that sleeping until a
165locked region is unlocked would cause a deadlock and fails with an
166.Er EDEADLK
167error.
168.Pp
169The
170.Fn lockf ,
171.Xr fcntl 2 ,
172and
173.Xr flock 2
174locks are compatible.
175Processes using different locking interfaces can cooperate
176over the same file safely.
177However, only one of such interfaces should be used within
178the same process.
179If a file is locked by a process through
180.Xr flock 2 ,
181any record within the file will be seen as locked
182from the viewpoint of another process using
183.Xr fcntl 2
184or
185.Fn lockf ,
186and vice versa.
187.Pp
188Blocking on a section is interrupted by any signal.
189.Sh RETURN VALUES
190.Rv -std lockf
191In the case of a failure, existing locks are not changed.
192.Sh ERRORS
193The
194.Fn lockf
195function
196will fail if:
197.Bl -tag -width Er
198.It Bq Er EAGAIN
199The argument
200.Fa function
201is
202.Dv F_TLOCK
203or
204.Dv F_TEST
205and the section is already locked by another process.
206.It Bq Er EBADF
207The argument
208.Fa fd
209is not a valid open file descriptor.
210.Pp
211The argument
212.Fa function
213is
214.Dv F_LOCK
215or
216.Dv F_TLOCK ,
217and
218.Fa fd
219is not a valid file descriptor open for writing.
220.It Bq Er EDEADLK
221The argument
222.Fa function
223is
224.Dv F_LOCK
225and a deadlock is detected.
226.It Bq Er EINTR
227The argument
228.Fa function
229is F_LOCK
230and
231.Fn lockf
232was interrupted by the delivery of a signal.
233.It Bq Er EINVAL
234The argument
235.Fa function
236is not one of
237.Dv F_ULOCK ,
238.Dv F_LOCK ,
239.Dv F_TLOCK
240or
241.Dv F_TEST .
242.Pp
243The argument
244.Fa fd
245refers to a file that does not support locking.
246.It Bq Er ENOLCK
247The argument
248.Fa function
249is
250.Dv F_ULOCK ,
251.Dv F_LOCK
252or
253.Dv F_TLOCK ,
254and satisfying the lock or unlock request would result in the number
255of locked regions in the system exceeding a system-imposed limit.
256.El
257.Sh SEE ALSO
258.Xr fcntl 2 ,
259.Xr flock 2
260.Sh STANDARDS
261The
262.Fn lockf
263function conforms to
264.St -xpg4.2 .
265