1flock(2)                      System Calls Manual                     flock(2)
2
3
4

NAME

6       flock - apply or remove an advisory lock on an open file
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/file.h>
13
14       int flock(int fd, int operation);
15

DESCRIPTION

17       Apply or remove an advisory lock on the open file specified by fd.  The
18       argument operation is one of the following:
19
20           LOCK_SH  Place a shared lock.  More than one  process  may  hold  a
21                    shared lock for a given file at a given time.
22
23           LOCK_EX  Place an exclusive lock.  Only one process may hold an ex‐
24                    clusive lock for a given file at a given time.
25
26           LOCK_UN  Remove an existing lock held by this process.
27
28       A call to flock() may block if an incompatible lock is held by  another
29       process.   To  make  a  nonblocking request, include LOCK_NB (by ORing)
30       with any of the above operations.
31
32       A single file may not simultaneously have  both  shared  and  exclusive
33       locks.
34
35       Locks  created  by flock() are associated with an open file description
36       (see open(2)).  This means that duplicate file descriptors (created by,
37       for  example,  fork(2) or dup(2)) refer to the same lock, and this lock
38       may be modified or released using any of these file descriptors.   Fur‐
39       thermore,  the lock is released either by an explicit LOCK_UN operation
40       on any of these duplicate file descriptors, or when all such  file  de‐
41       scriptors have been closed.
42
43       If a process uses open(2) (or similar) to obtain more than one file de‐
44       scriptor for the same file, these file descriptors are treated indepen‐
45       dently by flock().  An attempt to lock the file using one of these file
46       descriptors may be denied by a lock that the calling  process  has  al‐
47       ready placed via another file descriptor.
48
49       A  process  may  hold  only one type of lock (shared or exclusive) on a
50       file.  Subsequent flock() calls on an already locked file will  convert
51       an existing lock to the new lock mode.
52
53       Locks created by flock() are preserved across an execve(2).
54
55       A  shared  or  exclusive lock can be placed on a file regardless of the
56       mode in which the file was opened.
57

RETURN VALUE

59       On success, zero is returned.  On error, -1 is returned, and  errno  is
60       set to indicate the error.
61

ERRORS

63       EBADF  fd is not an open file descriptor.
64
65       EINTR  While waiting to acquire a lock, the call was interrupted by de‐
66              livery of a signal caught by a handler; see signal(7).
67
68       EINVAL operation is invalid.
69
70       ENOLCK The kernel ran out of memory for allocating lock records.
71
72       EWOULDBLOCK
73              The file is locked and the LOCK_NB flag was selected.
74

VERSIONS

76       Since Linux 2.0, flock() is implemented as a system  call  in  its  own
77       right  rather than being emulated in the GNU C library as a call to fc‐
78       ntl(2).  With this implementation, there is no interaction between  the
79       types  of lock placed by flock() and fcntl(2), and flock() does not de‐
80       tect deadlock.  (Note, however, that on some systems, such as the  mod‐
81       ern BSDs, flock() and fcntl(2) locks do interact with one another.)
82
83   CIFS details
84       Up  to Linux 5.4, flock() is not propagated over SMB.  A file with such
85       locks will not appear locked for remote clients.
86
87       Since Linux 5.5, flock() locks are emulated with SMB  byte-range  locks
88       on  the  entire  file.   Similarly to NFS, this means that fcntl(2) and
89       flock() locks interact with one another.  Another important side-effect
90       is  that  the  locks  are not advisory anymore: any IO on a locked file
91       will always fail with EACCES when done from a separate file descriptor.
92       This  difference  originates from the design of locks in the SMB proto‐
93       col, which provides mandatory locking semantics.
94
95       Remote and mandatory locking semantics  may  vary  with  SMB  protocol,
96       mount options and server type.  See mount.cifs(8) for additional infor‐
97       mation.
98

STANDARDS

100       BSD.
101

HISTORY

103       4.4BSD (the flock() call first  appeared  in  4.2BSD).   A  version  of
104       flock(),  possibly  implemented  in  terms of fcntl(2), appears on most
105       UNIX systems.
106
107   NFS details
108       Up to Linux 2.6.11, flock() does not lock files  over  NFS  (i.e.,  the
109       scope  of  locks  was limited to the local system).  Instead, one could
110       use fcntl(2) byte-range locking, which does work over NFS, given a suf‐
111       ficiently recent version of Linux and a server which supports locking.
112
113       Since Linux 2.6.12, NFS clients support flock() locks by emulating them
114       as fcntl(2) byte-range locks on the entire file.  This means  that  fc‐
115       ntl(2)  and  flock()  locks  do interact with one another over NFS.  It
116       also means that in order to place an exclusive lock, the file  must  be
117       opened for writing.
118
119       Since  Linux  2.6.37, the kernel supports a compatibility mode that al‐
120       lows flock() locks (and also fcntl(2) byte region locks) to be  treated
121       as local; see the discussion of the local_lock option in nfs(5).
122

NOTES

124       flock()  places  advisory  locks  only; given suitable permissions on a
125       file, a process is free to ignore the use of flock() and perform I/O on
126       the file.
127
128       flock()  and  fcntl(2)  locks  have different semantics with respect to
129       forked processes and dup(2).  On systems that implement  flock()  using
130       fcntl(2),  the  semantics  of  flock() will be different from those de‐
131       scribed in this manual page.
132
133       Converting a lock (shared to exclusive, or vice versa) is  not  guaran‐
134       teed  to  be atomic: the existing lock is first removed, and then a new
135       lock is established.  Between these two steps, a pending  lock  request
136       by  another process may be granted, with the result that the conversion
137       either blocks, or fails if LOCK_NB was specified.  (This is the  origi‐
138       nal BSD behavior, and occurs on many other implementations.)
139

SEE ALSO

141       flock(1),  close(2),  dup(2),  execve(2),  fcntl(2),  fork(2), open(2),
142       lockf(3), lslocks(8)
143
144       Documentation/filesystems/locks.txt in the  Linux  kernel  source  tree
145       (Documentation/locks.txt in older kernels)
146
147
148
149Linux man-pages 6.05              2023-03-30                          flock(2)
Impressum