1FCNTL(2)                   Linux Programmer's Manual                  FCNTL(2)
2
3
4

NAME

6       fcntl - manipulate file descriptor
7

SYNOPSIS

9       #include <fcntl.h>
10
11       int fcntl(int fd, int cmd, ... /* arg */ );
12

DESCRIPTION

14       fcntl() performs one of the operations described below on the open file
15       descriptor fd.  The operation is determined by cmd.
16
17       fcntl() can take an optional third argument.  Whether or not this argu‐
18       ment  is  required is determined by cmd.  The required argument type is
19       indicated in parentheses after each cmd name (in most  cases,  the  re‐
20       quired  type  is int, and we identify the argument using the name arg),
21       or void is specified if the argument is not required.
22
23       Certain of the operations below are supported only since  a  particular
24       Linux  kernel  version.   The  preferred method of checking whether the
25       host kernel supports a particular operation is to invoke  fcntl()  with
26       the  desired  cmd value and then test whether the call failed with EIN‐
27       VAL, indicating that the kernel does not recognize this value.
28
29   Duplicating a file descriptor
30       F_DUPFD (int)
31              Duplicate the  file  descriptor  fd  using  the  lowest-numbered
32              available file descriptor greater than or equal to arg.  This is
33              different from dup2(2), which uses exactly the  file  descriptor
34              specified.
35
36              On success, the new file descriptor is returned.
37
38              See dup(2) for further details.
39
40       F_DUPFD_CLOEXEC (int; since Linux 2.6.24)
41              As  for F_DUPFD, but additionally set the close-on-exec flag for
42              the duplicate file descriptor.  Specifying this flag  permits  a
43              program  to avoid an additional fcntl() F_SETFD operation to set
44              the FD_CLOEXEC flag.  For an explanation of  why  this  flag  is
45              useful, see the description of O_CLOEXEC in open(2).
46
47   File descriptor flags
48       The  following commands manipulate the flags associated with a file de‐
49       scriptor.  Currently, only one such flag is  defined:  FD_CLOEXEC,  the
50       close-on-exec  flag.  If the FD_CLOEXEC bit is set, the file descriptor
51       will automatically be closed during a successful  execve(2).   (If  the
52       execve(2)  fails, the file descriptor is left open.)  If the FD_CLOEXEC
53       bit is not set, the file descriptor will  remain  open  across  an  ex‐
54       ecve(2).
55
56       F_GETFD (void)
57              Return  (as  the function result) the file descriptor flags; arg
58              is ignored.
59
60       F_SETFD (int)
61              Set the file descriptor flags to the value specified by arg.
62
63       In multithreaded programs, using fcntl() F_SETFD to set  the  close-on-
64       exec  flag  at  the same time as another thread performs a fork(2) plus
65       execve(2) is vulnerable to a