400 lines
8.8 KiB
HTML
400 lines
8.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of DUP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>DUP</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
dup, dup2, dup3 - duplicate a file descriptor
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>int dup(int </B><I>oldfd</I><B>);</B>
|
|
<B>int dup2(int </B><I>oldfd</I><B>, int </B><I>newfd</I><B>);</B>
|
|
|
|
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
|
|
<B>#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>></B> /* Obtain O_* constant definitions */
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>int dup3(int </B><I>oldfd</I><B>, int </B><I>newfd</I><B>, int </B><I>flags</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>dup</B>()
|
|
|
|
system call creates a copy of the file descriptor
|
|
<I>oldfd</I>,
|
|
|
|
using the lowest-numbered unused file descriptor for the new descriptor.
|
|
<P>
|
|
|
|
After a successful return,
|
|
the old and new file descriptors may be used interchangeably.
|
|
They refer to the same open file description (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
|
|
|
|
and thus share file offset and file status flags;
|
|
for example, if the file offset is modified by using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2)
|
|
|
|
on one of the file descriptors, the offset is also changed for the other.
|
|
<P>
|
|
|
|
The two file descriptors do not share file descriptor flags
|
|
(the close-on-exec flag).
|
|
The close-on-exec flag
|
|
(<B>FD_CLOEXEC</B>;
|
|
|
|
see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2))
|
|
|
|
for the duplicate descriptor is off.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>dup2()</H3>
|
|
|
|
The
|
|
<B>dup2</B>()
|
|
|
|
system call performs the same task as
|
|
<B>dup</B>(),
|
|
|
|
but instead of using the lowest-numbered unused file descriptor,
|
|
it uses the file descriptor number specified in
|
|
<I>newfd</I>.
|
|
|
|
If the file descriptor
|
|
<I>newfd</I>
|
|
|
|
was previously open, it is silently closed before being reused.
|
|
<P>
|
|
|
|
The steps of closing and reusing the file descriptor
|
|
<I>newfd</I>
|
|
|
|
are performed
|
|
<I>atomically</I>.
|
|
|
|
This is important, because trying to implement equivalent functionality using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)
|
|
|
|
and
|
|
<B>dup</B>()
|
|
|
|
would be
|
|
subject to race conditions, whereby
|
|
<I>newfd</I>
|
|
|
|
might be reused between the two steps.
|
|
Such reuse could happen because the main program is interrupted
|
|
by a signal handler that allocates a file descriptor,
|
|
or because a parallel thread allocates a file descriptor.
|
|
<P>
|
|
|
|
Note the following points:
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
If
|
|
<I>oldfd</I>
|
|
|
|
is not a valid file descriptor, then the call fails, and
|
|
<I>newfd</I>
|
|
|
|
is not closed.
|
|
<DT id="2">*<DD>
|
|
If
|
|
<I>oldfd</I>
|
|
|
|
is a valid file descriptor, and
|
|
<I>newfd</I>
|
|
|
|
has the same value as
|
|
<I>oldfd</I>,
|
|
|
|
then
|
|
<B>dup2</B>()
|
|
|
|
does nothing, and returns
|
|
<I>newfd</I>.
|
|
|
|
|
|
</DL>
|
|
<A NAME="lbAF"> </A>
|
|
<H3>dup3()</H3>
|
|
|
|
<B>dup3</B>()
|
|
|
|
is the same as
|
|
<B>dup2</B>(),
|
|
|
|
except that:
|
|
<DL COMPACT>
|
|
<DT id="3">*<DD>
|
|
The caller can force the close-on-exec flag to be set
|
|
for the new file descriptor by specifying
|
|
<B>O_CLOEXEC</B>
|
|
|
|
in
|
|
<I>flags</I>.
|
|
|
|
See the description of the same flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
for reasons why this may be useful.
|
|
<DT id="4">*<DD>
|
|
|
|
|
|
|
|
If
|
|
<I>oldfd</I>
|
|
|
|
equals
|
|
<I>newfd</I>,
|
|
|
|
then
|
|
<B>dup3</B>()
|
|
|
|
fails with the error
|
|
<B>EINVAL</B>.
|
|
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these system calls
|
|
return the new file descriptor.
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="5"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>oldfd</I>
|
|
|
|
isn't an open file descriptor.
|
|
<DT id="6"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>newfd</I>
|
|
|
|
is out of the allowed range for file descriptors (see the discussion of
|
|
<B>RLIMIT_NOFILE</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2)).
|
|
|
|
<DT id="7"><B>EBUSY</B>
|
|
|
|
<DD>
|
|
(Linux only) This may be returned by
|
|
<B>dup2</B>()
|
|
|
|
or
|
|
<B>dup3</B>()
|
|
|
|
during a race condition with
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
and
|
|
<B>dup</B>().
|
|
|
|
<DT id="8"><B>EINTR</B>
|
|
|
|
<DD>
|
|
The
|
|
<B>dup2</B>()
|
|
|
|
or
|
|
<B>dup3</B>()
|
|
|
|
call was interrupted by a signal; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
<DT id="9"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
(<B>dup3</B>())
|
|
|
|
<I>flags</I>
|
|
|
|
contain an invalid value.
|
|
<DT id="10"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
(<B>dup3</B>())
|
|
|
|
<I>oldfd</I>
|
|
|
|
was equal to
|
|
<I>newfd</I>.
|
|
|
|
<DT id="11"><B>EMFILE</B>
|
|
|
|
<DD>
|
|
The per-process limit on the number of open file descriptors has been reached
|
|
(see the discussion of
|
|
<B>RLIMIT_NOFILE</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2)).
|
|
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>dup3</B>()
|
|
|
|
was added to Linux in version 2.6.27;
|
|
glibc support is available starting with
|
|
version 2.9.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>dup</B>(),
|
|
|
|
<B>dup2</B>():
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
|
|
<P>
|
|
|
|
<B>dup3</B>()
|
|
|
|
is Linux-specific.
|
|
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The error returned by
|
|
<B>dup2</B>()
|
|
|
|
is different from that returned by
|
|
<B>fcntl(</B>..., <B>F_DUPFD</B>, ...<B>)</B>
|
|
|
|
when
|
|
<I>newfd</I>
|
|
|
|
is out of range.
|
|
On some systems,
|
|
<B>dup2</B>()
|
|
|
|
also sometimes returns
|
|
<B>EINVAL</B>
|
|
|
|
like
|
|
<B>F_DUPFD</B>.
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>newfd</I>
|
|
|
|
was open, any errors that would have been reported at
|
|
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)
|
|
|
|
time are lost.
|
|
If this is of concern,
|
|
then---unless the program is single-threaded and does not allocate
|
|
file descriptors in signal handlers---the correct approach is
|
|
<I>not</I>
|
|
|
|
to close
|
|
<I>newfd</I>
|
|
|
|
before calling
|
|
<B>dup2</B>(),
|
|
|
|
because of the race condition described above.
|
|
Instead, code something like the following could be used:
|
|
<P>
|
|
|
|
|
|
<BR> /* Obtain a duplicate of 'newfd' that can subsequently
|
|
<BR> be used to check for close() errors; an EBADF error
|
|
<BR> means that 'newfd' was not open. */
|
|
<P>
|
|
<BR> tmpfd = dup(newfd);
|
|
<BR> if (tmpfd == -1 && errno != EBADF) {
|
|
<BR> /* Handle unexpected dup() error */
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Atomically duplicate 'oldfd' on 'newfd' */
|
|
<P>
|
|
<BR> if (dup2(oldfd, newfd) == -1) {
|
|
<BR> /* Handle dup2() error */
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Now check for close() errors on the file originally
|
|
<BR> referred to by 'newfd' */
|
|
<P>
|
|
<BR> if (tmpfd != -1) {
|
|
<BR> if (close(tmpfd) == -1) {
|
|
<BR> /* Handle errors from close */
|
|
<BR> }
|
|
<BR> }
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="12"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="13"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="14"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="15"><A HREF="#lbAE">dup2()</A><DD>
|
|
<DT id="16"><A HREF="#lbAF">dup3()</A><DD>
|
|
</DL>
|
|
<DT id="17"><A HREF="#lbAG">RETURN VALUE</A><DD>
|
|
<DT id="18"><A HREF="#lbAH">ERRORS</A><DD>
|
|
<DT id="19"><A HREF="#lbAI">VERSIONS</A><DD>
|
|
<DT id="20"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="21"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DT id="22"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="23"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:32 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|