]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | '\" t |
fea681da | 2 | .\" This manpage is Copyright (C) 1992 Drew Eckhardt; |
99b5ab79 MK |
3 | .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson; |
4 | .\" and Copyright (C) 1998 Jamie Lokier; | |
5 | .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk; | |
6 | .\" and Copyright (C) 2014 Jeff Layton | |
6bdad35b | 7 | .\" and Copyright (C) 2014 David Herrmann |
580082a1 | 8 | .\" and Copyright (C) 2017 Jens Axboe |
fea681da | 9 | .\" |
93015253 | 10 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
11 | .\" Permission is granted to make and distribute verbatim copies of this |
12 | .\" manual provided the copyright notice and this permission notice are | |
13 | .\" preserved on all copies. | |
14 | .\" | |
15 | .\" Permission is granted to copy and distribute modified versions of this | |
16 | .\" manual under the conditions for verbatim copying, provided that the | |
17 | .\" entire resulting derived work is distributed under the terms of a | |
18 | .\" permission notice identical to this one. | |
c13182ef | 19 | .\" |
fea681da MK |
20 | .\" Since the Linux kernel and libraries are constantly changing, this |
21 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
22 | .\" responsibility for errors or omissions, or for damages resulting from | |
23 | .\" the use of the information contained herein. The author(s) may not | |
24 | .\" have taken the same level of care in the production of this manual, | |
25 | .\" which is licensed free of charge, as they might when working | |
26 | .\" professionally. | |
c13182ef | 27 | .\" |
fea681da MK |
28 | .\" Formatted or processed versions of this manual, if unaccompanied by |
29 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 30 | .\" %%%LICENSE_END |
fea681da MK |
31 | .\" |
32 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
33 | .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl> | |
34 | .\" and again on 960413 and 980804 and 981223. | |
35 | .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie> | |
36 | .\" Applied correction by Christian Ehrhardt - aeb, 990712 | |
c11b1abf | 37 | .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
38 | .\" Added note on F_SETFL and O_DIRECT |
39 | .\" Complete rewrite + expansion of material on file locking | |
40 | .\" Incorporated description of F_NOTIFY, drawing on | |
41 | .\" Stephen Rothwell's notes in Documentation/dnotify.txt. | |
42 | .\" Added description of F_SETLEASE and F_GETLEASE | |
43 | .\" Corrected and polished, aeb, 020527. | |
c11b1abf | 44 | .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
45 | .\" Modified description of file leases: fixed some errors of detail |
46 | .\" Replaced the term "lease contestant" by "lease breaker" | |
c11b1abf | 47 | .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 48 | .\" Added notes on capability requirements |
1c1e15ed | 49 | .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool |
c13182ef | 50 | .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb. |
2f36a807 | 51 | .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk |
d9bfdb9c | 52 | .\" Described behavior of F_SETOWN/F_SETSIG in |
71fea607 | 53 | .\" multithreaded processes, and generally cleaned |
2f36a807 | 54 | .\" up the discussion of F_SETOWN. |
c13182ef | 55 | .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>, |
9d2a7b1f MK |
56 | .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4 |
57 | .\" and earlier. Added text on permissions required to send signal. | |
dd164bfc MK |
58 | .\" 2009-09-30, Michael Kerrisk |
59 | .\" Note obsolete F_SETOWN behavior with threads. | |
60 | .\" Document F_SETOWN_EX and F_GETOWN_EX | |
14f7d1a4 MK |
61 | .\" 2010-06-17, Michael Kerrisk |
62 | .\" Document F_SETPIPE_SZ and F_GETPIPE_SZ. | |
a25d701d | 63 | .\" 2014-07-08, David Herrmann <dh.herrmann@gmail.com> |
6bdad35b | 64 | .\" Document F_ADD_SEALS and F_GET_SEALS |
580082a1 JA |
65 | .\" 2017-06-26, Jens Axboe <axboe@kernel.dk> |
66 | .\" Document F_{GET,SET}_RW_HINT and F_{GET,SET}_FILE_RW_HINT | |
fea681da | 67 | .\" |
9ba01802 | 68 | .TH FCNTL 2 2019-03-06 "Linux" "Linux Programmer's Manual" |
fea681da MK |
69 | .SH NAME |
70 | fcntl \- manipulate file descriptor | |
71 | .SH SYNOPSIS | |
72 | .nf | |
73 | .B #include <unistd.h> | |
74 | .B #include <fcntl.h> | |
778ac627 | 75 | .PP |
68e0696a | 76 | .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );" |
fea681da MK |
77 | .fi |
78 | .SH DESCRIPTION | |
cff88e99 MK |
79 | .BR fcntl () |
80 | performs one of the operations described below on the open file descriptor | |
fea681da | 81 | .IR fd . |
cff88e99 | 82 | The operation is determined by |
fea681da | 83 | .IR cmd . |
778ac627 | 84 | .PP |
68e0696a MK |
85 | .BR fcntl () |
86 | can take an optional third argument. | |
87 | Whether or not this argument is required is determined by | |
88 | .IR cmd . | |
63a6efda | 89 | The required argument type is indicated in parentheses after each |
68e0696a MK |
90 | .I cmd |
91 | name (in most cases, the required type is | |
ed9821bb | 92 | .IR int , |
68e0696a MK |
93 | and we identify the argument using the name |
94 | .IR arg ), | |
95 | or | |
96 | .I void | |
97 | is specified if the argument is not required. | |
778ac627 | 98 | .PP |
6024e733 MK |
99 | Certain of the operations below are supported only since a particular |
100 | Linux kernel version. | |
101 | The preferred method of checking whether the host kernel supports | |
102 | a particular operation is to invoke | |
103 | .BR fcntl () | |
104 | with the desired | |
105 | .IR cmd | |
106 | value and then test whether the call failed with | |
107 | .BR EINVAL , | |
108 | indicating that the kernel does not recognize this value. | |
73d8cece | 109 | .SS Duplicating a file descriptor |
fea681da | 110 | .TP |
ed9821bb | 111 | .BR F_DUPFD " (\fIint\fP)" |
6a459f6f MK |
112 | Duplicate the file descriptor |
113 | .IR fd | |
114 | using the lowest-numbered available file descriptor greater than or equal to | |
115 | .IR arg . | |
cff88e99 | 116 | This is different from |
68e0696a | 117 | .BR dup2 (2), |
d9cb0d7d | 118 | which uses exactly the file descriptor specified. |
ca92ce95 | 119 | .IP |
d9cb0d7d | 120 | On success, the new file descriptor is returned. |
ca92ce95 | 121 | .IP |
cff88e99 MK |
122 | See |
123 | .BR dup (2) | |
124 | for further details. | |
ef203e45 | 125 | .TP |
ed9821bb | 126 | .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)" |
635d3959 | 127 | As for |
ef203e45 MK |
128 | .BR F_DUPFD , |
129 | but additionally set the | |
d9cb0d7d | 130 | close-on-exec flag for the duplicate file descriptor. |
ef203e45 | 131 | Specifying this flag permits a program to avoid an additional |
0e92e8cc | 132 | .BR fcntl () |
ef203e45 MK |
133 | .B F_SETFD |
134 | operation to set the | |
135 | .B FD_CLOEXEC | |
136 | flag. | |
137 | For an explanation of why this flag is useful, | |
e4b4caa6 | 138 | see the description of |
ef203e45 MK |
139 | .B O_CLOEXEC |
140 | in | |
141 | .BR open (2). | |
73d8cece | 142 | .SS File descriptor flags |
cff88e99 | 143 | The following commands manipulate the flags associated with |
c13182ef | 144 | a file descriptor. |
cff88e99 MK |
145 | Currently, only one such flag is defined: |
146 | .BR FD_CLOEXEC , | |
147 | the close-on-exec flag. | |
148 | If the | |
fea681da | 149 | .B FD_CLOEXEC |
8b4be846 MK |
150 | bit is set, |
151 | the file descriptor will automatically be closed during a successful | |
152 | .BR execve (2). | |
153 | (If the | |
154 | .BR execve (2) | |
155 | fails, the file descriptor is left open.) | |
156 | If the | |
157 | .B FD_CLOEXEC | |
158 | bit is not set, the file descriptor will remain open across an | |
159 | .BR execve (2). | |
fea681da | 160 | .TP |
68e0696a | 161 | .BR F_GETFD " (\fIvoid\fP)" |
f157f403 | 162 | Return (as the function result) the file descriptor flags; |
68e0696a MK |
163 | .I arg |
164 | is ignored. | |
cff88e99 | 165 | .TP |
ed9821bb | 166 | .BR F_SETFD " (\fIint\fP)" |
cff88e99 | 167 | Set the file descriptor flags to the value specified by |
fea681da | 168 | .IR arg . |
2f4772d2 MK |
169 | .PP |
170 | In multithreaded programs, using | |
171 | .BR fcntl () | |
172 | .B F_SETFD | |
173 | to set the close-on-exec flag at the same time as another thread performs a | |
174 | .BR fork (2) | |
175 | plus | |
176 | .BR execve (2) | |
177 | is vulnerable to a race condition that may unintentionally leak | |
178 | the file descriptor to the program executed in the child process. | |
179 | See the discussion of the | |
180 | .BR O_CLOEXEC | |
181 | flag in | |
182 | .BR open (2) | |
183 | for details and a remedy to the problem. | |
73d8cece | 184 | .SS File status flags |
08478724 MK |
185 | Each open file description has certain associated status flags, |
186 | initialized by | |
fea681da MK |
187 | .BR open (2) |
188 | .\" or | |
189 | .\" .BR creat (2), | |
190 | and possibly modified by | |
2777b1ca | 191 | .BR fcntl (). |
c13182ef | 192 | Duplicated file descriptors |
cff88e99 | 193 | (made with |
0bfa087b | 194 | .BR dup (2), |
cff88e99 | 195 | .BR fcntl (F_DUPFD), |
0bfa087b | 196 | .BR fork (2), |
cff88e99 MK |
197 | etc.) refer to the same open file description, and thus |
198 | share the same file status flags. | |
778ac627 | 199 | .PP |
cff88e99 | 200 | The file status flags and their semantics are described in |
fea681da MK |
201 | .BR open (2). |
202 | .TP | |
68e0696a | 203 | .BR F_GETFL " (\fIvoid\fP)" |
f157f403 MK |
204 | Return (as the function result) |
205 | the file access mode and the file status flags; | |
68e0696a MK |
206 | .I arg |
207 | is ignored. | |
fea681da | 208 | .TP |
ed9821bb | 209 | .BR F_SETFL " (\fIint\fP)" |
cff88e99 | 210 | Set the file status flags to the value specified by |
fea681da | 211 | .IR arg . |
c13182ef | 212 | File access mode |
cff88e99 MK |
213 | .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR ) |
214 | and file creation flags | |
68b93ce2 MK |
215 | (i.e., |
216 | .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC ) | |
cff88e99 | 217 | in |
fea681da MK |
218 | .I arg |
219 | are ignored. | |
329ad271 | 220 | On Linux, this command can change only the |
c13182ef | 221 | .BR O_APPEND , |
1c1e15ed MK |
222 | .BR O_ASYNC , |
223 | .BR O_DIRECT , | |
224 | .BR O_NOATIME , | |
c13182ef | 225 | and |
0daa9e92 | 226 | .B O_NONBLOCK |
1c1e15ed | 227 | flags. |
639b33d7 MK |
228 | It is not possible to change the |
229 | .BR O_DSYNC | |
230 | and | |
231 | .BR O_SYNC | |
232 | flags; see BUGS, below. | |
9584daf7 | 233 | .SS Advisory record locking |
7562b10e MK |
234 | Linux implements traditional ("process-associated") UNIX record locks, |
235 | as standardized by POSIX. | |
236 | For a Linux-specific alternative with better semantics, | |
237 | see the discussion of open file description locks below. | |
778ac627 | 238 | .PP |
dc45cae3 MK |
239 | .BR F_SETLK , |
240 | .BR F_SETLKW , | |
241 | and | |
242 | .BR F_GETLK | |
fea681da | 243 | are used to acquire, release, and test for the existence of record |
9584daf7 | 244 | locks (also known as byte-range, file-segment, or file-region locks). |
68e0696a MK |
245 | The third argument, |
246 | .IR lock , | |
fea681da MK |
247 | is a pointer to a structure that has at least the following fields |
248 | (in unspecified order). | |
778ac627 | 249 | .PP |
088a639b | 250 | .in +4n |
778ac627 | 251 | .EX |
fea681da MK |
252 | struct flock { |
253 | ... | |
254 | short l_type; /* Type of lock: F_RDLCK, | |
255 | F_WRLCK, F_UNLCK */ | |
256 | short l_whence; /* How to interpret l_start: | |
257 | SEEK_SET, SEEK_CUR, SEEK_END */ | |
258 | off_t l_start; /* Starting offset for lock */ | |
259 | off_t l_len; /* Number of bytes to lock */ | |
260 | pid_t l_pid; /* PID of process blocking our lock | |
99b5ab79 | 261 | (set by F_GETLK and F_OFD_GETLK) */ |
fea681da MK |
262 | ... |
263 | }; | |
778ac627 | 264 | .EE |
088a639b | 265 | .in |
778ac627 | 266 | .PP |
fea681da MK |
267 | The |
268 | .IR l_whence ", " l_start ", and " l_len | |
269 | fields of this structure specify the range of bytes we wish to lock. | |
057dce78 MK |
270 | Bytes past the end of the file may be locked, |
271 | but not bytes before the start of the file. | |
778ac627 | 272 | .PP |
fea681da MK |
273 | .I l_start |
274 | is the starting offset for the lock, and is interpreted | |
275 | relative to either: | |
276 | the start of the file (if | |
277 | .I l_whence | |
278 | is | |
279 | .BR SEEK_SET ); | |
280 | the current file offset (if | |
281 | .I l_whence | |
282 | is | |
283 | .BR SEEK_CUR ); | |
284 | or the end of the file (if | |
285 | .I l_whence | |
286 | is | |
287 | .BR SEEK_END ). | |
288 | In the final two cases, | |
289 | .I l_start | |
290 | can be a negative number provided the | |
291 | offset does not lie before the start of the file. | |
778ac627 | 292 | .PP |
fea681da | 293 | .I l_len |
057dce78 MK |
294 | specifies the number of bytes to be locked. |
295 | If | |
296 | .I l_len | |
297 | is positive, then the range to be locked covers bytes | |
298 | .I l_start | |
299 | up to and including | |
95dadb39 | 300 | .IR l_start + l_len \-1. |
fea681da MK |
301 | Specifying 0 for |
302 | .I l_len | |
303 | has the special meaning: lock all bytes starting at the | |
304 | location specified by | |
305 | .IR l_whence " and " l_start | |
306 | through to the end of file, no matter how large the file grows. | |
778ac627 | 307 | .PP |
057dce78 MK |
308 | POSIX.1-2001 allows (but does not require) |
309 | an implementation to support a negative | |
310 | .I l_len | |
311 | value; if | |
312 | .I l_len | |
313 | is negative, the interval described by | |
314 | .I lock | |
315 | covers bytes | |
316 | .IR l_start + l_len | |
317 | up to and including | |
318 | .IR l_start \-1. | |
319 | This is supported by Linux since kernel versions 2.4.21 and 2.5.49. | |
778ac627 | 320 | .PP |
fea681da MK |
321 | The |
322 | .I l_type | |
323 | field can be used to place a read | |
324 | .RB ( F_RDLCK ) | |
325 | or a write | |
1c287bbf | 326 | .RB ( F_WRLCK ) |
fea681da MK |
327 | lock on a file. |
328 | Any number of processes may hold a read lock (shared lock) | |
329 | on a file region, but only one process may hold a write lock | |
7c93fec0 MK |
330 | (exclusive lock). |
331 | An exclusive lock excludes all other locks, | |
fea681da MK |
332 | both shared and exclusive. |
333 | A single process can hold only one type of lock on a file region; | |
334 | if a new lock is applied to an already-locked region, | |
8c450534 | 335 | then the existing lock is converted to the new lock type. |
fea681da MK |
336 | (Such conversions may involve splitting, shrinking, or coalescing with |
337 | an existing lock if the byte range specified by the new lock does not | |
338 | precisely coincide with the range of the existing lock.) | |
339 | .TP | |
68e0696a | 340 | .BR F_SETLK " (\fIstruct flock *\fP)" |
fea681da MK |
341 | Acquire a lock (when |
342 | .I l_type | |
343 | is | |
344 | .B F_RDLCK | |
345 | or | |
346 | .BR F_WRLCK ) | |
347 | or release a lock (when | |
348 | .I l_type | |
349 | is | |
350 | .BR F_UNLCK ) | |
351 | on the bytes specified by the | |
352 | .IR l_whence ", " l_start ", and " l_len | |
353 | fields of | |
354 | .IR lock . | |
355 | If a conflicting lock is held by another process, | |
356 | this call returns \-1 and sets | |
357 | .I errno | |
358 | to | |
359 | .B EACCES | |
360 | or | |
361 | .BR EAGAIN . | |
d068eb3b MK |
362 | (The error returned in this case differs across implementations, |
363 | so POSIX requires a portable application to check for both errors.) | |
fea681da | 364 | .TP |
68e0696a | 365 | .BR F_SETLKW " (\fIstruct flock *\fP)" |
fea681da MK |
366 | As for |
367 | .BR F_SETLK , | |
368 | but if a conflicting lock is held on the file, then wait for that | |
369 | lock to be released. | |
370 | If a signal is caught while waiting, then the call is interrupted | |
371 | and (after the signal handler has returned) | |
372 | returns immediately (with return value \-1 and | |
373 | .I errno | |
374 | set to | |
01538d0d MK |
375 | .BR EINTR ; |
376 | see | |
377 | .BR signal (7)). | |
fea681da | 378 | .TP |
68e0696a | 379 | .BR F_GETLK " (\fIstruct flock *\fP)" |
fea681da MK |
380 | On input to this call, |
381 | .I lock | |
382 | describes a lock we would like to place on the file. | |
383 | If the lock could be placed, | |
384 | .BR fcntl () | |
385 | does not actually place it, but returns | |
386 | .B F_UNLCK | |
387 | in the | |
388 | .I l_type | |
389 | field of | |
390 | .I lock | |
391 | and leaves the other fields of the structure unchanged. | |
778ac627 | 392 | .IP |
fea681da MK |
393 | If one or more incompatible locks would prevent |
394 | this lock being placed, then | |
395 | .BR fcntl () | |
99b5ab79 | 396 | returns details about one of those locks in the |
fea681da MK |
397 | .IR l_type ", " l_whence ", " l_start ", and " l_len |
398 | fields of | |
99b5ab79 MK |
399 | .IR lock . |
400 | If the conflicting lock is a traditional (process-associated) record lock, | |
401 | then the | |
fea681da | 402 | .I l_pid |
99b5ab79 MK |
403 | field is set to the PID of the process holding that lock. |
404 | If the conflicting lock is an open file description lock, then | |
2d3e4b83 | 405 | .I l_pid |
99b5ab79 MK |
406 | is set to \-1. |
407 | Note that the returned information | |
eceaee47 | 408 | may already be out of date by the time the caller inspects it. |
11ac5b51 | 409 | .PP |
fea681da MK |
410 | In order to place a read lock, |
411 | .I fd | |
412 | must be open for reading. | |
413 | In order to place a write lock, | |
414 | .I fd | |
415 | must be open for writing. | |
416 | To place both types of lock, open a file read-write. | |
778ac627 | 417 | .PP |
76868835 MK |
418 | When placing locks with |
419 | .BR F_SETLKW , | |
420 | the kernel detects | |
421 | .IR deadlocks , | |
422 | whereby two or more processes have their | |
423 | lock requests mutually blocked by locks held by the other processes. | |
424 | For example, suppose process A holds a write lock on byte 100 of a file, | |
425 | and process B holds a write lock on byte 200. | |
426 | If each process then attempts to lock the byte already | |
427 | locked by the other process using | |
428 | .BR F_SETLKW , | |
429 | then, without deadlock detection, | |
430 | both processes would remain blocked indefinitely. | |
431 | When the kernel detects such deadlocks, | |
432 | it causes one of the blocking lock requests to immediately fail with the error | |
433 | .BR EDEADLK ; | |
434 | an application that encounters such an error should release | |
435 | some of its locks to allow other applications to proceed before | |
436 | attempting regain the locks that it requires. | |
437 | Circular deadlocks involving more than two processes are also detected. | |
438 | Note, however, that there are limitations to the kernel's | |
439 | deadlock-detection algorithm; see BUGS. | |
778ac627 | 440 | .PP |
fea681da MK |
441 | As well as being removed by an explicit |
442 | .BR F_UNLCK , | |
1a93af04 | 443 | record locks are automatically released when the process terminates. |
11ac5b51 | 444 | .PP |
fea681da MK |
445 | Record locks are not inherited by a child created via |
446 | .BR fork (2), | |
447 | but are preserved across an | |
448 | .BR execve (2). | |
11ac5b51 | 449 | .PP |
fea681da MK |
450 | Because of the buffering performed by the |
451 | .BR stdio (3) | |
452 | library, the use of record locking with routines in that package | |
453 | should be avoided; use | |
35e21ba7 | 454 | .BR read (2) |
c13182ef | 455 | and |
35e21ba7 | 456 | .BR write (2) |
fea681da | 457 | instead. |
778ac627 | 458 | .PP |
1a93af04 MK |
459 | The record locks described above are associated with the process |
460 | (unlike the open file description locks described below). | |
461 | This has some unfortunate consequences: | |
462 | .IP * 3 | |
d9312ec7 | 463 | If a process closes |
1a93af04 | 464 | .I any |
d9312ec7 MK |
465 | file descriptor referring to a file, |
466 | then all of the process's locks on that file are released, | |
467 | regardless of the file descriptor(s) on which the locks were obtained. | |
1a93af04 MK |
468 | .\" (Additional file descriptors referring to the same file |
469 | .\" may have been obtained by calls to | |
470 | .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().) | |
471 | This is bad: it means that a process can lose its locks on | |
472 | a file such as | |
473 | .I /etc/passwd | |
474 | or | |
475 | .I /etc/mtab | |
476 | when for some reason a library function decides to open, read, | |
477 | and close the same file. | |
478 | .IP * | |
479 | The threads in a process share locks. | |
480 | In other words, | |
481 | a multithreaded program can't use record locking to ensure | |
97e03523 | 482 | that threads don't simultaneously access the same region of a file. |
1a93af04 MK |
483 | .PP |
484 | Open file description locks solve both of these problems. | |
2d3e4b83 | 485 | .SS Open file description locks (non-POSIX) |
bfb14716 MK |
486 | Open file description locks are advisory byte-range locks whose operation is |
487 | in most respects identical to the traditional record locks described above. | |
488 | This lock type is Linux-specific, | |
38702b6c | 489 | and available since Linux 3.15. |
728aff57 MK |
490 | (There is a proposal with the Austin Group |
491 | .\" FIXME . Review progress into POSIX | |
492 | .\" http://austingroupbugs.net/view.php?id=768 | |
493 | to include this lock type in the next revision of POSIX.1.) | |
64515702 | 494 | For an explanation of open file descriptions, see |
b7a3dc84 | 495 | .BR open (2). |
778ac627 | 496 | .PP |
99b5ab79 MK |
497 | The principal difference between the two lock types |
498 | is that whereas traditional record locks | |
499 | are associated with a process, | |
500 | open file description locks are associated with the | |
501 | open file description on which they are acquired, | |
502 | much like locks acquired with | |
503 | .BR flock (2). | |
504 | Consequently (and unlike traditional advisory record locks), | |
505 | open file description locks are inherited across | |
2d3e4b83 | 506 | .BR fork (2) |
99b5ab79 | 507 | (and |
2d3e4b83 JL |
508 | .BR clone (2) |
509 | with | |
99b5ab79 MK |
510 | .BR CLONE_FILES ), |
511 | and are only automatically released on the last close | |
512 | of the open file description, | |
513 | instead of being released on any close of the file. | |
2d3e4b83 | 514 | .PP |
ee662773 MK |
515 | Conflicting lock combinations |
516 | (i.e., a read lock and a write lock or two write locks) | |
517 | where one lock is an open file description lock and the other | |
518 | is a traditional record lock conflict | |
2d3e4b83 | 519 | even when they are acquired by the same process on the same file descriptor. |
778ac627 | 520 | .PP |
97e03523 MK |
521 | Open file description locks placed via the same open file description |
522 | (i.e., via the same file descriptor, | |
523 | or via a duplicate of the file descriptor created by | |
524 | .BR fork (2), | |
525 | .BR dup (2), | |
bf7bc8b8 | 526 | .BR fcntl () |
97e03523 MK |
527 | .BR F_DUPFD , |
528 | and so on) are always compatible: | |
529 | if a new lock is placed on an already locked region, | |
530 | then the existing lock is converted to the new lock type. | |
531 | (Such conversions may result in splitting, shrinking, or coalescing with | |
532 | an existing lock as discussed above.) | |
778ac627 | 533 | .PP |
97e03523 MK |
534 | On the other hand, open file description locks may conflict with |
535 | each other when they are acquired via different open file descriptions. | |
536 | Thus, the threads in a multithreaded program can use | |
537 | open file description locks to synchronize access to a file region | |
538 | by having each thread perform its own | |
539 | .BR open (2) | |
540 | on the file and applying locks via the resulting file descriptor. | |
2d3e4b83 | 541 | .PP |
99b5ab79 MK |
542 | As with traditional advisory locks, the third argument to |
543 | .BR fcntl (), | |
544 | .IR lock , | |
545 | is a pointer to an | |
546 | .IR flock | |
547 | structure. | |
548 | By contrast with traditional record locks, the | |
2d3e4b83 | 549 | .I l_pid |
99b5ab79 MK |
550 | field of that structure must be set to zero |
551 | when using the commands described below. | |
778ac627 | 552 | .PP |
99b5ab79 MK |
553 | The commands for working with open file description locks are analogous |
554 | to those used with traditional locks: | |
2d3e4b83 JL |
555 | .TP |
556 | .BR F_OFD_SETLK " (\fIstruct flock *\fP)" | |
557 | Acquire an open file description lock (when | |
558 | .I l_type | |
559 | is | |
560 | .B F_RDLCK | |
561 | or | |
562 | .BR F_WRLCK ) | |
563 | or release an open file description lock (when | |
564 | .I l_type | |
565 | is | |
566 | .BR F_UNLCK ) | |
567 | on the bytes specified by the | |
568 | .IR l_whence ", " l_start ", and " l_len | |
569 | fields of | |
570 | .IR lock . | |
571 | If a conflicting lock is held by another process, | |
572 | this call returns \-1 and sets | |
573 | .I errno | |
574 | to | |
2d3e4b83 JL |
575 | .BR EAGAIN . |
576 | .TP | |
577 | .BR F_OFD_SETLKW " (\fIstruct flock *\fP)" | |
578 | As for | |
579 | .BR F_OFD_SETLK , | |
580 | but if a conflicting lock is held on the file, then wait for that lock to be | |
99b5ab79 MK |
581 | released. |
582 | If a signal is caught while waiting, then the call is interrupted | |
583 | and (after the signal handler has returned) returns immediately | |
584 | (with return value \-1 and | |
2d3e4b83 JL |
585 | .I errno |
586 | set to | |
587 | .BR EINTR ; | |
588 | see | |
589 | .BR signal (7)). | |
590 | .TP | |
591 | .BR F_OFD_GETLK " (\fIstruct flock *\fP)" | |
592 | On input to this call, | |
593 | .I lock | |
594 | describes an open file description lock we would like to place on the file. | |
595 | If the lock could be placed, | |
596 | .BR fcntl () | |
597 | does not actually place it, but returns | |
598 | .B F_UNLCK | |
599 | in the | |
600 | .I l_type | |
601 | field of | |
602 | .I lock | |
603 | and leaves the other fields of the structure unchanged. | |
99b5ab79 MK |
604 | If one or more incompatible locks would prevent this lock being placed, |
605 | then details about one of these locks are returned via | |
606 | .IR lock , | |
607 | as described above for | |
608 | .BR F_GETLK . | |
d2e429c1 MK |
609 | .PP |
610 | In the current implementation, | |
611 | .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7 | |
612 | no deadlock detection is performed for open file description locks. | |
613 | (This contrasts with process-associated record locks, | |
614 | for which the kernel does perform deadlock detection.) | |
615 | .\" | |
73d8cece | 616 | .SS Mandatory locking |
f5cdb4b0 MK |
617 | .IR Warning : |
618 | the Linux implementation of mandatory locking is unreliable. | |
619 | See BUGS below. | |
e1124af0 MK |
620 | Because of these bugs, |
621 | and the fact that the feature is believed to be little used, | |
622 | since Linux 4.5, mandatory locking has been made an optional feature, | |
623 | governed by a configuration option | |
624 | .RB ( CONFIG_MANDATORY_FILE_LOCKING ). | |
625 | This is an initial step toward removing this feature completely. | |
778ac627 | 626 | .PP |
9acee578 MK |
627 | By default, both traditional (process-associated) and open file description |
628 | record locks are advisory. | |
fea681da | 629 | Advisory locks are not enforced and are useful only between |
c13182ef | 630 | cooperating processes. |
778ac627 | 631 | .PP |
9acee578 | 632 | Both lock types can also be mandatory. |
c13182ef | 633 | Mandatory locks are enforced for all processes. |
00d7bbcf MK |
634 | If a process tries to perform an incompatible access (e.g., |
635 | .BR read (2) | |
636 | or | |
637 | .BR write (2)) | |
638 | on a file region that has an incompatible mandatory lock, | |
639 | then the result depends upon whether the | |
640 | .B O_NONBLOCK | |
641 | flag is enabled for its open file description. | |
c13182ef | 642 | If the |
00d7bbcf MK |
643 | .B O_NONBLOCK |
644 | flag is not enabled, then | |
6799ba26 | 645 | the system call is blocked until the lock is removed |
00d7bbcf | 646 | or converted to a mode that is compatible with the access. |
c13182ef MK |
647 | If the |
648 | .B O_NONBLOCK | |
00d7bbcf | 649 | flag is enabled, then the system call fails with the error |