FCNTL(2)

NAME
fcntl - manipulate file descriptor

SYNOPSIS
#include <unistd.h>
#include <fcntl.h>

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

DESCRIPTION
fcntl performs one of various miscellaneous operations on fd. The operation in question is determined by cmd:

F_DUPFD
    Makes arg be a copy of fd, closing fd first if necessary.

    The same functionality can be more easily achieved by using dup2.

    The old and new descriptors may be used interchangeably. They share locks, file position pointers and flags; for example, if the file position is modified by using lseek on one of the descriptors, the position is also changed for the other.

    The two descriptors do not share the close-on-exec flag, however.

    On success, the new descriptor is returned.

F_GETFD
    Read the close-on-exec flag. If the low-order bit is 0, the file will remain open across exec, otherwise it will be closed.

F_SETFD
    Set the close-on-exec flag to the value specified by arg (only the least significant bit is used).

F_GETFL
    Read the descriptor's flags (all flags (as set by open(2)) are returned).

F_SETFL
    Set the descriptor's flags to the value specified by arg. Only O_APPEND and O_NONBLOCK may be set.

    The flags are shared between copies (made with dup etc.) of the same file descriptor. The flags are shared between copies (made with dup etc.) of the same file descriptor.

    The flags and their semantics are described in open(2).

F_GETLK, F_SETLK, and F_SETLKW
    Manage discretionary file locks.

F_GETOWN
    Get the process ID (or process group) of the owner of a socket.

    Process groups are returned as negative values.

F_SETOWN
    Set the process or process group that owns a socket.

    For these commands, ownership means receiving SIGIO or SIGURG signals.

    Process groups are specified using negative values.

RETURN VALUE
The return value depends on the operation:

F_DUPFD
    The new descriptor.

F_GETFD
    Value of flag.

F_GETFL
    Value of flags.

F_GETOWN
    Value of descriptor owner.

On error, -1 is returned, and errno is set appropriately.

ERRORS

EBADF
    fs is not an open file descriptor.

EINVAL
    For F_DUPFD, arg is negative or is greater than the maximum allowable value.

EINVAL
    For F_DUPFD, the process already has the maximum number of file descriptors open.

NOTES
The errors returned by dup2 are different from those returned by F_DUPFD.

CONFORMING TO
SVID, AT&T, POSIX, X/OPEN, BSD 4.3

SEE ALSO
open(2), F_DUPFD(2), F_GETFD(2), F_GETFL(2), F_GETLK(2), socket(2).