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