| |||||
| |||||
![]() |
|||||
|
FCNTL
2
2008-09-05
Linux
Linux Programmer's Manual
performs one of the operations described below on the open file descriptor fd . The operation is determined by cmd . fcntl () can take an optional third argument. Whether or not this argument is required is determined by cmd . The required argument type is indicated in parentheses after each cmd name (in most cases, the required type is long , and we identify the argument using the name arg ), or void is specified if the argument is not required. "Duplicating a file descriptor" Advisory locks are not enforced and are useful only between cooperating processes. Mandatory locks are enforced for all processes. If a process tries to perform an incompatible access (e.g., read(2) or write(2) ) on a file region that has an incompatible mandatory lock, then the result depends upon whether the O_NONBLOCK flag is enabled for its open file description. If the O_NONBLOCK flag is not enabled, then system call is blocked until the lock is removed or converted to a mode that is compatible with the access. If the O_NONBLOCK flag is enabled, then the system call fails with the error EAGAIN or EWOULDBLOCK . To make use of mandatory locks, mandatory locking must be enabled both on the file system that contains the file to be locked, and on the file itself. Mandatory locking is enabled on a file system using the "-o mand" option to mount(8) , or the MS_MANDLOCK flag for mount(2) . Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permission bit (see chmod(1) and chmod(2) ). The Linux implementation of mandatory locking is unreliable. See BUGS below. "Managing signals" F_GETOWN ", " F_SETOWN ", " F_GETSIG " and " F_SETSIG are used to manage I/O availability signals: 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. is sent in any situation where select(2) would report the socket as having an "exceptional condition".) If a non-zero 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 . Note also 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. Directory notifications are normally "one-shot", and the application must re-register 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) .
F_DUPFD_CLOEXEC is specified in POSIX.1-2008. F_GETSIG , F_SETSIG , F_NOTIFY , F_GETLEASE , and F_SETLEASE are Linux-specific. (Define the _GNU_SOURCE macro to obtain these definitions.)
Since kernel 2.0, there is no interaction between the types of lock placed by flock(2) and fcntl (). Several systems have more fields in "struct flock" such as, for example, l_sysid . Clearly, l_pid alone is not going to be very useful if the process holding the lock may live on a different machine.
In Linux 2.4 and earlier, there is bug that can occur when an unprivileged process uses F_SETOWN to specify the owner of a socket file descriptor as a process (group) other than the caller. In this case, fcntl () can return -1 with errno set to EPERM , even when the owner process (group) is one that the caller has permission to send signals to. Despite this error return, the file descriptor owner is set, and signals will be sent to the owner. The implementation of mandatory locking in all known versions of Linux is subject to race conditions which render it unreliable: a write(2) call that overlaps with a lock may modify data after the mandatory lock is acquired; a read(2) call that overlaps with a lock may detect changes to data that were made only after a write lock was acquired. Similar races exist between mandatory locks and mmap(2) . It is therefore inadvisable to rely on mandatory locking.
| |||||
|
| |||||