OSDN Git Service

(split) LDP: Update original to LDP v3.38.
[linuxjm/LDP_man-pages.git] / original / man2 / flock.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
4 .\" and Copyright 2002 Michael Kerrisk
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
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 .TH FLOCK 2 2009-07-25 "Linux" "Linux Programmer's Manual"
35 .SH NAME
36 flock \- apply or remove an advisory lock on an open file
37 .SH SYNOPSIS
38 .B #include <sys/file.h>
39 .sp
40 .BI "int flock(int " fd ", int " operation );
41 .SH DESCRIPTION
42 Apply or remove an advisory lock on the open file specified by
43 .IR fd .
44 The argument
45 .I operation
46 is one of the following:
47 .RS 4
48 .TP 9
49 .B LOCK_SH
50 Place a shared lock.
51 More than one process may hold a shared lock for a given file
52 at a given time.
53 .TP
54 .B LOCK_EX
55 Place an exclusive lock.
56 Only one process may hold an exclusive lock for a given
57 file at a given time.
58 .TP
59 .B LOCK_UN
60 Remove an existing lock held by this process.
61 .RE
62 .PP
63 A call to
64 .BR flock ()
65 may block if an incompatible lock is held by another process.
66 To make a nonblocking request, include
67 .B LOCK_NB
68 (by ORing)
69 with any of the above operations.
70
71 A single file may not simultaneously have both shared and exclusive locks.
72
73 Locks created by
74 .BR flock ()
75 are associated with an open file table entry.
76 This means that duplicate file descriptors (created by, for example,
77 .BR fork (2)
78 or
79 .BR dup (2))
80 refer to the same lock, and this lock may be modified
81 or released using any of these descriptors.
82 Furthermore, the lock is released either by an explicit
83 .B LOCK_UN
84 operation on any of these duplicate descriptors, or when all
85 such descriptors have been closed.
86
87 If a process uses
88 .BR open (2)
89 (or similar) to obtain more than one descriptor for the same file,
90 these descriptors are treated independently by
91 .BR flock ().
92 An attempt to lock the file using one of these file descriptors
93 may be denied by a lock that the calling process has
94 already placed via another descriptor.
95
96 A process may only hold one type of lock (shared or exclusive)
97 on a file.
98 Subsequent
99 .BR flock ()
100 calls on an already locked file will convert an existing lock to the new
101 lock mode.
102
103 Locks created by
104 .BR flock ()
105 are preserved across an
106 .BR execve (2).
107
108 A shared or exclusive lock can be placed on a file regardless of the
109 mode in which the file was opened.
110 .SH "RETURN VALUE"
111 On success, zero is returned.
112 On error, \-1 is returned, and
113 .I errno
114 is set appropriately.
115 .SH ERRORS
116 .TP
117 .B EBADF
118 .I fd
119 is not an open file descriptor.
120 .TP
121 .B EINTR
122 While waiting to acquire a lock, the call was interrupted by
123 delivery of a signal caught by a handler; see
124 .BR signal (7).
125 .TP
126 .B EINVAL
127 .I operation
128 is invalid.
129 .TP
130 .B ENOLCK
131 The kernel ran out of memory for allocating lock records.
132 .TP
133 .B EWOULDBLOCK
134 The file is locked and the
135 .B LOCK_NB
136 flag was selected.
137 .SH "CONFORMING TO"
138 4.4BSD (the
139 .BR flock ()
140 call first appeared in 4.2BSD).
141 A version of
142 .BR flock (),
143 possibly implemented in terms of
144 .BR fcntl (2),
145 appears on most UNIX systems.
146 .SH NOTES
147 .BR flock ()
148 does not lock files over NFS.
149 Use
150 .BR fcntl (2)
151 instead: that does work over NFS, given a sufficiently recent version of
152 Linux and a server which supports locking.
153 .PP
154 Since kernel 2.0,
155 .BR flock ()
156 is implemented as a system call in its own right rather
157 than being emulated in the GNU C library as a call to
158 .BR fcntl (2).
159 This yields true BSD semantics:
160 there is no interaction between the types of lock
161 placed by
162 .BR flock ()
163 and
164 .BR fcntl (2),
165 and
166 .BR flock ()
167 does not detect deadlock.
168 .PP
169 .BR flock ()
170 places advisory locks only; given suitable permissions on a file,
171 a process is free to ignore the use of
172 .BR flock ()
173 and perform I/O on the file.
174 .PP
175 .BR flock ()
176 and
177 .BR fcntl (2)
178 locks have different semantics with respect to forked processes and
179 .BR dup (2).
180 On systems that implement
181 .BR flock ()
182 using
183 .BR fcntl (2),
184 the semantics of
185 .BR flock ()
186 will be different from those described in this manual page.
187 .PP
188 Converting a lock
189 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
190 the existing lock is first removed, and then a new lock is established.
191 Between these two steps,
192 a pending lock request by another process may be granted,
193 with the result that the conversion either blocks, or fails if
194 .B LOCK_NB
195 was specified.
196 (This is the original BSD behavior,
197 and occurs on many other implementations.)
198 .\" Kernel 2.5.21 changed things a little: during lock conversion
199 .\" it is now the highest priority process that will get the lock -- mtk
200 .SH "SEE ALSO"
201 .BR close (2),
202 .BR dup (2),
203 .BR execve (2),
204 .BR fcntl (2),
205 .BR fork (2),
206 .BR open (2),
207 .BR lockf (3)
208
209 See also
210 .I Documentation/filesystem/locks.txt
211 in the kernel source
212 .RI ( Documentation/locks.txt
213 in older kernels).