1.\" Copyright (c) 1983, 1991 The Regents of the University of California. 2.\" 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. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)flock.2 6.7 (Berkeley) 3/10/91 33.\" 34.Dd March 10, 1991 35.Dt FLOCK 2 36.Os BSD 4.2 37.Sh NAME 38.Nm flock 39.Nd "apply or remove an advisory lock on an open file" 40.Sh SYNOPSIS 41.Fd #include <sys/file.h> 42.Fd #define LOCK_SH 1 /* shared lock */ 43.Fd #define LOCK_EX 2 /* exclusive lock */ 44.Fd #define LOCK_NB 4 /* don't block when locking */ 45.Fd #define LOCK_UN 8 /* unlock */ 46.Ft int 47.Fn flock "int fd" "int operation" 48.Sh DESCRIPTION 49.Fn Flock 50applies or removes an 51.Em advisory 52lock on the file associated with the file descriptor 53.Fa fd . 54A lock is applied by specifying an 55.Fa operation 56parameter that is the inclusive or of 57.Dv LOCK_SH 58or 59.Dv LOCK_EX 60and, possibly, 61.Dv LOCK_NB . 62To unlock 63an existing lock 64.Dv operation 65should be 66.Dv LOCK_UN . 67.Pp 68Advisory locks allow cooperating processes to perform 69consistent operations on files, but do not guarantee 70consistency (i.e., processes may still access files 71without using advisory locks possibly resulting in 72inconsistencies). 73.Pp 74The locking mechanism allows two types of locks: 75.Em shared 76locks and 77.Em exclusive 78locks. 79At any time multiple shared locks may be applied to a file, 80but at no time are multiple exclusive, or both shared and exclusive, 81locks allowed simultaneously on a file. 82.Pp 83A shared lock may be 84.Em upgraded 85to an exclusive lock, and vice versa, simply by specifying 86the appropriate lock type; this results in the previous 87lock being released and the new lock applied (possibly 88after other processes have gained and released the lock). 89.Pp 90Requesting a lock on an object that is already locked 91normally causes the caller to be blocked until the lock may be 92acquired. If 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. That is, file descriptors 102duplicated through 103.Xr dup 2 104or 105.Xr fork 2 106do not result in multiple instances of a lock, but rather multiple 107references to a single lock. If a process holding a lock on a file 108forks and the child explicitly unlocks the file, the parent will 109lose its lock. 110.Pp 111Processes blocked awaiting a lock may be awakened by signals. 112.Sh RETURN VALUES 113Zero is returned if the operation was successful; 114on an error a -1 is returned and an error code is left in 115the global location 116.Va errno . 117.Sh ERRORS 118The 119.Fn flock 120call fails if: 121.Bl -tag -width EWOULDBLOCKAA 122.It Bq Er EWOULDBLOCK 123The file is locked and the 124.Dv LOCK_NB 125option was specified. 126.It Bq Er EBADF 127The argument 128.Fa fd 129is an invalid descriptor. 130.It Bq Er EINVAL 131The argument 132.Fa fd 133refers to an object other than a file. 134.El 135.Sh SEE ALSO 136.Xr open 2 , 137.Xr close 2 , 138.Xr dup 2 , 139.Xr execve 2 , 140.Xr fork 2 141.Sh HISTORY 142The 143.Nm 144function call appeared in 145.Bx 4.2 . 146