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
8 .\" %%%LICENSE_START(VERBATIM)
9 .\" Permission is granted to make and distribute verbatim copies of this
10 .\" manual provided the copyright notice and this permission notice are
11 .\" preserved on all copies.
13 .\" Permission is granted to copy and distribute modified versions of this
14 .\" manual under the conditions for verbatim copying, provided that the
15 .\" entire resulting derived work is distributed under the terms of a
16 .\" permission notice identical to this one.
18 .\" Since the Linux kernel and libraries are constantly changing, this
19 .\" manual page may be incorrect or out-of-date. The author(s) assume no
20 .\" responsibility for errors or omissions, or for damages resulting from
21 .\" the use of the information contained herein. The author(s) may not
22 .\" have taken the same level of care in the production of this manual,
23 .\" which is licensed free of charge, as they might when working
26 .\" Formatted or processed versions of this manual, if unaccompanied by
27 .\" the source, must acknowledge the copyright and authors of this work.
30 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
31 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
32 .\" and again on 960413 and 980804 and 981223.
33 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
34 .\" Applied correction by Christian Ehrhardt - aeb, 990712
35 .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Added note on F_SETFL and O_DIRECT
37 .\" Complete rewrite + expansion of material on file locking
38 .\" Incorporated description of F_NOTIFY, drawing on
39 .\" Stephen Rothwell's notes in Documentation/dnotify.txt.
40 .\" Added description of F_SETLEASE and F_GETLEASE
41 .\" Corrected and polished, aeb, 020527.
42 .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com>
43 .\" Modified description of file leases: fixed some errors of detail
44 .\" Replaced the term "lease contestant" by "lease breaker"
45 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
46 .\" Added notes on capability requirements
47 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
48 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
49 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
50 .\" Described behavior of F_SETOWN/F_SETSIG in
51 .\" multithreaded processes, and generally cleaned
52 .\" up the discussion of F_SETOWN.
53 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
54 .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
55 .\" and earlier. Added text on permissions required to send signal.
56 .\" 2009-09-30, Michael Kerrisk
57 .\" Note obsolete F_SETOWN behavior with threads.
58 .\" Document F_SETOWN_EX and F_GETOWN_EX
59 .\" 2010-06-17, Michael Kerrisk
60 .\" Document F_SETPIPE_SZ and F_GETPIPE_SZ.
62 .TH FCNTL 2 2014-05-21 "Linux" "Linux Programmer's Manual"
64 fcntl \- manipulate file descriptor
67 .B #include <unistd.h>
70 .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );"
74 performs one of the operations described below on the open file descriptor
76 The operation is determined by
80 can take an optional third argument.
81 Whether or not this argument is required is determined by
83 The required argument type is indicated in parentheses after each
85 name (in most cases, the required type is
87 and we identify the argument using the name
91 is specified if the argument is not required.
93 Certain of the operations below are supported only since a particular
95 The preferred method of checking whether the host kernel supports
96 a particular operation is to invoke
100 value and then test whether the call failed with
102 indicating that the kernel does not recognize this value.
103 .SS Duplicating a file descriptor
105 .BR F_DUPFD " (\fIint\fP)"
106 Find the lowest numbered available file descriptor
107 greater than or equal to
109 and make it be a copy of
111 This is different from
113 which uses exactly the descriptor specified.
115 On success, the new descriptor is returned.
121 .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
124 but additionally set the
125 close-on-exec flag for the duplicate descriptor.
126 Specifying this flag permits a program to avoid an additional
132 For an explanation of why this flag is useful,
133 see the description of
137 .SS File descriptor flags
138 The following commands manipulate the flags associated with
140 Currently, only one such flag is defined:
142 the close-on-exec flag.
145 bit is 0, the file descriptor will remain open across an
147 otherwise it will be closed.
149 .BR F_GETFD " (\fIvoid\fP)"
150 Read the file descriptor flags;
154 .BR F_SETFD " (\fIint\fP)"
155 Set the file descriptor flags to the value specified by
158 In multithreaded programs, using
161 to set the close-on-exec flag at the same time as another thread performs a
165 is vulnerable to a race condition that may unintentionally leak
166 the file descriptor to the program executed in the child process.
167 See the discussion of the
171 for details and a remedy to the problem.
172 .SS File status flags
173 Each open file description has certain associated status flags,
178 and possibly modified by
180 Duplicated file descriptors
185 etc.) refer to the same open file description, and thus
186 share the same file status flags.
188 The file status flags and their semantics are described in
191 .BR F_GETFL " (\fIvoid\fP)"
192 Get the file access mode and the file status flags;
196 .BR F_SETFL " (\fIint\fP)"
197 Set the file status flags to the value specified by
200 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
201 and file creation flags
203 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
207 On Linux this command can change only the
215 It is not possible to change the
219 flags; see BUGS, below.
220 .SS Advisory record locking
221 Linux implements traditional ("process-associated") UNIX record locks,
222 as standardized by POSIX.
223 For a Linux-specific alternative with better semantics,
224 see the discussion of open file description locks below.
230 are used to acquire, release, and test for the existence of record
231 locks (also known as byte-range, file-segment, or file-region locks).
234 is a pointer to a structure that has at least the following fields
235 (in unspecified order).
241 short l_type; /* Type of lock: F_RDLCK,
243 short l_whence; /* How to interpret l_start:
244 SEEK_SET, SEEK_CUR, SEEK_END */
245 off_t l_start; /* Starting offset for lock */
246 off_t l_len; /* Number of bytes to lock */
247 pid_t l_pid; /* PID of process blocking our lock
248 (set by F_GETLK and F_OFD_GETLK) */
255 .IR l_whence ", " l_start ", and " l_len
256 fields of this structure specify the range of bytes we wish to lock.
257 Bytes past the end of the file may be locked,
258 but not bytes before the start of the file.
261 is the starting offset for the lock, and is interpreted
263 the start of the file (if
267 the current file offset (if
271 or the end of the file (if
275 In the final two cases,
277 can be a negative number provided the
278 offset does not lie before the start of the file.
281 specifies the number of bytes to be locked.
284 is positive, then the range to be locked covers bytes
287 .IR l_start + l_len \-1.
290 has the special meaning: lock all bytes starting at the
291 location specified by
292 .IR l_whence " and " l_start
293 through to the end of file, no matter how large the file grows.
295 POSIX.1-2001 allows (but does not require)
296 an implementation to support a negative
300 is negative, the interval described by
306 This is supported by Linux since kernel versions 2.4.21 and 2.5.49.
310 field can be used to place a read
315 Any number of processes may hold a read lock (shared lock)
316 on a file region, but only one process may hold a write lock
318 An exclusive lock excludes all other locks,
319 both shared and exclusive.
320 A single process can hold only one type of lock on a file region;
321 if a new lock is applied to an already-locked region,
322 then the existing lock is converted to the new lock type.
323 (Such conversions may involve splitting, shrinking, or coalescing with
324 an existing lock if the byte range specified by the new lock does not
325 precisely coincide with the range of the existing lock.)
327 .BR F_SETLK " (\fIstruct flock *\fP)"
334 or release a lock (when
338 on the bytes specified by the
339 .IR l_whence ", " l_start ", and " l_len
342 If a conflicting lock is held by another process,
343 this call returns \-1 and sets
350 .BR F_SETLKW " (\fIstruct flock *\fP)"
353 but if a conflicting lock is held on the file, then wait for that
355 If a signal is caught while waiting, then the call is interrupted
356 and (after the signal handler has returned)
357 returns immediately (with return value \-1 and
364 .BR F_GETLK " (\fIstruct flock *\fP)"
365 On input to this call,
367 describes a lock we would like to place on the file.
368 If the lock could be placed,
370 does not actually place it, but returns
376 and leaves the other fields of the structure unchanged.
378 If one or more incompatible locks would prevent
379 this lock being placed, then
381 returns details about one of those locks in the
382 .IR l_type ", " l_whence ", " l_start ", and " l_len
385 If the conflicting lock is a traditional (process-associated) record lock,
388 field is set to the PID of the process holding that lock.
389 If the conflicting lock is an open file description lock, then
392 Note that the returned information
393 may already be out of date by the time the caller inspects it.
395 In order to place a read lock,
397 must be open for reading.
398 In order to place a write lock,
400 must be open for writing.
401 To place both types of lock, open a file read-write.
403 As well as being removed by an explicit
405 record locks are automatically released when the process terminates.
407 Record locks are not inherited by a child created via
409 but are preserved across an
412 Because of the buffering performed by the
414 library, the use of record locking with routines in that package
415 should be avoided; use
421 The record locks described above are associated with the process
422 (unlike the open file description locks described below).
423 This has some unfortunate consequences:
427 file descriptor referring to a file,
428 then all of the process's locks on that file are released,
429 regardless of the file descriptor(s) on which the locks were obtained.
430 .\" (Additional file descriptors referring to the same file
431 .\" may have been obtained by calls to
432 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
433 This is bad: it means that a process can lose its locks on
438 when for some reason a library function decides to open, read,
439 and close the same file.
441 The threads in a process share locks.
443 a multithreaded program can't use record locking to ensure
444 that threads don't simultaneously access the same region of a file.
446 Open file description locks solve both of these problems.
447 .SS Open file description locks (non-POSIX)
448 Open file description locks are advisory byte-range locks whose operation is
449 in most respects identical to the traditional record locks described above.
450 This lock type is Linux-specific,
451 and available since Linux 3.15.
453 The principal difference between the two lock types
454 is that whereas traditional record locks
455 are associated with a process,
456 open file description locks are associated with the
457 open file description on which they are acquired,
458 much like locks acquired with
460 Consequently (and unlike traditional advisory record locks),
461 open file description locks are inherited across
467 and are only automatically released on the last close
468 of the open file description,
469 instead of being released on any close of the file.
471 Open file description locks always conflict with traditional record locks,
472 even when they are acquired by the same process on the same file descriptor.
474 Open file description locks placed via the same open file description
475 (i.e., via the same file descriptor,
476 or via a duplicate of the file descriptor created by
481 and so on) are always compatible:
482 if a new lock is placed on an already locked region,
483 then the existing lock is converted to the new lock type.
484 (Such conversions may result in splitting, shrinking, or coalescing with
485 an existing lock as discussed above.)
487 On the other hand, open file description locks may conflict with
488 each other when they are acquired via different open file descriptions.
489 Thus, the threads in a multithreaded program can use
490 open file description locks to synchronize access to a file region
491 by having each thread perform its own
493 on the file and applying locks via the resulting file descriptor.
495 As with traditional advisory locks, the third argument to
501 By contrast with traditional record locks, the
503 field of that structure must be set to zero
504 when using the commands described below.
506 The commands for working with open file description locks are analogous
507 to those used with traditional locks:
509 .BR F_OFD_SETLK " (\fIstruct flock *\fP)"
510 Acquire an open file description lock (when
516 or release an open file description lock (when
520 on the bytes specified by the
521 .IR l_whence ", " l_start ", and " l_len
524 If a conflicting lock is held by another process,
525 this call returns \-1 and sets
530 .BR F_OFD_SETLKW " (\fIstruct flock *\fP)"
533 but if a conflicting lock is held on the file, then wait for that lock to be
535 If a signal is caught while waiting, then the call is interrupted
536 and (after the signal handler has returned) returns immediately
537 (with return value \-1 and
544 .BR F_OFD_GETLK " (\fIstruct flock *\fP)"
545 On input to this call,
547 describes an open file description lock we would like to place on the file.
548 If the lock could be placed,
550 does not actually place it, but returns
556 and leaves the other fields of the structure unchanged.
557 If one or more incompatible locks would prevent this lock being placed,
558 then details about one of these locks are returned via
560 as described above for
562 .SS Mandatory locking
564 the Linux implementation of mandatory locking is unreliable.
567 By default, both traditional (process-associated) and open file description
568 record locks are advisory.
569 Advisory locks are not enforced and are useful only between
570 cooperating processes.
572 Both lock types can also be mandatory.
573 Mandatory locks are enforced for all processes.
574 If a process tries to perform an incompatible access (e.g.,
578 on a file region that has an incompatible mandatory lock,
579 then the result depends upon whether the
581 flag is enabled for its open file description.
584 flag is not enabled, then
585 the system call is blocked until the lock is removed
586 or converted to a mode that is compatible with the access.
589 flag is enabled, then the system call fails with the error
592 To make use of mandatory locks, mandatory locking must be enabled
593 both on the filesystem that contains the file to be locked,
594 and on the file itself.
595 Mandatory locking is enabled on a filesystem
596 using the "\-o mand" option to
602 Mandatory locking is enabled on a file by disabling
603 group execute permission on the file and enabling the set-group-ID
609 Mandatory locking is not specified by POSIX.
610 Some other systems also support mandatory locking,
611 although the details of how to enable it vary across systems.
620 are used to manage I/O availability signals:
622 .BR F_GETOWN " (\fIvoid\fP)"
623 Return (as the function result)
624 the process ID or process group currently receiving
628 signals for events on file descriptor
630 Process IDs are returned as positive values;
631 process group IDs are returned as negative values (but see BUGS below).
635 .BR F_SETOWN " (\fIint\fP)"
636 Set the process ID or process group ID that will receive
640 signals for events on file descriptor
644 A process ID is specified as a positive value;
645 a process group ID is specified as a negative value.
646 Most commonly, the calling process specifies itself as the owner
655 status flag on a file descriptor by using the
661 signal is sent whenever input or output becomes possible
662 on that file descriptor.
664 can be used to obtain delivery of a signal other than
666 If this permission check fails, then the signal is
669 Sending a signal to the owner process (group) specified by
671 is subject to the same permissions checks as are described for
673 where the sending process is the one that employs
675 (but see BUGS below).
677 If the file descriptor
684 signals that are delivered when out-of-band
685 data arrives on that socket.
687 is sent in any situation where
689 would report the socket as having an "exceptional condition".)
690 .\" The following appears to be rubbish. It doesn't seem to
691 .\" be true according to the kernel source, and I can write
692 .\" a program that gets a terminal-generated SIGIO even though
693 .\" it is not the foreground process group of the terminal.
696 .\" If the file descriptor
698 .\" refers to a terminal device, then SIGIO
699 .\" signals are sent to the foreground process group of the terminal.
701 The following was true in 2.6.x kernels up to and including
705 If a nonzero value is given to
707 in a multithreaded process running with a threading library
708 that supports thread groups (e.g., NPTL),
709 then a positive value given to
711 has a different meaning:
712 .\" The relevant place in the (2.6) kernel source is the
713 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
714 instead of being a process ID identifying a whole process,
715 it is a thread ID identifying a specific thread within a process.
716 Consequently, it may be necessary to pass
722 to get sensible results when
725 (In current Linux threading implementations,
726 a main thread's thread ID is the same as its process ID.
727 This means that a single-threaded program can equally use
732 Note, however, that the statements in this paragraph do not apply
735 signal generated for out-of-band data on a socket:
736 this signal is always sent to either a process or a process group,
737 depending on the value given to
739 .\" send_sigurg()/send_sigurg_to_task() bypasses
740 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
741 .\" to directly call send_group_sig_info()
742 .\" -- MTK, Apr 2005 (kernel 2.6.11)
745 The above behavior was accidentally dropped in Linux 2.6.12,
746 and won't be restored.
747 From Linux 2.6.32 onward, use
753 signals at a particular thread.
755 .BR F_GETOWN_EX " (struct f_owner_ex *) (since Linux 2.6.32)"
756 Return the current file descriptor owner settings
757 as defined by a previous
760 The information is returned in the structure pointed to by
762 which has the following form:
775 field will have one of the values
782 field is a positive integer representing a thread ID, process ID,
788 .BR F_SETOWN_EX " (struct f_owner_ex *) (since Linux 2.6.32)"
789 This operation performs a similar task to
791 It allows the caller to direct I/O availability signals
792 to a specific thread, process, or process group.
793 The caller specifies the target of signals via
795 which is a pointer to a
800 field has one of the following values, which define how
806 Send the signal to the thread whose thread ID
807 (the value returned by a call to
815 Send the signal to the process whose ID
820 Send the signal to the process group whose ID
823 (Note that, unlike with
825 a process group ID is specified as a positive value here.)
828 .BR F_GETSIG " (\fIvoid\fP)"
829 Return (as the function result)
830 the signal sent when input or output becomes possible.
831 A value of zero means
834 Any other value (including
837 signal sent instead, and in this case additional info is available to
838 the signal handler if installed with
843 .BR F_SETSIG " (\fIint\fP)"
844 Set the signal sent when input or output becomes possible
845 to the value given in
847 A value of zero means to send the default
850 Any other value (including
852 is the signal to send instead, and in this case additional info
853 is available to the signal handler if installed with
856 .\" The following was true only up until 2.6.11:
858 .\" Additionally, passing a nonzero value to
860 .\" changes the signal recipient from a whole process to a specific thread
861 .\" within a process.
862 .\" See the description of
864 .\" for more details.
868 with a nonzero value, and setting
873 extra information about I/O events is passed to
879 field indicates the source is
883 field gives the file descriptor associated with the event.
885 there is no indication which file descriptors are pending, and you
886 should use the usual mechanisms
892 set etc.) to determine which file descriptors are available for I/O.
894 By selecting a real time signal (value >=
896 multiple I/O events may be queued using the same signal numbers.
897 (Queuing is dependent on available memory).
898 Extra information is available
901 is set for the signal handler, as above.
903 Note that Linux imposes a limit on the
904 number of real-time signals that may be queued to a
909 and if this limit is reached, then the kernel reverts to
912 and this signal is delivered to the entire
913 process rather than to a specific thread.
914 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
916 Using these mechanisms, a program can implement fully asynchronous I/O
925 is specific to BSD and Linux.
930 specified in POSIX.1 is in conjunction with the use of the
933 (POSIX does not specify the
942 POSIX has asynchronous I/O and the
944 structure to achieve similar things; these are also available
945 in Linux as part of the GNU C Library (Glibc).
950 (Linux 2.4 onward) are used (respectively) to establish a new lease,
951 and retrieve the current lease, on the open file description
952 referred to by the file descriptor
954 A file lease provides a mechanism whereby the process holding
955 the lease (the "lease holder") is notified (via delivery of a signal)
956 when a process (the "lease breaker") tries to
960 the file referred to by that file descriptor.
962 .BR F_SETLEASE " (\fIint\fP)"
963 Set or remove a file lease according to which of the following
964 values is specified in the integer
969 Take out a read lease.
970 This will cause the calling process to be notified when
971 the file is opened for writing or is truncated.
972 .\" The following became true in kernel 2.6.10:
973 .\" See the man-pages-2.09 Changelog for further info.
974 A read lease can be placed only on a file descriptor that
978 Take out a write lease.
979 This will cause the caller to be notified when
980 the file is opened for reading or writing or is truncated.
981 A write lease may be placed on a file only if there are no
982 other open file descriptors for the file.
985 Remove our lease from the file.
988 Leases are associated with an open file description (see
990 This means that duplicate file descriptors (created by, for example,
994 refer to the same lease, and this lease may be modified
995 or released using any of these descriptors.
996 Furthermore, the lease is released by either an explicit
998 operation on any of these duplicate descriptors, or when all
999 such descriptors have been closed.
1001 Leases may be taken out only on regular files.
1002 An unprivileged process may take out a lease only on a file whose
1003 UID (owner) matches the filesystem UID of the process.
1006 capability may take out leases on arbitrary files.
1008 .BR F_GETLEASE " (\fIvoid\fP)"
1009 Indicates what type of lease is associated with the file descriptor
1012 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
1013 indicating, respectively, a read lease , a write lease, or no lease.
1017 When a process (the "lease breaker") performs an
1021 that conflicts with a lease established via
1023 the system call is blocked by the kernel and
1024 the kernel notifies the lease holder by sending it a signal
1027 The lease holder should respond to receipt of this signal by doing
1028 whatever cleanup is required in preparation for the file to be
1029 accessed by another process (e.g., flushing cached buffers) and
1030 then either remove or downgrade its lease.
1031 A lease is removed by performing an
1037 If the lease holder currently holds a write lease on the file,
1038 and the lease breaker is opening the file for reading,
1039 then it is sufficient for the lease holder to downgrade
1040 the lease to a read lease.
1041 This is done by performing an
1048 If the lease holder fails to downgrade or remove the lease within
1049 the number of seconds specified in
1050 .IR /proc/sys/fs/lease-break-time ,
1051 then the kernel forcibly removes or downgrades the lease holder's lease.
1053 Once a lease break has been initiated,
1055 returns the target lease type (either
1059 depending on what would be compatible with the lease breaker)
1060 until the lease holder voluntarily downgrades or removes the lease or
1061 the kernel forcibly does so after the lease break timer expires.
1063 Once the lease has been voluntarily or forcibly removed or downgraded,
1064 and assuming the lease breaker has not unblocked its system call,
1065 the kernel permits the lease breaker's system call to proceed.
1067 If the lease breaker's blocked
1071 is interrupted by a signal handler,
1072 then the system call fails with the error
1074 but the other steps still occur as described above.
1075 If the lease breaker is killed by a signal while blocked in
1079 then the other steps still occur as described above.
1080 If the lease breaker specifies the
1084 then the call immediately fails with the error
1086 but the other steps still occur as described above.
1088 The default signal used to notify the lease holder is
1090 but this can be changed using the
1096 command is performed (even one specifying
1099 handler is established using
1101 then the handler will receive a
1103 structure as its second argument, and the
1105 field of this argument will hold the descriptor of the leased file
1106 that has been accessed by another process.
1107 (This is useful if the caller holds leases against multiple files).
1108 .SS File and directory change notification (dnotify)
1110 .BR F_NOTIFY " (\fIint\fP)"
1112 Provide notification when the directory referred to by
1114 or any of the files that it contains is changed.
1115 The events to be notified are specified in
1117 which is a bit mask specified by ORing together zero or more of
1124 A file was accessed (read, pread, readv)
1127 A file was modified (write, pwrite, writev, truncate, ftruncate).
1130 A file was created (open, creat, mknod, mkdir, link, symlink, rename).
1133 A file was unlinked (unlink, rename to another directory, rmdir).
1136 A file was renamed within this directory (rename).
1139 The attributes of a file were changed (chown, chmod, utime[s]).
1143 (In order to obtain these definitions, the
1145 feature test macro must be defined before including
1149 Directory notifications are normally "one-shot", and the application
1150 must reregister to receive further notifications.
1155 then notification will remain in effect until explicitly removed.
1157 .\" The following does seem a poor API-design choice...
1160 requests is cumulative, with the events in
1162 being added to the set already monitored.
1163 To disable notification of all events, make an
1169 Notification occurs via delivery of a signal.
1170 The default signal is
1172 but this can be changed using the
1176 In the latter case, the signal handler receives a
1178 structure as its second argument (if the handler was
1183 field of this structure contains the file descriptor which
1184 generated the notification (useful when establishing notification
1185 on multiple directories).
1187 Especially when using
1189 a real time signal should be used for notification,
1190 so that multiple notifications can be queued.
1193 New applications should use the
1195 interface (available since kernel 2.6.13),
1196 which provides a much superior interface for obtaining notifications of
1200 .SS Changing the capacity of a pipe
1202 .BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)"
1203 Change the capacity of the pipe referred to by
1208 An unprivileged process can adjust the pipe capacity to any value
1209 between the system page size and the limit defined in
1210 .IR /proc/sys/fs/pipe-max-size
1213 Attempts to set the pipe capacity below the page size are silently
1214 rounded up to the page size.
1215 Attempts by an unprivileged process to set the pipe capacity above the limit in
1216 .IR /proc/sys/fs/pipe-max-size
1219 a privileged process
1220 .RB ( CAP_SYS_RESOURCE )
1221 can override the limit.
1222 When allocating the buffer for the pipe,
1223 the kernel may use a capacity larger than
1225 if that is convenient for the implementation.
1228 operation returns the actual size used.
1229 Attempting to set the pipe capacity smaller than the amount
1230 of buffer space currently used to store data produces the error
1233 .BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)"
1234 Return (as the function result) the capacity of the pipe referred to by
1237 For a successful call, the return value depends on the operation:
1243 Value of file descriptor flags.
1246 Value of file status flags.
1249 Type of lease held on file descriptor.
1252 Value of descriptor owner.
1255 Value of signal sent when read or write becomes possible, or zero
1266 On error, \-1 is returned, and
1268 is set appropriately.
1271 .BR EACCES " or " EAGAIN
1272 Operation is prohibited by locks held by other processes.
1275 The operation is prohibited because the file has been memory-mapped by
1280 is not an open file descriptor, or the command was
1284 and the file descriptor open mode doesn't match with the
1285 type of lock requested.
1288 It was detected that the specified
1290 command would cause a deadlock.
1294 is outside your accessible address space.
1299 the command was interrupted by a signal; see
1302 .BR F_GETLK " and " F_SETLK ,
1303 the command was interrupted by a signal before the lock was checked or
1305 Most likely when locking a remote file (e.g., locking over
1306 NFS), but can sometimes happen locally.
1309 The value specified in
1311 is not recognized by this kernel.
1317 is negative or is greater than the maximum allowable value.
1321 is not an allowable signal number.
1332 was not specified as zero.
1337 the process already has the maximum number of file descriptors open.
1340 Too many segment locks open, lock table is full, or a remote locking
1341 protocol failed (e.g., locking over NFS).
1344 Attempted to clear the
1346 flag on a file that has the append-only attribute set.
1348 SVr4, 4.3BSD, POSIX.1-2001.
1359 are specified in POSIX.1-2001.
1364 are specified in POSIX.1-2001.
1365 (To get their definitions, define
1369 with the value 500 or greater, or define
1371 with the value 200809L or greater.)
1374 is specified in POSIX.1-2008.
1375 (To get this definition, define
1377 with the value 200809L or greater, or
1379 with the value 700 or greater.)
1394 macro to obtain these definitions.)
1396 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
1403 but work is being done to have them included in the next version of POSIX.1.
1404 .\" FIXME Presumably, glibc will require _GNU_SOURCE
1406 The errors returned by
1408 are different from those returned by
1414 system call was not designed to handle large file offsets
1420 system call was added in Linux 2.4.
1421 The newer system call employs a different structure for file locking,
1423 and corresponding commands,
1428 However, these details can be ignored by applications using glibc, whose
1430 wrapper function transparently employs the more recent system call
1431 where it is available.
1433 The errors returned by
1435 are different from those returned by
1438 Since kernel 2.0, there is no interaction between the types of lock
1444 Several systems have more fields in
1446 such as, for example,
1448 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
1449 .\" documents it in fcntl(5). mtk, May 2007
1450 .\" Also, FreeBSD documents it (Apr 2014).
1453 alone is not going to be very useful if the process holding the lock
1454 may live on a different machine.
1458 system call was not designed to handle large file offsets
1464 system call was added in Linux 2.4.
1465 The newer system call employs a different structure for file locking,
1467 and corresponding commands,
1472 However, these details can be ignored by applications using glibc, whose
1474 wrapper function transparently employs the more recent system call
1475 where it is available.
1476 .SS Record locking and NFS
1477 Before Linux 3.12, if an NFSv4 client
1478 loses contact with the server for a period of time
1479 (defined as more than 90 seconds with no communication),
1481 .\" Neil Brown: With NFSv3 the failure mode is the reverse. If
1482 .\" the server loses contact with a client then any lock stays in place
1483 .\" indefinitely ("why can't I read my mail"... I remember it well).
1485 it might lose and regain a lock without ever being aware of the fact.
1486 (The period of time after which contact is assumed lost is known as
1487 the NFSv4 leasetime.
1488 On a Linux NFS server, this can be determined by looking at
1489 .IR /proc/fs/nfsd/nfsv4leasetime ,
1490 which expresses the period in seconds.
1491 The default value for this file is 90.)
1494 .\" Note that this is not a firm timeout. The server runs a job
1495 .\" periodically to clean out expired stateful objects, and it's likely
1496 .\" that there is some time (maybe even up to another whole lease period)
1497 .\" between when the timeout expires and the job actually runs. If the
1498 .\" client gets a RENEW in there within that window, its lease will be
1499 .\" renewed and its state preserved.
1501 This scenario potentially risks data corruption,
1502 since another process might acquire a lock in the intervening period
1503 and perform file I/O.
1506 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
1507 if an NFSv4 client loses contact with the server,
1508 any I/O to the file by a process which "thinks" it holds
1509 a lock will fail until that process closes and reopens the file.
1511 .IR nfs.recover_lost_locks ,
1512 can be set to 1 to obtain the pre-3.12 behavior,
1513 whereby the client will attempt to recover lost locks
1514 when contact is reestablished with the server.
1515 Because of the attendant risk of data corruption,
1516 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
1517 this parameter defaults to 0 (disabled).
1520 It is not possible to use
1522 to change the state of the
1527 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
1528 .\" via fcntl(2), but currently Linux does not permit this
1529 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
1530 Attempts to change the state of these flags are silently ignored.
1532 A limitation of the Linux system call conventions on some
1533 architectures (notably i386) means that if a (negative)
1534 process group ID to be returned by
1536 falls in the range \-1 to \-4095, then the return value is wrongly
1537 interpreted by glibc as an error in the system call;
1538 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
1539 that is, the return value of
1543 will contain the (positive) process group ID.
1546 operation avoids this problem.
1547 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
1548 .\" indicate that ANY negative PGID value will cause F_GETOWN
1549 .\" to misinterpret the return as an error. Some other architectures
1550 .\" seem to have the same range check as i386.
1551 Since glibc version 2.11, glibc makes the kernel
1553 problem invisible by implementing
1558 In Linux 2.4 and earlier, there is bug that can occur
1559 when an unprivileged process uses
1561 to specify the owner
1562 of a socket file descriptor
1563 as a process (group) other than the caller.
1570 even when the owner process (group) is one that the caller
1571 has permission to send signals to.
1572 Despite this error return, the file descriptor owner is set,
1573 and signals will be sent to the owner.
1574 .SS Mandatory locking
1575 The Linux implementation of mandatory locking
1576 is subject to race conditions which render it unreliable:
1577 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
1579 .\" Reconfirmed by Jeff Layton
1580 .\" From: Jeff Layton <jlayton <at> redhat.com>
1581 .\" Subject: Re: Status of fcntl() mandatory locking
1582 .\" Newsgroups: gmane.linux.file-systems
1583 .\" Date: 2014-04-28 10:07:57 GMT
1584 .\" http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
1586 call that overlaps with a lock may modify data after the mandatory lock is
1590 call that overlaps with a lock may detect changes to data that were made
1591 only after a write lock was acquired.
1592 Similar races exist between mandatory locks and
1594 It is therefore inadvisable to rely on mandatory locking.
1601 .BR capabilities (7),
1602 .BR feature_test_macros (7)
1605 .IR mandatory-locking.txt ,
1608 in the Linux kernel source directory
1609 .IR Documentation/filesystems/
1610 (on older kernels, these files are directly under the
1613 .I mandatory-locking.txt