1.\" $NetBSD: lockf.3,v 1.8 2002/10/01 16:59:47 wiz 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. 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 124.Dv F_LOCK 125and 126.Dv F_TLOCK 127requests differ only by the action taken if the section is not 128available. 129.Dv F_LOCK 130blocks the calling process until the section is available. 131.Dv F_TLOCK 132makes the function fail if the section is already locked by another 133process. 134.Pp 135File locks are released on first close by the locking process of any 136file descriptor for the file. 137.Pp 138.Dv F_ULOCK 139requests release (wholly or in part) one or more locked sections 140controlled by the process. 141Locked sections will be unlocked starting 142at the current file offset through 143.Fa size 144bytes or to the end of file if size is 0. 145When all of a locked section 146is not released (that is, when the beginning or end of the area to be 147unlocked falls within a locked section), the remaining portions of 148that section are still locked by the process. 149Releasing the center 150portion of a locked section will cause the remaining locked beginning 151and end portions to become two separate locked sections. 152If the 153request would cause the number of locks in the system to exceed a 154system-imposed limit, the request will fail. 155.Pp 156An 157.Dv F_ULOCK 158request in which size is non-zero and the offset of the last byte of 159the requested section is the maximum value for an object of type 160off_t, when the process has an existing lock in which size is 0 and 161which includes the last byte of the requested section, will be treated 162as a request to unlock from the start of the requested section with a 163size equal to 0. 164Otherwise an 165.Dv F_ULOCK 166request will attempt to unlock only the requested section. 167.Pp 168A potential for deadlock occurs if a process controlling a locked 169region is put to sleep by attempting to lock the locked region of 170another process. 171This implementation detects that sleeping until a 172locked region is unlocked would cause a deadlock and fails with an 173.Er EDEADLK 174error. 175.Pp 176.Fn lockf , 177.Xr fcntl 2 178and 179.Xr flock 2 180locks may be safely used concurrently. 181.Pp 182Blocking on a section is interrupted by any signal. 183.Sh RETURN VALUES 184If successful, the 185.Fn lockf 186function returns 0. 187Otherwise, it returns \-1, sets 188.Va errno 189to indicate an error, and existing locks are not changed. 190.Sh ERRORS 191.Fn lockf 192will fail if: 193.Bl -tag -width Er 194.It Bq Er EAGAIN 195The argument 196.Fa function 197is 198.Dv F_TLOCK 199or 200.Dv F_TEST 201and the section is already locked by another process. 202.It Bq Er EBADF 203The argument 204.Fa filedes 205is not a valid open file descriptor. 206.Pp 207The argument 208.Fa function 209is 210.Dv F_LOCK 211or 212.Dv F_TLOCK , 213and 214.Fa filedes 215is not a valid file descriptor open for writing. 216.It Bq Er EDEADLK 217The argument 218.Fa function 219is 220.Dv F_LOCK 221and a deadlock is detected. 222.It Bq Er EINTR 223The argument 224.Fa function 225is F_LOCK 226and 227.Fn lockf 228was interrupted by the delivery of a signal. 229.It Bq Er EINVAL 230The argument 231.Fa function 232is not one of 233.Dv F_ULOCK , 234.Dv F_LOCK , 235.Dv F_TLOCK 236or 237.Dv F_TEST . 238.Pp 239The argument 240.Fa filedes 241refers to a file that does not support locking. 242.It Bq Er ENOLCK 243The argument 244.Fa function 245is 246.Dv F_ULOCK , 247.Dv F_LOCK 248or 249.Dv F_TLOCK , 250and satisfying the lock or unlock request would result in the number 251of locked regions in the system exceeding a system-imposed limit. 252.El 253.Sh SEE ALSO 254.Xr fcntl 2 , 255.Xr flock 2 256.Sh STANDARDS 257The 258.Fn lockf 259function conforms to 260.St -xpg4.2 . 261