| 1 |
.\" Copyright, the authors of the Linux man-pages project |
| 2 |
.\" |
| 3 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 4 |
.\" |
| 5 |
.TH F_DUPFD 2const 2026-01-05 "Linux man-pages 6.18" |
| 6 |
.SH NAME |
| 7 |
F_DUPFD, |
| 8 |
F_DUPFD_CLOEXEC |
| 9 |
\- |
| 10 |
duplicate a file descriptor |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <fcntl.h> |
| 17 |
.P |
| 18 |
.BI "int fcntl(int " fd ", F_DUPFD, int " arg ); |
| 19 |
.BI "int fcntl(int " fd ", F_DUPFD_CLOEXEC, int " arg ); |
| 20 |
.fi |
| 21 |
.SH DESCRIPTION |
| 22 |
.TP |
| 23 |
.B F_DUPFD |
| 24 |
Duplicate the file descriptor |
| 25 |
.I fd |
| 26 |
using the lowest-numbered available file descriptor greater than or equal to |
| 27 |
.IR arg . |
| 28 |
This is different from |
| 29 |
.BR dup2 (2), |
| 30 |
which uses exactly the file descriptor specified. |
| 31 |
.IP |
| 32 |
On success, the new file descriptor is returned. |
| 33 |
.IP |
| 34 |
See |
| 35 |
.BR dup (2) |
| 36 |
for further details. |
| 37 |
.TP |
| 38 |
.B F_DUPFD_CLOEXEC |
| 39 |
As for |
| 40 |
.BR F_DUPFD , |
| 41 |
but additionally set the |
| 42 |
close-on-exec flag for the duplicate file descriptor. |
| 43 |
Specifying this flag permits a program to avoid an additional |
| 44 |
.BR F_SETFD (2const) |
| 45 |
operation to set the |
| 46 |
.B FD_CLOEXEC |
| 47 |
flag. |
| 48 |
For an explanation of why this flag is useful, |
| 49 |
see the description of |
| 50 |
.B O_CLOEXEC |
| 51 |
in |
| 52 |
.BR open (2). |
| 53 |
.SH RETURN VALUE |
| 54 |
The new file descriptor. |
| 55 |
.P |
| 56 |
On error, \-1 is returned, and |
| 57 |
.I errno |
| 58 |
is set to indicate the error. |
| 59 |
.SH ERRORS |
| 60 |
See |
| 61 |
.BR fcntl (2). |
| 62 |
.TP |
| 63 |
.B EINVAL |
| 64 |
.I arg |
| 65 |
is negative or is greater than the maximum allowable value |
| 66 |
(see the discussion of |
| 67 |
.B RLIMIT_NOFILE |
| 68 |
in |
| 69 |
.BR getrlimit (2)). |
| 70 |
.TP |
| 71 |
.B EMFILE |
| 72 |
The per-process limit on the number of open file descriptors |
| 73 |
has been reached. |
| 74 |
.SH VERSIONS |
| 75 |
POSIX.1-2024 specifies |
| 76 |
.BR F_DUPFD_CLOFORK , |
| 77 |
but Linux doesn't support it. |
| 78 |
.SH STANDARDS |
| 79 |
POSIX.1-2024. |
| 80 |
.SH HISTORY |
| 81 |
.TP |
| 82 |
.B F_DUPFD |
| 83 |
SVr4, |
| 84 |
4.3BSD, |
| 85 |
POSIX.1-1988. |
| 86 |
.TP |
| 87 |
.B F_DUPFD_CLOEXEC |
| 88 |
Linux 2.6.24. |
| 89 |
POSIX.1-2008. |
| 90 |
(To get this definition, define |
| 91 |
.B _POSIX_C_SOURCE |
| 92 |
with the value 200809L or greater, or |
| 93 |
.B _XOPEN_SOURCE |
| 94 |
with the value 700 or greater.) |
| 95 |
.SH CAVEATS |
| 96 |
The errors returned by |
| 97 |
.BR dup2 (2) |
| 98 |
are different from those returned by |
| 99 |
.BR F_DUPFD . |
| 100 |
.SH SEE ALSO |
| 101 |
.BR dup2 (2), |
| 102 |
.BR fcntl (2) |