1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2002 Michael Kerrisk
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
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.
12 .\" FIXME Maybe document LOCK_MAND, LOCK_RW, LOCK_READ, LOCK_WRITE
13 .\" which only have effect for SAMBA.
15 .TH flock 2 (date) "Linux man-pages (unreleased)"
17 flock \- apply or remove an advisory lock on an open file
20 .RI ( libc ", " \-lc )
23 .B #include <sys/file.h>
25 .BI "int flock(int " fd ", int " operation );
28 Apply or remove an advisory lock on the open file specified by
32 is one of the following:
37 More than one process may hold a shared lock for a given file
41 Place an exclusive lock.
42 Only one process may hold an exclusive lock for a given
46 Remove an existing lock held by this process.
51 may block if an incompatible lock is held by another process.
52 To make a nonblocking request, include
55 with any of the above operations.
57 A single file may not simultaneously have both shared and exclusive locks.
61 are associated with an open file description (see
63 This means that duplicate file descriptors (created by, for example,
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
71 operation on any of these duplicate file descriptors, or when all
72 such file descriptors have been closed.
76 (or similar) to obtain more than one file descriptor for the same file,
77 these file descriptors are treated independently by
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.
83 A process may hold only one type of lock (shared or exclusive)
87 calls on an already locked file will convert an existing lock to the new
92 are preserved across an
95 A shared or exclusive lock can be placed on a file regardless of the
96 mode in which the file was opened.
98 On success, zero is returned.
99 On error, \-1 is returned, and
101 is set to indicate the error.
106 is not an open file descriptor.
109 While waiting to acquire a lock, the call was interrupted by
110 delivery of a signal caught by a handler; see
118 The kernel ran out of memory for allocating lock records.
121 The file is locked and the
127 is implemented as a system call in its own right rather
128 than being emulated in the GNU C library as a call to
130 With this implementation,
131 there is no interaction between the types of lock
138 does not detect deadlock.
139 (Note, however, that on some systems, such as the modern BSDs,
140 .\" E.g., according to the flock(2) man page, FreeBSD since at least 5.3
146 interact with one another.)
150 is not propagated over SMB.
151 A file with such locks will not appear locked for remote clients.
155 locks are emulated with SMB byte-range locks on the entire file.
156 Similarly to NFS, this means that
160 locks interact with one another.
161 Another important side-effect is that the locks are not advisory anymore:
162 any IO on a locked file will always fail with
164 when done from a separate file descriptor.
165 This difference originates from the design of locks in the SMB protocol,
166 which provides mandatory locking semantics.
168 Remote and mandatory locking semantics may vary with
169 SMB protocol, mount options and server type.
172 for additional information.
178 call first appeared in 4.2BSD).
181 possibly implemented in terms of
183 appears on most UNIX systems.
187 does not lock files over NFS
188 (i.e., the scope of locks was limited to the local system).
189 Instead, one could use
191 byte-range locking, which does work over NFS,
192 given a sufficiently recent version of
193 Linux and a server which supports locking.
195 Since Linux 2.6.12, NFS clients support
197 locks by emulating them as
199 byte-range locks on the entire file.
206 interact with one another over NFS.
207 It also means that in order to place an exclusive lock,
208 the file must be opened for writing.
211 .\" commit 5eebde23223aeb0ad2d9e3be6590ff8bbfab0fc2
212 the kernel supports a compatibility mode that allows
216 byte region locks) to be treated as local;
217 see the discussion of the
223 places advisory locks only; given suitable permissions on a file,
224 a process is free to ignore the use of
226 and perform I/O on the file.
231 locks have different semantics with respect to forked processes and
233 On systems that implement
239 will be different from those described in this manual page.
242 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
243 the existing lock is first removed, and then a new lock is established.
244 Between these two steps,
245 a pending lock request by another process may be granted,
246 with the result that the conversion either blocks, or fails if
249 (This is the original BSD behavior,
250 and occurs on many other implementations.)
251 .\" Kernel 2.5.21 changed things a little: during lock conversion
252 .\" it is now the highest priority process that will get the lock -- mtk
264 .I Documentation/filesystems/locks.txt
265 in the Linux kernel source tree
266 .RI ( Documentation/locks.txt