2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
5 .\" Copyright (C) 1993 Michael Haardt, Ian Jackson;
6 .\" Copyright (C) 1998 Jamie Lokier;
7 .\" Copyright (C) 2002 Michael Kerrisk.
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.
29 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
31 .\" and again on 960413 and 980804 and 981223.
32 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
33 .\" Applied correction by Christian Ehrhardt - aeb, 990712
34 .\" Modified 2002-04-23 by Michael Kerrisk <mtk-manpages@gmx.net>
35 .\" Added note on F_SETFL and O_DIRECT
36 .\" Complete rewrite + expansion of material on file locking
37 .\" Incorporated description of F_NOTIFY, drawing on
38 .\" Stephen Rothwell's notes in Documentation/dnotify.txt.
39 .\" Added description of F_SETLEASE and F_GETLEASE
40 .\" Corrected and polished, aeb, 020527.
41 .\" Modified 2004-03-03 by Michael Kerrisk <mtk-manpages@gmx.net>
42 .\" Modified description of file leases: fixed some errors of detail
43 .\" Replaced the term "lease contestant" by "lease breaker"
44 .\" Modified, 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net>
45 .\" Added notes on capability requirements
46 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
47 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
48 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
49 .\" Described behaviour of F_SETOWN/F_SETSIG in
50 .\" multi-threaded processes, and generally cleaned
51 .\" up the discussion of F_SETOWN.
52 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
53 .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
54 .\" and earlier. Added text on permissions required to send signal.
56 .TH FCNTL 2 2005-05-20 "Linux 2.6.14" "Linux Programmer's Manual"
58 fcntl \- manipulate file descriptor
61 .B #include <unistd.h>
64 .BI "int fcntl(int " fd ", int " cmd );
65 .BI "int fcntl(int " fd ", int " cmd ", long " arg );
66 .BI "int fcntl(int " fd ", int " cmd ", struct flock *" lock );
70 performs one of the operations described below on the open file descriptor
72 The operation is determined by
75 .SS "Duplicating a file descriptor"
78 Find the lowest numbered available file descriptor
79 greater than or equal to
81 and make it be a copy of
83 This is different from
85 which uses exactly the descriptor specified.
87 On success, the new descriptor is returned.
93 .SS "File descriptor flags"
94 The following commands manipulate the flags associated with
96 Currently, only one such flag is defined:
98 the close-on-exec flag.
101 bit is 0, the file descriptor will remain open across an
103 otherwise it will be closed.
106 Read the file descriptor flags.
109 Set the file descriptor flags to the value specified by
112 .SS "File status flags"
113 Each open file description has certain associated status flags,
118 and possibly modified by
120 Duplicated file descriptors
125 etc.) refer to the same open file description, and thus
126 share the same file status flags.
128 The file status flags and their semantics are described in
132 Read the file status flags.
135 Set the file status flags to the value specified by
138 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
139 and file creation flags
141 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
145 On Linux this command can only change the
153 .\" FIXME According to SUSv3, O_SYNC should also be modifiable
154 .\" via fcntl(2), but currently Linux does not permit this
155 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
157 .SS "Advisory locking"
158 .BR F_GETLK ", " F_SETLK " and " F_SETLKW
159 are used to acquire, release, and test for the existence of record
160 locks (also known as file-segment or file-region locks).
163 is a pointer to a structure that has at least the following fields
164 (in unspecified order).
170 short l_type; /* Type of lock: F_RDLCK,
172 short l_whence; /* How to interpret l_start:
173 SEEK_SET, SEEK_CUR, SEEK_END */
174 off_t l_start; /* Starting offset for lock */
175 off_t l_len; /* Number of bytes to lock */
176 pid_t l_pid; /* PID of process blocking our lock
184 .IR l_whence ", " l_start ", and " l_len
185 fields of this structure specify the range of bytes we wish to lock.
187 is the starting offset for the lock, and is interpreted
189 the start of the file (if
193 the current file offset (if
197 or the end of the file (if
201 In the final two cases,
203 can be a negative number provided the
204 offset does not lie before the start of the file.
206 is a non-negative integer (but see the NOTES below) specifying
207 the number of bytes to be locked.
208 Bytes past the end of the file may be locked,
209 but not bytes before the start of the file.
212 has the special meaning: lock all bytes starting at the
213 location specified by
214 .IR l_whence " and " l_start
215 through to the end of file, no matter how large the file grows.
219 field can be used to place a read
224 Any number of processes may hold a read lock (shared lock)
225 on a file region, but only one process may hold a write lock
226 (exclusive lock). An exclusive lock excludes all other locks,
227 both shared and exclusive.
228 A single process can hold only one type of lock on a file region;
229 if a new lock is applied to an already-locked region,
230 then the existing lock is converted to the new lock type.
231 (Such conversions may involve splitting, shrinking, or coalescing with
232 an existing lock if the byte range specified by the new lock does not
233 precisely coincide with the range of the existing lock.)
242 or release a lock (when
246 on the bytes specified by the
247 .IR l_whence ", " l_start ", and " l_len
250 If a conflicting lock is held by another process,
251 this call returns \-1 and sets
261 but if a conflicting lock is held on the file, then wait for that
263 If a signal is caught while waiting, then the call is interrupted
264 and (after the signal handler has returned)
265 returns immediately (with return value \-1 and
271 On input to this call,
273 describes a lock we would like to place on the file.
274 If the lock could be placed,
276 does not actually place it, but returns
282 and leaves the other fields of the structure unchanged.
283 If one or more incompatible locks would prevent
284 this lock being placed, then
286 returns details about one of these locks in the
287 .IR l_type ", " l_whence ", " l_start ", and " l_len
292 to be the PID of the process holding that lock.
294 In order to place a read lock,
296 must be open for reading.
297 In order to place a write lock,
299 must be open for writing.
300 To place both types of lock, open a file read-write.
302 As well as being removed by an explicit
304 record locks are automatically released when the process
305 terminates or if it closes
307 file descriptor referring to a file on which locks are held.
308 .\" (Additional file descriptors referring to the same file
309 .\" may have been obtained by calls to
310 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl (2).)
311 This is bad: it means that a process can lose the locks on
316 when for some reason a library function decides to open, read
319 Record locks are not inherited by a child created via
321 but are preserved across an
324 Because of the buffering performed by the
326 library, the use of record locking with routines in that package
327 should be avoided; use
333 .SS "Mandatory locking"
335 The above record locks may be either advisory or mandatory,
336 and are advisory by default.
338 Advisory locks are not enforced and are useful only between
339 cooperating processes.
341 Mandatory locks are enforced for all processes.
342 If a process tries to perform an incompatible access (e.g.,
346 on a file region that has an incompatible mandatory lock,
347 then the result depends upon whether the
349 flag is enabled for its open file description.
352 flag is not enabled, then
353 system call is blocked until the lock is removed
354 or converted to a mode that is compatible with the access.
357 flag is enabled, then the system call fails with the error
361 To make use of mandatory locks, mandatory locking must be enabled
362 both on the file system that contains the file to be locked,
363 and on the file itself.
364 Mandatory locking is enabled on a file system
365 using the "\-o mand" option to
371 Mandatory locking is enabled on a file by disabling
372 group execute permission on the file and enabling the set-group-ID
378 .SS "Managing signals"
379 .BR F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG
380 are used to manage I/O availability signals:
383 Get the process ID or process group currently receiving SIGIO
384 and SIGURG signals for events on file descriptor
386 Process IDs are returned as positive values;
387 process group IDs are returned as negative values (but see BUGS below).
390 Set the process ID or process group ID that will receive SIGIO
391 and SIGURG signals for events on file descriptor
393 A process ID is specified as a positive value;
394 a process group ID is specified as a negative value.
395 Most commonly, the calling process specifies itself as the owner
404 status flag on a file descriptor (either by providing this flag with the
406 .\" FIXME The statement that O_ASYNC can be used in open() does not
407 .\" match reality; setting O_ASYNC via open() does not seem to be
409 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
410 call, or by using the
414 a SIGIO signal is sent whenever input or output becomes possible
415 on that file descriptor.
417 can be used to obtain delivery of a signal other than SIGIO.
418 If this permission check fails, then the signal is
421 Sending a signal to the owner process (group) specified by
423 is subject to the same permissions checks as are described for
425 where the sending process is the one that employs
427 (but see BUGS below).
429 If the file descriptor
434 the recipient of SIGURG signals that are delivered when out-of-band
435 data arrives on that socket. (SIGURG is sent in any situation where
437 would report the socket as having an "exceptional condition".)
438 .\" The following appears to be rubbish. It doesn't seem to
439 .\" be true according to the kernel source, and I can write
440 .\" a program that gets a terminal-generated SIGIO even though
441 .\" it is not the foreground process group of the terminal.
444 .\" If the file descriptor
446 .\" refers to a terminal device, then SIGIO
447 .\" signals are sent to the foreground process group of the terminal.
449 If a non-zero value is given to
451 in a multi-threaded process running with a threading library
452 that supports thread groups (e.g., NPTL),
453 then a positive value given to
455 has a different meaning:
456 .\" The relevant place in the (2.6) kernel source is the
457 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
458 instead of being a process ID identifying a whole process,
459 it is a thread ID identifying a specific thread within a process.
460 Consequently, it may be necessary to pass
466 to get sensible results when
469 (In current Linux threading implementations,
470 a main thread's thread ID is the same as its process ID.
471 This means that a single-threaded program can equally use
476 Note, however, that the statements in this paragraph do not apply
477 to the SIGURG signal generated for out-of-band data on a socket:
478 this signal is always sent to either a process or a process group,
479 depending on the value given to
481 .\" send_sigurg()/send_sigurg_to_task() bypasses
482 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
483 .\" to directly call send_group_sig_info()
484 .\" -- MTK, Apr 2005 (kernel 2.6.11)
485 Note also that Linux imposes a limit on the
486 number of real-time signals that may be queued to a
491 and if this limit is reached, then the kernel reverts to
492 delivering SIGIO, and this signal is delivered to the entire
493 process rather than to a specific thread.
494 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
497 Get the signal sent when input or output becomes possible. A value of
498 zero means SIGIO is sent. Any other value (including SIGIO) is the
499 signal sent instead, and in this case additional info is available to
500 the signal handler if installed with SA_SIGINFO.
503 Sets the signal sent when input or output becomes possible. A value of
504 zero means to send the default SIGIO signal. Any other value (including
505 SIGIO) is the signal to send instead, and in this case additional info
506 is available to the signal handler if installed with SA_SIGINFO.
508 Additionally, passing a non-zero value to
510 changes the signal recipient from a whole process to a specific thread
512 See the description of
518 with a non-zero value, and setting SA_SIGINFO for the
521 extra information about I/O events is passed to
527 field indicates the source is SI_SIGIO, the
529 field gives the file descriptor associated with the event. Otherwise,
530 there is no indication which file descriptors are pending, and you
531 should use the usual mechanisms
537 set etc.) to determine which file descriptors are available for I/O.
539 By selecting a POSIX.1b real time signal (value >= SIGRTMIN), multiple
540 I/O events may be queued using the same signal numbers. (Queuing is
541 dependent on available memory). Extra information is available
542 if SA_SIGINFO is set for the signal handler, as above.
544 Using these mechanisms, a program can implement fully asynchronous I/O
555 is specific to BSD and Linux.
559 are Linux-specific. POSIX has asynchronous I/O and the
561 structure to achieve similar things; these are also available
562 in Linux as part of the GNU C Library (Glibc).
568 (Linux 2.4 onwards) are used (respectively) to establish and
569 retrieve the current setting of the calling process's lease on
570 the file referred to by
572 A file lease provides a mechanism whereby the process holding
573 the lease (the "lease holder") is notified (via delivery of a signal)
574 when a process (the "lease breaker") tries to
581 Set or remove a file lease according to which of the following
582 values is specified in the integer
588 Take out a read lease.
589 This will cause the calling process to be notified when
590 the file is opened for writing or is truncated.
591 .\" The following became true in kernel 2.6.10:
592 .\" See the man-pages-2.09 Changelog for further info.
593 A read lease can only be placed on a file descriptor that
597 Take out a write lease.
598 This will cause the caller to be notified when
599 the file is opened for reading or writing or is truncated.
600 A write lease may be placed on a file only if no other process
601 currently has the file open.
604 Remove our lease from the file.
607 A process may hold only one type of lease on a file.
609 Leases may only be taken out on regular files.
610 An unprivileged process may only take out a lease on a file whose
611 UID matches the file system UID of the process.
614 capability may take out leases on arbitrary files.
617 Indicates what type of lease we hold on the file
621 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK,
622 indicating, respectively, that the calling process holds a
623 read, a write, or no lease on the file.
624 (The third argument to
628 When a process (the "lease breaker") performs an
632 that conflicts with a lease established via
634 the system call is blocked by the kernel and
635 the kernel notifies the lease holder by sending it a signal
637 The lease holder should respond to receipt of this signal by doing
638 whatever cleanup is required in preparation for the file to be
639 accessed by another process (e.g., flushing cached buffers) and
640 then either remove or downgrade its lease.
641 A lease is removed by performing an
647 If we currently hold a write lease on the file,
648 and the lease breaker is opening the file for reading,
649 then it is sufficient to downgrade the lease to a read lease.
650 This is done by performing an
657 If the lease holder fails to downgrade or remove the lease within
658 the number of seconds specified in
659 .I /proc/sys/fs/lease-break-time
660 then the kernel forcibly removes or downgrades the lease holder's lease.
662 Once the lease has been voluntarily or forcibly removed or downgraded,
663 and assuming the lease breaker has not unblocked its system call,
664 the kernel permits the lease breaker's system call to proceed.
666 If the lease breaker's blocked
670 is interrupted by a signal handler,
671 then the system call fails with the error
673 but the other steps still occur as described above.
674 If the lease breaker is killed by a signal while blocked in
678 then the other steps still occur as described above.
679 If the lease breaker specifies the
683 then the call immediately fails with the error
685 but the other steps still occur as described above.
687 The default signal used to notify the lease holder is SIGIO,
688 but this can be changed using the
694 command is performed (even one specifying SIGIO), and the signal
695 handler is established using SA_SIGINFO, then the handler will
698 structure as its second argument, and the
700 field of this argument will hold the descriptor of the leased file
701 that has been accessed by another process.
702 (This is useful if the caller holds leases against multiple files).
704 .SS "File and directory change notification"
708 Provide notification when the directory referred to by
710 or any of the files that it contains is changed.
711 The events to be notified are specified in
713 which is a bit mask specified by ORing together zero or more of
720 Bit Description (event in directory)
721 DN_ACCESS A file was accessed (read, pread, readv)
722 DN_MODIFY A file was modified (write, pwrite,
723 writev, truncate, ftruncate)
724 DN_CREATE A file was created (open, creat, mknod,
725 mkdir, link, symlink, rename)
726 DN_DELETE A file was unlinked (unlink, rename to
727 another directory, rmdir)
728 DN_RENAME A file was renamed within this
730 DN_ATTRIB The attributes of a file were changed
731 (chown, chmod, utime[s])
734 (In order to obtain these definitions, the _GNU_SOURCE feature test macro
737 Directory notifications are normally "one-shot", and the application
738 must re-register to receive further notifications.
743 then notification will remain in effect until explicitly removed.
745 .\" The following does seem a poor API-design choice...
748 requests is cumulative, with the events in
750 being added to the set already monitored.
751 To disable notification of all events, make an
757 Notification occurs via delivery of a signal.
758 The default signal is SIGIO, but this can be changed using the
762 In the latter case, the signal handler receives a
764 structure as its second argument (if the handler was
765 established using SA_SIGINFO) and the
767 field of this structure contains the file descriptor which
768 generated the notification (useful when establishing notification
769 on multiple directories).
771 Especially when using
773 a POSIX.1b real time signal should be used for notification,
774 so that multiple notifications can be queued.
777 New applications should consider using the
779 interface (available since kernel 2.6.13),
780 which provides a superior interface for obtaining notifications of
781 file system events. See
784 For a successful call, the return value depends on the operation:
796 Value of descriptor owner.
799 Value of signal sent when read or write becomes possible, or zero
800 for traditional SIGIO behaviour.
805 On error, \-1 is returned, and
807 is set appropriately.
810 .BR EACCES " or " EAGAIN
811 Operation is prohibited by locks held by other processes.
814 The operation is prohibited because the file has been memory-mapped by
819 is not an open file descriptor, or the command was
823 and the file descriptor open mode doesn't match with the
824 type of lock requested.
827 It was detected that the specified
829 command would cause a deadlock.
833 is outside your accessible address space.
838 the command was interrupted by a signal.
840 .BR F_GETLK " and " F_SETLK ,
841 the command was interrupted by a signal before the lock was checked or
842 acquired. Most likely when locking a remote file (e.g. locking over
843 NFS), but can sometimes happen locally.
849 is negative or is greater than the maximum allowable value. For
852 is not an allowable signal number.
857 the process already has the maximum number of file descriptors open.
860 Too many segment locks open, lock table is full, or a remote locking
861 protocol failed (e.g. locking over NFS).
864 Attempted to clear the
866 flag on a file that has the append-only attribute set.
868 The errors returned by
870 are different from those returned by
873 Since kernel 2.0, there is no interaction between the types of lock
879 POSIX 1003.1-2001 allows
881 to be negative. (And if it is, the interval described by the lock
886 This is supported by Linux since Linux 2.4.21 and 2.5.49.
888 Several systems have more fields in
894 alone is not going to be very useful if the process holding the lock
895 may live on a different machine.
897 A limitation of the Linux system call conventions on some
898 architectures (notably x86) means that if a (negative)
899 process group ID to be returned by
901 falls in the range \-1 to \-4095, then the return value is wrongly
902 interpreted by glibc as an error in the system call;
903 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
904 that is, the return value of
908 will contain the (positive) process group ID.
909 .\" FIXME Dec 04: some limited testing on alpha and ia64 seems to
910 .\" indicate that ANY negative PGID value will cause F_GETOWN
911 .\" to misinterpret the return as an error. Some other architectures
912 .\" seem to have the same range check as x86. Must document
913 .\" the reality on other architectures -- MTK
915 In Linux 2.4 and earlier, there is bug that can occur
916 when an unprivileged process uses
919 of a socket file descriptor
920 as a process (group) other than the caller.
927 even when the owner process (group) is one that the caller
928 has permission to send signals to.
929 Despite this error return, the file descriptor owner is set,
930 and signals will be sent to the owner.
932 SVr4, SVID, POSIX, X/OPEN, 4.3BSD. Only the operations F_DUPFD,
933 F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK, F_SETLKW,
934 F_GETOWN, and F_SETOWN are specified in POSIX.1.
935 F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE
937 (Define the _GNU_SOURCE macro to obtain these definitions.)
938 The flags legal for F_GETFL/F_SETFL are those supported by
940 and vary between these systems; O_APPEND, O_NONBLOCK, O_RDONLY,
941 and O_RDWR are specified in POSIX.1. SVr4 supports several other
942 options and flags not documented here.
944 SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
951 .BR capabilities (7),
952 .BR feature_test_macros (7)
954 See also locks.txt, mandatory.txt, and dnotify.txt in
955 /usr/src/linux/Documentation.