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