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