DUP(3POSIX)
β¨ PROLOG β¨
ok so first things first bestie π this page is part of the POSIX Programmer's Manual. the Linux version of this interface may be different (so like, go check the matching Linux man page for the tea on Linux behavior), or honestly it might not even be implemented on Linux at all. π no cap.
π¦ NAME π¦
dup, dup2 β duplicate an open file descriptor. yes literally just copy-pasting a file descriptor, iconic behavior honestly π
πΈ SYNOPSIS πΈ
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);
π DESCRIPTION π
ok so dup() is basically the β¨aestheticβ¨ alternative interface to the service that fcntl() gives you with the F_DUPFD command. so like, the call dup(fildes) shall be equivalent to:
fcntl(fildes, F_DUPFD, 0);
periodt. π
now dup2() β she's the main character π β shall cause the file descriptor fildes2 to refer to the same open file description as the file descriptor fildes, and to share any locks, and shall return fildes2. sharing locks?? we love a collaborative queen π«Ά
if fildes2 is already a valid open file descriptor, it shall be closed first, UNLESS fildes is equal to fildes2, in which case dup2() shall return fildes2 without closing it. (if it's the same fd, she just says "yeah that's me" and moves on, no drama π)
but listen β if the close operation fails to close fildes2, dup2() shall return -1 without changing the open file description to which fildes2 refers. and if fildes is not a valid file descriptor, dup2() shall return -1 and shall not close fildes2. π
βοΈ
also if fildes2 is less than 0 or greater than or equal to {OPEN_MAX}, dup2() shall return -1 with errno set to [EBADF]. π
upon successful completion, if fildes is not equal to fildes2, the FD_CLOEXEC flag associated with fildes2 shall be cleared. π§Ή but if fildes is equal to fildes2, the FD_CLOEXEC flag associated with fildes2 shall not be changed. she said leave my flag alone π
heads up bestie: iffildesrefers to a typed memory object, the result of thedup2()function is unspecified. so like... nobody knows. it's giving mystery. π¦
β RETURN VALUE β
upon successful completion, a non-negative integer β namely the file descriptor β shall be returned. slay! β¨ otherwise -1 shall be returned and errno set to indicate the error. π
π₯ ERRORS π₯
the dup() function shall fail if:
EBADFβ thefildesargument is not a valid open file descriptor. she's fake fr fr πEMFILEβ all file descriptors available to the process are currently open. no more room at the inn, we're full bestie π
the dup2() function shall fail if:
EBADFβ thefildesargument is not a valid open file descriptor, or the argumentfildes2is negative or greater than or equal to{OPEN_MAX}. πEINTRβ thedup2()function was interrupted by a signal. rude. π
the dup2() function may fail if:
EIOβ an I/O error occurred while attempting to closefildes2. oof. π
the following sections are informative. (translation: they're the bonus content, not the legally-binding main quest π)
π EXAMPLES π
Redirecting Standard Output to a File S
the following example closes standard output for the current processes, re-assigns standard output to go to the file referenced by pfd, and closes the original file descriptor to clean up. very demure, very mindful π§
#include <unistd.h>
...
int pfd;
...
close(1);
dup(pfd);
close(pfd);
...
Redirecting Error Messages
the following example redirects messages from stderr to stdout. π
#include <unistd.h>
...
dup2(1, 2);
...
π APPLICATION USAGE π
ok so here's the thing bestie β implementations may use file descriptors that must be inherited into child processes for the child process to remain conforming, like for message catalog or tracing purposes. π
so therefore, an application that calls dup2() with an arbitrary integer for fildes2 risks non-conforming behavior. π¬ and dup2() can only portably be used to overwrite file descriptor values that the application has obtained through explicit actions, or for the three file descriptors corresponding to the standard file streams. those three are the safe ones, iykyk π
and to avoid a race condition of leaking an unintended file descriptor into a child process, an application should consider opening all file descriptors with the FD_CLOEXEC bit set, unless the file descriptor is intended to be inherited across exec. protection first, always π‘οΈβ¨
π§ RATIONALE π§
the dup() function is redundant. π its services are also provided by the fcntl() function. it's been included in this volume of POSIX.1β2017 primarily for historical reasons, since many existing applications use it. so like... she's here for the nostalgia. legacy queen. π
on the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor. atomic?? we love an atomic girlie βοΈβ¨
the dup2() function is not marked obsolescent because it presents a type-safe version of functionality provided in a type-unsafe version by fcntl(). and it's used in the POSIX Ada binding. π«Ά
the dup2() function is not intended for use in critical regions as a synchronization mechanism. not me trying to use dup2 as a lock π don't do that.
in the description of [EBADF], the case of fildes being out of range is covered by the given case of fildes not being valid. the descriptions for fildes and fildes2 are different because the only kind of invalidity that is relevant for fildes2 is whether it is out of range; that is, it does not matter whether fildes2 refers to an open file when the dup2() call is made. π got it? got it. π
π¦ FUTURE DIRECTIONS π¦
none. β¨ she's perfect as she is. periodt.
π SEE ALSO π
close()fcntl()open()
the Base Definitions volume of POSIX.1β2017, <unistd.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's a wrap bestie!! π this whole thing came to you from IEEE/The Open Group, 2017, filed under DUP(3POSIX) β go off, stay atomic, and always set that FD_CLOEXEC π
β¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms