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