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> /* Definition of O_* constants */
16 #include <unistd.h>
17
18 int dup3(int oldfd, int newfd, int flags);
19
21 The dup() system call allocates a new file descriptor that refers to
22 the same open file description as the descriptor oldfd. (For an expla‐
23 nation of open file descriptions, see open(2).) The new file descrip‐
24 tor number is guaranteed to be the lowest-numbered file descriptor that
25 was unused in the calling process.
26
27 After a successful return, the old and new file descriptors may be used
28 interchangeably. Since the two file descriptors refer to the same open
29 file description, they share file offset and file status flags; for ex‐
30 ample, if the file offset is modified by using lseek(2) on one of the
31 file descriptors, the offset is also changed for the other file de‐
32 scriptor.
33
34 The two file descriptors do not share file descriptor flags (the close-
35 on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for
36 the duplicate descriptor is off.
37
38 dup2()
39 The dup2() system call performs the same task as dup(), but instead of
40 using the lowest-numbered unused file descriptor, it uses the file de‐
41 scriptor number specified in newfd. In other words, the file descrip‐
42 tor newfd is adjusted so that it now refers to the same open file de‐
43 scription as oldfd.
44
45 If the file descriptor newfd was previously open, it is closed before
46 being reused; the close is performed silently (i.e., any errors during
47 the close are not reported by dup2()).
48
49 The steps of closing and reusing the file descriptor newfd are per‐
50 formed atomically. This is important, because trying to implement
51 equivalent functionality using close(2) and dup() would be subject to
52 race conditions, whereby newfd might be reused between the two steps.
53 Such reuse could happen because the main program is interrupted by a
54 signal handler that allocates a file descriptor, or because a parallel
55 thread allocates a file descriptor.
56
57 Note the following points:
58
59 * If oldfd is not a valid file descriptor, then the call fails, and
60 newfd is not closed.
61
62 * If oldfd is a valid file descriptor, and newfd has the same value as
63 oldfd, then dup2() does nothing, and returns newfd.
64
65 dup3()
66 dup3() is the same as dup2(), except that:
67
68 * The caller can force the close-on-exec flag to be set for the new
69 file descriptor by specifying O_CLOEXEC in flags. See the descrip‐
70 tion of the same flag in open(2) for reasons why this may be useful.
71
72 * If oldfd equals newfd, then dup3() fails with the error EINVAL.
73
75 On success, these system calls return the new file descriptor. On er‐
76 ror, -1 is returned, and errno is set to indicate the error.
77
79 EBADF oldfd isn't an open file descriptor.
80
81 EBADF newfd is out of the allowed range for file descriptors (see the
82 discussion of RLIMIT_NOFILE in getrlimit(2)).
83
84 EBUSY (Linux only) This may be returned by dup2() or dup3() during a
85 race condition with open(2) and dup().
86
87 EINTR The dup2() or dup3() call was interrupted by a signal; see sig‐
88 nal(7).
89
90 EINVAL (dup3()) flags contain an invalid value.
91
92 EINVAL (dup3()) oldfd was equal to newfd.
93
94 EMFILE The per-process limit on the number of open file descriptors has
95 been reached (see the discussion of RLIMIT_NOFILE in getr‐
96 limit(2)).
97
99 dup3() was added to Linux in version 2.6.27; glibc support is available
100 starting with version 2.9.
101
103 dup(), dup2(): POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
104
105 dup3() is Linux-specific.
106
108 The error returned by dup2() is different from that returned by fc‐
109 ntl(..., F_DUPFD, ...) when newfd is out of range. On some systems,
110 dup2() also sometimes returns EINVAL like F_DUPFD.
111
112 If newfd was open, any errors that would have been reported at close(2)
113 time are lost. If this is of concern, then—unless the program is sin‐
114 gle-threaded and does not allocate file descriptors in signal handlers—
115 the correct approach is not to close newfd before calling dup2(), be‐
116 cause of the race condition described above. Instead, code something
117 like the following could be used:
118
119 /* Obtain a duplicate of 'newfd' that can subsequently
120 be used to check for close() errors; an EBADF error
121 means that 'newfd' was not open. */
122
123 tmpfd = dup(newfd);
124 if (tmpfd == -1 && errno != EBADF) {
125 /* Handle unexpected dup() error. */
126 }
127
128 /* Atomically duplicate 'oldfd' on 'newfd'. */
129
130 if (dup2(oldfd, newfd) == -1) {
131 /* Handle dup2() error. */
132 }
133
134 /* Now check for close() errors on the file originally
135 referred to by 'newfd'. */
136
137 if (tmpfd != -1) {
138 if (close(tmpfd) == -1) {
139 /* Handle errors from close. */
140 }
141 }
142
144 close(2), fcntl(2), open(2), pidfd_getfd(2)
145
147 This page is part of release 5.12 of the Linux man-pages project. A
148 description of the project, information about reporting bugs, and the
149 latest version of this page, can be found at
150 https://www.kernel.org/doc/man-pages/.
151
152
153
154Linux 2021-03-22 DUP(2)