1.\" $NetBSD: flock.2,v 1.25 2022/12/04 11:18:58 uwe Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)flock.2 8.2 (Berkeley) 12/11/93 31.\" 32.Dd October 15, 2011 33.Dt FLOCK 2 34.Os 35.Sh NAME 36.Nm flock 37.Nd "apply or remove an advisory lock on an open file" 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In fcntl.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 one of 57.Dv LOCK_SH 58or 59.Dv LOCK_EX 60with the optional addition of 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. 93If 94.Dv LOCK_NB 95is included in 96.Fa operation , 97then this will not happen; instead the call will fail and 98the error 99.Er EAGAIN 100will be returned. 101.Sh NOTES 102Locks are on files, not file descriptors. 103That is, file descriptors duplicated 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 113Processes blocked awaiting a lock may be awakened by signals. 114.Sh RETURN VALUES 115Zero is returned if the operation was successful; 116on an error a \-1 is returned and an error code is left in 117the global location 118.Va errno . 119.Sh ERRORS 120The 121.Fn flock 122call fails if: 123.Bl -tag -width Er 124.It Bq Er EAGAIN 125The file is locked and the 126.Dv LOCK_NB 127option was specified. 128.It Bq Er EBADF 129The argument 130.Fa fd 131is an invalid descriptor. 132.It Bq Er EINVAL 133The argument 134.Fa operation 135does not include exactly one of 136.Dv LOCK_EX , 137.Dv LOCK_SH , 138or 139.Dv LOCK_UN . 140.It Bq Er ENOMEM 141The file lock limit for the current unprivileged user 142has been reached. 143It can be modified using the 144.Li kern.maxfiles 145.Xr sysctl 7 . 146.It Bq Er EOPNOTSUPP 147The argument 148.Fa fd 149refers to an object other than a file. 150.El 151.Sh SEE ALSO 152.Xr close 2 , 153.Xr dup 2 , 154.Xr execve 2 , 155.Xr fork 2 , 156.Xr open 2 , 157.Xr flockfile 3 , 158.Xr lockf 3 159.Sh HISTORY 160The 161.Fn flock 162function call appeared in 163.Bx 4.2 . 164