1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)flock.2 8.2 (Berkeley) 12/11/93 29.\" 30.Dd November 9, 2011 31.Dt FLOCK 2 32.Os 33.Sh NAME 34.Nm flock 35.Nd "apply or remove an advisory lock on an open file" 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In sys/file.h 40.Fd "#define LOCK_SH 0x01 /* shared file lock */" 41.Fd "#define LOCK_EX 0x02 /* exclusive file lock */" 42.Fd "#define LOCK_NB 0x04 /* do not block when locking */" 43.Fd "#define LOCK_UN 0x08 /* unlock file */" 44.Ft int 45.Fn flock "int fd" "int operation" 46.Sh DESCRIPTION 47The 48.Fn flock 49system call applies or removes an 50.Em advisory 51lock on the file associated with the file descriptor 52.Fa fd . 53A lock is applied by specifying an 54.Fa operation 55argument that is one of 56.Dv LOCK_SH 57or 58.Dv LOCK_EX 59with the optional addition of 60.Dv LOCK_NB . 61To unlock 62an existing lock 63.Dv operation 64should be 65.Dv LOCK_UN . 66.Pp 67Advisory locks allow cooperating processes to perform 68consistent operations on files, but do not guarantee 69consistency (i.e., processes may still access files 70without using advisory locks possibly resulting in 71inconsistencies). 72.Pp 73The locking mechanism allows two types of locks: 74.Em shared 75locks and 76.Em exclusive 77locks. 78At any time multiple shared locks may be applied to a file, 79but at no time are multiple exclusive, or both shared and exclusive, 80locks allowed simultaneously on a file. 81.Pp 82A shared lock may be 83.Em upgraded 84to an exclusive lock, and vice versa, simply by specifying 85the appropriate lock type; this results in the previous 86lock being released and the new lock applied (possibly 87after other processes have gained and released the lock). 88.Pp 89Requesting a lock on an object that is already locked 90normally causes the caller to be blocked until the lock may be 91acquired. 92If 93.Dv LOCK_NB 94is included in 95.Fa operation , 96then this will not happen; instead the call will fail and 97the error 98.Er EWOULDBLOCK 99will be returned. 100.Sh NOTES 101Locks are on files, not file descriptors. 102That is, file descriptors 103duplicated through 104.Xr dup 2 105or 106.Xr fork 2 107do not result in multiple instances of a lock, but rather multiple 108references to a single lock. 109If a process holding a lock on a file 110forks and the child explicitly unlocks the file, the parent will 111lose its lock. 112.Pp 113The 114.Fn flock , 115.Xr fcntl 2 , 116and 117.Xr lockf 3 118locks are compatible. 119Processes using different locking interfaces can cooperate 120over the same file safely. 121However, only one of such interfaces should be used within 122the same process. 123If a file is locked by a process through 124.Fn flock , 125any record within the file will be seen as locked 126from the viewpoint of another process using 127.Xr fcntl 2 128or 129.Xr lockf 3 , 130and vice versa. 131.Pp 132Processes blocked awaiting a lock may be awakened by signals. 133.Sh RETURN VALUES 134.Rv -std flock 135.Sh ERRORS 136The 137.Fn flock 138system call fails if: 139.Bl -tag -width Er 140.It Bq Er EWOULDBLOCK 141The file is locked and the 142.Dv LOCK_NB 143option was specified. 144.It Bq Er EBADF 145The argument 146.Fa fd 147is an invalid descriptor. 148.It Bq Er EINVAL 149The argument 150.Fa fd 151refers to an object other than a file. 152.It Bq Er EOPNOTSUPP 153The argument 154.Fa fd 155refers to an object that does not support file locking. 156.It Bq Er ENOLCK 157A lock was requested, but no locks are available. 158.El 159.Sh SEE ALSO 160.Xr close 2 , 161.Xr dup 2 , 162.Xr execve 2 , 163.Xr fcntl 2 , 164.Xr fork 2 , 165.Xr open 2 , 166.Xr flopen 3 , 167.Xr lockf 3 168.Sh HISTORY 169The 170.Fn flock 171system call appeared in 172.Bx 4.2 . 173