1DUP(2)                     Linux Programmer's Manual                    DUP(2)
2
3
4

NAME

6       dup, dup2, dup3 - duplicate a file descriptor
7

SYNOPSIS

9       #include <unistd.h>
10
11       int dup(int oldfd);
12       int dup2(int oldfd, int newfd);
13
14       #define _GNU_SOURCE
15       #include <unistd.h>
16
17       int dup3(int oldfd, int newfd, int flags);
18

DESCRIPTION

20       These system calls create a copy of the file descriptor oldfd.
21
22       dup()  uses  the lowest-numbered unused descriptor for the new descrip‐
23       tor.
24
25       dup2() makes newfd be the copy of oldfd, closing newfd first if  neces‐
26       sary, but note the following:
27
28       *  If  oldfd  is  not a valid file descriptor, then the call fails, and
29          newfd is not closed.
30
31       *  If oldfd is a valid file descriptor, and newfd has the same value as
32          oldfd, then dup2() does nothing, and returns newfd.
33
34       After  a  successful return from one of these system calls, the old and
35       new file descriptors may be used interchangeably.  They  refer  to  the
36       same open file description (see open(2)) and thus share file offset and
37       file status flags; for example, if the file offset is modified by using
38       lseek(2)  on one of the descriptors, the offset is also changed for the
39       other.
40
41       The two descriptors do not share file descriptor flags  (the  close-on-
42       exec  flag).  The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the
43       duplicate descriptor is off.
44
45       dup3() is the same as dup2(), except that:
46
47       *  The caller can force the close-on-exec flag to be set  for  the  new
48          file  descriptor by specifying O_CLOEXEC in flags.  See the descrip‐
49          tion of the same flag in open(2) for reasons why this may be useful.
50
51       *  If oldfd equals newfd, then dup3() fails with the error EINVAL.
52

RETURN VALUE

54       On success, these system calls return the new descriptor.  On error, -1
55       is returned, and errno is set appropriately.
56

ERRORS

58       EBADF  oldfd  isn't  an  open  file  descriptor, or newfd is out of the
59              allowed range for file descriptors.
60
61       EBUSY  (Linux only) This may be returned by dup2() or dup3()  during  a
62              race condition with open(2) and dup().
63
64       EINTR  The  dup2() or dup3() call was interrupted by a signal; see sig‐
65              nal(7).
66
67       EINVAL (dup3()) flags contain an invalid value.  Or, oldfd was equal to
68              newfd.
69
70       EMFILE The  process  already has the maximum number of file descriptors
71              open and tried to open a new one.
72

VERSIONS

74       dup3() was added to Linux in version 2.6.27; glibc support is available
75       starting with version 2.9.
76

CONFORMING TO

78       dup(), dup2(): SVr4, 4.3BSD, POSIX.1-2001.
79
80       dup3() is Linux-specific.
81

NOTES

83       The  error  returned  by  dup2()  is  different  from  that returned by
84       fcntl(..., F_DUPFD, ...)  when newfd is out of range.  On some  systems
85       dup2() also sometimes returns EINVAL like F_DUPFD.
86
87       If newfd was open, any errors that would have been reported at close(2)
88       time are lost.  A careful programmer will  not  use  dup2()  or  dup3()
89       without closing newfd first.
90

SEE ALSO

92       close(2), fcntl(2), open(2)
93

COLOPHON

95       This  page  is  part of release 3.25 of the Linux man-pages project.  A
96       description of the project, and information about reporting  bugs,  can
97       be found at http://www.kernel.org/doc/man-pages/.
98
99
100
101Linux                             2008-10-09                            DUP(2)
Impressum