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