xref: /freebsd/lib/libsys/lockf.3 (revision cc697d22c4a43d6d40744799695a65a1076bf3e8)
1*cc697d22SBrooks Davis.\" $NetBSD: lockf.3,v 1.10 2008/04/30 13:10:50 martin Exp $
2*cc697d22SBrooks Davis.\"
3*cc697d22SBrooks Davis.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4*cc697d22SBrooks Davis.\" All rights reserved.
5*cc697d22SBrooks Davis.\"
6*cc697d22SBrooks Davis.\" This code is derived from software contributed to The NetBSD Foundation
7*cc697d22SBrooks Davis.\" by Klaus Klein and S.P. Zeidler.
8*cc697d22SBrooks Davis.\"
9*cc697d22SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
10*cc697d22SBrooks Davis.\" modification, are permitted provided that the following conditions
11*cc697d22SBrooks Davis.\" are met:
12*cc697d22SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
13*cc697d22SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
14*cc697d22SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
15*cc697d22SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
16*cc697d22SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
17*cc697d22SBrooks Davis.\"
18*cc697d22SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*cc697d22SBrooks Davis.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*cc697d22SBrooks Davis.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*cc697d22SBrooks Davis.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*cc697d22SBrooks Davis.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*cc697d22SBrooks Davis.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*cc697d22SBrooks Davis.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*cc697d22SBrooks Davis.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*cc697d22SBrooks Davis.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*cc697d22SBrooks Davis.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*cc697d22SBrooks Davis.\" POSSIBILITY OF SUCH DAMAGE.
29*cc697d22SBrooks Davis.\"
30*cc697d22SBrooks Davis.Dd September 11, 2013
31*cc697d22SBrooks Davis.Dt LOCKF 3
32*cc697d22SBrooks Davis.Os
33*cc697d22SBrooks Davis.Sh NAME
34*cc697d22SBrooks Davis.Nm lockf
35*cc697d22SBrooks Davis.Nd record locking on files
36*cc697d22SBrooks Davis.Sh LIBRARY
37*cc697d22SBrooks Davis.Lb libc
38*cc697d22SBrooks Davis.Sh SYNOPSIS
39*cc697d22SBrooks Davis.In unistd.h
40*cc697d22SBrooks Davis.Ft int
41*cc697d22SBrooks Davis.Fn lockf "int fd" "int function" "off_t size"
42*cc697d22SBrooks Davis.Sh DESCRIPTION
43*cc697d22SBrooks DavisThe
44*cc697d22SBrooks Davis.Fn lockf
45*cc697d22SBrooks Davisfunction allows sections of a file to be locked with advisory-mode locks.
46*cc697d22SBrooks DavisCalls to
47*cc697d22SBrooks Davis.Fn lockf
48*cc697d22SBrooks Davisfrom other processes which attempt to lock the locked file section will
49*cc697d22SBrooks Daviseither return an error value or block until the section becomes unlocked.
50*cc697d22SBrooks DavisAll the locks for a process are removed when the process terminates.
51*cc697d22SBrooks Davis.Pp
52*cc697d22SBrooks DavisThe argument
53*cc697d22SBrooks Davis.Fa fd
54*cc697d22SBrooks Davisis an open file descriptor.
55*cc697d22SBrooks DavisThe file descriptor must have been opened either for write-only
56*cc697d22SBrooks Davis.Dv ( O_WRONLY )
57*cc697d22SBrooks Davisor read/write
58*cc697d22SBrooks Davis.Dv ( O_RDWR )
59*cc697d22SBrooks Davisoperation.
60*cc697d22SBrooks Davis.Pp
61*cc697d22SBrooks DavisThe
62*cc697d22SBrooks Davis.Fa function
63*cc697d22SBrooks Davisargument is a control value which specifies the action to be taken.
64*cc697d22SBrooks DavisThe permissible values for
65*cc697d22SBrooks Davis.Fa function
66*cc697d22SBrooks Davisare as follows:
67*cc697d22SBrooks Davis.Bl -tag -width F_ULOCKXX -compact -offset indent
68*cc697d22SBrooks Davis.It Sy Function
69*cc697d22SBrooks Davis.Sy Description
70*cc697d22SBrooks Davis.It Dv F_ULOCK
71*cc697d22SBrooks Davisunlock locked sections
72*cc697d22SBrooks Davis.It Dv F_LOCK
73*cc697d22SBrooks Davislock a section for exclusive use
74*cc697d22SBrooks Davis.It Dv F_TLOCK
75*cc697d22SBrooks Davistest and lock a section for exclusive use
76*cc697d22SBrooks Davis.It Dv F_TEST
77*cc697d22SBrooks Davistest a section for locks by other processes
78*cc697d22SBrooks Davis.El
79*cc697d22SBrooks Davis.Pp
80*cc697d22SBrooks Davis.Dv F_ULOCK
81*cc697d22SBrooks Davisremoves locks from a section of the file;
82*cc697d22SBrooks Davis.Dv F_LOCK
83*cc697d22SBrooks Davisand
84*cc697d22SBrooks Davis.Dv F_TLOCK
85*cc697d22SBrooks Davisboth lock a section of a file if the section is available;
86*cc697d22SBrooks Davis.Dv F_TEST
87*cc697d22SBrooks Davisdetects if a lock by another process is present on the specified section.
88*cc697d22SBrooks Davis.Pp
89*cc697d22SBrooks DavisThe
90*cc697d22SBrooks Davis.Fa size
91*cc697d22SBrooks Davisargument is the number of contiguous bytes to be locked or
92*cc697d22SBrooks Davisunlocked.
93*cc697d22SBrooks DavisThe section to be locked or unlocked starts at the current
94*cc697d22SBrooks Davisoffset in the file and extends forward for a positive size or backward
95*cc697d22SBrooks Davisfor a negative size (the preceding bytes up to but not including the
96*cc697d22SBrooks Daviscurrent offset).
97*cc697d22SBrooks DavisHowever, it is not permitted to lock a section that
98*cc697d22SBrooks Davisstarts or extends before the beginning of the file.
99*cc697d22SBrooks DavisIf
100*cc697d22SBrooks Davis.Fa size
101*cc697d22SBrooks Davisis 0, the section from the current offset through the largest possible
102*cc697d22SBrooks Davisfile offset is locked (that is, from the current offset through the
103*cc697d22SBrooks Davispresent or any future end-of-file).
104*cc697d22SBrooks Davis.Pp
105*cc697d22SBrooks DavisThe sections locked with
106*cc697d22SBrooks Davis.Dv F_LOCK
107*cc697d22SBrooks Davisor
108*cc697d22SBrooks Davis.Dv F_TLOCK
109*cc697d22SBrooks Davismay, in whole or in part, contain or be contained by a previously
110*cc697d22SBrooks Davislocked section for the same process.
111*cc697d22SBrooks DavisWhen this occurs, or if adjacent
112*cc697d22SBrooks Davislocked sections would occur, the sections are combined into a single
113*cc697d22SBrooks Davislocked section.
114*cc697d22SBrooks DavisIf the request would cause the number of locks to
115*cc697d22SBrooks Davisexceed a system-imposed limit, the request will fail.
116*cc697d22SBrooks Davis.Pp
117*cc697d22SBrooks Davis.Dv F_LOCK
118*cc697d22SBrooks Davisand
119*cc697d22SBrooks Davis.Dv F_TLOCK
120*cc697d22SBrooks Davisrequests differ only by the action taken if the section is not
121*cc697d22SBrooks Davisavailable.
122*cc697d22SBrooks Davis.Dv F_LOCK
123*cc697d22SBrooks Davisblocks the calling process until the section is available.
124*cc697d22SBrooks Davis.Dv F_TLOCK
125*cc697d22SBrooks Davismakes the function fail if the section is already locked by another
126*cc697d22SBrooks Davisprocess.
127*cc697d22SBrooks Davis.Pp
128*cc697d22SBrooks DavisFile locks are released on first close by the locking process of any
129*cc697d22SBrooks Davisfile descriptor for the file.
130*cc697d22SBrooks Davis.Pp
131*cc697d22SBrooks Davis.Dv F_ULOCK
132*cc697d22SBrooks Davisrequests release (wholly or in part) one or more locked sections
133*cc697d22SBrooks Daviscontrolled by the process.
134*cc697d22SBrooks DavisLocked sections will be unlocked starting
135*cc697d22SBrooks Davisat the current file offset through
136*cc697d22SBrooks Davis.Fa size
137*cc697d22SBrooks Davisbytes or to the end of file if size is 0.
138*cc697d22SBrooks DavisWhen all of a locked section
139*cc697d22SBrooks Davisis not released (that is, when the beginning or end of the area to be
140*cc697d22SBrooks Davisunlocked falls within a locked section), the remaining portions of
141*cc697d22SBrooks Davisthat section are still locked by the process.
142*cc697d22SBrooks DavisReleasing the center
143*cc697d22SBrooks Davisportion of a locked section will cause the remaining locked beginning
144*cc697d22SBrooks Davisand end portions to become two separate locked sections.
145*cc697d22SBrooks DavisIf the
146*cc697d22SBrooks Davisrequest would cause the number of locks in the system to exceed a
147*cc697d22SBrooks Davissystem-imposed limit, the request will fail.
148*cc697d22SBrooks Davis.Pp
149*cc697d22SBrooks DavisAn
150*cc697d22SBrooks Davis.Dv F_ULOCK
151*cc697d22SBrooks Davisrequest in which size is non-zero and the offset of the last byte of
152*cc697d22SBrooks Davisthe requested section is the maximum value for an object of type
153*cc697d22SBrooks Davisoff_t, when the process has an existing lock in which size is 0 and
154*cc697d22SBrooks Daviswhich includes the last byte of the requested section, will be treated
155*cc697d22SBrooks Davisas a request to unlock from the start of the requested section with a
156*cc697d22SBrooks Davissize equal to 0.
157*cc697d22SBrooks DavisOtherwise an
158*cc697d22SBrooks Davis.Dv F_ULOCK
159*cc697d22SBrooks Davisrequest will attempt to unlock only the requested section.
160*cc697d22SBrooks Davis.Pp
161*cc697d22SBrooks DavisA potential for deadlock occurs if a process controlling a locked
162*cc697d22SBrooks Davisregion is put to sleep by attempting to lock the locked region of
163*cc697d22SBrooks Davisanother process.
164*cc697d22SBrooks DavisThis implementation detects that sleeping until a
165*cc697d22SBrooks Davislocked region is unlocked would cause a deadlock and fails with an
166*cc697d22SBrooks Davis.Er EDEADLK
167*cc697d22SBrooks Daviserror.
168*cc697d22SBrooks Davis.Pp
169*cc697d22SBrooks DavisThe
170*cc697d22SBrooks Davis.Fn lockf ,
171*cc697d22SBrooks Davis.Xr fcntl 2 ,
172*cc697d22SBrooks Davisand
173*cc697d22SBrooks Davis.Xr flock 2
174*cc697d22SBrooks Davislocks are compatible.
175*cc697d22SBrooks DavisProcesses using different locking interfaces can cooperate
176*cc697d22SBrooks Davisover the same file safely.
177*cc697d22SBrooks DavisHowever, only one of such interfaces should be used within
178*cc697d22SBrooks Davisthe same process.
179*cc697d22SBrooks DavisIf a file is locked by a process through
180*cc697d22SBrooks Davis.Xr flock 2 ,
181*cc697d22SBrooks Davisany record within the file will be seen as locked
182*cc697d22SBrooks Davisfrom the viewpoint of another process using
183*cc697d22SBrooks Davis.Xr fcntl 2
184*cc697d22SBrooks Davisor
185*cc697d22SBrooks Davis.Fn lockf ,
186*cc697d22SBrooks Davisand vice versa.
187*cc697d22SBrooks Davis.Pp
188*cc697d22SBrooks DavisBlocking on a section is interrupted by any signal.
189*cc697d22SBrooks Davis.Sh RETURN VALUES
190*cc697d22SBrooks Davis.Rv -std lockf
191*cc697d22SBrooks DavisIn the case of a failure, existing locks are not changed.
192*cc697d22SBrooks Davis.Sh ERRORS
193*cc697d22SBrooks DavisThe
194*cc697d22SBrooks Davis.Fn lockf
195*cc697d22SBrooks Davisfunction
196*cc697d22SBrooks Daviswill fail if:
197*cc697d22SBrooks Davis.Bl -tag -width Er
198*cc697d22SBrooks Davis.It Bq Er EAGAIN
199*cc697d22SBrooks DavisThe argument
200*cc697d22SBrooks Davis.Fa function
201*cc697d22SBrooks Davisis
202*cc697d22SBrooks Davis.Dv F_TLOCK
203*cc697d22SBrooks Davisor
204*cc697d22SBrooks Davis.Dv F_TEST
205*cc697d22SBrooks Davisand the section is already locked by another process.
206*cc697d22SBrooks Davis.It Bq Er EBADF
207*cc697d22SBrooks DavisThe argument
208*cc697d22SBrooks Davis.Fa fd
209*cc697d22SBrooks Davisis not a valid open file descriptor.
210*cc697d22SBrooks Davis.Pp
211*cc697d22SBrooks DavisThe argument
212*cc697d22SBrooks Davis.Fa function
213*cc697d22SBrooks Davisis
214*cc697d22SBrooks Davis.Dv F_LOCK
215*cc697d22SBrooks Davisor
216*cc697d22SBrooks Davis.Dv F_TLOCK ,
217*cc697d22SBrooks Davisand
218*cc697d22SBrooks Davis.Fa fd
219*cc697d22SBrooks Davisis not a valid file descriptor open for writing.
220*cc697d22SBrooks Davis.It Bq Er EDEADLK
221*cc697d22SBrooks DavisThe argument
222*cc697d22SBrooks Davis.Fa function
223*cc697d22SBrooks Davisis
224*cc697d22SBrooks Davis.Dv F_LOCK
225*cc697d22SBrooks Davisand a deadlock is detected.
226*cc697d22SBrooks Davis.It Bq Er EINTR
227*cc697d22SBrooks DavisThe argument
228*cc697d22SBrooks Davis.Fa function
229*cc697d22SBrooks Davisis F_LOCK
230*cc697d22SBrooks Davisand
231*cc697d22SBrooks Davis.Fn lockf
232*cc697d22SBrooks Daviswas interrupted by the delivery of a signal.
233*cc697d22SBrooks Davis.It Bq Er EINVAL
234*cc697d22SBrooks DavisThe argument
235*cc697d22SBrooks Davis.Fa function
236*cc697d22SBrooks Davisis not one of
237*cc697d22SBrooks Davis.Dv F_ULOCK ,
238*cc697d22SBrooks Davis.Dv F_LOCK ,
239*cc697d22SBrooks Davis.Dv F_TLOCK
240*cc697d22SBrooks Davisor
241*cc697d22SBrooks Davis.Dv F_TEST .
242*cc697d22SBrooks Davis.Pp
243*cc697d22SBrooks DavisThe argument
244*cc697d22SBrooks Davis.Fa fd
245*cc697d22SBrooks Davisrefers to a file that does not support locking.
246*cc697d22SBrooks Davis.It Bq Er ENOLCK
247*cc697d22SBrooks DavisThe argument
248*cc697d22SBrooks Davis.Fa function
249*cc697d22SBrooks Davisis
250*cc697d22SBrooks Davis.Dv F_ULOCK ,
251*cc697d22SBrooks Davis.Dv F_LOCK
252*cc697d22SBrooks Davisor
253*cc697d22SBrooks Davis.Dv F_TLOCK ,
254*cc697d22SBrooks Davisand satisfying the lock or unlock request would result in the number
255*cc697d22SBrooks Davisof locked regions in the system exceeding a system-imposed limit.
256*cc697d22SBrooks Davis.El
257*cc697d22SBrooks Davis.Sh SEE ALSO
258*cc697d22SBrooks Davis.Xr fcntl 2 ,
259*cc697d22SBrooks Davis.Xr flock 2
260*cc697d22SBrooks Davis.Sh STANDARDS
261*cc697d22SBrooks DavisThe
262*cc697d22SBrooks Davis.Fn lockf
263*cc697d22SBrooks Davisfunction conforms to
264*cc697d22SBrooks Davis.St -xpg4.2 .
265