readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man2 / flock.2
blob61822c9bc8bded59ac184308960682a636e9a5c0
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2002 Michael Kerrisk
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Fri Jan 31 16:26:07 1997 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Dec 11 17:57:27 1998 by Jamie Lokier <jamie@imbolc.ucc.ie>
28 .\" Modified 24 Apr 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
29 .\"     Substantial rewrites and additions
30 .\" 2005-05-10 mtk, noted that lock conversions are not atomic.
31 .\"
32 .\" FIXME Maybe document LOCK_MAND, LOCK_RW, LOCK_READ, LOCK_WRITE
33 .\" which only have effect for SAMBA.
34 .\"
35 .TH FLOCK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 flock \- apply or remove an advisory lock on an open file
38 .SH SYNOPSIS
39 .nf
40 .B #include <sys/file.h>
41 .PP
42 .BI "int flock(int " fd ", int " operation );
43 .fi
44 .SH DESCRIPTION
45 Apply or remove an advisory lock on the open file specified by
46 .IR fd .
47 The argument
48 .I operation
49 is one of the following:
50 .RS 4
51 .TP 9
52 .B LOCK_SH
53 Place a shared lock.
54 More than one process may hold a shared lock for a given file
55 at a given time.
56 .TP
57 .B LOCK_EX
58 Place an exclusive lock.
59 Only one process may hold an exclusive lock for a given
60 file at a given time.
61 .TP
62 .B LOCK_UN
63 Remove an existing lock held by this process.
64 .RE
65 .PP
66 A call to
67 .BR flock ()
68 may block if an incompatible lock is held by another process.
69 To make a nonblocking request, include
70 .B LOCK_NB
71 (by ORing)
72 with any of the above operations.
73 .PP
74 A single file may not simultaneously have both shared and exclusive locks.
75 .PP
76 Locks created by
77 .BR flock ()
78 are associated with an open file description (see
79 .BR open (2)).
80 This means that duplicate file descriptors (created by, for example,
81 .BR fork (2)
83 .BR dup (2))
84 refer to the same lock, and this lock may be modified
85 or released using any of these file descriptors.
86 Furthermore, the lock is released either by an explicit
87 .B LOCK_UN
88 operation on any of these duplicate file descriptors, or when all
89 such file descriptors have been closed.
90 .PP
91 If a process uses
92 .BR open (2)
93 (or similar) to obtain more than one file descriptor for the same file,
94 these file descriptors are treated independently by
95 .BR flock ().
96 An attempt to lock the file using one of these file descriptors
97 may be denied by a lock that the calling process has
98 already placed via another file descriptor.
99 .PP
100 A process may hold only one type of lock (shared or exclusive)
101 on a file.
102 Subsequent
103 .BR flock ()
104 calls on an already locked file will convert an existing lock to the new
105 lock mode.
107 Locks created by
108 .BR flock ()
109 are preserved across an
110 .BR execve (2).
112 A shared or exclusive lock can be placed on a file regardless of the
113 mode in which the file was opened.
114 .SH RETURN VALUE
115 On success, zero is returned.
116 On error, \-1 is returned, and
117 .I errno
118 is set to indicate the error.
119 .SH ERRORS
121 .B EBADF
122 .I fd
123 is not an open file descriptor.
125 .B EINTR
126 While waiting to acquire a lock, the call was interrupted by
127 delivery of a signal caught by a handler; see
128 .BR signal (7).
130 .B EINVAL
131 .I operation
132 is invalid.
134 .B ENOLCK
135 The kernel ran out of memory for allocating lock records.
137 .B EWOULDBLOCK
138 The file is locked and the
139 .B LOCK_NB
140 flag was selected.
141 .SH CONFORMING TO
142 4.4BSD (the
143 .BR flock ()
144 call first appeared in 4.2BSD).
145 A version of
146 .BR flock (),
147 possibly implemented in terms of
148 .BR fcntl (2),
149 appears on most UNIX systems.
150 .SH NOTES
151 Since kernel 2.0,
152 .BR flock ()
153 is implemented as a system call in its own right rather
154 than being emulated in the GNU C library as a call to
155 .BR fcntl (2).
156 With this implementation,
157 there is no interaction between the types of lock
158 placed by
159 .BR flock ()
161 .BR fcntl (2),
163 .BR flock ()
164 does not detect deadlock.
165 (Note, however, that on some systems, such as the modern BSDs,
166 .\" E.g., according to the flock(2) man page, FreeBSD since at least 5.3
167 .BR flock ()
169 .BR fcntl (2)
170 locks
171 .I do
172 interact with one another.)
174 .BR flock ()
175 places advisory locks only; given suitable permissions on a file,
176 a process is free to ignore the use of
177 .BR flock ()
178 and perform I/O on the file.
180 .BR flock ()
182 .BR fcntl (2)
183 locks have different semantics with respect to forked processes and
184 .BR dup (2).
185 On systems that implement
186 .BR flock ()
187 using
188 .BR fcntl (2),
189 the semantics of
190 .BR flock ()
191 will be different from those described in this manual page.
193 Converting a lock
194 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
195 the existing lock is first removed, and then a new lock is established.
196 Between these two steps,
197 a pending lock request by another process may be granted,
198 with the result that the conversion either blocks, or fails if
199 .B LOCK_NB
200 was specified.
201 (This is the original BSD behavior,
202 and occurs on many other implementations.)
203 .\" Kernel 2.5.21 changed things a little: during lock conversion
204 .\" it is now the highest priority process that will get the lock -- mtk
205 .SS NFS details
206 In Linux kernels up to 2.6.11,
207 .BR flock ()
208 does not lock files over NFS
209 (i.e., the scope of locks was limited to the local system).
210 Instead, one could use
211 .BR fcntl (2)
212 byte-range locking, which does work over NFS,
213 given a sufficiently recent version of
214 Linux and a server which supports locking.
216 Since Linux 2.6.12, NFS clients support
217 .BR flock ()
218 locks by emulating them as
219 .BR fcntl (2)
220 byte-range locks on the entire file.
221 This means that
222 .BR fcntl (2)
224 .BR flock ()
225 locks
226 .I do
227 interact with one another over NFS.
228 It also means that in order to place an exclusive lock,
229 the file must be opened for writing.
231 Since Linux 2.6.37,
232 .\" commit 5eebde23223aeb0ad2d9e3be6590ff8bbfab0fc2
233 the kernel supports a compatibility mode that allows
234 .BR flock ()
235 locks (and also
236 .BR fcntl (2)
237 byte region locks) to be treated as local;
238 see the discussion of the
239 .I "local_lock"
240 option in
241 .BR nfs (5).
242 .SS CIFS details
243 In Linux kernels up to 5.4,
244 .BR flock ()
245 is not propagated over SMB.
246 A file with such locks will not appear locked for remote clients.
248 Since Linux 5.5,
249 .BR flock ()
250 locks are emulated with SMB byte-range locks on the entire file.
251 Similarly to NFS, this means that
252 .BR fcntl (2)
254 .BR flock ()
255 locks interact with one another.
256 Another important side-effect is that the locks are not advisory anymore:
257 any IO on a locked file will always fail with
258 .BR EACCES
259 when done from a separate file descriptor.
260 This difference originates from the design of locks in the SMB protocol,
261 which provides mandatory locking semantics.
263 Remote and mandatory locking semantics may vary with SMB protocol, mount options and server type.
265 .BR mount.cifs (8)
266 for additional information.
267 .SH SEE ALSO
268 .BR flock (1),
269 .BR close (2),
270 .BR dup (2),
271 .BR execve (2),
272 .BR fcntl (2),
273 .BR fork (2),
274 .BR open (2),
275 .BR lockf (3),
276 .BR lslocks (8)
278 .I Documentation/filesystems/locks.txt
279 in the Linux kernel source tree
280 .RI ( Documentation/locks.txt
281 in older kernels)