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