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