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