xref: /netbsd-src/lib/libc/sys/flock.2 (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     from: @(#)flock.2	6.7 (Berkeley) 3/10/91
33.\"	$Id: flock.2,v 1.4 1993/11/29 21:25:06 jtc Exp $
34.\"
35.Dd March 10, 1991
36.Dt FLOCK 2
37.Os BSD 4.2
38.Sh NAME
39.Nm flock
40.Nd "apply or remove an advisory lock on an open file"
41.Sh SYNOPSIS
42.Fd #include <sys/file.h>
43.Fd #define	LOCK_SH	1	/* shared lock */
44.Fd #define	LOCK_EX	2	/* exclusive lock */
45.Fd #define	LOCK_NB	4	/* don't block when locking */
46.Fd #define	LOCK_UN	8	/* unlock */
47.Ft int
48.Fn flock "int fd" "int operation"
49.Sh DESCRIPTION
50.Fn Flock
51applies or removes an
52.Em advisory
53lock on the file associated with the file descriptor
54.Fa fd .
55A lock is applied by specifying an
56.Fa operation
57parameter that is the inclusive or of
58.Dv LOCK_SH
59or
60.Dv LOCK_EX
61and, possibly,
62.Dv LOCK_NB .
63To unlock
64an existing lock
65.Dv operation
66should be
67.Dv LOCK_UN .
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
92normally causes the caller to be blocked until the lock may be
93acquired.  If
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 EWOULDBLOCK
100will be returned.
101.Sh NOTES
102Locks are on files, not file descriptors.  That is, file descriptors
103duplicated 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.  If a process holding a lock on a file
109forks and the child explicitly unlocks the file, the parent will
110lose its lock.
111.Pp
112Processes blocked awaiting a lock may be awakened by signals.
113.Sh RETURN VALUES
114Zero is returned if the operation was successful;
115on an error a -1 is returned and an error code is left in
116the global location
117.Va errno .
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 fd
134refers to an object other than a file.
135.El
136.Sh SEE ALSO
137.Xr open 2 ,
138.Xr close 2 ,
139.Xr dup 2 ,
140.Xr execve 2 ,
141.Xr fork 2
142.Sh HISTORY
143The
144.Fn flock
145function call appeared in
146.Bx 4.2 .
147