xref: /minix3/lib/libc/sys/flock.2 (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc.\"	$NetBSD: flock.2,v 1.22 2011/10/15 21:35:50 rmind Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     @(#)flock.2	8.2 (Berkeley) 12/11/93
312fe8fb19SBen Gras.\"
32*f14fb602SLionel Sambuc.Dd October 15, 2011
332fe8fb19SBen Gras.Dt FLOCK 2
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm flock
372fe8fb19SBen Gras.Nd "apply or remove an advisory lock on an open file"
382fe8fb19SBen Gras.Sh LIBRARY
392fe8fb19SBen Gras.Lb libc
402fe8fb19SBen Gras.Sh SYNOPSIS
412fe8fb19SBen Gras.In fcntl.h
422fe8fb19SBen Gras.Fd #define	LOCK_SH	1	/* shared lock */
432fe8fb19SBen Gras.Fd #define	LOCK_EX	2	/* exclusive lock */
442fe8fb19SBen Gras.Fd #define	LOCK_NB	4	/* don't block when locking */
452fe8fb19SBen Gras.Fd #define	LOCK_UN	8	/* unlock */
462fe8fb19SBen Gras.Ft int
472fe8fb19SBen Gras.Fn flock "int fd" "int operation"
482fe8fb19SBen Gras.Sh DESCRIPTION
492fe8fb19SBen Gras.Fn flock
502fe8fb19SBen Grasapplies or removes an
512fe8fb19SBen Gras.Em advisory
522fe8fb19SBen Graslock on the file associated with the file descriptor
532fe8fb19SBen Gras.Fa fd .
542fe8fb19SBen GrasA lock is applied by specifying an
552fe8fb19SBen Gras.Fa operation
562fe8fb19SBen Grasparameter that is one of
572fe8fb19SBen Gras.Dv LOCK_SH
582fe8fb19SBen Grasor
592fe8fb19SBen Gras.Dv LOCK_EX
602fe8fb19SBen Graswith the optional addition of
612fe8fb19SBen Gras.Dv LOCK_NB .
622fe8fb19SBen GrasTo unlock
632fe8fb19SBen Grasan existing lock
642fe8fb19SBen Gras.Dv operation
652fe8fb19SBen Grasshould be
662fe8fb19SBen Gras.Dv LOCK_UN .
672fe8fb19SBen Gras.Pp
682fe8fb19SBen GrasAdvisory locks allow cooperating processes to perform
692fe8fb19SBen Grasconsistent operations on files, but do not guarantee
702fe8fb19SBen Grasconsistency (i.e., processes may still access files
712fe8fb19SBen Graswithout using advisory locks possibly resulting in
722fe8fb19SBen Grasinconsistencies).
732fe8fb19SBen Gras.Pp
742fe8fb19SBen GrasThe locking mechanism allows two types of locks:
752fe8fb19SBen Gras.Em shared
762fe8fb19SBen Graslocks and
772fe8fb19SBen Gras.Em exclusive
782fe8fb19SBen Graslocks.
792fe8fb19SBen GrasAt any time multiple shared locks may be applied to a file,
802fe8fb19SBen Grasbut at no time are multiple exclusive, or both shared and exclusive,
812fe8fb19SBen Graslocks allowed simultaneously on a file.
822fe8fb19SBen Gras.Pp
832fe8fb19SBen GrasA shared lock may be
842fe8fb19SBen Gras.Em upgraded
852fe8fb19SBen Grasto an exclusive lock, and vice versa, simply by specifying
862fe8fb19SBen Grasthe appropriate lock type; this results in the previous
872fe8fb19SBen Graslock being released and the new lock applied (possibly
882fe8fb19SBen Grasafter other processes have gained and released the lock).
892fe8fb19SBen Gras.Pp
902fe8fb19SBen GrasRequesting a lock on an object that is already locked
912fe8fb19SBen Grasnormally causes the caller to be blocked until the lock may be
922fe8fb19SBen Grasacquired.
932fe8fb19SBen GrasIf
942fe8fb19SBen Gras.Dv LOCK_NB
952fe8fb19SBen Grasis included in
962fe8fb19SBen Gras.Fa operation ,
972fe8fb19SBen Grasthen this will not happen; instead the call will fail and
982fe8fb19SBen Grasthe error
992fe8fb19SBen Gras.Er EAGAIN
1002fe8fb19SBen Graswill be returned.
1012fe8fb19SBen Gras.Sh NOTES
1022fe8fb19SBen GrasLocks are on files, not file descriptors.
1032fe8fb19SBen GrasThat is, file descriptors duplicated through
1042fe8fb19SBen Gras.Xr dup 2
1052fe8fb19SBen Grasor
1062fe8fb19SBen Gras.Xr fork 2
1072fe8fb19SBen Grasdo not result in multiple instances of a lock, but rather multiple
1082fe8fb19SBen Grasreferences to a single lock.
1092fe8fb19SBen GrasIf a process holding a lock on a file
1102fe8fb19SBen Grasforks and the child explicitly unlocks the file, the parent will
1112fe8fb19SBen Graslose its lock.
1122fe8fb19SBen Gras.Pp
1132fe8fb19SBen GrasProcesses blocked awaiting a lock may be awakened by signals.
1142fe8fb19SBen Gras.Sh RETURN VALUES
1152fe8fb19SBen GrasZero is returned if the operation was successful;
1162fe8fb19SBen Grason an error a \-1 is returned and an error code is left in
1172fe8fb19SBen Grasthe global location
1182fe8fb19SBen Gras.Va errno .
1192fe8fb19SBen Gras.Sh ERRORS
1202fe8fb19SBen GrasThe
1212fe8fb19SBen Gras.Fn flock
1222fe8fb19SBen Grascall fails if:
1232fe8fb19SBen Gras.Bl -tag -width Er
1242fe8fb19SBen Gras.It Bq Er EAGAIN
1252fe8fb19SBen GrasThe file is locked and the
1262fe8fb19SBen Gras.Dv LOCK_NB
1272fe8fb19SBen Grasoption was specified.
1282fe8fb19SBen Gras.It Bq Er EBADF
1292fe8fb19SBen GrasThe argument
1302fe8fb19SBen Gras.Fa fd
1312fe8fb19SBen Grasis an invalid descriptor.
1322fe8fb19SBen Gras.It Bq Er EINVAL
1332fe8fb19SBen GrasThe argument
1342fe8fb19SBen Gras.Fa operation
1352fe8fb19SBen Grasdoes not include exactly one of
1362fe8fb19SBen Gras.Dv LOCK_EX ,
1372fe8fb19SBen Gras.Dv LOCK_SH ,
1382fe8fb19SBen Grasor
1392fe8fb19SBen Gras.Dv LOCK_UN .
1402fe8fb19SBen Gras.It Bq Er EOPNOTSUPP
1412fe8fb19SBen GrasThe argument
1422fe8fb19SBen Gras.Fa fd
1432fe8fb19SBen Grasrefers to an object other than a file.
1442fe8fb19SBen Gras.El
1452fe8fb19SBen Gras.Sh SEE ALSO
1462fe8fb19SBen Gras.Xr close 2 ,
1472fe8fb19SBen Gras.Xr dup 2 ,
1482fe8fb19SBen Gras.Xr execve 2 ,
1492fe8fb19SBen Gras.Xr fork 2 ,
150*f14fb602SLionel Sambuc.Xr open 2 ,
151*f14fb602SLionel Sambuc.Xr flockfile 3 ,
152*f14fb602SLionel Sambuc.Xr lockf 3
1532fe8fb19SBen Gras.Sh HISTORY
1542fe8fb19SBen GrasThe
1552fe8fb19SBen Gras.Fn flock
1562fe8fb19SBen Grasfunction call appeared in
1572fe8fb19SBen Gras.Bx 4.2 .
158