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 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
31 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 2002-09-28, aeb
33 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
34 .\" details for dup2().
35 .\" 2008-10-09, mtk: add description of dup3()
37 .TH DUP 2 2017-09-15 "Linux" "Linux Programmer's Manual"
39 dup, dup2, dup3 \- duplicate a file descriptor
42 .B #include <unistd.h>
44 .BI "int dup(int " oldfd );
45 .BI "int dup2(int " oldfd ", int " newfd );
47 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
48 .BR "#include <fcntl.h>" " /* Obtain O_* constant definitions */
49 .B #include <unistd.h>
51 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
56 system call creates a copy of the file descriptor
58 using the lowest-numbered unused file descriptor for the new descriptor.
60 After a successful return,
61 the old and new file descriptors may be used interchangeably.
62 They refer to the same open file description (see
64 and thus share file offset and file status flags;
65 for example, if the file offset is modified by using
67 on one of the file descriptors, the offset is also changed for the other.
69 The two file descriptors do not share file descriptor flags
70 (the close-on-exec flag).
71 The close-on-exec flag
75 for the duplicate descriptor is off.
80 system call performs the same task as
82 but instead of using the lowest-numbered unused file descriptor,
83 it uses the file descriptor number specified in
85 If the file descriptor
87 was previously open, it is silently closed before being reused.
89 The steps of closing and reusing the file descriptor
93 This is important, because trying to implement equivalent functionality using
98 subject to race conditions, whereby
100 might be reused between the two steps.
101 Such reuse could happen because the main program is interrupted
102 by a signal handler that allocates a file descriptor,
103 or because a parallel thread allocates a file descriptor.
105 Note the following points:
109 is not a valid file descriptor, then the call fails, and
115 is a valid file descriptor, and
117 has the same value as
121 does nothing, and returns
130 The caller can force the close-on-exec flag to be set
131 for the new file descriptor by specifying
135 See the description of the same flag in
137 for reasons why this may be useful.
139 .\" Ulrich Drepper, LKML, 2008-10-09:
140 .\" We deliberately decided on this change. Otherwise, what is the
141 .\" result of dup3(fd, fd, O_CLOEXEC)?
151 On success, these system calls
152 return the new file descriptor.
153 On error, \-1 is returned, and
155 is set appropriately.
160 isn't an open file descriptor.
164 is out of the allowed range for file descriptors (see the discussion of
170 (Linux only) This may be returned by
174 during a race condition with
184 call was interrupted by a signal; see
190 contain an invalid value.
199 The per-process limit on the number of open file descriptors has been reached
200 (see the discussion of
206 was added to Linux in version 2.6.27;
207 glibc support is available starting with
212 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
216 .\" SVr4 documents additional
217 .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
218 .\" The EBUSY return is Linux-specific.
220 The error returned by
222 is different from that returned by
223 .BR fcntl( "..., " F_DUPFD ", ..." )
229 also sometimes returns
236 was open, any errors that would have been reported at
239 If this is of concern,
240 then\(emunless the program is single-threaded and does not allocate
241 file descriptors in signal handlers\(emthe correct approach is
247 because of the race condition described above.
248 Instead, code something like the following could be used:
251 /* Obtain a duplicate of 'newfd' that can subsequently
252 be used to check for close() errors; an EBADF error
253 means that 'newfd' was not open. */
256 if (tmpfd == \-1 && errno != EBADF) {
257 /* Handle unexpected dup() error */
260 /* Atomically duplicate 'oldfd' on 'newfd' */
262 if (dup2(oldfd, newfd) == \-1) {
263 /* Handle dup2() error */
266 /* Now check for close() errors on the file originally
267 referred to by 'newfd' */
270 if (close(tmpfd) == \-1) {
271 /* Handle errors from close */