1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
5 .\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
30 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
31 .\" Modified 2002-09-28, aeb
32 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
33 .\" details for dup2().
34 .\" 2008-10-09, mtk: add description of dup3()
36 .TH DUP 2 2012-02-14 "Linux" "Linux Programmer's Manual"
38 dup, dup2, dup3 \- duplicate a file descriptor
41 .B #include <unistd.h>
43 .BI "int dup(int " oldfd );
44 .BI "int dup2(int " oldfd ", int " newfd );
46 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
47 .BR "#include <fcntl.h>" " /* Obtain O_* constant definitions */
48 .B #include <unistd.h>
50 .BI "int dup3(int " oldfd ", int " newfd ", int " flags );
53 These system calls create a copy of the file descriptor
57 uses the lowest-numbered unused descriptor for the new descriptor.
60 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
61 first if necessary, but note the following:
65 is not a valid file descriptor, then the call fails, and
71 is a valid file descriptor, and
77 does nothing, and returns
80 After a successful return from one of these system calls,
81 the old and new file descriptors may be used interchangeably.
82 They refer to the same open file description (see
84 and thus share file offset and file status flags;
85 for example, if the file offset is modified by using
87 on one of the descriptors, the offset is also changed for the other.
89 The two descriptors do not share file descriptor flags
90 (the close-on-exec flag).
91 The close-on-exec flag
95 for the duplicate descriptor is off.
102 The caller can force the close-on-exec flag to be set
103 for the new file descriptor by specifying
107 See the description of the same flag in
109 for reasons why this may be useful.
111 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
121 On success, these system calls
122 return the new descriptor.
123 On error, \-1 is returned, and
125 is set appropriately.
130 isn't an open file descriptor, or
132 is out of the allowed range for file descriptors.
135 (Linux only) This may be returned by
139 during a race condition with
149 call was interrupted by a signal; see
155 contain an invalid value.
156 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale
163 The process already has the maximum number of file
164 descriptors open and tried to open a new one.
167 was added to Linux in version 2.6.27;
168 glibc support is available starting with
173 SVr4, 4.3BSD, POSIX.1-2001.
177 .\" SVr4 documents additional
178 .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
179 .\" The EBUSY return is Linux-specific.
181 The error returned by
183 is different from that returned by
184 .BR fcntl( "..., " F_DUPFD ", ..." )
190 also sometimes returns
197 was open, any errors that would have been reported at
200 A careful programmer will not use