fcntl() provides for control over open files.
fildes is a file descriptor of an open file. fcntl() can take a third argument, whose data type and value depend upon the value of the passed command cmd. The command cmd specifies the operation to be performed by fcntl() and may be one of the following:
F_DUPFD
| Returns a new file descriptor with the following characteristics: Lowest numbered available (i.e. open) file descriptor greater than or equal to the integer value passed as the third argument (arg). Same open file (or pipe) as the original file. Same file position indicator as the original file (i.e. both file descriptors share one file position indicator). Same access mode (read, write, or read/write) as the original file. Same file status bits as the original file. The close-on-exec flag (see F_GETFD ) associated with the new file descriptor is set to remain open across exec system calls.
| F_GETFD
| Gets the close-on-exec flag associated with file descriptor fildes. If the loworder bit is 0, the file will remain open across exec . Otherwise, the file will be closed upon execution of exec . | F_SETFD
| Sets the close-on-exec flag associated with fildes to the low-order bit of the integer value given as the third argument (0 or 1 as above). | F_GETFL
| Gets the fildes status flag. | F_SETFL
| Sets the fildes status flag to the integer value given as the third argument. Only certain flags can be set (see fcntl() ). | Extension | F_FREESP
| Frees storage space associated with a section of the ordinary file fildes. The section is specified by a variable of data type struct flock pointed to by the third argument arg. The data type struct flock is defined in the fcntl.h header file (see fcntl() ) and contains the following members: l_whence is 0, 1 or 2 to indicate that the relative offset l_start will be measured from the start of the file, the current position, or the end of the file, respectively.
l_start is the offset from the position specified in l_whence . l_len is the size of the section. An l_len of 0 frees up to the end of the file; in this case, the end of file (i.e., file size) is set to the beginning of the section freed. Any data previously written into this section is no longer accessible. (End)
| The following commands are used for file and record-locking. Locks may be placed on an entire file or on segments of a file. F_SETLK
| Set or clear a file segment lock according to the flock structure that arg points to (see fcntl() ). The cmd F_SETLK is used to establish read (F_RDLCK ) and write (F_WRLCK ) locks, as well as remove either type of lock (F_UNLCK ). If a read or write lock cannot be set, fcntl() will return immediately with an error value of -1. | F_SETLKW
| This cmd is the same as F_SETLK except that if a read or write lock request is blocked by other locks, the process will wait until the segment is free to be locked. | F_GETLK
| If the lock request described by the flock structure that arg points to could be created, then the structure is passed back unchanged except that the lock type is set to F_UNLCK, and the l_whence field will be set to SEEK_SET . If a lock is found that would prevent this lock from being created, then the structure is overwritten with a description of the first lock that is preventing such a lock from being created. This command never creates a lock; it simply tests whether a particular lock could be created. | F_RSETLK , F_RSETLKW , F_RGETLK
|
| These commands are used by the network daemon lockd to lock NFS files with the NFS server.. | A read lock prevents any process from write locking the protected area. More than one read lock may exist for a given segment of a file at a given time. The file descriptor on which a read lock is being placed must have been opened with read access. A write lock prevents any process from read locking or write locking the protected area. Only one write lock and no read locks may exist for a given segment of a file at a given time. The file descriptor on which a write lock is being placed must have been opened with write access. The flock structure describes the type (l_type ), starting offset (l_whence ), relative offset (l_start ), size (l_len ), process ID (l_pid ), and system ID (l_sysid ) of the relevant segment of the file. The value of l_whence is SEEK_SET , SEEK_CUR or SEEK_END to indicate that the relative offset l_start bytes will be measured from the start of the file, current position or end of the file, respectively. The value of l_len is the number of consecutive bytes to be locked. The value of l_len may be negative (where the definition of off_t permits negative values of l_len ). The l_pid field is only used with F_GETLK to return the process ID of the process holding a blocking lock. After a successful F_GETLK request, i.e. one in which a lock was found, the value of l_whence will be SEEK_SET . If l_len is positive, the area affected starts at l_start and ends at l_start + l_len -1. If l_len is negative, the area affected starts at l_start + l_len and ends at l_start -1. Locks may start and extend beyond the current end of a file, but must not be negative relative to the beginning of the file. A lock will be set to extend to the largest possible value of the file offset for that file by setting l_len to 0. If such a lock also has l_start set to 0 and l_whence is set to SEEK_SET , the whole file will be locked. There will be at most one type of lock set for each byte in the file. If the calling process already has existing locks on bytes in the region specified by the request, the previous lock type for each byte in the specified region will be replaced by the new lock type before a successful return from an F_SETLK or an F_SETLKW request. As specified above under the descriptions of shared locks and exclusive locks, an F_SETLK or an F_SETLKW request will (respectively) fail or block when another process has existing locks on bytes in the specified region and the type of any of those locks conflicts with the type specified in the request. All locks associated with a file for a given process are removed when a file descriptor for that file is closed by that process or the process holding that file descriptor terminates. Locks are not inherited by a child process created using the fork() function. A potential for deadlock occurs if a process controlling a locked region is put to sleep by attempting to lock another process's locked region. If the system detects that sleeping until a locked region is unlocked would cause a deadlock, the fcntl() function will fail with an EDEADLK error. When mandatory file and record locking is active on a file (see chmod() ), open() , read() and write() system calls issued on the file will be affected by the record locks in effect. The following additional value can be used when creating oflag : O_LARGEFILE
| If this value is set, the offset maximum specified in the internal description of the open file is the highest value that can be properly represented in an object of type off64_t . | The O_LARGEFILE flag can be enabled and disabled with F_SETFL . The response of the following values is the same as the response for F_GETLK , F_SETLK , F_SETLKW and F_FREESP except that an argument of type struct flock64 must be passed instead of an argument of type struct flock : F_GETLK64 , F_SETLK64 , F_SETLKW64 and F_FREESP64 .
The flock64 structure is defined like the flock structure (see <fcntl() ) except for: off64_t l_start and off64_t l_len.
If threads are used, then the function affects the process or a thread in the following manner: When the F_SETLKW command is called, the thread waits until the request can be fulfilled. |