fcntl(2)

NAME
fcntl - manipulate file descriptor

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS
#include <fcntl.h>

int fcntl(int fd, int op, ...);

DESCRIPTION
fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by op.

Certain of the operations below are supported only since a particular Linux kernel version. The preferred method of checking whether the host kernel supports a particular operation is to invoke fcntl() with the desired op value and then test whether the call failed with EINVAL, indicating that the kernel does not recognize this value.

  Duplicating a file descriptor

F_DUPFD(2const)
F_DUPFD_CLOEXEC(2const)

  File descriptor flags

F_GETFD(2const)
F_SETFD(2const)

  File status flags

F_GETFL(2const)
F_SETFL(2const)

  Advisory record locking

F_SETLK(2const)
F_SETLKW(2const)
F_GETLK(2const)

  Open file description locks (non-POSIX)

F_OFD_SETLK(2const)
F_OFD_SETLKW(2const)
F_OFD_GETLK(2const)

  Managing signals

F_GETOWN(2const)
F_SETOWN(2const)
F_GETOWN_EX(2const)
F_SETOWN_EX(2const)
F_GETSIG(2const)
F_SETSIG(2const)

  Leases

F_SETLEASE(2const)
F_GETLEASE(2const)

  File and directory change notification (dnotify)

F_NOTIFY(2const)

  Changing the capacity of a pipe

F_SETPIPE_SZ(2const)
F_GETPIPE_SZ(2const)

  File Sealing

F_ADD_SEALS(2const)
F_GET_SEALS(2const)

  File read/write hints

F_GET_RW_HINT(2const)
F_SET_RW_HINT(2const)
F_GET_FILE_RW_HINT(2const)
F_SET_FILE_RW_HINT(2const)

RETURN VALUE
For a successful call, the return value depends on the operation.

On error, -1 is returned, and errno is set to indicate the error.

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

EINVAL
    The value specified in op is not recognized by this kernel.

VERSIONS
POSIX.1-2024 specifies FD_CLOFORK and F_DUPFD_CLOFORK, but Linux doesn't support them.

STANDARDS
POSIX.1-2024.

HISTORY
SVr4, 4.3BSD, POSIX.1-2001.

SEE ALSO
dup2(2), flock(2), open(2), socket(2), lockf(3), capabilities(7), feature_test_macros(7), lslocks(8)