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