1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" and Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
9 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
11 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
12 .\" Modified 2002-09-28, aeb
13 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
14 .\" details for dup2().
15 .\" 2008-10-09, mtk: add description of dup3()
17 .TH DUP 2 2022-09-09 "Linux man-pages (unreleased)"
19 dup, dup2, dup3 \- duplicate a file descriptor
22 .RI ( libc ", " \-lc )
25 .B #include <unistd.h>
27 .BI "int dup(int " oldfd );
28 .BI "int dup2(int " oldfd ", int " newfd );
30 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
31 .BR "#include <fcntl.h>" " /* Definition of " O_* " constants */"
32 .B #include <unistd.h>
34 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
39 system call allocates a new file descriptor that refers to the same
40 open file description as the descriptor
42 (For an explanation of open file descriptions, see
44 The new file descriptor number is guaranteed to be the lowest-numbered
45 file descriptor that was unused in the calling process.
47 After a successful return,
48 the old and new file descriptors may be used interchangeably.
49 Since the two file descriptors refer to the same open file description,
50 they share file offset and file status flags;
51 for example, if the file offset is modified by using
53 on one of the file descriptors,
54 the offset is also changed for the other file descriptor.
56 The two file descriptors do not share file descriptor flags
57 (the close-on-exec flag).
58 The close-on-exec flag
62 for the duplicate descriptor is off.
67 system call performs the same task as
69 but instead of using the lowest-numbered unused file descriptor,
70 it uses the file descriptor number specified in
75 is adjusted so that it now refers to the same open file description as
78 If the file descriptor
80 was previously open, it is closed before being reused;
81 the close is performed silently
82 (i.e., any errors during the close are not reported by
85 The steps of closing and reusing the file descriptor
89 This is important, because trying to implement equivalent functionality using
94 subject to race conditions, whereby
96 might be reused between the two steps.
97 Such reuse could happen because the main program is interrupted
98 by a signal handler that allocates a file descriptor,
99 or because a parallel thread allocates a file descriptor.
101 Note the following points:
105 is not a valid file descriptor, then the call fails, and
111 is a valid file descriptor, and
113 has the same value as
117 does nothing, and returns
126 The caller can force the close-on-exec flag to be set
127 for the new file descriptor by specifying
131 See the description of the same flag in
133 for reasons why this may be useful.
135 .\" Ulrich Drepper, LKML, 2008-10-09:
136 .\" We deliberately decided on this change. Otherwise, what is the
137 .\" result of dup3(fd, fd, O_CLOEXEC)?
147 On success, these system calls
148 return the new file descriptor.
149 On error, \-1 is returned, and
151 is set to indicate the error.
156 isn't an open file descriptor.
160 is out of the allowed range for file descriptors (see the discussion of
166 (Linux only) This may be returned by
170 during a race condition with
180 call was interrupted by a signal; see
186 contain an invalid value.
195 The per-process limit on the number of open file descriptors has been reached
196 (see the discussion of
202 was added to Linux in version 2.6.27;
203 glibc support is available starting with
208 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
212 .\" SVr4 documents additional
213 .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
214 .\" The EBUSY return is Linux-specific.
216 The error returned by
218 is different from that returned by
219 .BR fcntl( "..., " F_DUPFD ", ..." )
225 also sometimes returns
232 was open, any errors that would have been reported at
235 If this is of concern,
236 then\(emunless the program is single-threaded and does not allocate
237 file descriptors in signal handlers\(emthe correct approach is
243 because of the race condition described above.
244 Instead, code something like the following could be used:
248 /* Obtain a duplicate of \(aqnewfd\(aq that can subsequently
249 be used to check for close() errors; an EBADF error
250 means that \(aqnewfd\(aq was not open. */
253 if (tmpfd == \-1 && errno != EBADF) {
254 /* Handle unexpected dup() error. */
257 /* Atomically duplicate \(aqoldfd\(aq on \(aqnewfd\(aq. */
259 if (dup2(oldfd, newfd) == \-1) {
260 /* Handle dup2() error. */
263 /* Now check for close() errors on the file originally
264 referred to by \(aqnewfd\(aq. */
267 if (close(tmpfd) == \-1) {
268 /* Handle errors from close. */