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