| 1 |
.\" This manpage is Copyright (C) 1992 Drew Eckhardt; |
| 2 |
.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson; |
| 3 |
.\" and Copyright (C) 1998 Jamie Lokier; |
| 4 |
.\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk; |
| 5 |
.\" and Copyright (C) 2014 Jeff Layton |
| 6 |
.\" and Copyright (C) 2014 David Herrmann |
| 7 |
.\" and Copyright (C) 2017 Jens Axboe |
| 8 |
.\" |
| 9 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 10 |
.\" |
| 11 |
.\" Modified 1993-07-24 by Rik Faith <[email protected]> |
| 12 |
.\" Modified 1995-09-26 by Andries Brouwer <[email protected]> |
| 13 |
.\" and again on 960413 and 980804 and 981223. |
| 14 |
.\" Modified 1998-12-11 by Jamie Lokier <[email protected]> |
| 15 |
.\" Applied correction by Christian Ehrhardt - aeb, 990712 |
| 16 |
.\" Modified 2002-04-23 by Michael Kerrisk <[email protected]> |
| 17 |
.\" Added note on F_SETFL and O_DIRECT |
| 18 |
.\" Complete rewrite + expansion of material on file locking |
| 19 |
.\" Incorporated description of F_NOTIFY, drawing on |
| 20 |
.\" Stephen Rothwell's notes in Documentation/dnotify.txt. |
| 21 |
.\" Added description of F_SETLEASE and F_GETLEASE |
| 22 |
.\" Corrected and polished, aeb, 020527. |
| 23 |
.\" Modified 2004-03-03 by Michael Kerrisk <[email protected]> |
| 24 |
.\" Modified description of file leases: fixed some errors of detail |
| 25 |
.\" Replaced the term "lease contestant" by "lease breaker" |
| 26 |
.\" Modified, 27 May 2004, Michael Kerrisk <[email protected]> |
| 27 |
.\" Added notes on capability requirements |
| 28 |
.\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool |
| 29 |
.\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb. |
| 30 |
.\" 2005-04-08 Jamie Lokier <[email protected]>, mtk |
| 31 |
.\" Described behavior of F_SETOWN/F_SETSIG in |
| 32 |
.\" multithreaded processes, and generally cleaned |
| 33 |
.\" up the discussion of F_SETOWN. |
| 34 |
.\" 2005-05-20, Johannes Nicolai <[email protected]>, |
| 35 |
.\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4 |
| 36 |
.\" and earlier. Added text on permissions required to send signal. |
| 37 |
.\" 2009-09-30, Michael Kerrisk |
| 38 |
.\" Note obsolete F_SETOWN behavior with threads. |
| 39 |
.\" Document F_SETOWN_EX and F_GETOWN_EX |
| 40 |
.\" 2010-06-17, Michael Kerrisk |
| 41 |
.\" Document F_SETPIPE_SZ and F_GETPIPE_SZ. |
| 42 |
.\" 2014-07-08, David Herrmann <[email protected]> |
| 43 |
.\" Document F_ADD_SEALS and F_GET_SEALS |
| 44 |
.\" 2017-06-26, Jens Axboe <[email protected]> |
| 45 |
.\" Document F_{GET,SET}_RW_HINT and F_{GET,SET}_FILE_RW_HINT |
| 46 |
.\" |
| 47 |
.TH fcntl 2 2024-05-02 "Linux man-pages 6.9.1" |
| 48 |
.SH NAME |
| 49 |
fcntl \- manipulate file descriptor |
| 50 |
.SH LIBRARY |
| 51 |
Standard C library |
| 52 |
.RI ( libc ", " \-lc ) |
| 53 |
.SH SYNOPSIS |
| 54 |
.nf |
| 55 |
.B #include <fcntl.h> |
| 56 |
.P |
| 57 |
.BI "int fcntl(int " fd ", int " op ", ... /* " arg " */ );" |
| 58 |
.fi |
| 59 |
.SH DESCRIPTION |
| 60 |
.BR fcntl () |
| 61 |
performs one of the operations described below on the open file descriptor |
| 62 |
.IR fd . |
| 63 |
The operation is determined by |
| 64 |
.IR op . |
| 65 |
.P |
| 66 |
.BR fcntl () |
| 67 |
can take an optional third argument. |
| 68 |
Whether or not this argument is required is determined by |
| 69 |
.IR op . |
| 70 |
The required argument type is indicated in parentheses after each |
| 71 |
.I op |
| 72 |
name (in most cases, the required type is |
| 73 |
.IR int , |
| 74 |
and we identify the argument using the name |
| 75 |
.IR arg ), |
| 76 |
or |
| 77 |
.I void |
| 78 |
is specified if the argument is not required. |
| 79 |
.P |
| 80 |
Certain of the operations below are supported only since a particular |
| 81 |
Linux kernel version. |
| 82 |
The preferred method of checking whether the host kernel supports |
| 83 |
a particular operation is to invoke |
| 84 |
.BR fcntl () |
| 85 |
with the desired |
| 86 |
.I op |
| 87 |
value and then test whether the call failed with |
| 88 |
.BR EINVAL , |
| 89 |
indicating that the kernel does not recognize this value. |
| 90 |
.SS Duplicating a file descriptor |
| 91 |
.TP |
| 92 |
.BR F_DUPFD " (\fIint\fP)" |
| 93 |
Duplicate the file descriptor |
| 94 |
.I fd |
| 95 |
using the lowest-numbered available file descriptor greater than or equal to |
| 96 |
.IR arg . |
| 97 |
This is different from |
| 98 |
.BR dup2 (2), |
| 99 |
which uses exactly the file descriptor specified. |
| 100 |
.IP |
| 101 |
On success, the new file descriptor is returned. |
| 102 |
.IP |
| 103 |
See |
| 104 |
.BR dup (2) |
| 105 |
for further details. |
| 106 |
.TP |
| 107 |
.BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)" |
| 108 |
As for |
| 109 |
.BR F_DUPFD , |
| 110 |
but additionally set the |
| 111 |
close-on-exec flag for the duplicate file descriptor. |
| 112 |
Specifying this flag permits a program to avoid an additional |
| 113 |
.BR fcntl () |
| 114 |
.B F_SETFD |
| 115 |
operation to set the |
| 116 |
.B FD_CLOEXEC |
| 117 |
flag. |
| 118 |
For an explanation of why this flag is useful, |
| 119 |
see the description of |
| 120 |
.B O_CLOEXEC |
| 121 |
in |
| 122 |
.BR open (2). |
| 123 |
.SS File descriptor flags |
| 124 |
The following operations manipulate the flags associated with |
| 125 |
a file descriptor. |
| 126 |
Currently, only one such flag is defined: |
| 127 |
.BR FD_CLOEXEC , |
| 128 |
the close-on-exec flag. |
| 129 |
If the |
| 130 |
.B FD_CLOEXEC |
| 131 |
bit is set, |
| 132 |
the file descriptor will automatically be closed during a successful |
| 133 |
.BR execve (2). |
| 134 |
(If the |
| 135 |
.BR execve (2) |
| 136 |
fails, the file descriptor is left open.) |
| 137 |
If the |
| 138 |
.B FD_CLOEXEC |
| 139 |
bit is not set, the file descriptor will remain open across an |
| 140 |
.BR execve (2). |
| 141 |
.TP |
| 142 |
.BR F_GETFD " (\fIvoid\fP)" |
| 143 |
Return (as the function result) the file descriptor flags; |
| 144 |
.I arg |
| 145 |
is ignored. |
| 146 |
.TP |
| 147 |
.BR F_SETFD " (\fIint\fP)" |
| 148 |
Set the file descriptor flags to the value specified by |
| 149 |
.IR arg . |
| 150 |
.P |
| 151 |
In multithreaded programs, using |
| 152 |
.BR fcntl () |
| 153 |
.B F_SETFD |
| 154 |
to set the close-on-exec flag at the same time as another thread performs a |
| 155 |
.BR fork (2) |
| 156 |
plus |
| 157 |
.BR execve (2) |
| 158 |
is vulnerable to a race condition that may unintentionally leak |
| 159 |
the file descriptor to the program executed in the child process. |
| 160 |
See the discussion of the |
| 161 |
.B O_CLOEXEC |
| 162 |
flag in |
| 163 |
.BR open (2) |
| 164 |
for details and a remedy to the problem. |
| 165 |
.SS File status flags |
| 166 |
Each open file description has certain associated status flags, |
| 167 |
initialized by |
| 168 |
.BR open (2) |
| 169 |
.\" or |
| 170 |
.\" .BR creat (2), |
| 171 |
and possibly modified by |
| 172 |
.BR fcntl (). |
| 173 |
Duplicated file descriptors |
| 174 |
(made with |
| 175 |
.BR dup (2), |
| 176 |
.BR fcntl (F_DUPFD), |
| 177 |
.BR fork (2), |
| 178 |
etc.) refer to the same open file description, and thus |
| 179 |
share the same file status flags. |
| 180 |
.P |
| 181 |
The file status flags and their semantics are described in |
| 182 |
.BR open (2). |
| 183 |
.TP |
| 184 |
.BR F_GETFL " (\fIvoid\fP)" |
| 185 |
Return (as the function result) |
| 186 |
the file access mode and the file status flags; |
| 187 |
.I arg |
| 188 |
is ignored. |
| 189 |
.TP |
| 190 |
.BR F_SETFL " (\fIint\fP)" |
| 191 |
Set the file status flags to the value specified by |
| 192 |
.IR arg . |
| 193 |
File access mode |
| 194 |
.RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR ) |
| 195 |
and file creation flags |
| 196 |
(i.e., |
| 197 |
.BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC ) |
| 198 |
in |
| 199 |
.I arg |
| 200 |
are ignored. |
| 201 |
On Linux, this operation can change only the |
| 202 |
.BR O_APPEND , |
| 203 |
.BR O_ASYNC , |
| 204 |
.BR O_DIRECT , |
| 205 |
.BR O_NOATIME , |
| 206 |
and |
| 207 |
.B O_NONBLOCK |
| 208 |
flags. |
| 209 |
It is not possible to change the |
| 210 |
.B O_DSYNC |
| 211 |
and |
| 212 |
.B O_SYNC |
| 213 |
flags; see BUGS, below. |
| 214 |
.SS Advisory record locking |
| 215 |
Linux implements traditional ("process-associated") UNIX record locks, |
| 216 |
as standardized by POSIX. |
| 217 |
For a Linux-specific alternative with better semantics, |
| 218 |
see the discussion of open file description locks below. |
| 219 |
.P |
| 220 |
.BR F_SETLK , |
| 221 |
.BR F_SETLKW , |
| 222 |
and |
| 223 |
.B F_GETLK |
| 224 |
are used to acquire, release, and test for the existence of record |
| 225 |
locks (also known as byte-range, file-segment, or file-region locks). |
| 226 |
The third argument, |
| 227 |
.IR lock , |
| 228 |
is a pointer to a structure that has at least the following fields |
| 229 |
(in unspecified order). |
| 230 |
.P |
| 231 |
.in +4n |
| 232 |
.EX |
| 233 |
struct flock { |
| 234 |
... |
| 235 |
short l_type; /* Type of lock: F_RDLCK, |
| 236 |
F_WRLCK, F_UNLCK */ |
| 237 |
short l_whence; /* How to interpret l_start: |
| 238 |
SEEK_SET, SEEK_CUR, SEEK_END */ |
| 239 |
off_t l_start; /* Starting offset for lock */ |
| 240 |
off_t l_len; /* Number of bytes to lock */ |
| 241 |
pid_t l_pid; /* PID of process blocking our lock |
| 242 |
(set by F_GETLK and F_OFD_GETLK) */ |
| 243 |
... |
| 244 |
}; |
| 245 |
.EE |
| 246 |
.in |
| 247 |
.P |
| 248 |
The |
| 249 |
.IR l_whence ", " l_start ", and " l_len |
| 250 |
fields of this structure specify the range of bytes we wish to lock. |
| 251 |
Bytes past the end of the file may be locked, |
| 252 |
but not bytes before the start of the file. |
| 253 |
.P |
| 254 |
.I l_start |
| 255 |
is the starting offset for the lock, and is interpreted |
| 256 |
relative to either: |
| 257 |
the start of the file (if |
| 258 |
.I l_whence |
| 259 |
is |
| 260 |
.BR SEEK_SET ); |
| 261 |
the current file offset (if |
| 262 |
.I l_whence |
| 263 |
is |
| 264 |
.BR SEEK_CUR ); |
| 265 |
or the end of the file (if |
| 266 |
.I l_whence |
| 267 |
is |
| 268 |
.BR SEEK_END ). |
| 269 |
In the final two cases, |
| 270 |
.I l_start |
| 271 |
can be a negative number provided the |
| 272 |
offset does not lie before the start of the file. |
| 273 |
.P |
| 274 |
.I l_len |
| 275 |
specifies the number of bytes to be locked. |
| 276 |
If |
| 277 |
.I l_len |
| 278 |
is positive, then the range to be locked covers bytes |
| 279 |
.I l_start |
| 280 |
up to and including |
| 281 |
.IR l_start + l_len \-1. |
| 282 |
Specifying 0 for |
| 283 |
.I l_len |
| 284 |
has the special meaning: lock all bytes starting at the |
| 285 |
location specified by |
| 286 |
.IR l_whence " and " l_start |
| 287 |
through to the end of file, no matter how large the file grows. |
| 288 |
.P |
| 289 |
POSIX.1-2001 allows (but does not require) |
| 290 |
an implementation to support a negative |
| 291 |
.I l_len |
| 292 |
value; if |
| 293 |
.I l_len |
| 294 |
is negative, the interval described by |
| 295 |
.I lock |
| 296 |
covers bytes |
| 297 |
.IR l_start + l_len |
| 298 |
up to and including |
| 299 |
.IR l_start \-1. |
| 300 |
This is supported since Linux 2.4.21 and Linux 2.5.49. |
| 301 |
.P |
| 302 |
The |
| 303 |
.I l_type |
| 304 |
field can be used to place a read |
| 305 |
.RB ( F_RDLCK ) |
| 306 |
or a write |
| 307 |
.RB ( F_WRLCK ) |
| 308 |
lock on a file. |
| 309 |
Any number of processes may hold a read lock (shared lock) |
| 310 |
on a file region, but only one process may hold a write lock |
| 311 |
(exclusive lock). |
| 312 |
An exclusive lock excludes all other locks, |
| 313 |
both shared and exclusive. |
| 314 |
A single process can hold only one type of lock on a file region; |
| 315 |
if a new lock is applied to an already-locked region, |
| 316 |
then the existing lock is converted to the new lock type. |
| 317 |
(Such conversions may involve splitting, shrinking, or coalescing with |
| 318 |
an existing lock if the byte range specified by the new lock does not |
| 319 |
precisely coincide with the range of the existing lock.) |
| 320 |
.TP |
| 321 |
.BR F_SETLK " (\fIstruct flock *\fP)" |
| 322 |
Acquire a lock (when |
| 323 |
.I l_type |
| 324 |
is |
| 325 |
.B F_RDLCK |
| 326 |
or |
| 327 |
.BR F_WRLCK ) |
| 328 |
or release a lock (when |
| 329 |
.I l_type |
| 330 |
is |
| 331 |
.BR F_UNLCK ) |
| 332 |
on the bytes specified by the |
| 333 |
.IR l_whence ", " l_start ", and " l_len |
| 334 |
fields of |
| 335 |
.IR lock . |
| 336 |
If a conflicting lock is held by another process, |
| 337 |
this call returns \-1 and sets |
| 338 |
.I errno |
| 339 |
to |
| 340 |
.B EACCES |
| 341 |
or |
| 342 |
.BR EAGAIN . |
| 343 |
(The error returned in this case differs across implementations, |
| 344 |
so POSIX requires a portable application to check for both errors.) |
| 345 |
.TP |
| 346 |
.BR F_SETLKW " (\fIstruct flock *\fP)" |
| 347 |
As for |
| 348 |
.BR F_SETLK , |
| 349 |
but if a conflicting lock is held on the file, then wait for that |
| 350 |
lock to be released. |
| 351 |
If a signal is caught while waiting, then the call is interrupted |
| 352 |
and (after the signal handler has returned) |
| 353 |
returns immediately (with return value \-1 and |
| 354 |
.I errno |
| 355 |
set to |
| 356 |
.BR EINTR ; |
| 357 |
see |
| 358 |
.BR signal (7)). |
| 359 |
.TP |
| 360 |
.BR F_GETLK " (\fIstruct flock *\fP)" |
| 361 |
On input to this call, |
| 362 |
.I lock |
| 363 |
describes a lock we would like to place on the file. |
| 364 |
If the lock could be placed, |
| 365 |
.BR fcntl () |
| 366 |
does not actually place it, but returns |
| 367 |
.B F_UNLCK |
| 368 |
in the |
| 369 |
.I l_type |
| 370 |
field of |
| 371 |
.I lock |
| 372 |
and leaves the other fields of the structure unchanged. |
| 373 |
.IP |
| 374 |
If one or more incompatible locks would prevent |
| 375 |
this lock being placed, then |
| 376 |
.BR fcntl () |
| 377 |
returns details about one of those locks in the |
| 378 |
.IR l_type ", " l_whence ", " l_start ", and " l_len |
| 379 |
fields of |
| 380 |
.IR lock . |
| 381 |
If the conflicting lock is a traditional (process-associated) record lock, |
| 382 |
then the |
| 383 |
.I l_pid |
| 384 |
field is set to the PID of the process holding that lock. |
| 385 |
If the conflicting lock is an open file description lock, then |
| 386 |
.I l_pid |
| 387 |
is set to \-1. |
| 388 |
Note that the returned information |
| 389 |
may already be out of date by the time the caller inspects it. |
| 390 |
.P |
| 391 |
In order to place a read lock, |
| 392 |
.I fd |
| 393 |
must be open for reading. |
| 394 |
In order to place a write lock, |
| 395 |
.I fd |
| 396 |
must be open for writing. |
| 397 |
To place both types of lock, open a file read-write. |
| 398 |
.P |
| 399 |
When placing locks with |
| 400 |
.BR F_SETLKW , |
| 401 |
the kernel detects |
| 402 |
.IR deadlocks , |
| 403 |
whereby two or more processes have their |
| 404 |
lock requests mutually blocked by locks held by the other processes. |
| 405 |
For example, suppose process A holds a write lock on byte 100 of a file, |
| 406 |
and process B holds a write lock on byte 200. |
| 407 |
If each process then attempts to lock the byte already |
| 408 |
locked by the other process using |
| 409 |
.BR F_SETLKW , |
| 410 |
then, without deadlock detection, |
| 411 |
both processes would remain blocked indefinitely. |
| 412 |
When the kernel detects such deadlocks, |
| 413 |
it causes one of the blocking lock requests to immediately fail with the error |
| 414 |
.BR EDEADLK ; |
| 415 |
an application that encounters such an error should release |
| 416 |
some of its locks to allow other applications to proceed before |
| 417 |
attempting regain the locks that it requires. |
| 418 |
Circular deadlocks involving more than two processes are also detected. |
| 419 |
Note, however, that there are limitations to the kernel's |
| 420 |
deadlock-detection algorithm; see BUGS. |
| 421 |
.P |
| 422 |
As well as being removed by an explicit |
| 423 |
.BR F_UNLCK , |
| 424 |
record locks are automatically released when the process terminates. |
| 425 |
.P |
| 426 |
Record locks are not inherited by a child created via |
| 427 |
.BR fork (2), |
| 428 |
but are preserved across an |
| 429 |
.BR execve (2). |
| 430 |
.P |
| 431 |
Because of the buffering performed by the |
| 432 |
.BR stdio (3) |
| 433 |
library, the use of record locking with routines in that package |
| 434 |
should be avoided; use |
| 435 |
.BR read (2) |
| 436 |
and |
| 437 |
.BR write (2) |
| 438 |
instead. |
| 439 |
.P |
| 440 |
The record locks described above are associated with the process |
| 441 |
(unlike the open file description locks described below). |
| 442 |
This has some unfortunate consequences: |
| 443 |
.IP \[bu] 3 |
| 444 |
If a process closes |
| 445 |
.I any |
| 446 |
file descriptor referring to a file, |
| 447 |
then all of the process's locks on that file are released, |
| 448 |
regardless of the file descriptor(s) on which the locks were obtained. |
| 449 |
.\" (Additional file descriptors referring to the same file |
| 450 |
.\" may have been obtained by calls to |
| 451 |
.\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().) |
| 452 |
This is bad: it means that a process can lose its locks on |
| 453 |
a file such as |
| 454 |
.I /etc/passwd |
| 455 |
or |
| 456 |
.I /etc/mtab |
| 457 |
when for some reason a library function decides to open, read, |
| 458 |
and close the same file. |
| 459 |
.IP \[bu] |
| 460 |
The threads in a process share locks. |
| 461 |
In other words, |
| 462 |
a multithreaded program can't use record locking to ensure |
| 463 |
that threads don't simultaneously access the same region of a file. |
| 464 |
.P |
| 465 |
Open file description locks solve both of these problems. |
| 466 |
.SS Open file description locks (non-POSIX) |
| 467 |
Open file description locks are advisory byte-range locks whose operation is |
| 468 |
in most respects identical to the traditional record locks described above. |
| 469 |
This lock type is Linux-specific, |
| 470 |
and available since Linux 3.15. |
| 471 |
(There is a proposal with the Austin Group |
| 472 |
.\" FIXME . Review progress into POSIX |
| 473 |
.\" http://austingroupbugs.net/view.php?id=768 |
| 474 |
to include this lock type in the next revision of POSIX.1.) |
| 475 |
For an explanation of open file descriptions, see |
| 476 |
.BR open (2). |
| 477 |
.P |
| 478 |
The principal difference between the two lock types |
| 479 |
is that whereas traditional record locks |
| 480 |
are associated with a process, |
| 481 |
open file description locks are associated with the |
| 482 |
open file description on which they are acquired, |
| 483 |
much like locks acquired with |
| 484 |
.BR flock (2). |
| 485 |
Consequently (and unlike traditional advisory record locks), |
| 486 |
open file description locks are inherited across |
| 487 |
.BR fork (2) |
| 488 |
(and |
| 489 |
.BR clone (2) |
| 490 |
with |
| 491 |
.BR CLONE_FILES ), |
| 492 |
and are only automatically released on the last close |
| 493 |
of the open file description, |
| 494 |
instead of being released on any close of the file. |
| 495 |
.P |
| 496 |
Conflicting lock combinations |
| 497 |
(i.e., a read lock and a write lock or two write locks) |
| 498 |
where one lock is an open file description lock and the other |
| 499 |
is a traditional record lock conflict |
| 500 |
even when they are acquired by the same process on the same file descriptor. |
| 501 |
.P |
| 502 |
Open file description locks placed via the same open file description |
| 503 |
(i.e., via the same file descriptor, |
| 504 |
or via a duplicate of the file descriptor created by |
| 505 |
.BR fork (2), |
| 506 |
.BR dup (2), |
| 507 |
.BR fcntl () |
| 508 |
.BR F_DUPFD , |
| 509 |
and so on) are always compatible: |
| 510 |
if a new lock is placed on an already locked region, |
| 511 |
then the existing lock is converted to the new lock type. |
| 512 |
(Such conversions may result in splitting, shrinking, or coalescing with |
| 513 |
an existing lock as discussed above.) |
| 514 |
.P |
| 515 |
On the other hand, open file description locks may conflict with |
| 516 |
each other when they are acquired via different open file descriptions. |
| 517 |
Thus, the threads in a multithreaded program can use |
| 518 |
open file description locks to synchronize access to a file region |
| 519 |
by having each thread perform its own |
| 520 |
.BR open (2) |
| 521 |
on the file and applying locks via the resulting file descriptor. |
| 522 |
.P |
| 523 |
As with traditional advisory locks, the third argument to |
| 524 |
.BR fcntl (), |
| 525 |
.IR lock , |
| 526 |
is a pointer to an |
| 527 |
.I flock |
| 528 |
structure. |
| 529 |
By contrast with traditional record locks, the |
| 530 |
.I l_pid |
| 531 |
field of that structure must be set to zero |
| 532 |
when using the operations described below. |
| 533 |
.P |
| 534 |
The operations for working with open file description locks are analogous |
| 535 |
to those used with traditional locks: |
| 536 |
.TP |
| 537 |
.BR F_OFD_SETLK " (\fIstruct flock *\fP)" |
| 538 |
Acquire an open file description lock (when |
| 539 |
.I l_type |
| 540 |
is |
| 541 |
.B F_RDLCK |
| 542 |
or |
| 543 |
.BR F_WRLCK ) |
| 544 |
or release an open file description lock (when |
| 545 |
.I l_type |
| 546 |
is |
| 547 |
.BR F_UNLCK ) |
| 548 |
on the bytes specified by the |
| 549 |
.IR l_whence ", " l_start ", and " l_len |
| 550 |
fields of |
| 551 |
.IR lock . |
| 552 |
If a conflicting lock is held by another process, |
| 553 |
this call returns \-1 and sets |
| 554 |
.I errno |
| 555 |
to |
| 556 |
.BR EAGAIN . |
| 557 |
.TP |
| 558 |
.BR F_OFD_SETLKW " (\fIstruct flock *\fP)" |
| 559 |
As for |
| 560 |
.BR F_OFD_SETLK , |
| 561 |
but if a conflicting lock is held on the file, then wait for that lock to be |
| 562 |
released. |
| 563 |
If a signal is caught while waiting, then the call is interrupted |
| 564 |
and (after the signal handler has returned) returns immediately |
| 565 |
(with return value \-1 and |
| 566 |
.I errno |
| 567 |
set to |
| 568 |
.BR EINTR ; |
| 569 |
see |
| 570 |
.BR signal (7)). |
| 571 |
.TP |
| 572 |
.BR F_OFD_GETLK " (\fIstruct flock *\fP)" |
| 573 |
On input to this call, |
| 574 |
.I lock |
| 575 |
describes an open file description lock we would like to place on the file. |
| 576 |
If the lock could be placed, |
| 577 |
.BR fcntl () |
| 578 |
does not actually place it, but returns |
| 579 |
.B F_UNLCK |
| 580 |
in the |
| 581 |
.I l_type |
| 582 |
field of |
| 583 |
.I lock |
| 584 |
and leaves the other fields of the structure unchanged. |
| 585 |
If one or more incompatible locks would prevent this lock being placed, |
| 586 |
then details about one of these locks are returned via |
| 587 |
.IR lock , |
| 588 |
as described above for |
| 589 |
.BR F_GETLK . |
| 590 |
.P |
| 591 |
In the current implementation, |
| 592 |
.\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7 |
| 593 |
no deadlock detection is performed for open file description locks. |
| 594 |
(This contrasts with process-associated record locks, |
| 595 |
for which the kernel does perform deadlock detection.) |
| 596 |
.\" |
| 597 |
.SS Mandatory locking |
| 598 |
.IR Warning : |
| 599 |
the Linux implementation of mandatory locking is unreliable. |
| 600 |
See BUGS below. |
| 601 |
Because of these bugs, |
| 602 |
and the fact that the feature is believed to be little used, |
| 603 |
since Linux 4.5, mandatory locking has been made an optional feature, |
| 604 |
governed by a configuration option |
| 605 |
.RB ( CONFIG_MANDATORY_FILE_LOCKING ). |
| 606 |
This feature is no longer supported at all in Linux 5.15 and above. |
| 607 |
.P |
| 608 |
By default, both traditional (process-associated) and open file description |
| 609 |
record locks are advisory. |
| 610 |
Advisory locks are not enforced and are useful only between |
| 611 |
cooperating processes. |
| 612 |
.P |
| 613 |
Both lock types can also be mandatory. |
| 614 |
Mandatory locks are enforced for all processes. |
| 615 |
If a process tries to perform an incompatible access (e.g., |
| 616 |
.BR read (2) |
| 617 |
or |
| 618 |
.BR write (2)) |
| 619 |
on a file region that has an incompatible mandatory lock, |
| 620 |
then the result depends upon whether the |
| 621 |
.B O_NONBLOCK |
| 622 |
flag is enabled for its open file description. |
| 623 |
If the |
| 624 |
.B O_NONBLOCK |
| 625 |
flag is not enabled, then |
| 626 |
the system call is blocked until the lock is removed |
| 627 |
or converted to a mode that is compatible with the access. |
| 628 |
If the |
| 629 |
.B O_NONBLOCK |
| 630 |
flag is enabled, then the system call fails with the error |
| 631 |
.BR EAGAIN . |
| 632 |
.P |
| 633 |
To make use of mandatory locks, mandatory locking must be enabled |
| 634 |
both on the filesystem that contains the file to be locked, |
| 635 |
and on the file itself. |
| 636 |
Mandatory locking is enabled on a filesystem |
| 637 |
using the "\-o mand" option to |
| 638 |
.BR mount (8), |
| 639 |
or the |
| 640 |
.B MS_MANDLOCK |
| 641 |
flag for |
| 642 |
.BR mount (2). |
| 643 |
Mandatory locking is enabled on a file by disabling |
| 644 |
group execute permission on the file and enabling the set-group-ID |
| 645 |
permission bit (see |
| 646 |
.BR chmod (1) |
| 647 |
and |
| 648 |
.BR chmod (2)). |
| 649 |
.P |
| 650 |
Mandatory locking is not specified by POSIX. |
| 651 |
Some other systems also support mandatory locking, |
| 652 |
although the details of how to enable it vary across systems. |
| 653 |
.\" |
| 654 |
.SS Lost locks |
| 655 |
When an advisory lock is obtained on a networked filesystem such as |
| 656 |
NFS it is possible that the lock might get lost. |
| 657 |
This may happen due to administrative action on the server, or due to a |
| 658 |
network partition (i.e., loss of network connectivity with the server) |
| 659 |
which lasts long enough for the server to assume |
| 660 |
that the client is no longer functioning. |
| 661 |
.P |
| 662 |
When the filesystem determines that a lock has been lost, future |
| 663 |
.BR read (2) |
| 664 |
or |
| 665 |
.BR write (2) |
| 666 |
requests may fail with the error |
| 667 |
.BR EIO . |
| 668 |
This error will persist until the lock is removed or the file |
| 669 |
descriptor is closed. |
| 670 |
Since Linux 3.12, |
| 671 |
.\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d |
| 672 |
this happens at least for NFSv4 (including all minor versions). |
| 673 |
.P |
| 674 |
Some versions of UNIX send a signal |
| 675 |
.RB ( SIGLOST ) |
| 676 |
in this circumstance. |
| 677 |
Linux does not define this signal, and does not provide any |
| 678 |
asynchronous notification of lost locks. |
| 679 |
.\" |
| 680 |
.SS Managing signals |
| 681 |
.BR F_GETOWN , |
| 682 |
.BR F_SETOWN , |
| 683 |
.BR F_GETOWN_EX , |
| 684 |
.BR F_SETOWN_EX , |
| 685 |
.BR F_GETSIG , |
| 686 |
and |
| 687 |
.B F_SETSIG |
| 688 |
are used to manage I/O availability signals: |
| 689 |
.TP |
| 690 |
.BR F_GETOWN " (\fIvoid\fP)" |
| 691 |
Return (as the function result) |
| 692 |
the process ID or process group ID currently receiving |
| 693 |
.B SIGIO |
| 694 |
and |
| 695 |
.B SIGURG |
| 696 |
signals for events on file descriptor |
| 697 |
.IR fd . |
| 698 |
Process IDs are returned as positive values; |
| 699 |
process group IDs are returned as negative values (but see BUGS below). |
| 700 |
.I arg |
| 701 |
is ignored. |
| 702 |
.TP |
| 703 |
.BR F_SETOWN " (\fIint\fP)" |
| 704 |
Set the process ID or process group ID that will receive |
| 705 |
.B SIGIO |
| 706 |
and |
| 707 |
.B SIGURG |
| 708 |
signals for events on the file descriptor |
| 709 |
.IR fd . |
| 710 |
The target process or process group ID is specified in |
| 711 |
.IR arg . |
| 712 |
A process ID is specified as a positive value; |
| 713 |
a process group ID is specified as a negative value. |
| 714 |
Most commonly, the calling process specifies itself as the owner |
| 715 |
(that is, |
| 716 |
.I arg |
| 717 |
is specified as |
| 718 |
.BR getpid (2)). |
| 719 |
.IP |
| 720 |
As well as setting the file descriptor owner, |
| 721 |
one must also enable generation of signals on the file descriptor. |
| 722 |
This is done by using the |
| 723 |
.BR fcntl () |
| 724 |
.B F_SETFL |
| 725 |
operation to set the |
| 726 |
.B O_ASYNC |
| 727 |
file status flag on the file descriptor. |
| 728 |
Subsequently, a |
| 729 |
.B SIGIO |
| 730 |
signal is sent whenever input or output becomes possible |
| 731 |
on the file descriptor. |
| 732 |
The |
| 733 |
.BR fcntl () |
| 734 |
.B F_SETSIG |
| 735 |
operation can be used to obtain delivery of a signal other than |
| 736 |
.BR SIGIO . |
| 737 |
.IP |
| 738 |
Sending a signal to the owner process (group) specified by |
| 739 |
.B F_SETOWN |
| 740 |
is subject to the same permissions checks as are described for |
| 741 |
.BR kill (2), |
| 742 |
where the sending process is the one that employs |
| 743 |
.B F_SETOWN |
| 744 |
(but see BUGS below). |
| 745 |
If this permission check fails, then the signal is |
| 746 |
silently discarded. |
| 747 |
.IR Note : |
| 748 |
The |
| 749 |
.B F_SETOWN |
| 750 |
operation records the caller's credentials at the time of the |
| 751 |
.BR fcntl () |
| 752 |
call, |
| 753 |
and it is these saved credentials that are used for the permission checks. |
| 754 |
.IP |
| 755 |
If the file descriptor |
| 756 |
.I fd |
| 757 |
refers to a socket, |
| 758 |
.B F_SETOWN |
| 759 |
also selects |
| 760 |
the recipient of |
| 761 |
.B SIGURG |
| 762 |
signals that are delivered when out-of-band |
| 763 |
data arrives on that socket. |
| 764 |
.RB ( SIGURG |
| 765 |
is sent in any situation where |
| 766 |
.BR select (2) |
| 767 |
would report the socket as having an "exceptional condition".) |
| 768 |
.\" The following appears to be rubbish. It doesn't seem to |
| 769 |
.\" be true according to the kernel source, and I can write |
| 770 |
.\" a program that gets a terminal-generated SIGIO even though |
| 771 |
.\" it is not the foreground process group of the terminal. |
| 772 |
.\" -- MTK, 8 Apr 05 |
| 773 |
.\" |
| 774 |
.\" If the file descriptor |
| 775 |
.\" .I fd |
| 776 |
.\" refers to a terminal device, then SIGIO |
| 777 |
.\" signals are sent to the foreground process group of the terminal. |
| 778 |
.IP |
| 779 |
The following was true in Linux 2.6.x up to and including Linux 2.6.11: |
| 780 |
.RS |
| 781 |
.IP |
| 782 |
If a nonzero value is given to |
| 783 |
.B F_SETSIG |
| 784 |
in a multithreaded process running with a threading library |
| 785 |
that supports thread groups (e.g., NPTL), |
| 786 |
then a positive value given to |
| 787 |
.B F_SETOWN |
| 788 |
has a different meaning: |
| 789 |
.\" The relevant place in the (2.6) kernel source is the |
| 790 |
.\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005 |
| 791 |
instead of being a process ID identifying a whole process, |
| 792 |
it is a thread ID identifying a specific thread within a process. |
| 793 |
Consequently, it may be necessary to pass |
| 794 |
.B F_SETOWN |
| 795 |
the result of |
| 796 |
.BR gettid (2) |
| 797 |
instead of |
| 798 |
.BR getpid (2) |
| 799 |
to get sensible results when |
| 800 |
.B F_SETSIG |
| 801 |
is used. |
| 802 |
(In current Linux threading implementations, |
| 803 |
a main thread's thread ID is the same as its process ID. |
| 804 |
This means that a single-threaded program can equally use |
| 805 |
.BR gettid (2) |
| 806 |
or |
| 807 |
.BR getpid (2) |
| 808 |
in this scenario.) |
| 809 |
Note, however, that the statements in this paragraph do not apply |
| 810 |
to the |
| 811 |
.B SIGURG |
| 812 |
signal generated for out-of-band data on a socket: |
| 813 |
this signal is always sent to either a process or a process group, |
| 814 |
depending on the value given to |
| 815 |
.BR F_SETOWN . |
| 816 |
.\" send_sigurg()/send_sigurg_to_task() bypasses |
| 817 |
.\" kill_fasync()/send_sigio()/send_sigio_to_task() |
| 818 |
.\" to directly call send_group_sig_info() |
| 819 |
.\" -- MTK, Apr 2005 (kernel 2.6.11) |
| 820 |
.RE |
| 821 |
.IP |
| 822 |
The above behavior was accidentally dropped in Linux 2.6.12, |
| 823 |
and won't be restored. |
| 824 |
From Linux 2.6.32 onward, use |
| 825 |
.B F_SETOWN_EX |
| 826 |
to target |
| 827 |
.B SIGIO |
| 828 |
and |
| 829 |
.B SIGURG |
| 830 |
signals at a particular thread. |
| 831 |
.TP |
| 832 |
.BR F_GETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)" |
| 833 |
Return the current file descriptor owner settings |
| 834 |
as defined by a previous |
| 835 |
.B F_SETOWN_EX |
| 836 |
operation. |
| 837 |
The information is returned in the structure pointed to by |
| 838 |
.IR arg , |
| 839 |
which has the following form: |
| 840 |
.IP |
| 841 |
.in +4n |
| 842 |
.EX |
| 843 |
struct f_owner_ex { |
| 844 |
int type; |
| 845 |
pid_t pid; |
| 846 |
}; |
| 847 |
.EE |
| 848 |
.in |
| 849 |
.IP |
| 850 |
The |
| 851 |
.I type |
| 852 |
field will have one of the values |
| 853 |
.BR F_OWNER_TID , |
| 854 |
.BR F_OWNER_PID , |
| 855 |
or |
| 856 |
.BR F_OWNER_PGRP . |
| 857 |
The |
| 858 |
.I pid |
| 859 |
field is a positive integer representing a thread ID, process ID, |
| 860 |
or process group ID. |
| 861 |
See |
| 862 |
.B F_SETOWN_EX |
| 863 |
for more details. |
| 864 |
.TP |
| 865 |
.BR F_SETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)" |
| 866 |
This operation performs a similar task to |
| 867 |
.BR F_SETOWN . |
| 868 |
It allows the caller to direct I/O availability signals |
| 869 |
to a specific thread, process, or process group. |
| 870 |
The caller specifies the target of signals via |
| 871 |
.IR arg , |
| 872 |
which is a pointer to a |
| 873 |
.I f_owner_ex |
| 874 |
structure. |
| 875 |
The |
| 876 |
.I type |
| 877 |
field has one of the following values, which define how |
| 878 |
.I pid |
| 879 |
is interpreted: |
| 880 |
.RS |
| 881 |
.TP |
| 882 |
.B F_OWNER_TID |
| 883 |
Send the signal to the thread whose thread ID |
| 884 |
(the value returned by a call to |
| 885 |
.BR clone (2) |
| 886 |
or |
| 887 |
.BR gettid (2)) |
| 888 |
is specified in |
| 889 |
.IR pid . |
| 890 |
.TP |
| 891 |
.B F_OWNER_PID |
| 892 |
Send the signal to the process whose ID |
| 893 |
is specified in |
| 894 |
.IR pid . |
| 895 |
.TP |
| 896 |
.B F_OWNER_PGRP |
| 897 |
Send the signal to the process group whose ID |
| 898 |
is specified in |
| 899 |
.IR pid . |
| 900 |
(Note that, unlike with |
| 901 |
.BR F_SETOWN , |
| 902 |
a process group ID is specified as a positive value here.) |
| 903 |
.RE |
| 904 |
.TP |
| 905 |
.BR F_GETSIG " (\fIvoid\fP)" |
| 906 |
Return (as the function result) |
| 907 |
the signal sent when input or output becomes possible. |
| 908 |
A value of zero means |
| 909 |
.B SIGIO |
| 910 |
is sent. |
| 911 |
Any other value (including |
| 912 |
.BR SIGIO ) |
| 913 |
is the |
| 914 |
signal sent instead, and in this case additional info is available to |
| 915 |
the signal handler if installed with |
| 916 |
.BR SA_SIGINFO . |
| 917 |
.I arg |
| 918 |
is ignored. |
| 919 |
.TP |
| 920 |
.BR F_SETSIG " (\fIint\fP)" |
| 921 |
Set the signal sent when input or output becomes possible |
| 922 |
to the value given in |
| 923 |
.IR arg . |
| 924 |
A value of zero means to send the default |
| 925 |
.B SIGIO |
| 926 |
signal. |
| 927 |
Any other value (including |
| 928 |
.BR SIGIO ) |
| 929 |
is the signal to send instead, and in this case additional info |
| 930 |
is available to the signal handler if installed with |
| 931 |
.BR SA_SIGINFO . |
| 932 |
.\" |
| 933 |
.\" The following was true only up until Linux 2.6.11: |
| 934 |
.\" |
| 935 |
.\" Additionally, passing a nonzero value to |
| 936 |
.\" .B F_SETSIG |
| 937 |
.\" changes the signal recipient from a whole process to a specific thread |
| 938 |
.\" within a process. |
| 939 |
.\" See the description of |
| 940 |
.\" .B F_SETOWN |
| 941 |
.\" for more details. |
| 942 |
.IP |
| 943 |
By using |
| 944 |
.B F_SETSIG |
| 945 |
with a nonzero value, and setting |
| 946 |
.B SA_SIGINFO |
| 947 |
for the |
| 948 |
signal handler (see |
| 949 |
.BR sigaction (2)), |
| 950 |
extra information about I/O events is passed to |
| 951 |
the handler in a |
| 952 |
.I siginfo_t |
| 953 |
structure. |
| 954 |
If the |
| 955 |
.I si_code |
| 956 |
field indicates the source is |
| 957 |
.BR SI_SIGIO , |
| 958 |
the |
| 959 |
.I si_fd |
| 960 |
field gives the file descriptor associated with the event. |
| 961 |
Otherwise, |
| 962 |
there is no indication which file descriptors are pending, and you |
| 963 |
should use the usual mechanisms |
| 964 |
.RB ( select (2), |
| 965 |
.BR poll (2), |
| 966 |
.BR read (2) |
| 967 |
with |
| 968 |
.B O_NONBLOCK |
| 969 |
set etc.) to determine which file descriptors are available for I/O. |
| 970 |
.IP |
| 971 |
Note that the file descriptor provided in |
| 972 |
.I si_fd |
| 973 |
is the one that was specified during the |
| 974 |
.B F_SETSIG |
| 975 |
operation. |
| 976 |
This can lead to an unusual corner case. |
| 977 |
If the file descriptor is duplicated |
| 978 |
.RB ( dup (2) |
| 979 |
or similar), and the original file descriptor is closed, |
| 980 |
then I/O events will continue to be generated, but the |
| 981 |
.I si_fd |
| 982 |
field will contain the number of the now closed file descriptor. |
| 983 |
.IP |
| 984 |
By selecting a real time signal (value >= |
| 985 |
.BR SIGRTMIN ), |
| 986 |
multiple I/O events may be queued using the same signal numbers. |
| 987 |
(Queuing is dependent on available memory.) |
| 988 |
Extra information is available |
| 989 |
if |
| 990 |
.B SA_SIGINFO |
| 991 |
is set for the signal handler, as above. |
| 992 |
.IP |
| 993 |
Note that Linux imposes a limit on the |
| 994 |
number of real-time signals that may be queued to a |
| 995 |
process (see |
| 996 |
.BR getrlimit (2) |
| 997 |
and |
| 998 |
.BR signal (7)) |
| 999 |
and if this limit is reached, then the kernel reverts to |
| 1000 |
delivering |
| 1001 |
.BR SIGIO , |
| 1002 |
and this signal is delivered to the entire |
| 1003 |
process rather than to a specific thread. |
| 1004 |
.\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05 |
| 1005 |
.P |
| 1006 |
Using these mechanisms, a program can implement fully asynchronous I/O |
| 1007 |
without using |
| 1008 |
.BR select (2) |
| 1009 |
or |
| 1010 |
.BR poll (2) |
| 1011 |
most of the time. |
| 1012 |
.P |
| 1013 |
The use of |
| 1014 |
.B O_ASYNC |
| 1015 |
is specific to BSD and Linux. |
| 1016 |
The only use of |
| 1017 |
.B F_GETOWN |
| 1018 |
and |
| 1019 |
.B F_SETOWN |
| 1020 |
specified in POSIX.1 is in conjunction with the use of the |
| 1021 |
.B SIGURG |
| 1022 |
signal on sockets. |
| 1023 |
(POSIX does not specify the |
| 1024 |
.B SIGIO |
| 1025 |
signal.) |
| 1026 |
.BR F_GETOWN_EX , |
| 1027 |
.BR F_SETOWN_EX , |
| 1028 |
.BR F_GETSIG , |
| 1029 |
and |
| 1030 |
.B F_SETSIG |
| 1031 |
are Linux-specific. |
| 1032 |
POSIX has asynchronous I/O and the |
| 1033 |
.I aio_sigevent |
| 1034 |
structure to achieve similar things; these are also available |
| 1035 |
in Linux as part of the GNU C Library (glibc). |
| 1036 |
.SS Leases |
| 1037 |
.B F_SETLEASE |
| 1038 |
and |
| 1039 |
.B F_GETLEASE |
| 1040 |
(Linux 2.4 onward) are used to establish a new lease, |
| 1041 |
and retrieve the current lease, on the open file description |
| 1042 |
referred to by the file descriptor |
| 1043 |
.IR fd . |
| 1044 |
A file lease provides a mechanism whereby the process holding |
| 1045 |
the lease (the "lease holder") is notified (via delivery of a signal) |
| 1046 |
when a process (the "lease breaker") tries to |
| 1047 |
.BR open (2) |
| 1048 |
or |
| 1049 |
.BR truncate (2) |
| 1050 |
the file referred to by that file descriptor. |
| 1051 |
.TP |
| 1052 |
.BR F_SETLEASE " (\fIint\fP)" |
| 1053 |
Set or remove a file lease according to which of the following |
| 1054 |
values is specified in the integer |
| 1055 |
.IR arg : |
| 1056 |
.RS |
| 1057 |
.TP |
| 1058 |
.B F_RDLCK |
| 1059 |
Take out a read lease. |
| 1060 |
This will cause the calling process to be notified when |
| 1061 |
the file is opened for writing or is truncated. |
| 1062 |
.\" The following became true in Linux 2.6.10: |
| 1063 |
.\" See the man-pages-2.09 Changelog for further info. |
| 1064 |
A read lease can be placed only on a file descriptor that |
| 1065 |
is opened read-only. |
| 1066 |
.TP |
| 1067 |
.B F_WRLCK |
| 1068 |
Take out a write lease. |
| 1069 |
This will cause the caller to be notified when |
| 1070 |
the file is opened for reading or writing or is truncated. |
| 1071 |
A write lease may be placed on a file only if there are no |
| 1072 |
other open file descriptors for the file. |
| 1073 |
.TP |
| 1074 |
.B F_UNLCK |
| 1075 |
Remove our lease from the file. |
| 1076 |
.RE |
| 1077 |
.P |
| 1078 |
Leases are associated with an open file description (see |
| 1079 |
.BR open (2)). |
| 1080 |
This means that duplicate file descriptors (created by, for example, |
| 1081 |
.BR fork (2) |
| 1082 |
or |
| 1083 |
.BR dup (2)) |
| 1084 |
refer to the same lease, and this lease may be modified |
| 1085 |
or released using any of these descriptors. |
| 1086 |
Furthermore, the lease is released by either an explicit |
| 1087 |
.B F_UNLCK |
| 1088 |
operation on any of these duplicate file descriptors, or when all |
| 1089 |
such file descriptors have been closed. |
| 1090 |
.P |
| 1091 |
Leases may be taken out only on regular files. |
| 1092 |
An unprivileged process may take out a lease only on a file whose |
| 1093 |
UID (owner) matches the filesystem UID of the process. |
| 1094 |
A process with the |
| 1095 |
.B CAP_LEASE |
| 1096 |
capability may take out leases on arbitrary files. |
| 1097 |
.TP |
| 1098 |
.BR F_GETLEASE " (\fIvoid\fP)" |
| 1099 |
Indicates what type of lease is associated with the file descriptor |
| 1100 |
.I fd |
| 1101 |
by returning either |
| 1102 |
.BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK , |
| 1103 |
indicating, respectively, a read lease , a write lease, or no lease. |
| 1104 |
.I arg |
| 1105 |
is ignored. |
| 1106 |
.P |
| 1107 |
When a process (the "lease breaker") performs an |
| 1108 |
.BR open (2) |
| 1109 |
or |
| 1110 |
.BR truncate (2) |
| 1111 |
that conflicts with a lease established via |
| 1112 |
.BR F_SETLEASE , |
| 1113 |
the system call is blocked by the kernel and |
| 1114 |
the kernel notifies the lease holder by sending it a signal |
| 1115 |
.RB ( SIGIO |
| 1116 |
by default). |
| 1117 |
The lease holder should respond to receipt of this signal by doing |
| 1118 |
whatever cleanup is required in preparation for the file to be |
| 1119 |
accessed by another process (e.g., flushing cached buffers) and |
| 1120 |
then either remove or downgrade its lease. |
| 1121 |
A lease is removed by performing an |
| 1122 |
.B F_SETLEASE |
| 1123 |
operation specifying |
| 1124 |
.I arg |
| 1125 |
as |
| 1126 |
.BR F_UNLCK . |
| 1127 |
If the lease holder currently holds a write lease on the file, |
| 1128 |
and the lease breaker is opening the file for reading, |
| 1129 |
then it is sufficient for the lease holder to downgrade |
| 1130 |
the lease to a read lease. |
| 1131 |
This is done by performing an |
| 1132 |
.B F_SETLEASE |
| 1133 |
operation specifying |
| 1134 |
.I arg |
| 1135 |
as |
| 1136 |
.BR F_RDLCK . |
| 1137 |
.P |
| 1138 |
If the lease holder fails to downgrade or remove the lease within |
| 1139 |
the number of seconds specified in |
| 1140 |
.IR /proc/sys/fs/lease\-break\-time , |
| 1141 |
then the kernel forcibly removes or downgrades the lease holder's lease. |
| 1142 |
.P |
| 1143 |
Once a lease break has been initiated, |
| 1144 |
.B F_GETLEASE |
| 1145 |
returns the target lease type (either |
| 1146 |
.B F_RDLCK |
| 1147 |
or |
| 1148 |
.BR F_UNLCK , |
| 1149 |
depending on what would be compatible with the lease breaker) |
| 1150 |
until the lease holder voluntarily downgrades or removes the lease or |
| 1151 |
the kernel forcibly does so after the lease break timer expires. |
| 1152 |
.P |
| 1153 |
Once the lease has been voluntarily or forcibly removed or downgraded, |
| 1154 |
and assuming the lease breaker has not unblocked its system call, |
| 1155 |
the kernel permits the lease breaker's system call to proceed. |
| 1156 |
.P |
| 1157 |
If the lease breaker's blocked |
| 1158 |
.BR open (2) |
| 1159 |
or |
| 1160 |
.BR truncate (2) |
| 1161 |
is interrupted by a signal handler, |
| 1162 |
then the system call fails with the error |
| 1163 |
.BR EINTR , |
| 1164 |
but the other steps still occur as described above. |
| 1165 |
If the lease breaker is killed by a signal while blocked in |
| 1166 |
.BR open (2) |
| 1167 |
or |
| 1168 |
.BR truncate (2), |
| 1169 |
then the other steps still occur as described above. |
| 1170 |
If the lease breaker specifies the |
| 1171 |
.B O_NONBLOCK |
| 1172 |
flag when calling |
| 1173 |
.BR open (2), |
| 1174 |
then the call immediately fails with the error |
| 1175 |
.BR EWOULDBLOCK , |
| 1176 |
but the other steps still occur as described above. |
| 1177 |
.P |
| 1178 |
The default signal used to notify the lease holder is |
| 1179 |
.BR SIGIO , |
| 1180 |
but this can be changed using the |
| 1181 |
.B F_SETSIG |
| 1182 |
operation to |
| 1183 |
.BR fcntl (). |
| 1184 |
If a |
| 1185 |
.B F_SETSIG |
| 1186 |
operation is performed (even one specifying |
| 1187 |
.BR SIGIO ), |
| 1188 |
and the signal |
| 1189 |
handler is established using |
| 1190 |
.BR SA_SIGINFO , |
| 1191 |
then the handler will receive a |
| 1192 |
.I siginfo_t |
| 1193 |
structure as its second argument, and the |
| 1194 |
.I si_fd |
| 1195 |
field of this argument will hold the file descriptor of the leased file |
| 1196 |
that has been accessed by another process. |
| 1197 |
(This is useful if the caller holds leases against multiple files.) |
| 1198 |
.SS File and directory change notification (dnotify) |
| 1199 |
.TP |
| 1200 |
.BR F_NOTIFY " (\fIint\fP)" |
| 1201 |
(Linux 2.4 onward) |
| 1202 |
Provide notification when the directory referred to by |
| 1203 |
.I fd |
| 1204 |
or any of the files that it contains is changed. |
| 1205 |
The events to be notified are specified in |
| 1206 |
.IR arg , |
| 1207 |
which is a bit mask specified by ORing together zero or more of |
| 1208 |
the following bits: |
| 1209 |
.P |
| 1210 |
.RS |
| 1211 |
.PD 0 |
| 1212 |
.TP |
| 1213 |
.B DN_ACCESS |
| 1214 |
A file was accessed |
| 1215 |
.RB ( read (2), |
| 1216 |
.BR pread (2), |
| 1217 |
.BR readv (2), |
| 1218 |
and similar) |
| 1219 |
.TP |
| 1220 |
.B DN_MODIFY |
| 1221 |
A file was modified |
| 1222 |
.RB ( write (2), |
| 1223 |
.BR pwrite (2), |
| 1224 |
.BR writev (2), |
| 1225 |
.BR truncate (2), |
| 1226 |
.BR ftruncate (2), |
| 1227 |
and similar). |
| 1228 |
.TP |
| 1229 |
.B DN_CREATE |
| 1230 |
A file was created |
| 1231 |
.RB ( open (2), |
| 1232 |
.BR creat (2), |
| 1233 |
.BR mknod (2), |
| 1234 |
.BR mkdir (2), |
| 1235 |
.BR link (2), |
| 1236 |
.BR symlink (2), |
| 1237 |
.BR rename (2) |
| 1238 |
into this directory). |
| 1239 |
.TP |
| 1240 |
.B DN_DELETE |
| 1241 |
A file was unlinked |
| 1242 |
.RB ( unlink (2), |
| 1243 |
.BR rename (2) |
| 1244 |
to another directory, |
| 1245 |
.BR rmdir (2)). |
| 1246 |
.TP |
| 1247 |
.B DN_RENAME |
| 1248 |
A file was renamed within this directory |
| 1249 |
.RB ( rename (2)). |
| 1250 |
.TP |
| 1251 |
.B DN_ATTRIB |
| 1252 |
The attributes of a file were changed |
| 1253 |
.RB ( chown (2), |
| 1254 |
.BR chmod (2), |
| 1255 |
.BR utime (2), |
| 1256 |
.BR utimensat (2), |
| 1257 |
and similar). |
| 1258 |
.PD |
| 1259 |
.RE |
| 1260 |
.IP |
| 1261 |
(In order to obtain these definitions, the |
| 1262 |
.B _GNU_SOURCE |
| 1263 |
feature test macro must be defined before including |
| 1264 |
.I any |
| 1265 |
header files.) |
| 1266 |
.IP |
| 1267 |
Directory notifications are normally "one-shot", and the application |
| 1268 |
must reregister to receive further notifications. |
| 1269 |
Alternatively, if |
| 1270 |
.B DN_MULTISHOT |
| 1271 |
is included in |
| 1272 |
.IR arg , |
| 1273 |
then notification will remain in effect until explicitly removed. |
| 1274 |
.IP |
| 1275 |
.\" The following does seem a poor API-design choice... |
| 1276 |
A series of |
| 1277 |
.B F_NOTIFY |
| 1278 |
requests is cumulative, with the events in |
| 1279 |
.I arg |
| 1280 |
being added to the set already monitored. |
| 1281 |
To disable notification of all events, make an |
| 1282 |
.B F_NOTIFY |
| 1283 |
call specifying |
| 1284 |
.I arg |
| 1285 |
as 0. |
| 1286 |
.IP |
| 1287 |
Notification occurs via delivery of a signal. |
| 1288 |
The default signal is |
| 1289 |
.BR SIGIO , |
| 1290 |
but this can be changed using the |
| 1291 |
.B F_SETSIG |
| 1292 |
operation to |
| 1293 |
.BR fcntl (). |
| 1294 |
(Note that |
| 1295 |
.B SIGIO |
| 1296 |
is one of the nonqueuing standard signals; |
| 1297 |
switching to the use of a real-time signal means that |
| 1298 |
multiple notifications can be queued to the process.) |
| 1299 |
In the latter case, the signal handler receives a |
| 1300 |
.I siginfo_t |
| 1301 |
structure as its second argument (if the handler was |
| 1302 |
established using |
| 1303 |
.BR SA_SIGINFO ) |
| 1304 |
and the |
| 1305 |
.I si_fd |
| 1306 |
field of this structure contains the file descriptor which |
| 1307 |
generated the notification (useful when establishing notification |
| 1308 |
on multiple directories). |
| 1309 |
.IP |
| 1310 |
Especially when using |
| 1311 |
.BR DN_MULTISHOT , |
| 1312 |
a real time signal should be used for notification, |
| 1313 |
so that multiple notifications can be queued. |
| 1314 |
.IP |
| 1315 |
.B NOTE: |
| 1316 |
New applications should use the |
| 1317 |
.I inotify |
| 1318 |
interface (available since Linux 2.6.13), |
| 1319 |
which provides a much superior interface for obtaining notifications of |
| 1320 |
filesystem events. |
| 1321 |
See |
| 1322 |
.BR inotify (7). |
| 1323 |
.SS Changing the capacity of a pipe |
| 1324 |
.TP |
| 1325 |
.BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)" |
| 1326 |
Change the capacity of the pipe referred to by |
| 1327 |
.I fd |
| 1328 |
to be at least |
| 1329 |
.I arg |
| 1330 |
bytes. |
| 1331 |
An unprivileged process can adjust the pipe capacity to any value |
| 1332 |
between the system page size and the limit defined in |
| 1333 |
.I /proc/sys/fs/pipe\-max\-size |
| 1334 |
(see |
| 1335 |
.BR proc (5)). |
| 1336 |
Attempts to set the pipe capacity below the page size are silently |
| 1337 |
rounded up to the page size. |
| 1338 |
Attempts by an unprivileged process to set the pipe capacity above the limit in |
| 1339 |
.I /proc/sys/fs/pipe\-max\-size |
| 1340 |
yield the error |
| 1341 |
.BR EPERM ; |
| 1342 |
a privileged process |
| 1343 |
.RB ( CAP_SYS_RESOURCE ) |
| 1344 |
can override the limit. |
| 1345 |
.IP |
| 1346 |
When allocating the buffer for the pipe, |
| 1347 |
the kernel may use a capacity larger than |
| 1348 |
.IR arg , |
| 1349 |
if that is convenient for the implementation. |
| 1350 |
(In the current implementation, |
| 1351 |
the allocation is the next higher power-of-two page-size multiple |
| 1352 |
of the requested size.) |
| 1353 |
The actual capacity (in bytes) that is set is returned as the function result. |
| 1354 |
.IP |
| 1355 |
Attempting to set the pipe capacity smaller than the amount |
| 1356 |
of buffer space currently used to store data produces the error |
| 1357 |
.BR EBUSY . |
| 1358 |
.IP |
| 1359 |
Note that because of the way the pages of the pipe buffer |
| 1360 |
are employed when data is written to the pipe, |
| 1361 |
the number of bytes that can be written may be less than the nominal size, |
| 1362 |
depending on the size of the writes. |
| 1363 |
.TP |
| 1364 |
.BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)" |
| 1365 |
Return (as the function result) the capacity of the pipe referred to by |
| 1366 |
.IR fd . |
| 1367 |
.\" |
| 1368 |
.SS File Sealing |
| 1369 |
File seals limit the set of allowed operations on a given file. |
| 1370 |
For each seal that is set on a file, |
| 1371 |
a specific set of operations will fail with |
| 1372 |
.B EPERM |
| 1373 |
on this file from now on. |
| 1374 |
The file is said to be sealed. |
| 1375 |
The default set of seals depends on the type of the underlying |
| 1376 |
file and filesystem. |
| 1377 |
For an overview of file sealing, a discussion of its purpose, |
| 1378 |
and some code examples, see |
| 1379 |
.BR memfd_create (2). |
| 1380 |
.P |
| 1381 |
Currently, |
| 1382 |
file seals can be applied only to a file descriptor returned by |
| 1383 |
.BR memfd_create (2) |
| 1384 |
(if the |
| 1385 |
.B MFD_ALLOW_SEALING |
| 1386 |
was employed). |
| 1387 |
On other filesystems, all |
| 1388 |
.BR fcntl () |
| 1389 |
operations that operate on seals will return |
| 1390 |
.BR EINVAL . |
| 1391 |
.P |
| 1392 |
Seals are a property of an inode. |
| 1393 |
Thus, all open file descriptors referring to the same inode share |
| 1394 |
the same set of seals. |
| 1395 |
Furthermore, seals can never be removed, only added. |
| 1396 |
.TP |
| 1397 |
.BR F_ADD_SEALS " (\fIint\fP; since Linux 3.17)" |
| 1398 |
Add the seals given in the bit-mask argument |
| 1399 |
.I arg |
| 1400 |
to the set of seals of the inode referred to by the file descriptor |
| 1401 |
.IR fd . |
| 1402 |
Seals cannot be removed again. |
| 1403 |
Once this call succeeds, the seals are enforced by the kernel immediately. |
| 1404 |
If the current set of seals includes |
| 1405 |
.B F_SEAL_SEAL |
| 1406 |
(see below), then this call will be rejected with |
| 1407 |
.BR EPERM . |
| 1408 |
Adding a seal that is already set is a no-op, in case |
| 1409 |
.B F_SEAL_SEAL |
| 1410 |
is not set already. |
| 1411 |
In order to place a seal, the file descriptor |
| 1412 |
.I fd |
| 1413 |
must be writable. |
| 1414 |
.TP |
| 1415 |
.BR F_GET_SEALS " (\fIvoid\fP; since Linux 3.17)" |
| 1416 |
Return (as the function result) the current set of seals |
| 1417 |
of the inode referred to by |
| 1418 |
.IR fd . |
| 1419 |
If no seals are set, 0 is returned. |
| 1420 |
If the file does not support sealing, \-1 is returned and |
| 1421 |
.I errno |
| 1422 |
is set to |
| 1423 |
.BR EINVAL . |
| 1424 |
.P |
| 1425 |
The following seals are available: |
| 1426 |
.TP |
| 1427 |
.B F_SEAL_SEAL |
| 1428 |
If this seal is set, any further call to |
| 1429 |
.BR fcntl () |
| 1430 |
with |
| 1431 |
.B F_ADD_SEALS |
| 1432 |
fails with the error |
| 1433 |
.BR EPERM . |
| 1434 |
Therefore, this seal prevents any modifications to the set of seals itself. |
| 1435 |
If the initial set of seals of a file includes |
| 1436 |
.BR F_SEAL_SEAL , |
| 1437 |
then this effectively causes the set of seals to be constant and locked. |
| 1438 |
.TP |
| 1439 |
.B F_SEAL_SHRINK |
| 1440 |
If this seal is set, the file in question cannot be reduced in size. |
| 1441 |
This affects |
| 1442 |
.BR open (2) |
| 1443 |
with the |
| 1444 |
.B O_TRUNC |
| 1445 |
flag as well as |
| 1446 |
.BR truncate (2) |
| 1447 |
and |
| 1448 |
.BR ftruncate (2). |
| 1449 |
Those calls fail with |
| 1450 |
.B EPERM |
| 1451 |
if you try to shrink the file in question. |
| 1452 |
Increasing the file size is still possible. |
| 1453 |
.TP |
| 1454 |
.B F_SEAL_GROW |
| 1455 |
If this seal is set, the size of the file in question cannot be increased. |
| 1456 |
This affects |
| 1457 |
.BR write (2) |
| 1458 |
beyond the end of the file, |
| 1459 |
.BR truncate (2), |
| 1460 |
.BR ftruncate (2), |
| 1461 |
and |
| 1462 |
.BR fallocate (2). |
| 1463 |
These calls fail with |
| 1464 |
.B EPERM |
| 1465 |
if you use them to increase the file size. |
| 1466 |
If you keep the size or shrink it, those calls still work as expected. |
| 1467 |
.TP |
| 1468 |
.B F_SEAL_WRITE |
| 1469 |
If this seal is set, you cannot modify the contents of the file. |
| 1470 |
Note that shrinking or growing the size of the file is |
| 1471 |
still possible and allowed. |
| 1472 |
.\" One or more other seals are typically used with F_SEAL_WRITE |
| 1473 |
.\" because, given a file with the F_SEAL_WRITE seal set, then, |
| 1474 |
.\" while it would no longer be possible to (say) write zeros into |
| 1475 |
.\" the last 100 bytes of a file, it would still be possible |
| 1476 |
.\" to (say) shrink the file by 100 bytes using ftruncate(), and |
| 1477 |
.\" then increase the file size by 100 bytes, which would have |
| 1478 |
.\" the effect of replacing the last hundred bytes by zeros. |
| 1479 |
.\" |
| 1480 |
Thus, this seal is normally used in combination with one of the other seals. |
| 1481 |
This seal affects |
| 1482 |
.BR write (2) |
| 1483 |
and |
| 1484 |
.BR fallocate (2) |
| 1485 |
(only in combination with the |
| 1486 |
.B FALLOC_FL_PUNCH_HOLE |
| 1487 |
flag). |
| 1488 |
Those calls fail with |
| 1489 |
.B EPERM |
| 1490 |
if this seal is set. |
| 1491 |
Furthermore, trying to create new shared, writable memory-mappings via |
| 1492 |
.BR mmap (2) |
| 1493 |
will also fail with |
| 1494 |
.BR EPERM . |
| 1495 |
.IP |
| 1496 |
Using the |
| 1497 |
.B F_ADD_SEALS |
| 1498 |
operation to set the |
| 1499 |
.B F_SEAL_WRITE |
| 1500 |
seal fails with |
| 1501 |
.B EBUSY |
| 1502 |
if any writable, shared mapping exists. |
| 1503 |
Such mappings must be unmapped before you can add this seal. |
| 1504 |
Furthermore, if there are any asynchronous I/O operations |
| 1505 |
.RB ( io_submit (2)) |
| 1506 |
pending on the file, |
| 1507 |
all outstanding writes will be discarded. |
| 1508 |
.TP |
| 1509 |
.BR F_SEAL_FUTURE_WRITE " (since Linux 5.1)" |
| 1510 |
The effect of this seal is similar to |
| 1511 |
.BR F_SEAL_WRITE , |
| 1512 |
but the contents of the file can still be modified via |
| 1513 |
shared writable mappings that were created prior to the seal being set. |
| 1514 |
Any attempt to create a new writable mapping on the file via |
| 1515 |
.BR mmap (2) |
| 1516 |
will fail with |
| 1517 |
.BR EPERM . |
| 1518 |
Likewise, an attempt to write to the file via |
| 1519 |
.BR write (2) |
| 1520 |
will fail with |
| 1521 |
.BR EPERM . |
| 1522 |
.IP |
| 1523 |
Using this seal, |
| 1524 |
one process can create a memory buffer that it can continue to modify |
| 1525 |
while sharing that buffer on a "read-only" basis with other processes. |
| 1526 |
.\" |
| 1527 |
.SS File read/write hints |
| 1528 |
Write lifetime hints can be used to inform the kernel about the relative |
| 1529 |
expected lifetime of writes on a given inode or |
| 1530 |
via a particular open file description. |
| 1531 |
(See |
| 1532 |
.BR open (2) |
| 1533 |
for an explanation of open file descriptions.) |
| 1534 |
In this context, the term "write lifetime" means |
| 1535 |
the expected time the data will live on media, before |
| 1536 |
being overwritten or erased. |
| 1537 |
.P |
| 1538 |
An application may use the different hint values specified below to |
| 1539 |
separate writes into different write classes, |
| 1540 |
so that multiple users or applications running on a single storage back-end |
| 1541 |
can aggregate their I/O patterns in a consistent manner. |
| 1542 |
However, there are no functional semantics implied by these flags, |
| 1543 |
and different I/O classes can use the write lifetime hints |
| 1544 |
in arbitrary ways, so long as the hints are used consistently. |
| 1545 |
.P |
| 1546 |
The following operations can be applied to the file descriptor, |
| 1547 |
.IR fd : |
| 1548 |
.TP |
| 1549 |
.BR F_GET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)" |
| 1550 |
Returns the value of the read/write hint associated with the underlying inode |
| 1551 |
referred to by |
| 1552 |
.IR fd . |
| 1553 |
.TP |
| 1554 |
.BR F_SET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)" |
| 1555 |
Sets the read/write hint value associated with the |
| 1556 |
underlying inode referred to by |
| 1557 |
.IR fd . |
| 1558 |
This hint persists until either it is explicitly modified or |
| 1559 |
the underlying filesystem is unmounted. |
| 1560 |
.TP |
| 1561 |
.BR F_GET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)" |
| 1562 |
Returns the value of the read/write hint associated with |
| 1563 |
the open file description referred to by |
| 1564 |
.IR fd . |
| 1565 |
.TP |
| 1566 |
.BR F_SET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)" |
| 1567 |
Sets the read/write hint value associated with the open file description |
| 1568 |
referred to by |
| 1569 |
.IR fd . |
| 1570 |
.P |
| 1571 |
If an open file description has not been assigned a read/write hint, |
| 1572 |
then it shall use the value assigned to the inode, if any. |
| 1573 |
.P |
| 1574 |
The following read/write |
| 1575 |
hints are valid since Linux 4.13: |
| 1576 |
.TP |
| 1577 |
.B RWH_WRITE_LIFE_NOT_SET |
| 1578 |
No specific hint has been set. |
| 1579 |
This is the default value. |
| 1580 |
.TP |
| 1581 |
.B RWH_WRITE_LIFE_NONE |
| 1582 |
No specific write lifetime is associated with this file or inode. |
| 1583 |
.TP |
| 1584 |
.B RWH_WRITE_LIFE_SHORT |
| 1585 |
Data written to this inode or via this open file description |
| 1586 |
is expected to have a short lifetime. |
| 1587 |
.TP |
| 1588 |
.B RWH_WRITE_LIFE_MEDIUM |
| 1589 |
Data written to this inode or via this open file description |
| 1590 |
is expected to have a lifetime longer than |
| 1591 |
data written with |
| 1592 |
.BR RWH_WRITE_LIFE_SHORT . |
| 1593 |
.TP |
| 1594 |
.B RWH_WRITE_LIFE_LONG |
| 1595 |
Data written to this inode or via this open file description |
| 1596 |
is expected to have a lifetime longer than |
| 1597 |
data written with |
| 1598 |
.BR RWH_WRITE_LIFE_MEDIUM . |
| 1599 |
.TP |
| 1600 |
.B RWH_WRITE_LIFE_EXTREME |
| 1601 |
Data written to this inode or via this open file description |
| 1602 |
is expected to have a lifetime longer than |
| 1603 |
data written with |
| 1604 |
.BR RWH_WRITE_LIFE_LONG . |
| 1605 |
.P |
| 1606 |
All the write-specific hints are relative to each other, |
| 1607 |
and no individual absolute meaning should be attributed to them. |
| 1608 |
.SH RETURN VALUE |
| 1609 |
For a successful call, the return value depends on the operation: |
| 1610 |
.TP |
| 1611 |
.B F_DUPFD |
| 1612 |
The new file descriptor. |
| 1613 |
.TP |
| 1614 |
.B F_GETFD |
| 1615 |
Value of file descriptor flags. |
| 1616 |
.TP |
| 1617 |
.B F_GETFL |
| 1618 |
Value of file status flags. |
| 1619 |
.TP |
| 1620 |
.B F_GETLEASE |
| 1621 |
Type of lease held on file descriptor. |
| 1622 |
.TP |
| 1623 |
.B F_GETOWN |
| 1624 |
Value of file descriptor owner. |
| 1625 |
.TP |
| 1626 |
.B F_GETSIG |
| 1627 |
Value of signal sent when read or write becomes possible, or zero |
| 1628 |
for traditional |
| 1629 |
.B SIGIO |
| 1630 |
behavior. |
| 1631 |
.TP |
| 1632 |
.B F_GETPIPE_SZ |
| 1633 |
.TQ |
| 1634 |
.B F_SETPIPE_SZ |
| 1635 |
The pipe capacity. |
| 1636 |
.TP |
| 1637 |
.B F_GET_SEALS |
| 1638 |
A bit mask identifying the seals that have been set |
| 1639 |
for the inode referred to by |
| 1640 |
.IR fd . |
| 1641 |
.TP |
| 1642 |
All other operations |
| 1643 |
Zero. |
| 1644 |
.P |
| 1645 |
On error, \-1 is returned, and |
| 1646 |
.I errno |
| 1647 |
is set to indicate the error. |
| 1648 |
.SH ERRORS |
| 1649 |
.TP |
| 1650 |
.BR EACCES " or " EAGAIN |
| 1651 |
Operation is prohibited by locks held by other processes. |
| 1652 |
.TP |
| 1653 |
.B EAGAIN |
| 1654 |
The operation is prohibited because the file has been memory-mapped by |
| 1655 |
another process. |
| 1656 |
.TP |
| 1657 |
.B EBADF |
| 1658 |
.I fd |
| 1659 |
is not an open file descriptor |
| 1660 |
.TP |
| 1661 |
.B EBADF |
| 1662 |
.I op |
| 1663 |
is |
| 1664 |
.B F_SETLK |
| 1665 |
or |
| 1666 |
.B F_SETLKW |
| 1667 |
and the file descriptor open mode doesn't match with the |
| 1668 |
type of lock requested. |
| 1669 |
.TP |
| 1670 |
.B EBUSY |
| 1671 |
.I op |
| 1672 |
is |
| 1673 |
.B F_SETPIPE_SZ |
| 1674 |
and the new pipe capacity specified in |
| 1675 |
.I arg |
| 1676 |
is smaller than the amount of buffer space currently |
| 1677 |
used to store data in the pipe. |
| 1678 |
.TP |
| 1679 |
.B EBUSY |
| 1680 |
.I op |
| 1681 |
is |
| 1682 |
.BR F_ADD_SEALS , |
| 1683 |
.I arg |
| 1684 |
includes |
| 1685 |
.BR F_SEAL_WRITE , |
| 1686 |
and there exists a writable, shared mapping on the file referred to by |
| 1687 |
.IR fd . |
| 1688 |
.TP |
| 1689 |
.B EDEADLK |
| 1690 |
It was detected that the specified |
| 1691 |
.B F_SETLKW |
| 1692 |
operation would cause a deadlock. |
| 1693 |
.TP |
| 1694 |
.B EFAULT |
| 1695 |
.I lock |
| 1696 |
is outside your accessible address space. |
| 1697 |
.TP |
| 1698 |
.B EINTR |
| 1699 |
.I op |
| 1700 |
is |
| 1701 |
.B F_SETLKW |
| 1702 |
or |
| 1703 |
.B F_OFD_SETLKW |
| 1704 |
and the operation was interrupted by a signal; see |
| 1705 |
.BR signal (7). |
| 1706 |
.TP |
| 1707 |
.B EINTR |
| 1708 |
.I op |
| 1709 |
is |
| 1710 |
.BR F_GETLK , |
| 1711 |
.BR F_SETLK , |
| 1712 |
.BR F_OFD_GETLK , |
| 1713 |
or |
| 1714 |
.BR F_OFD_SETLK , |
| 1715 |
and the operation was interrupted by a signal before the lock was checked or |
| 1716 |
acquired. |
| 1717 |
Most likely when locking a remote file (e.g., locking over |
| 1718 |
NFS), but can sometimes happen locally. |
| 1719 |
.TP |
| 1720 |
.B EINVAL |
| 1721 |
The value specified in |
| 1722 |
.I op |
| 1723 |
is not recognized by this kernel. |
| 1724 |
.TP |
| 1725 |
.B EINVAL |
| 1726 |
.I op |
| 1727 |
is |
| 1728 |
.B F_ADD_SEALS |
| 1729 |
and |
| 1730 |
.I arg |
| 1731 |
includes an unrecognized sealing bit. |
| 1732 |
.TP |
| 1733 |
.B EINVAL |
| 1734 |
.I op |
| 1735 |
is |
| 1736 |
.B F_ADD_SEALS |
| 1737 |
or |
| 1738 |
.B F_GET_SEALS |
| 1739 |
and the filesystem containing the inode referred to by |
| 1740 |
.I fd |
| 1741 |
does not support sealing. |
| 1742 |
.TP |
| 1743 |
.B EINVAL |
| 1744 |
.I op |
| 1745 |
is |
| 1746 |
.B F_DUPFD |
| 1747 |
and |
| 1748 |
.I arg |
| 1749 |
is negative or is greater than the maximum allowable value |
| 1750 |
(see the discussion of |
| 1751 |
.B RLIMIT_NOFILE |
| 1752 |
in |
| 1753 |
.BR getrlimit (2)). |
| 1754 |
.TP |
| 1755 |
.B EINVAL |
| 1756 |
.I op |
| 1757 |
is |
| 1758 |
.B F_SETSIG |
| 1759 |
and |
| 1760 |
.I arg |
| 1761 |
is not an allowable signal number. |
| 1762 |
.TP |
| 1763 |
.B EINVAL |
| 1764 |
.I op |
| 1765 |
is |
| 1766 |
.BR F_OFD_SETLK , |
| 1767 |
.BR F_OFD_SETLKW , |
| 1768 |
or |
| 1769 |
.BR F_OFD_GETLK , |
| 1770 |
and |
| 1771 |
.I l_pid |
| 1772 |
was not specified as zero. |
| 1773 |
.TP |
| 1774 |
.B EMFILE |
| 1775 |
.I op |
| 1776 |
is |
| 1777 |
.B F_DUPFD |
| 1778 |
and the per-process limit on the number of open file descriptors |
| 1779 |
has been reached. |
| 1780 |
.TP |
| 1781 |
.B ENOLCK |
| 1782 |
Too many segment locks open, lock table is full, or a remote locking |
| 1783 |
protocol failed (e.g., locking over NFS). |
| 1784 |
.TP |
| 1785 |
.B ENOTDIR |
| 1786 |
.B F_NOTIFY |
| 1787 |
was specified in |
| 1788 |
.IR op , |
| 1789 |
but |
| 1790 |
.I fd |
| 1791 |
does not refer to a directory. |
| 1792 |
.TP |
| 1793 |
.B EPERM |
| 1794 |
.I op |
| 1795 |
is |
| 1796 |
.B F_SETPIPE_SZ |
| 1797 |
and the soft or hard user pipe limit has been reached; see |
| 1798 |
.BR pipe (7). |
| 1799 |
.TP |
| 1800 |
.B EPERM |
| 1801 |
Attempted to clear the |
| 1802 |
.B O_APPEND |
| 1803 |
flag on a file that has the append-only attribute set. |
| 1804 |
.TP |
| 1805 |
.B EPERM |
| 1806 |
.I op |
| 1807 |
was |
| 1808 |
.BR F_ADD_SEALS , |
| 1809 |
but |
| 1810 |
.I fd |
| 1811 |
was not open for writing |
| 1812 |
or the current set of seals on the file already includes |
| 1813 |
.BR F_SEAL_SEAL . |
| 1814 |
.SH STANDARDS |
| 1815 |
POSIX.1-2008. |
| 1816 |
.P |
| 1817 |
.BR F_GETOWN_EX , |
| 1818 |
.BR F_SETOWN_EX , |
| 1819 |
.BR F_SETPIPE_SZ , |
| 1820 |
.BR F_GETPIPE_SZ , |
| 1821 |
.BR F_GETSIG , |
| 1822 |
.BR F_SETSIG , |
| 1823 |
.BR F_NOTIFY , |
| 1824 |
.BR F_GETLEASE , |
| 1825 |
and |
| 1826 |
.B F_SETLEASE |
| 1827 |
are Linux-specific. |
| 1828 |
(Define the |
| 1829 |
.B _GNU_SOURCE |
| 1830 |
macro to obtain these definitions.) |
| 1831 |
.\" .P |
| 1832 |
.\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions. |
| 1833 |
.P |
| 1834 |
.BR F_OFD_SETLK , |
| 1835 |
.BR F_OFD_SETLKW , |
| 1836 |
and |
| 1837 |
.B F_OFD_GETLK |
| 1838 |
are Linux-specific (and one must define |
| 1839 |
.B _GNU_SOURCE |
| 1840 |
to obtain their definitions), |
| 1841 |
but work is being done to have them included in the next version of POSIX.1. |
| 1842 |
.P |
| 1843 |
.B F_ADD_SEALS |
| 1844 |
and |
| 1845 |
.B F_GET_SEALS |
| 1846 |
are Linux-specific. |
| 1847 |
.\" FIXME . Once glibc adds support, add a note about FTM requirements |
| 1848 |
.SH HISTORY |
| 1849 |
SVr4, 4.3BSD, POSIX.1-2001. |
| 1850 |
.P |
| 1851 |
Only the operations |
| 1852 |
.BR F_DUPFD , |
| 1853 |
.BR F_GETFD , |
| 1854 |
.BR F_SETFD , |
| 1855 |
.BR F_GETFL , |
| 1856 |
.BR F_SETFL , |
| 1857 |
.BR F_GETLK , |
| 1858 |
.BR F_SETLK , |
| 1859 |
and |
| 1860 |
.B F_SETLKW |
| 1861 |
are specified in POSIX.1-2001. |
| 1862 |
.P |
| 1863 |
.B F_GETOWN |
| 1864 |
and |
| 1865 |
.B F_SETOWN |
| 1866 |
are specified in POSIX.1-2001. |
| 1867 |
(To get their definitions, define either |
| 1868 |
.\" .BR _BSD_SOURCE , |
| 1869 |
.\" or |
| 1870 |
.B _XOPEN_SOURCE |
| 1871 |
with the value 500 or greater, or |
| 1872 |
.B _POSIX_C_SOURCE |
| 1873 |
with the value 200809L or greater.) |
| 1874 |
.P |
| 1875 |
.B F_DUPFD_CLOEXEC |
| 1876 |
is specified in POSIX.1-2008. |
| 1877 |
(To get this definition, define |
| 1878 |
.B _POSIX_C_SOURCE |
| 1879 |
with the value 200809L or greater, or |
| 1880 |
.B _XOPEN_SOURCE |
| 1881 |
with the value 700 or greater.) |
| 1882 |
.SH NOTES |
| 1883 |
The errors returned by |
| 1884 |
.BR dup2 (2) |
| 1885 |
are different from those returned by |
| 1886 |
.BR F_DUPFD . |
| 1887 |
.\" |
| 1888 |
.SS File locking |
| 1889 |
The original Linux |
| 1890 |
.BR fcntl () |
| 1891 |
system call was not designed to handle large file offsets |
| 1892 |
(in the |
| 1893 |
.I flock |
| 1894 |
structure). |
| 1895 |
Consequently, an |
| 1896 |
.BR fcntl64 () |
| 1897 |
system call was added in Linux 2.4. |
| 1898 |
The newer system call employs a different structure for file locking, |
| 1899 |
.IR flock64 , |
| 1900 |
and corresponding operations, |
| 1901 |
.BR F_GETLK64 , |
| 1902 |
.BR F_SETLK64 , |
| 1903 |
and |
| 1904 |
.BR F_SETLKW64 . |
| 1905 |
However, these details can be ignored by applications using glibc, whose |
| 1906 |
.BR fcntl () |
| 1907 |
wrapper function transparently employs the more recent system call |
| 1908 |
where it is available. |
| 1909 |
.\" |
| 1910 |
.SS Record locks |
| 1911 |
Since Linux 2.0, there is no interaction between the types of lock |
| 1912 |
placed by |
| 1913 |
.BR flock (2) |
| 1914 |
and |
| 1915 |
.BR fcntl (). |
| 1916 |
.P |
| 1917 |
Several systems have more fields in |
| 1918 |
.I "struct flock" |
| 1919 |
such as, for example, |
| 1920 |
.I l_sysid |
| 1921 |
(to identify the machine where the lock is held). |
| 1922 |
.\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5 |
| 1923 |
.\" documents it in fcntl(5). mtk, May 2007 |
| 1924 |
.\" Also, FreeBSD documents it (Apr 2014). |
| 1925 |
Clearly, |
| 1926 |
.I l_pid |
| 1927 |
alone is not going to be very useful if the process holding the lock |
| 1928 |
may live on a different machine; |
| 1929 |
on Linux, while present on some architectures (such as MIPS32), |
| 1930 |
this field is not used. |
| 1931 |
.P |
| 1932 |
The original Linux |
| 1933 |
.BR fcntl () |
| 1934 |
system call was not designed to handle large file offsets |
| 1935 |
(in the |
| 1936 |
.I flock |
| 1937 |
structure). |
| 1938 |
Consequently, an |
| 1939 |
.BR fcntl64 () |
| 1940 |
system call was added in Linux 2.4. |
| 1941 |
The newer system call employs a different structure for file locking, |
| 1942 |
.IR flock64 , |
| 1943 |
and corresponding operations, |
| 1944 |
.BR F_GETLK64 , |
| 1945 |
.BR F_SETLK64 , |
| 1946 |
and |
| 1947 |
.BR F_SETLKW64 . |
| 1948 |
However, these details can be ignored by applications using glibc, whose |
| 1949 |
.BR fcntl () |
| 1950 |
wrapper function transparently employs the more recent system call |
| 1951 |
where it is available. |
| 1952 |
.SS Record locking and NFS |
| 1953 |
Before Linux 3.12, if an NFSv4 client |
| 1954 |
loses contact with the server for a period of time |
| 1955 |
(defined as more than 90 seconds with no communication), |
| 1956 |
.\" |
| 1957 |
.\" Neil Brown: With NFSv3 the failure mode is the reverse. If |
| 1958 |
.\" the server loses contact with a client then any lock stays in place |
| 1959 |
.\" indefinitely ("why can't I read my mail"... I remember it well). |
| 1960 |
.\" |
| 1961 |
it might lose and regain a lock without ever being aware of the fact. |
| 1962 |
(The period of time after which contact is assumed lost is known as |
| 1963 |
the NFSv4 leasetime. |
| 1964 |
On a Linux NFS server, this can be determined by looking at |
| 1965 |
.IR /proc/fs/nfsd/nfsv4leasetime , |
| 1966 |
which expresses the period in seconds. |
| 1967 |
The default value for this file is 90.) |
| 1968 |
.\" |
| 1969 |
.\" Jeff Layton: |
| 1970 |
.\" Note that this is not a firm timeout. The server runs a job |
| 1971 |
.\" periodically to clean out expired stateful objects, and it's likely |
| 1972 |
.\" that there is some time (maybe even up to another whole lease period) |
| 1973 |
.\" between when the timeout expires and the job actually runs. If the |
| 1974 |
.\" client gets a RENEW in there within that window, its lease will be |
| 1975 |
.\" renewed and its state preserved. |
| 1976 |
.\" |
| 1977 |
This scenario potentially risks data corruption, |
| 1978 |
since another process might acquire a lock in the intervening period |
| 1979 |
and perform file I/O. |
| 1980 |
.P |
| 1981 |
Since Linux 3.12, |
| 1982 |
.\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d |
| 1983 |
if an NFSv4 client loses contact with the server, |
| 1984 |
any I/O to the file by a process which "thinks" it holds |
| 1985 |
a lock will fail until that process closes and reopens the file. |
| 1986 |
A kernel parameter, |
| 1987 |
.IR nfs.recover_lost_locks , |
| 1988 |
can be set to 1 to obtain the pre-3.12 behavior, |
| 1989 |
whereby the client will attempt to recover lost locks |
| 1990 |
when contact is reestablished with the server. |
| 1991 |
Because of the attendant risk of data corruption, |
| 1992 |
.\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd |
| 1993 |
this parameter defaults to 0 (disabled). |
| 1994 |
.SH BUGS |
| 1995 |
.SS F_SETFL |
| 1996 |
It is not possible to use |
| 1997 |
.B F_SETFL |
| 1998 |
to change the state of the |
| 1999 |
.B O_DSYNC |
| 2000 |
and |
| 2001 |
.B O_SYNC |
| 2002 |
flags. |
| 2003 |
.\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable |
| 2004 |
.\" via fcntl(2), but currently Linux does not permit this |
| 2005 |
.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994 |
| 2006 |
Attempts to change the state of these flags are silently ignored. |
| 2007 |
.SS F_GETOWN |
| 2008 |
A limitation of the Linux system call conventions on some |
| 2009 |
architectures (notably i386) means that if a (negative) |
| 2010 |
process group ID to be returned by |
| 2011 |
.B F_GETOWN |
| 2012 |
falls in the range \-1 to \-4095, then the return value is wrongly |
| 2013 |
interpreted by glibc as an error in the system call; |
| 2014 |
.\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h |
| 2015 |
that is, the return value of |
| 2016 |
.BR fcntl () |
| 2017 |
will be \-1, and |
| 2018 |
.I errno |
| 2019 |
will contain the (positive) process group ID. |
| 2020 |
The Linux-specific |
| 2021 |
.B F_GETOWN_EX |
| 2022 |
operation avoids this problem. |
| 2023 |
.\" mtk, Dec 04: some limited testing on alpha and ia64 seems to |
| 2024 |
.\" indicate that ANY negative PGID value will cause F_GETOWN |
| 2025 |
.\" to misinterpret the return as an error. Some other architectures |
| 2026 |
.\" seem to have the same range check as i386. |
| 2027 |
Since glibc 2.11, glibc makes the kernel |
| 2028 |
.B F_GETOWN |
| 2029 |
problem invisible by implementing |
| 2030 |
.B F_GETOWN |
| 2031 |
using |
| 2032 |
.BR F_GETOWN_EX . |
| 2033 |
.SS F_SETOWN |
| 2034 |
In Linux 2.4 and earlier, there is bug that can occur |
| 2035 |
when an unprivileged process uses |
| 2036 |
.B F_SETOWN |
| 2037 |
to specify the owner |
| 2038 |
of a socket file descriptor |
| 2039 |
as a process (group) other than the caller. |
| 2040 |
In this case, |
| 2041 |
.BR fcntl () |
| 2042 |
can return \-1 with |
| 2043 |
.I errno |
| 2044 |
set to |
| 2045 |
.BR EPERM , |
| 2046 |
even when the owner process (group) is one that the caller |
| 2047 |
has permission to send signals to. |
| 2048 |
Despite this error return, the file descriptor owner is set, |
| 2049 |
and signals will be sent to the owner. |
| 2050 |
.\" |
| 2051 |
.SS Deadlock detection |
| 2052 |
The deadlock-detection algorithm employed by the kernel when dealing with |
| 2053 |
.B F_SETLKW |
| 2054 |
requests can yield both |
| 2055 |
false negatives (failures to detect deadlocks, |
| 2056 |
leaving a set of deadlocked processes blocked indefinitely) |
| 2057 |
and false positives |
| 2058 |
.RB ( EDEADLK |
| 2059 |
errors when there is no deadlock). |
| 2060 |
For example, |
| 2061 |
the kernel limits the lock depth of its dependency search to 10 steps, |
| 2062 |
meaning that circular deadlock chains that exceed |
| 2063 |
that size will not be detected. |
| 2064 |
In addition, the kernel may falsely indicate a deadlock |
| 2065 |
when two or more processes created using the |
| 2066 |
.BR clone (2) |
| 2067 |
.B CLONE_FILES |
| 2068 |
flag place locks that appear (to the kernel) to conflict. |
| 2069 |
.\" |
| 2070 |
.SS Mandatory locking |
| 2071 |
The Linux implementation of mandatory locking |
| 2072 |
is subject to race conditions which render it unreliable: |
| 2073 |
.\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2 |
| 2074 |
.\" |
| 2075 |
.\" Reconfirmed by Jeff Layton |
| 2076 |
.\" From: Jeff Layton <jlayton <at> redhat.com> |
| 2077 |
.\" Subject: Re: Status of fcntl() mandatory locking |
| 2078 |
.\" Newsgroups: gmane.linux.file-systems |
| 2079 |
.\" Date: 2014-04-28 10:07:57 GMT |
| 2080 |
.\" http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518 |
| 2081 |
a |
| 2082 |
.BR write (2) |
| 2083 |
call that overlaps with a lock may modify data after the mandatory lock is |
| 2084 |
acquired; |
| 2085 |
a |
| 2086 |
.BR read (2) |
| 2087 |
call that overlaps with a lock may detect changes to data that were made |
| 2088 |
only after a write lock was acquired. |
| 2089 |
Similar races exist between mandatory locks and |
| 2090 |
.BR mmap (2). |
| 2091 |
It is therefore inadvisable to rely on mandatory locking. |
| 2092 |
.SH SEE ALSO |
| 2093 |
.BR dup2 (2), |
| 2094 |
.BR flock (2), |
| 2095 |
.BR open (2), |
| 2096 |
.BR socket (2), |
| 2097 |
.BR lockf (3), |
| 2098 |
.BR capabilities (7), |
| 2099 |
.BR feature_test_macros (7), |
| 2100 |
.BR lslocks (8) |
| 2101 |
.P |
| 2102 |
.IR locks.txt , |
| 2103 |
.IR mandatory\-locking.txt , |
| 2104 |
and |
| 2105 |
.I dnotify.txt |
| 2106 |
in the Linux kernel source directory |
| 2107 |
.I Documentation/filesystems/ |
| 2108 |
(on older kernels, these files are directly under the |
| 2109 |
.I Documentation/ |
| 2110 |
directory, and |
| 2111 |
.I mandatory\-locking.txt |
| 2112 |
is called |
| 2113 |
.IR mandatory.txt ) |