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