table of contents
other versions
- wheezy 3.44-1
- jessie 3.74-1
- jessie-backports 4.10-2~bpo8+1
- testing 4.10-2
- unstable 4.10-2
other sections
| FCNTL(2) | Linux Programmer's Manual | FCNTL(2) |
NAME¶
fcntl - manipulate file descriptorSYNOPSIS¶
#include <unistd.h> #include <fcntl.h>int fcntl(int fd, int cmd, ... /* arg */ );
DESCRIPTION¶
fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd.Duplicating a file descriptor¶
- F_DUPFD (int)
- Find the lowest numbered available file descriptor greater than or equal to arg and make it be a copy of fd. This is different from dup2(2), which uses exactly the descriptor specified.
- On success, the new descriptor is returned.
- See dup(2) for further details.
- F_DUPFD_CLOEXEC (int; since Linux 2.6.24)
- As for F_DUPFD, but additionally set the close-on-exec flag for the duplicate descriptor. Specifying this flag permits a program to avoid an additional fcntl() F_SETFD operation to set the FD_CLOEXEC flag. For an explanation of why this flag is useful, see the description of O_CLOEXEC in open(2).
File descriptor flags¶
The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC, the close-on-exec flag. If the FD_CLOEXEC bit is 0, the file descriptor will remain open across an execve(2), otherwise it will be closed.- F_GETFD (void)
- Read the file descriptor flags; arg is ignored.
- F_SETFD (int)
- Set the file descriptor flags to the value specified by arg.
File status flags¶
Each open file description has certain associated status flags, initialized by open(2) and possibly modified by fcntl(). Duplicated file descriptors (made with dup(2), fcntl(F_DUPFD), fork(2), etc.) refer to the same open file description, and thus share the same file status flags.- F_GETFL (void)
- Get the file access mode and the file status flags; arg is ignored.
- F_SETFL (int)
- Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
Advisory locking¶
F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test for the existence of record locks (also known as file-segment or file-region locks). The third argument, lock, is a pointer to a structure that has at least the following fields (in unspecified order).struct flock { ... short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */ short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* Starting offset for lock */ off_t l_len; /* Number of bytes to lock */ pid_t l_pid; /* PID of process blocking our lock (F_GETLK only) */ ... };
The l_whence, l_start, and l_len fields of this structure specify the range of bytes we wish to lock. Bytes past the end of the file may be locked, but not bytes before the start of the file.
- F_SETLK (struct flock *)
- Acquire a lock (when l_type is F_RDLCK or F_WRLCK) or release a lock (when l_type is F_UNLCK) on the bytes specified by the l_whence, l_start, and l_len fields of lock. If a conflicting lock is held by another process, this call returns -1 and sets errno to EACCES or EAGAIN.
- F_SETLKW (struct flock *)
- As for F_SETLK, but if a conflicting lock is held on the file, then wait for that lock to be released. If a signal is caught while waiting, then the call is interrupted and (after the signal handler has returned) returns immediately (with return value -1 and errno set to EINTR; see signal(7)).
- F_GETLK (struct flock *)
- On input to this call, lock describes a lock we would like to place on the file. If the lock could be placed, fcntl() does not actually place it, but returns F_UNLCK in the l_type field of lock and leaves the other fields of the structure unchanged. If one or more incompatible locks would prevent this lock being placed, then fcntl() returns details about one of these locks in the l_type, l_whence, l_start, and l_len fields of lock and sets l_pid to be the PID of the process holding that lock.
Mandatory locking¶
(Non-POSIX.) The above record locks may be either advisory or mandatory, and are advisory by default.Managing signals¶
F_GETOWN, F_SETOWN, F_GETOWN_EX, F_SETOWN_EX, F_GETSIG and F_SETSIG are used to manage I/O availability signals:- F_GETOWN (void)
- Return (as the function result) the process ID or process group currently receiving SIGIO and SIGURG signals for events on file descriptor fd. Process IDs are returned as positive values; process group IDs are returned as negative values (but see BUGS below). arg is ignored.
- F_SETOWN (int)
- Set the process ID or process group ID that will receive
SIGIO and SIGURG signals for events on file descriptor
fd to the ID given in arg. A process ID is specified as a
positive value; a process group ID is specified as a negative value. Most
commonly, the calling process specifies itself as the owner (that is,
arg is specified as getpid(2)).
If you set the O_ASYNC status flag on a file descriptor by using the F_SETFL command of fcntl(), a SIGIO signal is sent whenever input or output becomes possible on that file descriptor. F_SETSIG can be used to obtain delivery of a signal other than SIGIO. If this permission check fails, then the signal is silently discarded.Sending a signal to the owner process (group) specified by F_SETOWN is subject to the same permissions checks as are described for kill(2), where the sending process is the one that employs F_SETOWN (but see BUGS below).If the file descriptor fd refers to a socket, F_SETOWN also selects the recipient of SIGURG signals that are delivered when out-of-band data arrives on that socket. (SIGURG is sent in any situation where select(2) would report the socket as having an "exceptional condition".)The following was true in 2.6.x kernels up to and including kernel 2.6.11:
- If a nonzero value is given to F_SETSIG in a multithreaded process running with a threading library that supports thread groups (e.g., NPTL), then a positive value given to F_SETOWN has a different meaning: instead of being a process ID identifying a whole process, it is a thread ID identifying a specific thread within a process. Consequently, it may be necessary to pass F_SETOWN the result of gettid(2) instead of getpid(2) to get sensible results when F_SETSIG is used. (In current Linux threading implementations, a main thread's thread ID is the same as its process ID. This means that a single-threaded program can equally use gettid(2) or getpid(2) in this scenario.) Note, however, that the statements in this paragraph do not apply to the SIGURG signal generated for out-of-band data on a socket: this signal is always sent to either a process or a process group, depending on the value given to F_SETOWN.
- The above behavior was accidentally dropped in Linux 2.6.12, and won't be restored. From Linux 2.6.32 onward, use F_SETOWN_EX to target SIGIO and SIGURG signals at a particular thread.
- F_GETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
- Return the current file descriptor owner settings as
defined by a previous F_SETOWN_EX operation. The information is
returned in the structure pointed to by arg, which has the
following form:
The type field will have one of the values F_OWNER_TID, F_OWNER_PID, or F_OWNER_PGRP. The pid field is a positive integer representing a thread ID, process ID, or process group ID. See F_SETOWN_EX for more details.
struct f_owner_ex { int type; pid_t pid; };
- F_SETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
- This operation performs a similar task to F_SETOWN. It allows the caller to direct I/O availability signals to a specific thread, process, or process group. The caller specifies the target of signals via arg, which is a pointer to a f_owner_ex structure. The type field has one of the following values, which define how pid is interpreted:
- F_OWNER_TID
- Send the signal to the thread whose thread ID (the value returned by a call to clone(2) or gettid(2)) is specified in pid.
- F_OWNER_PID
- Send the signal to the process whose ID is specified in pid.
- F_OWNER_PGRP
- Send the signal to the process group whose ID is specified in pid. (Note that, unlike with F_SETOWN, a process group ID is specified as a positive value here.)
- F_GETSIG (void)
- Return (as the function result) the signal sent when input or output becomes possible. A value of zero means SIGIO is sent. Any other value (including SIGIO) is the signal sent instead, and in this case additional info is available to the signal handler if installed with SA_SIGINFO. arg is ignored.
- F_SETSIG (int)
- Set the signal sent when input or output becomes possible
to the value given in arg. A value of zero means to send the
default SIGIO signal. Any other value (including SIGIO) is
the signal to send instead, and in this case additional info is available
to the signal handler if installed with SA_SIGINFO.
By using F_SETSIG with a nonzero value, and setting SA_SIGINFO for the signal handler (see sigaction(2)), extra information about I/O events is passed to the handler in a siginfo_t structure. If the si_code field indicates the source is SI_SIGIO, the si_fd field gives the file descriptor associated with the event. Otherwise, there is no indication which file descriptors are pending, and you should use the usual mechanisms (select(2), poll(2), read(2) with O_NONBLOCK set etc.) to determine which file descriptors are available for I/O.By selecting a real time signal (value >= SIGRTMIN), multiple I/O events may be queued using the same signal numbers. (Queuing is dependent on available memory). Extra information is available if SA_SIGINFO is set for the signal handler, as above.Note that Linux imposes a limit on the number of real-time signals that may be queued to a process (see getrlimit(2) and signal(7)) and if this limit is reached, then the kernel reverts to delivering SIGIO, and this signal is delivered to the entire process rather than to a specific thread.
Leases¶
F_SETLEASE and F_GETLEASE (Linux 2.4 onward) are used (respectively) to establish a new lease, and retrieve the current lease, on the open file description referred to by the file descriptor fd. A file lease provides a mechanism whereby the process holding the lease (the "lease holder") is notified (via delivery of a signal) when a process (the "lease breaker") tries to open(2) or truncate(2) the file referred to by that file descriptor.- F_SETLEASE (int)
- Set or remove a file lease according to which of the following values is specified in the integer arg:
- F_RDLCK
- Take out a read lease. This will cause the calling process to be notified when the file is opened for writing or is truncated. A read lease can only be placed on a file descriptor that is opened read-only.
- F_WRLCK
- Take out a write lease. This will cause the caller to be notified when the file is opened for reading or writing or is truncated. A write lease may be placed on a file only if there are no other open file descriptors for the file.
- F_UNLCK
- Remove our lease from the file.
- F_GETLEASE (void)
- Indicates what type of lease is associated with the file descriptor fd by returning either F_RDLCK, F_WRLCK, or F_UNLCK, indicating, respectively, a read lease , a write lease, or no lease. arg is ignored.
File and directory change notification (dnotify)¶
- F_NOTIFY (int)
- (Linux 2.4 onward) Provide notification when the directory referred to by fd or any of the files that it contains is changed. The events to be notified are specified in arg, which is a bit mask specified by ORing together zero or more of the following bits:
- DN_ACCESS
- A file was accessed (read, pread, readv)
- DN_MODIFY
- A file was modified (write, pwrite, writev, truncate, ftruncate).
- DN_CREATE
- A file was created (open, creat, mknod, mkdir, link, symlink, rename).
- DN_DELETE
- A file was unlinked (unlink, rename to another directory, rmdir).
- DN_RENAME
- A file was renamed within this directory (rename).
- DN_ATTRIB
- The attributes of a file were changed (chown, chmod, utime[s]).
- (In order to obtain these definitions, the
_GNU_SOURCE feature test macro must be defined before including
any header files.)
Directory notifications are normally "one-shot", and the application must reregister to receive further notifications. Alternatively, if DN_MULTISHOT is included in arg, then notification will remain in effect until explicitly removed.A series of F_NOTIFY requests is cumulative, with the events in arg being added to the set already monitored. To disable notification of all events, make an F_NOTIFY call specifying arg as 0.Notification occurs via delivery of a signal. The default signal is SIGIO, but this can be changed using the F_SETSIG command to fcntl(). In the latter case, the signal handler receives a siginfo_t structure as its second argument (if the handler was established using SA_SIGINFO) and the si_fd field of this structure contains the file descriptor which generated the notification (useful when establishing notification on multiple directories).Especially when using DN_MULTISHOT, a real time signal should be used for notification, so that multiple notifications can be queued.NOTE: New applications should use the inotify interface (available since kernel 2.6.13), which provides a much superior interface for obtaining notifications of file system events. See inotify(7).
Changing the capacity of a pipe¶
- F_SETPIPE_SZ (int; since Linux 2.6.35)
- Change the capacity of the pipe referred to by fd to be at least arg bytes. An unprivileged process can adjust the pipe capacity to any value between the system page size and the limit defined in /proc/sys/fs/pipe-max-size (see proc(5)). Attempts to set the pipe capacity below the page size are silently rounded up to the page size. Attempts by an unprivileged process to set the pipe capacity above the limit in /proc/sys/fs/pipe-max-size yield the error EPERM; a privileged process (CAP_SYS_RESOURCE) can override the limit. When allocating the buffer for the pipe, the kernel may use a capacity larger than arg, if that is convenient for the implementation. The F_GETPIPE_SZ operation returns the actual size used. Attempting to set the pipe capacity smaller than the amount of buffer space currently used to store data produces the error EBUSY.
- F_GETPIPE_SZ (void; since Linux 2.6.35)
- Return (as the function result) the capacity of the pipe referred to by fd.
RETURN VALUE¶
For a successful call, the return value depends on the operation:- F_DUPFD
- The new descriptor.
- F_GETFD
- Value of file descriptor flags.
- F_GETFL
- Value of file status flags.
- F_GETLEASE
- Type of lease held on file descriptor.
- F_GETOWN
- Value of descriptor owner.
- F_GETSIG
- Value of signal sent when read or write becomes possible, or zero for traditional SIGIO behavior.
- F_GETPIPE_SZ
- The pipe capacity.
- All other commands
- Zero.
ERRORS¶
- EACCES or EAGAIN
- Operation is prohibited by locks held by other processes.
- EAGAIN
- The operation is prohibited because the file has been memory-mapped by another process.
- EBADF
- fd is not an open file descriptor, or the command was F_SETLK or F_SETLKW and the file descriptor open mode doesn't match with the type of lock requested.
- EDEADLK
- It was detected that the specified F_SETLKW command would cause a deadlock.
- EFAULT
- lock is outside your accessible address space.
- EINTR
- For F_SETLKW, the command was interrupted by a signal; see signal(7). For F_GETLK and F_SETLK, the command was interrupted by a signal before the lock was checked or acquired. Most likely when locking a remote file (e.g., locking over NFS), but can sometimes happen locally.
- EINVAL
- For F_DUPFD, arg is negative or is greater than the maximum allowable value. For F_SETSIG, arg is not an allowable signal number.
- EMFILE
- For F_DUPFD, the process already has the maximum number of file descriptors open.
- ENOLCK
- Too many segment locks open, lock table is full, or a remote locking protocol failed (e.g., locking over NFS).
- EPERM
- Attempted to clear the O_APPEND flag on a file that has the append-only attribute set.
CONFORMING TO¶
SVr4, 4.3BSD, POSIX.1-2001. Only the operations F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK and F_SETLKW, are specified in POSIX.1-2001.NOTES¶
The original Linux fcntl() system call was not designed to handle large file offsets (in the flock structure). Consequently, an fcntl64() system call was added in Linux 2.4. The newer system call employs a different structure for file locking, flock64, and corresponding commands, F_GETLK64, F_SETLK64, and F_SETLKW64. However, these details can be ignored by applications using glibc, whose fcntl() wrapper function transparently employs the more recent system call where it is available.BUGS¶
A limitation of the Linux system call conventions on some architectures (notably i386) means that if a (negative) process group ID to be returned by F_GETOWN falls in the range -1 to -4095, then the return value is wrongly interpreted by glibc as an error in the system call; that is, the return value of fcntl() will be -1, and errno will contain the (positive) process group ID. The Linux-specific F_GETOWN_EX operation avoids this problem. Since glibc version 2.11, glibc makes the kernel F_GETOWN problem invisible by implementing F_GETOWN using F_GETOWN_EX.SEE ALSO¶
dup2(2), flock(2), open(2), socket(2), lockf(3), capabilities(7), feature_test_macros(7)COLOPHON¶
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.| 2012-04-15 | Linux |