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