OSDN Git Service

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