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