OSDN Git Service

d94225520aad137d285bca1a1686ccae8a654233
[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 2013-02-11 "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 table entry.
77 This means that duplicate file descriptors (created by, for example,
78 .BR fork (2)
79 or
80 .BR dup (2))
81 refer to the same lock, and this lock may be modified
82 or released using any of these descriptors.
83 Furthermore, the lock is released either by an explicit
84 .B LOCK_UN
85 operation on any of these duplicate descriptors, or when all
86 such descriptors have been closed.
87
88 If a process uses
89 .BR open (2)
90 (or similar) to obtain more than one descriptor for the same file,
91 these descriptors are treated independently by
92 .BR flock ().
93 An attempt to lock the file using one of these file descriptors
94 may be denied by a lock that the calling process has
95 already placed via another descriptor.
96
97 A process may hold only one type of lock (shared or exclusive)
98 on a file.
99 Subsequent
100 .BR flock ()
101 calls on an already locked file will convert an existing lock to the new
102 lock mode.
103
104 Locks created by
105 .BR flock ()
106 are preserved across an
107 .BR execve (2).
108
109 A shared or exclusive lock can be placed on a file regardless of the
110 mode in which the file was opened.
111 .SH RETURN VALUE
112 On success, zero is returned.
113 On error, \-1 is returned, and
114 .I errno
115 is set appropriately.
116 .SH ERRORS
117 .TP
118 .B EBADF
119 .I fd
120 is not an open file descriptor.
121 .TP
122 .B EINTR
123 While waiting to acquire a lock, the call was interrupted by
124 delivery of a signal caught by a handler; see
125 .BR signal (7).
126 .TP
127 .B EINVAL
128 .I operation
129 is invalid.
130 .TP
131 .B ENOLCK
132 The kernel ran out of memory for allocating lock records.
133 .TP
134 .B EWOULDBLOCK
135 The file is locked and the
136 .B LOCK_NB
137 flag was selected.
138 .SH CONFORMING TO
139 4.4BSD (the
140 .BR flock ()
141 call first appeared in 4.2BSD).
142 A version of
143 .BR flock (),
144 possibly implemented in terms of
145 .BR fcntl (2),
146 appears on most UNIX systems.
147 .SH NOTES
148 .BR flock ()
149 does not lock files over NFS.
150 Use
151 .BR fcntl (2)
152 instead: that does work over NFS, given a sufficiently recent version of
153 Linux and a server which supports locking.
154 .PP
155 Since kernel 2.0,
156 .BR flock ()
157 is implemented as a system call in its own right rather
158 than being emulated in the GNU C library as a call to
159 .BR fcntl (2).
160 This yields true BSD semantics:
161 there is no interaction between the types of lock
162 placed by
163 .BR flock ()
164 and
165 .BR fcntl (2),
166 and
167 .BR flock ()
168 does not detect deadlock.
169 .PP
170 .BR flock ()
171 places advisory locks only; given suitable permissions on a file,
172 a process is free to ignore the use of
173 .BR flock ()
174 and perform I/O on the file.
175 .PP
176 .BR flock ()
177 and
178 .BR fcntl (2)
179 locks have different semantics with respect to forked processes and
180 .BR dup (2).
181 On systems that implement
182 .BR flock ()
183 using
184 .BR fcntl (2),
185 the semantics of
186 .BR flock ()
187 will be different from those described in this manual page.
188 .PP
189 Converting a lock
190 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
191 the existing lock is first removed, and then a new lock is established.
192 Between these two steps,
193 a pending lock request by another process may be granted,
194 with the result that the conversion either blocks, or fails if
195 .B LOCK_NB
196 was specified.
197 (This is the original BSD behavior,
198 and occurs on many other implementations.)
199 .\" Kernel 2.5.21 changed things a little: during lock conversion
200 .\" it is now the highest priority process that will get the lock -- mtk
201 .SH SEE ALSO
202 .BR flock (1),
203 .BR close (2),
204 .BR dup (2),
205 .BR execve (2),
206 .BR fcntl (2),
207 .BR fork (2),
208 .BR open (2),
209 .BR lockf (3)
210
211 .I Documentation/filesystems/locks.txt
212 in the Linux kernel source tree
213 .RI ( Documentation/locks.txt
214 in older kernels)
215 .SH COLOPHON
216 This page is part of release 3.64 of the Linux
217 .I man-pages
218 project.
219 A description of the project,
220 and information about reporting bugs,
221 can be found at
222 \%http://www.kernel.org/doc/man\-pages/.