Manual Pages for Linux CentOS command on man fcntl64
MyWebUniversity

Manual Pages for Linux CentOS command on man fcntl64

FCNTL(2) Linux Programmer's Manual FCNTL(2)

NAME

fcntl - manipulate file descriptor SYNOPSIS

#include

#include 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. fcntl() can take an optional third argument. Whether or not this argu‐ ment 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 int, and we identify the argument using the name arg), or void is specified if the argument is not required. Duplicating a file descriptor FDUPFD (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. FDUPFDCLOEXEC (int; since Linux 2.6.24)

As for FDUPFD, but additionally set the close-on-exec flag for the duplicate descriptor. Specifying this flag permits a pro‐ gram to avoid an additional fcntl() FSETFD operation to set the FDCLOEXEC flag. For an explanation of why this flag is useful, see the description of OCLOEXEC in open(2). File descriptor flags The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FDCLOEXEC, the

close-on-exec flag. If the FDCLOEXEC bit is 0, the file descriptor will remain open across an execve(2), otherwise it will be closed. FGETFD (void) Read the file descriptor flags; arg is ignored. FSETFD (int) Set the file descriptor flags to the value specified by arg. File status flags Each open file description has certain associated status flags, ini‐ tialized by open(2) and possibly modified by fcntl(). Duplicated file descriptors (made with dup(2), fcntl(FDUPFD), fork(2), etc.) refer to the same open file description, and thus share the same file status flags. The file status flags and their semantics are described in open(2). FGETFL (void) Get the file access mode and the file status flags; arg is ignored. FSETFL (int) Set the file status flags to the value specified by arg. File access mode (ORDONLY, OWRONLY, ORDWR) and file creation flags (i.e., OCREAT, OEXCL, ONOCTTY, OTRUNC) in arg are ignored. On Linux this command can change only the OAPPEND, OASYNC, ODIRECT, ONOATIME, and ONONBLOCK flags. Advisory locking FGETLK, FSETLK and FSETLKW 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 ltype; /* Type of lock: FRDLCK, FWRLCK, FUNLCK */ short lwhence; /* How to interpret lstart: SEEKSET, SEEKCUR, SEEKEND */ offt lstart; /* Starting offset for lock */ offt llen; /* Number of bytes to lock */ pidt lpid; /* PID of process blocking our lock (FGETLK only) */ ... }; The lwhence, lstart, and llen 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. lstart is the starting offset for the lock, and is interpreted rela‐ tive to either: the start of the file (if lwhence is SEEKSET); the current file offset (if lwhence is SEEKCUR); or the end of the file (if lwhence is SEEKEND). In the final two cases, lstart can be a negative number provided the offset does not lie before the start of the file. llen specifies the number of bytes to be locked. If llen is posi‐ tive, then the range to be locked covers bytes lstart up to and

including lstart+llen-1. Specifying 0 for llen has the special meaning: lock all bytes starting at the location specified by lwhence and lstart through to the end of file, no matter how large the file grows.

POSIX.1-2001 allows (but does not require) an implementation to support a negative llen value; if llen is negative, the interval described by

lock covers bytes lstart+llen up to and including lstart-1. This is supported by Linux since kernel versions 2.4.21 and 2.5.49. The ltype field can be used to place a read (FRDLCK) or a write (FWRLCK) lock on a file. Any number of processes may hold a read lock (shared lock) on a file region, but only one process may hold a write lock (exclusive lock). An exclusive lock excludes all other locks, both shared and exclusive. A single process can hold only one type of

lock on a file region; if a new lock is applied to an already-locked region, then the existing lock is converted to the new lock type. (Such conversions may involve splitting, shrinking, or coalescing with an existing lock if the byte range specified by the new lock does not precisely coincide with the range of the existing lock.) FSETLK (struct flock *) Acquire a lock (when ltype is FRDLCK or FWRLCK) or release a lock (when ltype is FUNLCK) on the bytes specified by the lwhence, lstart, and llen fields of lock. If a conflicting

