xref: /netbsd/lib/libc/gen/lockf.3 (revision bf9ec67e)
1.\" $NetBSD: lockf.3,v 1.6 2002/02/07 07:00:15 ross 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.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd December 19, 1997
38.Dt LOCKF 3
39.Os
40.Sh NAME
41.Nm lockf
42.Nd record locking on files
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.Fd #include \*[Lt]unistd.h\*[Gt]
47.Ft int
48.Fn lockf "int filedes" "int function" "off_t size"
49.Sh DESCRIPTION
50The
51.Fn lockf
52function allows sections of a file to be locked with advisory-mode locks.
53Calls to
54.Fn lockf
55from other processes which attempt to lock the locked file section will
56either return an error value or block until the section becomes unlocked.
57All the locks for a process are removed when the process terminates.
58.Pp
59The argument
60.Fa filedes
61is an open file descriptor.
62The file descriptor must have been opened either for write-only
63.Dv ( O_WRONLY )
64or read/write
65.Dv ( O_RDWR )
66operation.
67.Pp
68The
69.Fa function
70argument is a control value which specifies the action to be taken.
71The permissible values for
72.Fa function
73are as follows:
74.Bl -tag -width F_ULOCKXX -compact -offset indent
75.It Sy Function
76.Sy Description
77.It Dv F_ULOCK
78unlock locked sections
79.It Dv F_LOCK
80lock a section for exclusive use
81.It Dv F_TLOCK
82test and lock a section for exclusive use
83.It Dv F_TEST
84test a section for locks by other processes
85.El
86.Pp
87.Dv F_ULOCK
88removes locks from a section of the file;
89.Dv F_LOCK
90and
91.Dv F_TLOCK
92both lock a section of a file if the section is available;
93.Dv F_TEST
94detects if a lock by another process is present on the specified section.
95.Pp
96The
97.Fa size
98argument is the number of contiguous bytes to be locked or
99unlocked. The section to be locked or unlocked starts at the current
100offset in the file and extends forward for a positive size or backward
101for a negative size (the preceding bytes up to but not including the
102current offset). However, it is not permitted to lock a section that
103starts or extends before the beginning of the file.
104If
105.Fa size
106is 0, the section from the current offset through the largest possible
107file offset is locked (that is, from the current offset through the
108present or any future end-of-file).
109.Pp
110The sections locked with
111.Dv F_LOCK
112or
113.Dv F_TLOCK
114may, in whole or in part, contain or be contained by a previously
115locked section for the same process. When this occurs, or if adjacent
116locked sections would occur, the sections are combined into a single
117locked section. If the request would cause the number of locks to
118exceed a system-imposed limit, the request will fail.
119.Pp
120.Dv F_LOCK
121and
122.Dv F_TLOCK
123requests differ only by the action taken if the section is not
124available.
125.Dv F_LOCK
126blocks the calling process until the section is available.
127.Dv F_TLOCK
128makes the function fail if the section is already locked by another
129process.
130.Pp
131File locks are released on first close by the locking process of any
132file descriptor for the file.
133.Pp
134.Dv F_ULOCK
135requests release (wholly or in part) one or more locked sections
136controlled by the process. Locked sections will be unlocked starting
137at the current file offset through
138.Fa size
139bytes or to the end of file if size is 0. When all of a locked section
140is not released (that is, when the beginning or end of the area to be
141unlocked falls within a locked section), the remaining portions of
142that section are still locked by the process. Releasing the center
143portion of a locked section will cause the remaining locked beginning
144and end portions to become two separate locked sections. If the
145request would cause the number of locks in the system to exceed a
146system-imposed limit, the request will fail.
147.Pp
148An
149.Dv F_ULOCK
150request in which size is non-zero and the offset of the last byte of
151the requested section is the maximum value for an object of type
152off_t, when the process has an existing lock in which size is 0 and
153which includes the last byte of the requested section, will be treated
154as a request to unlock from the start of the requested section with a
155size equal to 0. Otherwise an
156.Dv F_ULOCK
157request will attempt to unlock only the requested section.
158.Pp
159A potential for deadlock occurs if a process controlling a locked
160region is put to sleep by attempting to lock the locked region of
161another process.  This implementation detects that sleeping until a
162locked region is unlocked would cause a deadlock and fails with an
163.Er EDEADLK
164error.
165.Pp
166.Fn lockf ,
167.Xr fcntl 2
168and
169.Xr flock 2
170locks may be safely used concurrently.
171.Pp
172Blocking on a section is interrupted by any signal.
173.Sh RETURN VALUES
174If successful, the
175.Fn lockf
176function returns 0.
177Otherwise, it returns -1, sets
178.Va errno
179to indicate an error, and existing locks are not changed.
180.Sh ERRORS
181.Fn lockf
182will fail if:
183.Bl -tag -width Er
184.It Bq Er EAGAIN
185The argument
186.Fa function
187is
188.Dv F_TLOCK
189or
190.Dv F_TEST
191and the section is already locked by another process.
192.It Bq Er EBADF
193The argument
194.Fa filedes
195is not a valid open file descriptor.
196.Pp
197The argument
198.Fa function
199is
200.Dv F_LOCK
201or
202.Dv F_TLOCK ,
203and
204.Fa filedes
205is not a valid file descriptor open for writing.
206.It Bq Er EDEADLK
207The argument
208.Fa function
209is
210.Dv F_LOCK
211and a deadlock is detected.
212.It Bq Er EINTR
213The argument
214.Fa function
215is F_LOCK
216and
217.Fn lockf
218was interrupted by the delivery of a signal.
219.It Bq Er EINVAL
220The argument
221.Fa function
222is not one of
223.Dv F_ULOCK ,
224.Dv F_LOCK ,
225.Dv F_TLOCK
226or
227.Dv F_TEST .
228.Pp
229The argument
230.Fa filedes
231refers to a file that does not support locking.
232.It Bq Er ENOLCK
233The argument
234.Fa function
235is
236.Dv F_ULOCK ,
237.Dv F_LOCK
238or
239.Dv F_TLOCK ,
240and satisfying the lock or unlock request would result in the number
241of locked regions in the system exceeding a system-imposed limit.
242.El
243.Sh SEE ALSO
244.Xr fcntl 2 ,
245.Xr flock 2
246.Sh STANDARDS
247The
248.Fn lockf
249function conforms to
250.St -xpg4.2 .
251