1DUP(2) Linux Programmer's Manual DUP(2)
2
3
4
6 dup, dup2, dup3 - duplicate a file descriptor
7
9 #include <unistd.h>
10
11 int dup(int oldfd);
12 int dup2(int oldfd, int newfd);
13
14 #define _GNU_SOURCE /* See feature_test_macros(7) */
15 #include <fcntl.h> /* Obtain O_* constant definitions */
16 #include <unistd.h>
17
18 int dup3(int oldfd, int newfd, int flags);
19
21 These system calls create a copy of the file descriptor oldfd.
22
23 dup() uses the lowest-numbered unused descriptor for the new descrip‐
24 tor.
25
26 dup2() makes newfd be the copy of oldfd, closing newfd first if neces‐
27 sary, but note the following:
28
29 * If oldfd is not a valid file descriptor, then the call fails, and
30 newfd is not closed.
31
32 * If oldfd is a valid file descriptor, and newfd has the same value as
33 oldfd, then dup2() does nothing, and returns newfd.
34
35 After a successful return from one of these system calls, the old and
36 new file descriptors may be used interchangeably. They refer to the
37 same open file description (see open(2)) and thus share file offset and
38 file status flags; for example, if the file offset is modified by using
39 lseek(2) on one of the descriptors, the offset is also changed for the
40 other.
41
42 The two descriptors do not share file descriptor flags (the close-on-
43 exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the
44 duplicate descriptor is off.
45
46 dup3() is the same as dup2(), except that:
47
48 * The caller can force the close-on-exec flag to be set for the new
49 file descriptor by specifying O_CLOEXEC in flags. See the descrip‐
50 tion of the same flag in open(2) for reasons why this may be useful.
51
52 * If oldfd equals newfd, then dup3() fails with the error EINVAL.
53
55 On success, these system calls return the new descriptor. On error, -1
56 is returned, and errno is set appropriately.
57
59 EBADF oldfd isn't an open file descriptor, or newfd is out of the
60 allowed range for file descriptors.
61
62 EBUSY (Linux only) This may be returned by dup2() or dup3() during a
63 race condition with open(2) and dup().
64
65 EINTR The dup2() or dup3() call was interrupted by a signal; see sig‐
66 nal(7).
67
68 EINVAL (dup3()) flags contain an invalid value. Or, oldfd was equal to
69 newfd.
70
71 EMFILE The process already has the maximum number of file descriptors
72 open and tried to open a new one.
73
75 dup3() was added to Linux in version 2.6.27; glibc support is available
76 starting with version 2.9.
77
79 dup(), dup2(): SVr4, 4.3BSD, POSIX.1-2001.
80
81 dup3() is Linux-specific.
82
84 The error returned by dup2() is different from that returned by
85 fcntl(..., F_DUPFD, ...) when newfd is out of range. On some systems
86 dup2() also sometimes returns EINVAL like F_DUPFD.
87
88 If newfd was open, any errors that would have been reported at close(2)
89 time are lost. A careful programmer will not use dup2() or dup3()
90 without closing newfd first.
91
93 close(2), fcntl(2), open(2)
94
96 This page is part of release 3.53 of the Linux man-pages project. A
97 description of the project, and information about reporting bugs, can
98 be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102Linux 2012-02-14 DUP(2)