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