1.\" $OpenBSD: flock.2,v 1.16 2007/05/31 19:19:32 jmc 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: May 31 2007 $ 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.Fd #include <fcntl.h> 41.Pp 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 an existing lock, 63.Fa 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 normally causes 90the caller to be blocked until the lock may be acquired. 91If 92.Dv LOCK_NB 93is included in 94.Fa operation , 95then this will not happen; instead the call will fail and the error 96.Er EWOULDBLOCK 97will be returned. 98.Sh NOTES 99Locks are on files, not file descriptors. 100That is, file descriptors duplicated through 101.Xr dup 2 102or 103.Xr fork 2 104do not result in multiple instances of a lock, but rather multiple 105references to a single lock. 106If a process holding a lock on a file forks and the child explicitly 107unlocks the file, the parent will lose its lock. 108.Pp 109Processes blocked awaiting a lock may be awakened by signals. 110.Sh RETURN VALUES 111Zero is returned if the operation was successful; 112on an error a \-1 is returned and an error code is left in 113the global location 114.Va errno . 115.Sh ERRORS 116The 117.Fn flock 118call fails if: 119.Bl -tag -width Er 120.It Bq Er EWOULDBLOCK 121The file is locked and the 122.Dv LOCK_NB 123option was specified. 124.It Bq Er EBADF 125The argument 126.Fa fd 127is an invalid descriptor. 128.It Bq Er EINVAL 129The argument 130.Fa operation 131has an invalid value. 132.It Bq Er EOPNOTSUPP 133The referenced descriptor is not of the correct type. 134.El 135.Sh SEE ALSO 136.Xr close 2 , 137.Xr dup 2 , 138.Xr execve 2 , 139.Xr fcntl 2 , 140.Xr fork 2 , 141.Xr open 2 142.Sh HISTORY 143The 144.Fn flock 145function call appeared in 146.Bx 4.2 . 147