2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
5 .\" 1993 Michael Haardt, Ian Jackson;
7 .\" 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-20-13 "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
140 .RB (e.g., O_CREAT ", " O_EXCL ", " O_TRUNC )
144 On Linux this command can only change the
152 .\" FIXME According to SUSv3, O_SYNC should also be modifiable via
153 .\" fcntl(2), but currently Linux does not permit this -- MTK, Dec 04
155 .SS "Advisory locking"
156 .BR F_GETLK ", " F_SETLK " and " F_SETLKW
157 are used to acquire, release, and test for the existence of record
158 locks (also known as file-segment or file-region locks).
161 is a pointer to a structure that has at least the following fields
162 (in unspecified order).
168 short l_type; /* Type of lock: F_RDLCK,
170 short l_whence; /* How to interpret l_start:
171 SEEK_SET, SEEK_CUR, SEEK_END */
172 off_t l_start; /* Starting offset for lock */
173 off_t l_len; /* Number of bytes to lock */
174 pid_t l_pid; /* PID of process blocking our lock
182 .IR l_whence ", " l_start ", and " l_len
183 fields of this structure specify the range of bytes we wish to lock.
185 is the starting offset for the lock, and is interpreted
187 the start of the file (if
191 the current file offset (if
195 or the end of the file (if
199 In the final two cases,
201 can be a negative number provided the
202 offset does not lie before the start of the file.
204 is a non-negative integer (but see the NOTES below) specifying
205 the number of bytes to be locked.
206 Bytes past the end of the file may be locked,
207 but not bytes before the start of the file.
210 has the special meaning: lock all bytes starting at the
211 location specified by
212 .IR l_whence " and " l_start
213 through to the end of file, no matter how large the file grows.
217 field can be used to place a read
222 Any number of processes may hold a read lock (shared lock)
223 on a file region, but only one process may hold a write lock
224 (exclusive lock). An exclusive lock excludes all other locks,
225 both shared and exclusive.
226 A single process can hold only one type of lock on a file region;
227 if a new lock is applied to an already-locked region,
228 then the existing lock is converted to the new lock type.
229 (Such conversions may involve splitting, shrinking, or coalescing with
230 an existing lock if the byte range specified by the new lock does not
231 precisely coincide with the range of the existing lock.)
240 or release a lock (when
244 on the bytes specified by the
245 .IR l_whence ", " l_start ", and " l_len
248 If a conflicting lock is held by another process,
249 this call returns \-1 and sets
259 but if a conflicting lock is held on the file, then wait for that
261 If a signal is caught while waiting, then the call is interrupted
262 and (after the signal handler has returned)
263 returns immediately (with return value \-1 and
269 On input to this call,
271 describes a lock we would like to place on the file.
272 If the lock could be placed,
274 does not actually place it, but returns
280 and leaves the other fields of the structure unchanged.
281 If one or more incompatible locks would prevent
282 this lock being placed, then
284 returns details about one of these locks in the
285 .IR l_type ", " l_whence ", " l_start ", and " l_len
290 to be the PID of the process holding that lock.
292 In order to place a read lock,
294 must be open for reading.
295 In order to place a write lock,
297 must be open for writing.
298 To place both types of lock, open a file read-write.
300 As well as being removed by an explicit
302 record locks are automatically released when the process
303 terminates or if it closes
305 file descriptor referring to a file on which locks are held.
306 .\" (Additional file descriptors referring to the same file
307 .\" may have been obtained by calls to
308 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl (2).)
309 This is bad: it means that a process can lose the locks on
314 when for some reason a library function decides to open, read
317 Record locks are not inherited by a child created via
319 but are preserved across an
322 Because of the buffering performed by the
324 library, the use of record locking with routines in that package
325 should be avoided; use
331 .SS "Mandatory locking"
333 The above record locks may be either advisory or mandatory,
334 and are advisory by default.
335 To make use of mandatory locks, mandatory locking must be enabled
336 (using the "\-o mand" option to
338 for the file system containing the
339 file to be locked and enabled on the file itself (by disabling
340 group execute permission on the file and enabling the set-group-ID
343 Advisory locks are not enforced and are useful only between
344 cooperating processes. Mandatory locks are enforced for all
347 .SS "Managing signals"
348 .BR F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG
349 are used to manage I/O availability signals:
352 Get the process ID or process group currently receiving SIGIO
353 and SIGURG signals for events on file descriptor
355 Process IDs are returned as positive values;
356 process group IDs are returned as negative values (but see BUGS below).
359 Set the process ID or process group ID that will receive SIGIO
360 and SIGURG signals for events on file descriptor
362 A process ID is specified as a positive value;
363 a process group ID is specified as a negative value.
364 Most commonly, the calling process specifies itself as the owner
373 status flag on a file descriptor (either by providing this flag with the
375 .\" FIXME setting O_ASYNC via open() does not seem to be effective -- MTK
376 call, or by using the
380 a SIGIO signal is sent whenever input or output becomes possible
381 on that file descriptor.
383 can be used to obtain delivery of a signal other than SIGIO.
384 If this permission check fails, then the signal is
387 Sending a signal to the owner process (group) specified by
389 is subject to the same permissions checks as are described for
391 where the sending process is the one that employs
393 (but see BUGS below).
395 If the file descriptor
400 the recipient of SIGURG signals that are delivered when out-of-band
401 data arrives on that socket. (SIGURG is sent in any situation where
403 would report the socket as having an "exceptional condition".)
404 .\" The following appears to be rubbish. It doesn't seem to
405 .\" be true according to the kernel source, and I can write
406 .\" a program that gets a terminal-generated SIGIO even though
407 .\" it is not the foreground process group of the terminal.
410 .\" If the file descriptor
412 .\" refers to a terminal device, then SIGIO
413 .\" signals are sent to the foreground process group of the terminal.
415 If a non-zero value is given to
417 in a multi-threaded process running with a threading library
418 that supports thread groups (e.g., NPTL),
419 then a positive value given to
421 has a different meaning:
422 .\" The relevant place in the (2.6) kernel source is the
423 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
424 instead of being a process ID identifying a whole process,
425 it is a thread ID identifying a specific thread within a process.
426 Consequently, it may be necessary to pass
432 to get sensible results when
435 (In current Linux threading implementations,
436 a main thread's thread ID is the same as its process ID.
437 This means that a single-threaded program can equally use
442 Note, however, that the statements in this paragraph do not apply
443 to the SIGURG signal generated for out-of-band data on a socket:
444 this signal is always sent to either a process or a process group,
445 depending on the value given to
447 .\" send_sigurg()/send_sigurg_to_task() bypasses
448 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
449 .\" to directly call send_group_sig_info()
450 .\" -- MTK, Apr 2005 (kernel 2.6.11)
451 Note also that Linux imposes a limit on the
452 number of real-time signals that may be queued to a
457 and if this limit is reached, then the kernel reverts to
458 delivering SIGIO, and this signal is delivered to the entire
459 process rather than to a specific thread.
460 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
463 Get the signal sent when input or output becomes possible. A value of
464 zero means SIGIO is sent. Any other value (including SIGIO) is the
465 signal sent instead, and in this case additional info is available to
466 the signal handler if installed with SA_SIGINFO.
469 Sets the signal sent when input or output becomes possible. A value of
470 zero means to send the default SIGIO signal. Any other value (including
471 SIGIO) is the signal to send instead, and in this case additional info
472 is available to the signal handler if installed with SA_SIGINFO.
474 Additionally, passing a non-zero value to
476 changes the signal recipient from a whole process to a specific thread
478 See the description of
484 with a non-zero value, and setting SA_SIGINFO for the
487 extra information about I/O events is passed to
493 field indicates the source is SI_SIGIO, the
495 field gives the file descriptor associated with the event. Otherwise,
496 there is no indication which file descriptors are pending, and you
497 should use the usual mechanisms
503 set etc.) to determine which file descriptors are available for I/O.
505 By selecting a POSIX.1b real time signal (value >= SIGRTMIN), multiple
506 I/O events may be queued using the same signal numbers. (Queuing is
507 dependent on available memory). Extra information is available
508 if SA_SIGINFO is set for the signal handler, as above.
510 Using these mechanisms, a program can implement fully asynchronous I/O
521 is specific to BSD and Linux.
525 are Linux-specific. POSIX has asynchronous I/O and the
527 structure to achieve similar things; these are also available
528 in Linux as part of the GNU C Library (Glibc).
534 (Linux 2.4 onwards) are used (respectively) to establish and
535 retrieve the current setting of the calling process's lease on
536 the file referred to by
538 A file lease provides a mechanism whereby the process holding
539 the lease (the "lease holder") is notified (via delivery of a signal)
540 when a process (the "lease breaker") tries to
547 Set or remove a file lease according to which of the following
548 values is specified in the integer
554 Take out a read lease.
555 This will cause the calling process to be notified when
556 the file is opened for writing or is truncated.
557 .\" The following became true in kernel 2.6.10:
558 .\" See the man-pages-2.09 Changelog for further info.
559 A read lease can only be placed on a file descriptor that
563 Take out a write lease.
564 This will cause the caller to be notified when
565 the file is opened for reading or writing or is truncated.
566 A write lease may be placed on a file only if no other process
567 currently has the file open.
570 Remove our lease from the file.
573 A process may hold only one type of lease on a file.
575 Leases may only be taken out on regular files.
576 An unprivileged process may only take out a lease on a file whose
577 UID matches the file system UID of the process.
580 capability may take out leases on arbitrary files.
583 Indicates what type of lease we hold on the file
587 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK,
588 indicating, respectively, that the calling process holds a
589 read, a write, or no lease on the file.
590 (The third argument to
594 When a process (the "lease breaker") performs an
598 that conflicts with a lease established via
600 the system call is blocked by the kernel, unless the
602 flag was specified to
604 in which case the system call will return with the error
606 The kernel notifies the lease holder by sending it a signal
608 The lease holder should respond to receipt of this signal by doing
609 whatever cleanup is required in preparation for the file to be
610 accessed by another process (e.g., flushing cached buffers) and
611 then either remove or downgrade its lease.
612 A lease is removed by performing an
618 If we currently hold a write lease on the file,
619 and the lease breaker is opening the file for reading,
620 then it is sufficient to downgrade the lease to a read lease.
621 This is done by performing an
628 If the lease holder fails to downgrade or remove the lease within
629 the number of seconds specified in
630 .I /proc/sys/fs/lease-break-time
631 then the kernel forcibly removes or downgrades the lease holder's lease.
633 Once the lease has been voluntarily or forcibly removed or downgraded,
634 and assuming the lease breaker has not unblocked its system call,
635 the kernel permits the lease breaker's system call to proceed.
637 The default signal used to notify the lease holder is SIGIO,
638 but this can be changed using the
644 command is performed (even one specifying SIGIO), and the signal
645 handler is established using SA_SIGINFO, then the handler will
648 structure as its second argument, and the
650 field of this argument will hold the descriptor of the leased file
651 that has been accessed by another process.
652 (This is useful if the caller holds leases against multiple files).
654 .SS "File and directory change notification"
655 .\" FIXME -- Eventually we may want to add a note about inotify
656 .\" here (MTK, Jun 05)
660 Provide notification when the directory referred to by
662 or any of the files that it contains is changed.
663 The events to be notified are specified in
665 which is a bit mask specified by ORing together zero or more of
672 Bit Description (event in directory)
673 DN_ACCESS A file was accessed (read, pread, readv)
674 DN_MODIFY A file was modified (write, pwrite,
675 writev, truncate, ftruncate)
676 DN_CREATE A file was created (open, creat, mknod,
677 mkdir, link, symlink, rename)
678 DN_DELETE A file was unlinked (unlink, rename to
679 another directory, rmdir)
680 DN_RENAME A file was renamed within this
682 DN_ATTRIB The attributes of a file were changed
683 (chown, chmod, utime[s])
686 (In order to obtain these definitions, the _GNU_SOURCE macro must be
687 defined before including <fcntl.h>.)
689 Directory notifications are normally "one-shot", and the application
690 must re-register to receive further notifications.
695 then notification will remain in effect until explicitly removed.
697 .\" The following does seem a poor API-design choice...
700 requests is cumulative, with the events in
702 being added to the set already monitored.
703 To disable notification of all events, make an
709 Notification occurs via delivery of a signal.
710 The default signal is SIGIO, but this can be changed using the
714 In the latter case, the signal handler receives a
716 structure as its second argument (if the handler was
717 established using SA_SIGINFO) and the
719 field of this structure contains the file descriptor which
720 generated the notification (useful when establishing notification
721 on multiple directories).
723 Especially when using
725 a POSIX.1b real time signal should be used for notification,
726 so that multiple notifications can be queued.
728 For a successful call, the return value depends on the operation:
740 Value of descriptor owner.
743 Value of signal sent when read or write becomes possible, or zero
744 for traditional SIGIO behaviour.
749 On error, \-1 is returned, and
751 is set appropriately.
754 .BR EACCES " or " EAGAIN
755 Operation is prohibited by locks held by other processes.
756 Or, operation is prohibited because the file has been memory-mapped by
761 is not an open file descriptor, or the command was
765 and the file descriptor open mode doesn't match with the
766 type of lock requested.
769 It was detected that the specified
771 command would cause a deadlock.
775 is outside your accessible address space.
780 the command was interrupted by a signal.
782 .BR F_GETLK " and " F_SETLK ,
783 the command was interrupted by a signal before the lock was checked or
784 acquired. Most likely when locking a remote file (e.g. locking over
785 NFS), but can sometimes happen locally.
791 is negative or is greater than the maximum allowable value. For
794 is not an allowable signal number.
799 the process already has the maximum number of file descriptors open.
802 Too many segment locks open, lock table is full, or a remote locking
803 protocol failed (e.g. locking over NFS).
806 Attempted to clear the
808 flag on a file that has the append-only attribute set.
810 The errors returned by
812 are different from those returned by
815 Since kernel 2.0, there is no interaction between the types of lock
821 POSIX 1003.1-2001 allows
823 to be negative. (And if it is, the interval described by the lock
828 This is supported by Linux since Linux 2.4.21 and 2.5.49.
830 Several systems have more fields in
836 alone is not going to be very useful if the process holding the lock
837 may live on a different machine.
839 A limitation of the Linux system call conventions on some
840 architectures (notably x86) means that if a (negative)
841 process group ID to be returned by
843 falls in the range \-1 to \-4095, then the return value is wrongly
844 interpreted by glibc as an error in the system call;
845 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
846 that is, the return value of
850 will contain the (positive) process group ID.
851 .\" FIXME Dec 04: some limited testing on alpha and ia64 seems to
852 .\" indicate that ANY negative PGID value will cause F_GETOWN
853 .\" to misinterpret the return as an error. Some other architectures
854 .\" seem to have the same range check as x86. -- MTK
856 In Linux 2.4 and earlier, there is bug that can occur
857 when an unprivileged process uses
860 of a socket file descriptor
861 as a process (group) other than the caller.
868 even when the owner process (group) is one that the caller
869 has permission to send signals to.
870 Despite this error return, the file descriptor owner is set,
871 and signals will be sent to the owner.
873 SVr4, SVID, POSIX, X/OPEN, 4.3BSD. Only the operations F_DUPFD,
874 F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK, F_SETLKW,
875 F_GETOWN, and F_SETOWN are specified in POSIX.1.
876 F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE
878 (Define the _GNU_SOURCE macro before including <fcntl.h> to
879 obtain these definitions.)
880 The flags legal for F_GETFL/F_SETFL are those supported by
882 and vary between these systems; O_APPEND, O_NONBLOCK, O_RDONLY,
883 and O_RDWR are specified in POSIX.1. SVr4 supports several other
884 options and flags not documented here.
886 SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
895 See also locks.txt, mandatory.txt, and dnotify.txt in
896 /usr/src/linux/Documentation.