Go to the previous, next section.

fcntl

SYNOPSIS

int fcntl(int fd, int cmd);

int fcntl(int fd, int cmd, long arg);

PARAMETERS

fd: [in] the file descriptor affected by the call.

cmd: [in] the operation to apply on the file descriptor.

arg: [in] an optionnal argument to the operation.

DESCRIPTION

This call directly applies an operation on a file descriptor. The possible operations are:

F_DUPFD
Duplicates fd the the new file descriptor specified by arg. If arg specifies a file descriptor that was already opened, then the file descriptor is first closed. It has the same effect has dup2. See section dup and dup2

F_GETFD
Returns the close-on-exec flag of fd.

F_SETFD
Sets the close-on-exec flag of fd.

F_GETFL
Returns the file descriptor flags (as specified by open).

F_SETFL
Sets the file descriptor flags to arg. The only flags that can be modified are O_APPEND and O_NONBLOCK. See section creat and open

F_GETLK
Determine if the lock described by arg can be set on the file. If so, the l_type member is set to F_UNLCK. Otherwise, arg is modified to describe the lock preventing the set operation.
F_SETLK
Set the lock described by arg on the file or release an already existing lock.

F_SETLKW
Same as F_SETLK but block if the lock can not be set.

F_GETOWN
Returns the process id or the process group of a socket. A process group is returned as a negative value.

F_SETOWN
Sets the process id or the process group that owns a socket. The owner of the socket receives the SIGIO and SIGURG signals. A process group is specified by a negative value.
When using the F_GETLK, F_SETLK or F_SETLKW commands, the argument is a pointer to a flock structure. This structure has the following layout:

struct flock {
    short l_type;   /* read, write or unlock */
    short l_whence; /* how to interpret l_start */
    off_t l_start;  /* where to begin the locking area */
    off_t l_len;    /* the lenght of the area to lock */
    pid_t l_pid;    /* the pid of the task holding the lock:
                       returned by F_GETLK */
};

The l_whence member has the same meaning as for lseek. See section lseek l_type can take one of the following values:

F_RDLCK
for a shared read lock on.

F_WRLCK
for an exclusive write lock.

F_UNLCK
to unlock the region.

The system merges adjacent locking regions of the same type and owned by the same task. When a subregion inside a region is unlocked, the region is split in two parts.

RETURN VALUE

On success, it depends on the cmd parameter:

F_DUPFD
the new file descriptor.

F_GETFD
the value of the close-on-exec flag.

F_GETFL
the value of the file descriptor flags.

F_GETOWN
the owner of the file descriptor.

On error, the call returns -1 and errno is set to one of the following values:

Go to the previous, next section.