2 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
3 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
4 .\" and Copyright (C) 1998 Jamie Lokier;
5 .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk;
6 .\" and Copyright (C) 2014 Jeff Layton
7 .\" and Copyright (C) 2014 David Herrmann
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.
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.
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
27 .\" Formatted or processed versions of this manual, if unaccompanied by
28 .\" the source, must acknowledge the copyright and authors of this work.
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
65 .TH FCNTL 2 2015-12-28 "Linux" "Linux Programmer's Manual"
67 fcntl \- manipulate file descriptor
70 .B #include <unistd.h>
73 .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );"
77 performs one of the operations described below on the open file descriptor
79 The operation is determined by
83 can take an optional third argument.
84 Whether or not this argument is required is determined by
86 The required argument type is indicated in parentheses after each
88 name (in most cases, the required type is
90 and we identify the argument using the name
94 is specified if the argument is not required.
96 Certain of the operations below are supported only since a particular
98 The preferred method of checking whether the host kernel supports
99 a particular operation is to invoke
103 value and then test whether the call failed with
105 indicating that the kernel does not recognize this value.
106 .SS Duplicating a file descriptor
108 .BR F_DUPFD " (\fIint\fP)"
109 Find the lowest numbered available file descriptor
110 greater than or equal to
112 and make it be a copy of
114 This is different from
116 which uses exactly the file descriptor specified.
118 On success, the new file descriptor is returned.
124 .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
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
135 For an explanation of why this flag is useful,
136 see the description of
140 .SS File descriptor flags
141 The following commands manipulate the flags associated with
143 Currently, only one such flag is defined:
145 the close-on-exec flag.
148 bit is 0, the file descriptor will remain open across an
150 otherwise it will be closed.
152 .BR F_GETFD " (\fIvoid\fP)"
153 Read the file descriptor flags;
157 .BR F_SETFD " (\fIint\fP)"
158 Set the file descriptor flags to the value specified by
161 In multithreaded programs, using
164 to set the close-on-exec flag at the same time as another thread performs a
168 is vulnerable to a race condition that may unintentionally leak
169 the file descriptor to the program executed in the child process.
170 See the discussion of the
174 for details and a remedy to the problem.
175 .SS File status flags
176 Each open file description has certain associated status flags,
181 and possibly modified by
183 Duplicated file descriptors
188 etc.) refer to the same open file description, and thus
189 share the same file status flags.
191 The file status flags and their semantics are described in
194 .BR F_GETFL " (\fIvoid\fP)"
195 Get the file access mode and the file status flags;
199 .BR F_SETFL " (\fIint\fP)"
200 Set the file status flags to the value specified by
203 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
204 and file creation flags
206 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
210 On Linux this command can change only the
218 It is not possible to change the
222 flags; see BUGS, below.
223 .SS Advisory record locking
224 Linux implements traditional ("process-associated") UNIX record locks,
225 as standardized by POSIX.
226 For a Linux-specific alternative with better semantics,
227 see the discussion of open file description locks below.
233 are used to acquire, release, and test for the existence of record
234 locks (also known as byte-range, file-segment, or file-region locks).
237 is a pointer to a structure that has at least the following fields
238 (in unspecified order).
244 short l_type; /* Type of lock: F_RDLCK,
246 short l_whence; /* How to interpret l_start:
247 SEEK_SET, SEEK_CUR, SEEK_END */
248 off_t l_start; /* Starting offset for lock */
249 off_t l_len; /* Number of bytes to lock */
250 pid_t l_pid; /* PID of process blocking our lock
251 (set by F_GETLK and F_OFD_GETLK) */
258 .IR l_whence ", " l_start ", and " l_len
259 fields of this structure specify the range of bytes we wish to lock.
260 Bytes past the end of the file may be locked,
261 but not bytes before the start of the file.
264 is the starting offset for the lock, and is interpreted
266 the start of the file (if
270 the current file offset (if
274 or the end of the file (if
278 In the final two cases,
280 can be a negative number provided the
281 offset does not lie before the start of the file.
284 specifies the number of bytes to be locked.
287 is positive, then the range to be locked covers bytes
290 .IR l_start + l_len \-1.
293 has the special meaning: lock all bytes starting at the
294 location specified by
295 .IR l_whence " and " l_start
296 through to the end of file, no matter how large the file grows.
298 POSIX.1-2001 allows (but does not require)
299 an implementation to support a negative
303 is negative, the interval described by
309 This is supported by Linux since kernel versions 2.4.21 and 2.5.49.
313 field can be used to place a read
318 Any number of processes may hold a read lock (shared lock)
319 on a file region, but only one process may hold a write lock
321 An exclusive lock excludes all other locks,
322 both shared and exclusive.
323 A single process can hold only one type of lock on a file region;
324 if a new lock is applied to an already-locked region,
325 then the existing lock is converted to the new lock type.
326 (Such conversions may involve splitting, shrinking, or coalescing with
327 an existing lock if the byte range specified by the new lock does not
328 precisely coincide with the range of the existing lock.)
330 .BR F_SETLK " (\fIstruct flock *\fP)"
337 or release a lock (when
341 on the bytes specified by the
342 .IR l_whence ", " l_start ", and " l_len
345 If a conflicting lock is held by another process,
346 this call returns \-1 and sets
352 (The error returned in this case differs across implementations,
353 so POSIX requires a portable application to check for both errors.)
355 .BR F_SETLKW " (\fIstruct flock *\fP)"
358 but if a conflicting lock is held on the file, then wait for that
360 If a signal is caught while waiting, then the call is interrupted
361 and (after the signal handler has returned)
362 returns immediately (with return value \-1 and
369 .BR F_GETLK " (\fIstruct flock *\fP)"
370 On input to this call,
372 describes a lock we would like to place on the file.
373 If the lock could be placed,
375 does not actually place it, but returns
381 and leaves the other fields of the structure unchanged.
383 If one or more incompatible locks would prevent
384 this lock being placed, then
386 returns details about one of those locks in the
387 .IR l_type ", " l_whence ", " l_start ", and " l_len
390 If the conflicting lock is a traditional (process-associated) record lock,
393 field is set to the PID of the process holding that lock.
394 If the conflicting lock is an open file description lock, then
397 Note that the returned information
398 may already be out of date by the time the caller inspects it.
400 In order to place a read lock,
402 must be open for reading.
403 In order to place a write lock,
405 must be open for writing.
406 To place both types of lock, open a file read-write.
408 When placing locks with
412 whereby two or more processes have their
413 lock requests mutually blocked by locks held by the other processes.
414 For example, suppose process A holds a write lock on byte 100 of a file,
415 and process B holds a write lock on byte 200.
416 If each process then attempts to lock the byte already
417 locked by the other process using
419 then, without deadlock detection,
420 both processes would remain blocked indefinitely.
421 When the kernel detects such deadlocks,
422 it causes one of the blocking lock requests to immediately fail with the error
424 an application that encounters such an error should release
425 some of its locks to allow other applications to proceed before
426 attempting regain the locks that it requires.
427 Circular deadlocks involving more than two processes are also detected.
428 Note, however, that there are limitations to the kernel's
429 deadlock-detection algorithm; see BUGS.
431 As well as being removed by an explicit
433 record locks are automatically released when the process terminates.
435 Record locks are not inherited by a child created via
437 but are preserved across an
440 Because of the buffering performed by the
442 library, the use of record locking with routines in that package
443 should be avoided; use
449 The record locks described above are associated with the process
450 (unlike the open file description locks described below).
451 This has some unfortunate consequences:
455 file descriptor referring to a file,
456 then all of the process's locks on that file are released,
457 regardless of the file descriptor(s) on which the locks were obtained.
458 .\" (Additional file descriptors referring to the same file
459 .\" may have been obtained by calls to
460 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
461 This is bad: it means that a process can lose its locks on
466 when for some reason a library function decides to open, read,
467 and close the same file.
469 The threads in a process share locks.
471 a multithreaded program can't use record locking to ensure
472 that threads don't simultaneously access the same region of a file.
474 Open file description locks solve both of these problems.
475 .SS Open file description locks (non-POSIX)
476 Open file description locks are advisory byte-range locks whose operation is
477 in most respects identical to the traditional record locks described above.
478 This lock type is Linux-specific,
479 and available since Linux 3.15.
480 (There is a proposal with the Austin Group
481 .\" FIXME . Review progress into POSIX
482 .\" http://austingroupbugs.net/view.php?id=768
483 to include this lock type in the next revision of POSIX.1.)
484 For an explanation of open file descriptions, see
487 The principal difference between the two lock types
488 is that whereas traditional record locks
489 are associated with a process,
490 open file description locks are associated with the
491 open file description on which they are acquired,
492 much like locks acquired with
494 Consequently (and unlike traditional advisory record locks),
495 open file description locks are inherited across
501 and are only automatically released on the last close
502 of the open file description,
503 instead of being released on any close of the file.
505 Conflicting lock combinations
506 (i.e., a read lock and a write lock or two write locks)
507 where one lock is an open file description lock and the other
508 is a traditional record lock conflict
509 even when they are acquired by the same process on the same file descriptor.
511 Open file description locks placed via the same open file description
512 (i.e., via the same file descriptor,
513 or via a duplicate of the file descriptor created by
518 and so on) are always compatible:
519 if a new lock is placed on an already locked region,
520 then the existing lock is converted to the new lock type.
521 (Such conversions may result in splitting, shrinking, or coalescing with
522 an existing lock as discussed above.)
524 On the other hand, open file description locks may conflict with
525 each other when they are acquired via different open file descriptions.
526 Thus, the threads in a multithreaded program can use
527 open file description locks to synchronize access to a file region
528 by having each thread perform its own
530 on the file and applying locks via the resulting file descriptor.
532 As with traditional advisory locks, the third argument to
538 By contrast with traditional record locks, the
540 field of that structure must be set to zero
541 when using the commands described below.
543 The commands for working with open file description locks are analogous
544 to those used with traditional locks:
546 .BR F_OFD_SETLK " (\fIstruct flock *\fP)"
547 Acquire an open file description lock (when
553 or release an open file description lock (when
557 on the bytes specified by the
558 .IR l_whence ", " l_start ", and " l_len
561 If a conflicting lock is held by another process,
562 this call returns \-1 and sets
567 .BR F_OFD_SETLKW " (\fIstruct flock *\fP)"
570 but if a conflicting lock is held on the file, then wait for that lock to be
572 If a signal is caught while waiting, then the call is interrupted
573 and (after the signal handler has returned) returns immediately
574 (with return value \-1 and
581 .BR F_OFD_GETLK " (\fIstruct flock *\fP)"
582 On input to this call,
584 describes an open file description lock we would like to place on the file.
585 If the lock could be placed,
587 does not actually place it, but returns
593 and leaves the other fields of the structure unchanged.
594 If one or more incompatible locks would prevent this lock being placed,
595 then details about one of these locks are returned via
597 as described above for
600 In the current implementation,
601 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
602 no deadlock detection is performed for open file description locks.
603 (This contrasts with process-associated record locks,
604 for which the kernel does perform deadlock detection.)
606 .SS Mandatory locking
608 the Linux implementation of mandatory locking is unreliable.
611 By default, both traditional (process-associated) and open file description
612 record locks are advisory.
613 Advisory locks are not enforced and are useful only between
614 cooperating processes.
616 Both lock types can also be mandatory.
617 Mandatory locks are enforced for all processes.
618 If a process tries to perform an incompatible access (e.g.,
622 on a file region that has an incompatible mandatory lock,
623 then the result depends upon whether the
625 flag is enabled for its open file description.
628 flag is not enabled, then
629 the system call is blocked until the lock is removed
630 or converted to a mode that is compatible with the access.
633 flag is enabled, then the system call fails with the error
636 To make use of mandatory locks, mandatory locking must be enabled
637 both on the filesystem that contains the file to be locked,
638 and on the file itself.
639 Mandatory locking is enabled on a filesystem
640 using the "\-o mand" option to
646 Mandatory locking is enabled on a file by disabling
647 group execute permission on the file and enabling the set-group-ID
653 Mandatory locking is not specified by POSIX.
654 Some other systems also support mandatory locking,
655 although the details of how to enable it vary across systems.
664 are used to manage I/O availability signals:
666 .BR F_GETOWN " (\fIvoid\fP)"
667 Return (as the function result)
668 the process ID or process group currently receiving
672 signals for events on file descriptor
674 Process IDs are returned as positive values;
675 process group IDs are returned as negative values (but see BUGS below).
679 .BR F_SETOWN " (\fIint\fP)"
680 Set the process ID or process group ID that will receive
684 signals for events on file descriptor
688 A process ID is specified as a positive value;
689 a process group ID is specified as a negative value.
690 Most commonly, the calling process specifies itself as the owner
699 status flag on a file descriptor by using the
705 signal is sent whenever input or output becomes possible
706 on that file descriptor.
708 can be used to obtain delivery of a signal other than
710 If this permission check fails, then the signal is
713 Sending a signal to the owner process (group) specified by
715 is subject to the same permissions checks as are described for
717 where the sending process is the one that employs
719 (but see BUGS below).
721 If the file descriptor
728 signals that are delivered when out-of-band
729 data arrives on that socket.
731 is sent in any situation where
733 would report the socket as having an "exceptional condition".)
734 .\" The following appears to be rubbish. It doesn't seem to
735 .\" be true according to the kernel source, and I can write
736 .\" a program that gets a terminal-generated SIGIO even though
737 .\" it is not the foreground process group of the terminal.
740 .\" If the file descriptor
742 .\" refers to a terminal device, then SIGIO
743 .\" signals are sent to the foreground process group of the terminal.
745 The following was true in 2.6.x kernels up to and including
749 If a nonzero value is given to
751 in a multithreaded process running with a threading library
752 that supports thread groups (e.g., NPTL),
753 then a positive value given to
755 has a different meaning:
756 .\" The relevant place in the (2.6) kernel source is the
757 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
758 instead of being a process ID identifying a whole process,
759 it is a thread ID identifying a specific thread within a process.
760 Consequently, it may be necessary to pass
766 to get sensible results when
769 (In current Linux threading implementations,
770 a main thread's thread ID is the same as its process ID.
771 This means that a single-threaded program can equally use
776 Note, however, that the statements in this paragraph do not apply
779 signal generated for out-of-band data on a socket:
780 this signal is always sent to either a process or a process group,
781 depending on the value given to
783 .\" send_sigurg()/send_sigurg_to_task() bypasses
784 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
785 .\" to directly call send_group_sig_info()
786 .\" -- MTK, Apr 2005 (kernel 2.6.11)
789 The above behavior was accidentally dropped in Linux 2.6.12,
790 and won't be restored.
791 From Linux 2.6.32 onward, use
797 signals at a particular thread.
799 .BR F_GETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
800 Return the current file descriptor owner settings
801 as defined by a previous
804 The information is returned in the structure pointed to by
806 which has the following form:
819 field will have one of the values
826 field is a positive integer representing a thread ID, process ID,
832 .BR F_SETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
833 This operation performs a similar task to
835 It allows the caller to direct I/O availability signals
836 to a specific thread, process, or process group.
837 The caller specifies the target of signals via
839 which is a pointer to a
844 field has one of the following values, which define how
850 Send the signal to the thread whose thread ID
851 (the value returned by a call to
859 Send the signal to the process whose ID
864 Send the signal to the process group whose ID
867 (Note that, unlike with
869 a process group ID is specified as a positive value here.)
872 .BR F_GETSIG " (\fIvoid\fP)"
873 Return (as the function result)
874 the signal sent when input or output becomes possible.
875 A value of zero means
878 Any other value (including
881 signal sent instead, and in this case additional info is available to
882 the signal handler if installed with
887 .BR F_SETSIG " (\fIint\fP)"
888 Set the signal sent when input or output becomes possible
889 to the value given in
891 A value of zero means to send the default
894 Any other value (including
896 is the signal to send instead, and in this case additional info
897 is available to the signal handler if installed with
900 .\" The following was true only up until 2.6.11:
902 .\" Additionally, passing a nonzero value to
904 .\" changes the signal recipient from a whole process to a specific thread
905 .\" within a process.
906 .\" See the description of
908 .\" for more details.
912 with a nonzero value, and setting
917 extra information about I/O events is passed to
923 field indicates the source is
927 field gives the file descriptor associated with the event.
929 there is no indication which file descriptors are pending, and you
930 should use the usual mechanisms
936 set etc.) to determine which file descriptors are available for I/O.
938 Note that the file descriptor provided in
940 is the one that was specified during the
943 This can lead to an unusual corner case.
944 If the file descriptor is duplicated
946 or similar), and the original file descriptor is closed,
947 then I/O events will continue to be generated, but the
949 field will contain the number of the now closed file descriptor.
951 By selecting a real time signal (value >=
953 multiple I/O events may be queued using the same signal numbers.
954 (Queuing is dependent on available memory.)
955 Extra information is available
958 is set for the signal handler, as above.
960 Note that Linux imposes a limit on the
961 number of real-time signals that may be queued to a
966 and if this limit is reached, then the kernel reverts to
969 and this signal is delivered to the entire
970 process rather than to a specific thread.
971 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
973 Using these mechanisms, a program can implement fully asynchronous I/O
982 is specific to BSD and Linux.
987 specified in POSIX.1 is in conjunction with the use of the
990 (POSIX does not specify the
999 POSIX has asynchronous I/O and the
1001 structure to achieve similar things; these are also available
1002 in Linux as part of the GNU C Library (Glibc).
1007 (Linux 2.4 onward) are used (respectively) to establish a new lease,
1008 and retrieve the current lease, on the open file description
1009 referred to by the file descriptor
1011 A file lease provides a mechanism whereby the process holding
1012 the lease (the "lease holder") is notified (via delivery of a signal)
1013 when a process (the "lease breaker") tries to
1017 the file referred to by that file descriptor.
1019 .BR F_SETLEASE " (\fIint\fP)"
1020 Set or remove a file lease according to which of the following
1021 values is specified in the integer
1026 Take out a read lease.
1027 This will cause the calling process to be notified when
1028 the file is opened for writing or is truncated.
1029 .\" The following became true in kernel 2.6.10:
1030 .\" See the man-pages-2.09 Changelog for further info.
1031 A read lease can be placed only on a file descriptor that
1032 is opened read-only.
1035 Take out a write lease.
1036 This will cause the caller to be notified when
1037 the file is opened for reading or writing or is truncated.
1038 A write lease may be placed on a file only if there are no
1039 other open file descriptors for the file.
1042 Remove our lease from the file.
1045 Leases are associated with an open file description (see
1047 This means that duplicate file descriptors (created by, for example,
1051 refer to the same lease, and this lease may be modified
1052 or released using any of these descriptors.
1053 Furthermore, the lease is released by either an explicit
1055 operation on any of these duplicate file descriptors, or when all
1056 such file descriptors have been closed.
1058 Leases may be taken out only on regular files.
1059 An unprivileged process may take out a lease only on a file whose
1060 UID (owner) matches the filesystem UID of the process.
1063 capability may take out leases on arbitrary files.
1065 .BR F_GETLEASE " (\fIvoid\fP)"
1066 Indicates what type of lease is associated with the file descriptor
1069 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
1070 indicating, respectively, a read lease , a write lease, or no lease.
1074 When a process (the "lease breaker") performs an
1078 that conflicts with a lease established via
1080 the system call is blocked by the kernel and
1081 the kernel notifies the lease holder by sending it a signal
1084 The lease holder should respond to receipt of this signal by doing
1085 whatever cleanup is required in preparation for the file to be
1086 accessed by another process (e.g., flushing cached buffers) and
1087 then either remove or downgrade its lease.
1088 A lease is removed by performing an
1094 If the lease holder currently holds a write lease on the file,
1095 and the lease breaker is opening the file for reading,
1096 then it is sufficient for the lease holder to downgrade
1097 the lease to a read lease.
1098 This is done by performing an
1105 If the lease holder fails to downgrade or remove the lease within
1106 the number of seconds specified in
1107 .IR /proc/sys/fs/lease-break-time ,
1108 then the kernel forcibly removes or downgrades the lease holder's lease.
1110 Once a lease break has been initiated,
1112 returns the target lease type (either
1116 depending on what would be compatible with the lease breaker)
1117 until the lease holder voluntarily downgrades or removes the lease or
1118 the kernel forcibly does so after the lease break timer expires.
1120 Once the lease has been voluntarily or forcibly removed or downgraded,
1121 and assuming the lease breaker has not unblocked its system call,
1122 the kernel permits the lease breaker's system call to proceed.
1124 If the lease breaker's blocked
1128 is interrupted by a signal handler,
1129 then the system call fails with the error
1131 but the other steps still occur as described above.
1132 If the lease breaker is killed by a signal while blocked in
1136 then the other steps still occur as described above.
1137 If the lease breaker specifies the
1141 then the call immediately fails with the error
1143 but the other steps still occur as described above.
1145 The default signal used to notify the lease holder is
1147 but this can be changed using the
1153 command is performed (even one specifying
1156 handler is established using
1158 then the handler will receive a
1160 structure as its second argument, and the
1162 field of this argument will hold the file descriptor of the leased file
1163 that has been accessed by another process.
1164 (This is useful if the caller holds leases against multiple files.)
1165 .SS File and directory change notification (dnotify)
1167 .BR F_NOTIFY " (\fIint\fP)"
1169 Provide notification when the directory referred to by
1171 or any of the files that it contains is changed.
1172 The events to be notified are specified in
1174 which is a bit mask specified by ORing together zero or more of
1205 into this directory).
1211 to another directory,
1215 A file was renamed within this directory
1219 The attributes of a file were changed
1228 (In order to obtain these definitions, the
1230 feature test macro must be defined before including
1234 Directory notifications are normally "one-shot", and the application
1235 must reregister to receive further notifications.
1240 then notification will remain in effect until explicitly removed.
1242 .\" The following does seem a poor API-design choice...
1245 requests is cumulative, with the events in
1247 being added to the set already monitored.
1248 To disable notification of all events, make an
1254 Notification occurs via delivery of a signal.
1255 The default signal is
1257 but this can be changed using the
1263 is one of the nonqueuing standard signals;
1264 switching to the use of a real-time signal means that
1265 multiple notifications can be queued to the process.)
1266 In the latter case, the signal handler receives a
1268 structure as its second argument (if the handler was
1273 field of this structure contains the file descriptor which
1274 generated the notification (useful when establishing notification
1275 on multiple directories).
1277 Especially when using
1279 a real time signal should be used for notification,
1280 so that multiple notifications can be queued.
1283 New applications should use the
1285 interface (available since kernel 2.6.13),
1286 which provides a much superior interface for obtaining notifications of
1290 .SS Changing the capacity of a pipe
1292 .BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)"
1293 Change the capacity of the pipe referred to by
1298 An unprivileged process can adjust the pipe capacity to any value
1299 between the system page size and the limit defined in
1300 .IR /proc/sys/fs/pipe-max-size
1303 Attempts to set the pipe capacity below the page size are silently
1304 rounded up to the page size.
1305 Attempts by an unprivileged process to set the pipe capacity above the limit in
1306 .IR /proc/sys/fs/pipe-max-size
1309 a privileged process
1310 .RB ( CAP_SYS_RESOURCE )
1311 can override the limit.
1312 When allocating the buffer for the pipe,
1313 the kernel may use a capacity larger than
1315 if that is convenient for the implementation.
1316 The actual capacity that is set is returned as the function result.
1317 Attempting to set the pipe capacity smaller than the amount
1318 of buffer space currently used to store data produces the error
1321 .BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)"
1322 Return (as the function result) the capacity of the pipe referred to by
1326 File seals limit the set of allowed operations on a given file.
1327 For each seal that is set on a file,
1328 a specific set of operations will fail with
1330 on this file from now on.
1331 The file is said to be sealed.
1332 The default set of seals depends on the type of the underlying
1333 file and filesystem.
1334 For an overview of file sealing, a discussion of its purpose,
1335 and some code examples, see
1336 .BR memfd_create (2).
1340 filesystem supports sealing.
1341 On other filesystems, all
1343 operations that operate on seals will return
1346 Seals are a property of an inode.
1347 Thus, all open file descriptors referring to the same inode share
1348 the same set of seals.
1349 Furthermore, seals can never be removed, only added.
1351 .BR F_ADD_SEALS " (\fIint\fP; since Linux 3.17)"
1352 Add the seals given in the bit-mask argument
1354 to the set of seals of the inode referred to by the file descriptor
1356 Seals cannot be removed again.
1357 Once this call succeeds, the seals are enforced by the kernel immediately.
1358 If the current set of seals includes
1360 (see below), then this call will be rejected with
1362 Adding a seal that is already set is a no-op, in case
1365 In order to place a seal, the file descriptor
1369 .BR F_GET_SEALS " (\fIvoid\fP; since Linux 3.17)"
1370 Return (as the function result) the current set of seals
1371 of the inode referred to by
1373 If no seals are set, 0 is returned.
1374 If the file does not support sealing, \-1 is returned and
1379 The following seals are available:
1382 If this seal is set, any further call to
1388 Therefore, this seal prevents any modifications to the set of seals itself.
1389 If the initial set of seals of a file includes
1391 then this effectively causes the set of seals to be constant and locked.
1394 If this seal is set, the file in question cannot be reduced in size.
1403 Those calls will fail with
1405 if you try to shrink the file in question.
1406 Increasing the file size is still possible.
1409 If this seal is set, the size of the file in question cannot be increased.
1412 beyond the end of the file,
1417 These calls will fail with
1419 if you use them to increase the file size.
1420 If you keep the size or shrink it, those calls still work as expected.
1423 If this seal is set, you cannot modify the contents of the file.
1424 Note that shrinking or growing the size of the file is
1425 still possible and allowed.
1426 .\" One or more other seals are typically used with F_SEAL_WRITE
1427 .\" because, given a file with the F_SEAL_WRITE seal set, then,
1428 .\" while it would no longer be possible to (say) write zeros into
1429 .\" the last 100 bytes of a file, it would still be possible
1430 .\" to (say) shrink the file by 100 bytes using ftruncate(), and
1431 .\" then increase the file size by 100 bytes, which would have
1432 .\" the effect of replacing the last hundred bytes by zeros.
1434 Thus, this seal is normally used in combination with one of the other seals.
1439 (only in combination with the
1440 .B FALLOC_FL_PUNCH_HOLE
1442 Those calls will fail with
1444 if this seal is set.
1445 Furthermore, trying to create new shared, writable memory-mappings via
1458 if any writable, shared mapping exists.
1459 Such mappings must be unmapped before you can add this seal.
1460 Furthermore, if there are any asynchronous I/O operations
1461 .RB ( io_submit (2))
1462 pending on the file,
1463 all outstanding writes will be discarded.
1465 For a successful call, the return value depends on the operation:
1468 The new file descriptor.
1471 Value of file descriptor flags.
1474 Value of file status flags.
1477 Type of lease held on file descriptor.
1480 Value of file descriptor owner.
1483 Value of signal sent when read or write becomes possible, or zero
1488 .BR F_GETPIPE_SZ ", " F_SETPIPE_SZ
1492 A bit mask identifying the seals that have been set
1493 for the inode referred to by
1499 On error, \-1 is returned, and
1501 is set appropriately.
1504 .BR EACCES " or " EAGAIN
1505 Operation is prohibited by locks held by other processes.
1508 The operation is prohibited because the file has been memory-mapped by
1513 is not an open file descriptor
1521 and the file descriptor open mode doesn't match with the
1522 type of lock requested.
1528 and the new pipe capacity specified in
1530 is smaller than the amount of buffer space currently
1531 used to store data in the pipe.
1540 and there exists a writable, shared mapping on the file referred to by
1544 It was detected that the specified
1546 command would cause a deadlock.
1550 is outside your accessible address space.
1558 and the operation was interrupted by a signal; see
1569 and the operation was interrupted by a signal before the lock was checked or
1571 Most likely when locking a remote file (e.g., locking over
1572 NFS), but can sometimes happen locally.
1575 The value specified in
1577 is not recognized by this kernel.
1585 includes an unrecognized sealing bit.
1593 and the filesystem containing the inode referred to by
1595 does not support sealing.
1603 is negative or is greater than the maximum allowable value
1604 (see the discussion of
1615 is not an allowable signal number.
1626 was not specified as zero.
1632 and the per-process limit on the number of open file descriptors
1636 Too many segment locks open, lock table is full, or a remote locking
1637 protocol failed (e.g., locking over NFS).
1645 does not refer to a directory.
1648 Attempted to clear the
1650 flag on a file that has the append-only attribute set.
1658 was not open for writing
1659 or the current set of seals on the file already includes
1662 SVr4, 4.3BSD, POSIX.1-2001.
1673 are specified in POSIX.1-2001.
1678 are specified in POSIX.1-2001.
1679 (To get their definitions, define either
1683 with the value 500 or greater, or
1685 with the value 200809L or greater.)
1688 is specified in POSIX.1-2008.
1689 (To get this definition, define
1691 with the value 200809L or greater, or
1693 with the value 700 or greater.)
1708 macro to obtain these definitions.)
1710 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
1716 are Linux-specific (and one must define
1718 to obtain their definitions),
1719 but work is being done to have them included in the next version of POSIX.1.
1725 .\" FIXME . Once glibc adds support, add a note about FTM requirements
1727 The errors returned by
1729 are different from those returned by
1735 system call was not designed to handle large file offsets
1741 system call was added in Linux 2.4.
1742 The newer system call employs a different structure for file locking,
1744 and corresponding commands,
1749 However, these details can be ignored by applications using glibc, whose
1751 wrapper function transparently employs the more recent system call
1752 where it is available.
1754 The errors returned by
1756 are different from those returned by
1759 Since kernel 2.0, there is no interaction between the types of lock
1765 Several systems have more fields in
1767 such as, for example,
1769 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
1770 .\" documents it in fcntl(5). mtk, May 2007
1771 .\" Also, FreeBSD documents it (Apr 2014).
1774 alone is not going to be very useful if the process holding the lock
1775 may live on a different machine.
1779 system call was not designed to handle large file offsets
1785 system call was added in Linux 2.4.
1786 The newer system call employs a different structure for file locking,
1788 and corresponding commands,
1793 However, these details can be ignored by applications using glibc, whose
1795 wrapper function transparently employs the more recent system call
1796 where it is available.
1797 .SS Record locking and NFS
1798 Before Linux 3.12, if an NFSv4 client
1799 loses contact with the server for a period of time
1800 (defined as more than 90 seconds with no communication),
1802 .\" Neil Brown: With NFSv3 the failure mode is the reverse. If
1803 .\" the server loses contact with a client then any lock stays in place
1804 .\" indefinitely ("why can't I read my mail"... I remember it well).
1806 it might lose and regain a lock without ever being aware of the fact.
1807 (The period of time after which contact is assumed lost is known as
1808 the NFSv4 leasetime.
1809 On a Linux NFS server, this can be determined by looking at
1810 .IR /proc/fs/nfsd/nfsv4leasetime ,
1811 which expresses the period in seconds.
1812 The default value for this file is 90.)
1815 .\" Note that this is not a firm timeout. The server runs a job
1816 .\" periodically to clean out expired stateful objects, and it's likely
1817 .\" that there is some time (maybe even up to another whole lease period)
1818 .\" between when the timeout expires and the job actually runs. If the
1819 .\" client gets a RENEW in there within that window, its lease will be
1820 .\" renewed and its state preserved.
1822 This scenario potentially risks data corruption,
1823 since another process might acquire a lock in the intervening period
1824 and perform file I/O.
1827 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
1828 if an NFSv4 client loses contact with the server,
1829 any I/O to the file by a process which "thinks" it holds
1830 a lock will fail until that process closes and reopens the file.
1832 .IR nfs.recover_lost_locks ,
1833 can be set to 1 to obtain the pre-3.12 behavior,
1834 whereby the client will attempt to recover lost locks
1835 when contact is reestablished with the server.
1836 Because of the attendant risk of data corruption,
1837 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
1838 this parameter defaults to 0 (disabled).
1841 It is not possible to use
1843 to change the state of the
1848 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
1849 .\" via fcntl(2), but currently Linux does not permit this
1850 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
1851 Attempts to change the state of these flags are silently ignored.
1853 A limitation of the Linux system call conventions on some
1854 architectures (notably i386) means that if a (negative)
1855 process group ID to be returned by
1857 falls in the range \-1 to \-4095, then the return value is wrongly
1858 interpreted by glibc as an error in the system call;
1859 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
1860 that is, the return value of
1864 will contain the (positive) process group ID.
1867 operation avoids this problem.
1868 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
1869 .\" indicate that ANY negative PGID value will cause F_GETOWN
1870 .\" to misinterpret the return as an error. Some other architectures
1871 .\" seem to have the same range check as i386.
1872 Since glibc version 2.11, glibc makes the kernel
1874 problem invisible by implementing
1879 In Linux 2.4 and earlier, there is bug that can occur
1880 when an unprivileged process uses
1882 to specify the owner
1883 of a socket file descriptor
1884 as a process (group) other than the caller.
1891 even when the owner process (group) is one that the caller
1892 has permission to send signals to.
1893 Despite this error return, the file descriptor owner is set,
1894 and signals will be sent to the owner.
1896 .SS Deadlock detection
1897 The deadlock-detection algorithm employed by the kernel when dealing with
1899 requests can yield both
1900 false negatives (failures to detect deadlocks,
1901 leaving a set of deadlocked processes blocked indefinitely)
1904 errors when there is no deadlock).
1906 the kernel limits the lock depth of its dependency search to 10 steps,
1907 meaning that circular deadlock chains that exceed
1908 that size will not be detected.
1909 In addition, the kernel may falsely indicate a deadlock
1910 when two or more processes created using the
1913 flag place locks that appear (to the kernel) to conflict.
1915 .SS Mandatory locking
1916 The Linux implementation of mandatory locking
1917 is subject to race conditions which render it unreliable:
1918 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
1920 .\" Reconfirmed by Jeff Layton
1921 .\" From: Jeff Layton <jlayton <at> redhat.com>
1922 .\" Subject: Re: Status of fcntl() mandatory locking
1923 .\" Newsgroups: gmane.linux.file-systems
1924 .\" Date: 2014-04-28 10:07:57 GMT
1925 .\" http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
1928 call that overlaps with a lock may modify data after the mandatory lock is
1932 call that overlaps with a lock may detect changes to data that were made
1933 only after a write lock was acquired.
1934 Similar races exist between mandatory locks and
1936 It is therefore inadvisable to rely on mandatory locking.
1943 .BR capabilities (7),
1944 .BR feature_test_macros (7)
1947 .IR mandatory-locking.txt ,
1950 in the Linux kernel source directory
1951 .IR Documentation/filesystems/
1952 (on older kernels, these files are directly under the
1955 .I mandatory-locking.txt