lock is held by another process, this call returns -1 and sets errno to EACCES or EAGAIN. FSETLKW (struct flock *) As for FSETLK, 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 sig‐ nal handler has returned) returns immediately (with return value

-1 and errno set to EINTR; see signal(7)). FGETLK (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 FUNLCK in the ltype 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 ltype, lwhence, lstart, and llen fields of lock and sets lpid to be the PID of the process holding that lock. In order to place a read lock, fd must be open for reading. In order to place a write lock, fd must be open for writing. To place both

types of lock, open a file read-write. As well as being removed by an explicit FUNLCK, record locks are auto‐ matically released when the process terminates or if it closes any file descriptor referring to a file on which locks are held. This is bad: it means that a process can lose the locks on a file like /etc/passwd or /etc/mtab when for some reason a library function decides to open, read and close it. Record locks are not inherited by a child created via fork(2), but are preserved across an execve(2). Because of the buffering performed by the stdio(3) library, the use of record locking with routines in that package should be avoided; use read(2) and write(2) instead. Mandatory locking

(Non-POSIX.) The above record locks may be either advisory or manda‐ tory, and are advisory by default. 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 ONONBLOCK flag is enabled for its open file descrip‐ tion. If the ONONBLOCK flag is not enabled, then system call is blocked until the lock is removed or converted to a mode that is com‐ patible with the access. If the ONONBLOCK flag is enabled, then the system call fails with the error EAGAIN. 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 MSMANDLOCK flag for mount(2). Manda‐ tory 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 FGETOWN, FSETOWN, FGETOWNEX, FSETOWNEX, FGETSIG and FSETSIG are used to manage I/O availability signals: FGETOWN (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. FSETOWN (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 com‐ monly, the calling process specifies itself as the owner (that is, arg is specified as getpid(2)). If you set the OASYNC status flag on a file descriptor by using the FSETFL command of fcntl(), a SIGIO signal is sent whenever input or output becomes possible on that file descriptor. FSETSIG 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 FSETOWN is subject to the same permissions checks as are described for kill(2), where the sending process is the one that employs FSETOWN (but see BUGS below). If the file descriptor fd refers to a socket, FSETOWN 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 ker‐ nel 2.6.11: If a nonzero value is given to FSETSIG in a multi‐ threaded process running with a threading library that supports thread groups (e.g., NPTL), then a positive value given to FSETOWN 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 FSETOWN the result of gettid(2) instead of getpid(2) to get sensible results when FSETSIG is used. (In current Linux threading implementations, a main thread's thread ID is the same as its process ID. This means that a sin‐

gle-threaded program can equally use gettid(2) or get‐ pid(2) in this scenario.) Note, however, that the state‐ ments 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 FSETOWN. The above behavior was accidentally dropped in Linux 2.6.12, and won't be restored. From Linux 2.6.32 onward, use FSETOWNEX to target SIGIO and SIGURG signals at a particular thread. FGETOWNEX (struct fownerex *) (since Linux 2.6.32) Return the current file descriptor owner settings as defined by a previous FSETOWNEX operation. The information is returned in the structure pointed to by arg, which has the following form: struct fownerex { int type; pidt pid; }; The type field will have one of the values FOWNERTID, FOWNERPID, or FOWNERPGRP. The pid field is a positive inte‐ ger representing a thread ID, process ID, or process group ID. See FSETOWNEX for more details. FSETOWNEX (struct fownerex *) (since Linux 2.6.32) This operation performs a similar task to FSETOWN. 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 fownerex structure. The type field has one of the following values, which define how pid is interpreted: FOWNERTID 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. FOWNERPID Send the signal to the process whose ID is specified in pid. FOWNERPGRP Send the signal to the process group whose ID is speci‐ fied in pid. (Note that, unlike with FSETOWN, a process group ID is specified as a positive value here.) FGETSIG (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 han‐ dler if installed with SASIGINFO. arg is ignored. FSETSIG (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 SASIGINFO. By using FSETSIG with a nonzero value, and setting SASIGINFO for the signal handler (see sigaction(2)), extra information about I/O events is passed to the handler in a siginfot struc‐ ture. If the sicode field indicates the source is SISIGIO, the sifd 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 ONONBLOCK 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. (Queu‐ ing is dependent on available memory). Extra information is available if SASIGINFO is set for the signal handler, as above.

Note that Linux imposes a limit on the number of real-time sig‐ nals that may be queued to a process (see getrlimit(2) and sig‐ nal(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. Using these mechanisms, a program can implement fully asynchronous I/O without using select(2) or poll(2) most of the time. The use of OASYNC, FGETOWN, FSETOWN is specific to BSD and Linux.

FGETOWNEX, FSETOWNEX, FGETSIG, and FSETSIG are Linux-specific. POSIX has asynchronous I/O and the aiosigevent structure to achieve similar things; these are also available in Linux as part of the GNU C Library (Glibc). Leases FSETLEASE and FGETLEASE (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 pro‐ vides 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. FSETLEASE (int) Set or remove a file lease according to which of the following values is specified in the integer arg: FRDLCK Take out a read lease. This will cause the calling process to be notified when the file is opened for writ‐ ing or is truncated. A read lease can be placed only on

a file descriptor that is opened read-only. FWRLCK 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. FUNLCK Remove our lease from the file. Leases are associated with an open file description (see open(2)). This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lease, and this lease may be modi‐ fied or released using any of these descriptors. Furthermore, the lease is released by either an explicit FUNLCK operation on any of these duplicate descriptors, or when all such descriptors have been closed. Leases may be taken out only on regular files. An unprivileged process may take out a lease only on a file whose UID (owner) matches the file system UID of the process. A process with the CAPLEASE capability may take out leases on arbitrary files. FGETLEASE (void) Indicates what type of lease is associated with the file descriptor fd by returning either FRDLCK, FWRLCK, or FUNLCK, indicating, respectively, a read lease , a write lease, or no lease. arg is ignored. When a process (the "lease breaker") performs an open(2) or truncate(2) that conflicts with a lease established via FSETLEASE, the system call is blocked by the kernel and the kernel notifies the lease holder by sending it a signal (SIGIO by default). The lease holder should respond to receipt of this signal by doing whatever cleanup is required in preparation for the file to be accessed by another process (e.g., flushing cached buffers) and then either remove or downgrade its lease. A lease is removed by performing an FSETLEASE command specifying arg as FUNLCK. If the lease holder currently holds a write lease on the file, and the lease breaker is opening the file for reading, then it is sufficient for the lease holder to downgrade the lease to a read lease. This is done by performing an FSETLEASE command specifying arg as FRDLCK. If the lease holder fails to downgrade or remove the lease within the

number of seconds specified in /proc/sys/fs/lease-break-time then the kernel forcibly removes or downgrades the lease holder's lease. Once a lease break has been initiated, FGETLEASE returns the target lease type (either FRDLCK or FUNLCK, depending on what would be com‐ patible with the lease breaker) until the lease holder voluntarily downgrades or removes the lease or the kernel forcibly does so after the lease break timer expires. Once the lease has been voluntarily or forcibly removed or downgraded, and assuming the lease breaker has not unblocked its system call, the kernel permits the lease breaker's system call to proceed. If the lease breaker's blocked open(2) or truncate(2) is interrupted by a signal handler, then the system call fails with the error EINTR, but the other steps still occur as described above. If the lease breaker is killed by a signal while blocked in open(2) or truncate(2), then the other steps still occur as described above. If the lease breaker spec‐ ifies the ONONBLOCK flag when calling open(2), then the call immedi‐ ately fails with the error EWOULDBLOCK, but the other steps still occur as described above. The default signal used to notify the lease holder is SIGIO, but this can be changed using the FSETSIG command to fcntl(). If a FSETSIG command is performed (even one specifying SIGIO), and the signal han‐ dler is established using SASIGINFO, then the handler will receive a siginfot structure as its second argument, and the sifd field of this argument will hold the descriptor of the leased file that has been accessed by another process. (This is useful if the caller holds leases against multiple files). File and directory change notification (dnotify) FNOTIFY (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: DNACCESS A file was accessed (read, pread, readv) DNMODIFY A file was modified (write, pwrite, writev, trun‐ cate, ftruncate). DNCREATE A file was created (open, creat, mknod, mkdir, link, symlink, rename). DNDELETE A file was unlinked (unlink, rename to another directory, rmdir). DNRENAME A file was renamed within this directory (rename). DNATTRIB The attributes of a file were changed (chown, chmod, utime[s]). (In order to obtain these definitions, the GNUSOURCE feature test macro must be defined before including any header files.)

Directory notifications are normally "one-shot", and the appli‐ cation must reregister to receive further notifications. Alter‐ natively, if DNMULTISHOT is included in arg, then notification will remain in effect until explicitly removed. A series of FNOTIFY requests is cumulative, with the events in arg being added to the set already monitored. To disable noti‐ fication of all events, make an FNOTIFY call specifying arg as 0. Notification occurs via delivery of a signal. The default sig‐ nal is SIGIO, but this can be changed using the FSETSIG command to fcntl(). In the latter case, the signal handler receives a siginfot structure as its second argument (if the handler was established using SASIGINFO) and the sifd field of this struc‐ ture contains the file descriptor which generated the notifica‐ tion (useful when establishing notification on multiple directo‐ ries). Especially when using DNMULTISHOT, a real time signal should be used for notification, so that multiple notifications can be queued. NOTE: New applications should use the inotify interface (avail‐ able since kernel 2.6.13), which provides a much superior inter‐ face for obtaining notifications of file system events. See inotify(7). Changing the capacity of a pipe FSETPIPESZ (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 (CAPSYSRESOURCE) 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 FGETPIPESZ operation returns the actual size used. Attempting to set the pipe capac‐ ity smaller than the amount of buffer space currently used to store data produces the error EBUSY. FGETPIPESZ (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: FDUPFD The new descriptor. FGETFD Value of file descriptor flags. FGETFL Value of file status flags. FGETLEASE Type of lease held on file descriptor. FGETOWN Value of descriptor owner. FGETSIG Value of signal sent when read or write becomes possible, or zero for traditional SIGIO behavior. FGETPIPESZ The pipe capacity. All other commands Zero.

On error, -1 is returned, and errno is set appropriately. 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 FSETLK or FSETLKW and the file descriptor open mode doesn't match with the type of lock requested. EDEADLK It was detected that the specified FSETLKW command would cause a deadlock. EFAULT lock is outside your accessible address space. EINTR For FSETLKW, the command was interrupted by a signal; see sig‐ nal(7). For FGETLK and FSETLK, 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 FDUPFD, arg is negative or is greater than the maximum allowable value. For FSETSIG, arg is not an allowable signal number. EMFILE For FDUPFD, 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 OAPPEND flag on a file that has the

append-only attribute set. CONFORMING TO

SVr4, 4.3BSD, POSIX.1-2001. Only the operations FDUPFD, FGETFD, FSETFD, FGETFL, FSETFL, FGETLK, FSETLK and FSETLKW, are specified

in POSIX.1-2001.

FGETOWN and FSETOWN are specified in POSIX.1-2001. (To get their definitions, define BSDSOURCE, or XOPENSOURCE with the value 500 or greater, or define POSIXCSOURCE with the value 200809L or greater.)

FDUPFDCLOEXEC is specified in POSIX.1-2008. (To get this definition, define POSIXCSOURCE with the value 200809L or greater, or XOPENSOURCE with the value 700 or greater.) FGETOWNEX, FSETOWNEX, FSETPIPESZ, FGETPIPESZ, FGETSIG, FSET‐

SIG, FNOTIFY, FGETLEASE, and FSETLEASE are Linux-specific. (Define the GNUSOURCE macro to obtain these definitions.) NOTES The original Linux fcntl() system call was not designed to handle large file offsets (in the flock structure). Consequently, an fcntl64() sys‐ tem call was added in Linux 2.4. The newer system call employs a dif‐ ferent structure for file locking, flock64, and corresponding commands, FGETLK64, FSETLK64, and FSETLKW64. However, these details can be ignored by applications using glibc, whose fcntl() wrapper function transparently employs the more recent system call where it is avail‐ able. The errors returned by dup2(2) are different from those returned by FDUPFD. 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, lsysid. Clearly, lpid alone is not going to be very useful if the process holding the lock may live on a different machine. 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 FGETOWN 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 FGETOWNEX opera‐ tion avoids this problem. Since glibc version 2.11, glibc makes the kernel FGETOWN problem invisible by implementing FGETOWN using FGETOWNEX. In Linux 2.4 and earlier, there is bug that can occur when an unprivi‐ leged process uses FSETOWN 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 there‐ fore inadvisable to rely on mandatory locking. SEE ALSO dup2(2), flock(2), open(2), socket(2), lockf(3), capabilities(7), fea‐ turetestmacros(7)

locks.txt, mandatory-locking.txt, and dnotify.txt in the Linux kernel source directory Documentation/filesystems/ (on older kernels, these

files are directly under the Documentation/ directory, and mandatory- locking.txt is called mandatory.txt) COLOPHON

This page is part of release 3.53 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/.

Linux 2012-04-15 FCNTL(2)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™