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