]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/flock.2
Convert to American spelling conventions
[thirdparty/man-pages.git] / 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 .\" professionally.
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@gmx.net>
29 .\" Substantial rewrites and additions
30 .\" 2005-05-10 mtk, noted that lock conversions are not atomic.
31 .\"
32 .TH FLOCK 2 2002-04-24 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 flock \- apply or remove an advisory lock on an open file
35 .SH SYNOPSIS
36 .B #include <sys/file.h>
37 .sp
38 .BI "int flock(int " fd ", int " operation );
39 .SH DESCRIPTION
40 Apply or remove an advisory lock on the open file specified by
41 .IR fd .
42 The parameter
43 .I operation
44 is one of the following:
45 .RS
46 .TP 1.0i
47 .B LOCK_SH
48 Place a shared lock.
49 More than one process may hold a shared lock for a given file
50 at a given time.
51 .TP
52 .B LOCK_EX
53 Place an exclusive lock.
54 Only one process may hold an exclusive lock for a given
55 file at a given time.
56 .TP
57 .B LOCK_UN
58 Remove an existing lock held by this process.
59 .sp
60 .RE
61 A call to
62 .BR flock ()
63 may block if an incompatible lock is held by another process.
64 To make a non-blocking request, include
65 .B LOCK_NB
66 (by
67 .IR OR ing)
68 with any of the above operations.
69
70 A single file may not simultaneously have both shared and exclusive locks.
71
72 Locks created by
73 .BR flock ()
74 are associated with an open file table entry.
75 This means that duplicate file descriptors (created by, for example,
76 .BR fork (2)
77 or
78 .BR dup (2))
79 refer to the same lock, and this lock may be modified
80 or released using any of these descriptors.
81 Furthermore, the lock is released either by an explicit
82 .B LOCK_UN
83 operation on any of these duplicate descriptors, or when all
84 such descriptors have been closed.
85
86 If a process uses
87 .BR open (2)
88 (or similar) to obtain more than one descriptor for the same file,
89 these descriptors are treated independently by
90 .BR flock ().
91 An attempt to lock the file using one of these file descriptors
92 may be denied by a lock that the calling process has
93 already placed via another descriptor.
94
95 A process may only hold one type of lock (shared or exclusive)
96 on a file.
97 Subsequent
98 .BR flock ()
99 calls on an already locked file will convert an existing lock to the new
100 lock mode.
101
102 Locks created by
103 .BR flock ()
104 are preserved across an
105 .BR execve (2).
106
107 A shared or exclusive lock can be placed on a file regardless of the
108 mode in which the file was opened.
109 .SH "RETURN VALUE"
110 On success, zero is returned.
111 On error, \-1 is returned, and
112 .I errno
113 is set appropriately.
114 .SH ERRORS
115 .TP
116 .B EBADF
117 .I fd
118 is not a not an open file descriptor.
119 .TP
120 .B EINTR
121 While waiting to acquire a lock, the call was interrupted by
122 delivery of a signal caught by a handler.
123 .TP
124 .B EINVAL
125 .I operation
126 is invalid.
127 .TP
128 .B ENOLCK
129 The kernel ran out of memory for allocating lock records.
130 .TP
131 .B EWOULDBLOCK
132 The file is locked and the
133 .B LOCK_NB
134 flag was selected.
135 .SH "CONFORMING TO"
136 4.4BSD (the
137 .BR flock (2)
138 call first appeared in 4.2BSD).
139 A version of
140 .BR flock (2),
141 possibly implemented in terms of
142 .BR fcntl (2),
143 appears on most Unix systems.
144 .SH NOTES
145 .BR flock (2)
146 does not lock files over NFS.
147 Use
148 .BR fcntl (2)
149 instead: that does work over NFS, given a sufficiently recent version of
150 Linux and a server which supports locking.
151 .PP
152 Since kernel 2.0,
153 .BR flock (2)
154 is implemented as a system call in its own right rather
155 than being emulated in the GNU C library as a call to
156 .BR fcntl (2).
157 This yields true BSD semantics:
158 there is no interaction between the types of lock
159 placed by
160 .BR flock (2)
161 and
162 .BR fcntl (2),
163 and
164 .BR flock (2)
165 does not detect deadlock.
166 .PP
167 .BR flock (2)
168 places advisory locks only; given suitable permissions on a file,
169 a process is free to ignore the use of
170 .BR flock (2)
171 and perform I/O on the file.
172 .PP
173 .BR flock (2)
174 and
175 .BR fcntl (2)
176 locks have different semantics with respect to forked processes and
177 .BR dup (2).
178 On systems that implement
179 .BR flock ()
180 using
181 .BR fcntl (2),
182 the semantics of
183 .BR flock ()
184 will be different from those described in this manual page.
185 .PP
186 Converting a lock
187 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
188 the existing lock is first removed, and then a new lock is established.
189 Between these two steps,
190 a pending lock request by another process may be granted,
191 with the result that the conversion either blocks, or fails if
192 .B LOCK_NB
193 was specified.
194 (This is the original BSD behavior,
195 and occurs on many other implementations.)
196 .\" Kernel 2.5.21 changed things a little: during lock conversion
197 .\" it is now the highest priority process that will get the lock -- mtk
198 .SH "SEE ALSO"
199 .BR close (2),
200 .BR dup (2),
201 .BR execve (2),
202 .BR fcntl (2),
203 .BR fork (2),
204 .BR open (2),
205 .BR lockf (3)
206
207 See also
208 .I Documentation/locks.txt
209 and
210 .I Documentation/mandatory.txt
211 in the kernel source.