Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fcntl - control open file

&pagelevel(4)&pagelevel

Syntax

#include <fcntl.h>

Optional
#include <sys/types.h>
#include <unistd.h>

int fcntl(int fildes, int cmd, ... / * arg */); 

Description

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.

Return val.

A new file descriptor 



upon successful completion of the command F_DUPFD.

 

Value of process status flags, as defined in fcntl.h 

 


upon successful completion of the command F_GETFD.
The return value will not be negative.

 

Value other than -1 

 


upon successful completion of the commands F_SETFD, F_SETFLF_GETLK, F_SETLK and F_SETLK.

 

0

upon successful completion of the command F_FREESP.

 

Value of file status flags and access modes



upon successful completion of the command F_GETFL.
The return value will not be negative.

 

-1

if an error occurs. errno is set to indicate the error.

Errors

fcntl() will fail if: 

 

EACCES

cmd is F_SETLK, the type of lock (l_type) is a read lock (F_RDLCK), and the segment of a file to be locked is already write-locked by another process.

The type is a write lock (F_WRLCK) and the segment of a file to be locked is already read or write locked by another process.

 

EAGAIN

cmd is F_FREESP, the file exists, mandatory file/record locking is set, and there are outstanding record locks on the file.

 

Extension

 

 

EAGAIN

cmd is F_SETLK or F_SETLKW, and the file is currently being mapped to virtual
memory using mmap() (see mmap() ). (End)

 

EBADF

fildes is not a valid open file descriptor.

cmd is F_SETLK or F_SETLKW, the type of lock (l_type) is a write lock
(F_WRLCK), and fildes is not a valid file descriptor open for reading.

cmd is F_SETLK or F_SETLKW, the type of lock (l_type) is a write lock
(F_WRLCK), and fildes is not a valid file descriptor open for writing.

cmd is F_FREESP, and fildes is not a valid file descriptor open for writing.

 

Extension

 

 

EDEADLK

cmd is F_FREESP, mandatory record locking is enabled, O_NDELAY and
O_NONBLOCK are clear, and a deadlock condition was detected. (End)

 

EDEADLK

cmd is F_SETLKW, the lock is blocked by a lock from another process, and
putting the calling process to sleep (i.e. in a wait state) until that lock
becomes free would cause a deadlock situation.

 

Extension

 

 

EFAULT

cmd is F_FREESP, and the value pointed to by arg is located at an address
outside the address space used by the process.

cmd is F_GETLK, F_SET_LK or F_SETLKW, and the value pointed to by arg is
located at an address outside the address space used by the process. (End)

 

EINTR

EINVAL

A signal was caught during the fcntl() system call.

cmd is F_DUPFD, and arg is either negative or greater than or equal to the
value for the maximum number of open file descriptors permitted for each
user.

cmd is not a valid value.

cmd is F_GETLK, F_SETLK or SETLKW, and arg or the data it points to is not
valid, or fildes refers to a file that does not support locking.

An attempt was made to access a BS2000 file.

 

Extension

 

 

EIO

EMFILE

An I/O error occurred while reading from or writing to the file system. (End)

cmd is F_DUPFD, and the number of file descriptors currently open in the
calling process is the configured value for the maximum number of open file
descriptors allowed each user.

 

ENOLCK

cmd is F_SETLK or F_SETLKW, the type of lock is a read or write lock, and
there are no more record locks available (too many file segments locked)
because the system maximum has been exceeded.


ENOLINK

EOVERFLOW

fildes is on a remote computer and the connection to this computer is not
active or cmd is F_FREESP, the file on a remote computer and the connection
to it are not active.

One of the values returned cannot be represented correctly.

Notes

fcntl() is executed only for POSIX files

See also

close(), creat(), dup(), exec(), fork(), open(), sigaction(), pipe(), fcntl.h, sys/type.h, unistdt.h.