xref: /openbsd-src/lib/libc/sys/flock.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: flock.2,v 1.12 2000/10/18 05:12:09 aaron 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. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)flock.2	8.2 (Berkeley) 12/11/93
36.\"
37.Dd December 11, 1993
38.Dt FLOCK 2
39.Os
40.Sh NAME
41.Nm flock
42.Nd "apply or remove an advisory lock on an open file"
43.Sh SYNOPSIS
44.Fd #include <fcntl.h>
45
46.Fd #define	LOCK_SH	1	/* shared lock */
47.Fd #define	LOCK_EX	2	/* exclusive lock */
48.Fd #define	LOCK_NB	4	/* don't block when locking */
49.Fd #define	LOCK_UN	8	/* unlock */
50.Ft int
51.Fn flock "int fd" "int operation"
52.Sh DESCRIPTION
53.Fn flock
54applies or removes an
55.Em advisory
56lock on the file associated with the file descriptor
57.Fa fd .
58A lock is applied by specifying an
59.Fa operation
60parameter that is one of
61.Dv LOCK_SH
62or
63.Dv LOCK_EX
64with the optional addition of
65.Dv LOCK_NB .
66To unlock an existing lock
67.Dv operation
68should be
69.Dv LOCK_UN .
70.Pp
71Advisory locks allow cooperating processes to perform
72consistent operations on files, but do not guarantee
73consistency (i.e., processes may still access files
74without using advisory locks possibly resulting in
75inconsistencies).
76.Pp
77The locking mechanism allows two types of locks:
78.Em shared
79locks and
80.Em exclusive
81locks.
82At any time multiple shared locks may be applied to a file,
83but at no time are multiple exclusive, or both shared and exclusive,
84locks allowed simultaneously on a file.
85.Pp
86A shared lock may be
87.Em upgraded
88to an exclusive lock, and vice versa, simply by specifying
89the appropriate lock type; this results in the previous
90lock being released and the new lock applied (possibly
91after other processes have gained and released the lock).
92.Pp
93Requesting a lock on an object that is already locked normally causes
94the caller to be blocked until the lock may be acquired.
95If
96.Dv LOCK_NB
97is included in
98.Fa operation ,
99then this will not happen; instead the call will fail and the error
100.Er EWOULDBLOCK
101will be returned.
102.Sh NOTES
103Locks are on files, not file descriptors.
104That is, file descriptors duplicated through
105.Xr dup 2
106or
107.Xr fork 2
108do not result in multiple instances of a lock, but rather multiple
109references to a single lock.
110If a process holding a lock on a file forks and the child explicitly
111unlocks the file, the parent will lose 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 EWOULDBLOCK
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
135have an invalid value.
136.It Bq Er EOPNOTSUPP
137The referenced descriptor is not of the correct type.
138.El
139.Sh SEE ALSO
140.Xr close 2 ,
141.Xr dup 2 ,
142.Xr execve 2 ,
143.Xr fcntl 2 ,
144.Xr fork 2 ,
145.Xr open 2
146.Sh HISTORY
147The
148.Fn flock
149function call appeared in
150.Bx 4.2 .
151