Skip to content

Commit 7b5068a

Browse files
committed
unistd: document the dup, dup2, and dup3 calls
Signed-off-by: Paul Osborne <osbpau@gmail.com>
1 parent aea291e commit 7b5068a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/unistd.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,41 @@ pub fn gettid() -> pid_t {
140140
unsafe { libc::syscall(libc::SYS_gettid) as pid_t }
141141
}
142142

143+
/// Create a copy of the specified file descriptor (see
144+
/// [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
145+
///
146+
/// The new file descriptor will be have a new index but refer to the same
147+
/// resource as the old file descriptor and the old and new file descriptors may
148+
/// be used interchangeably. The new and old file descriptor share the same
149+
/// underlying resource, offset, and file status flags. The actual index used
150+
/// for the file descriptor will be the lowest fd index that is available.
151+
///
152+
/// The two file descriptors do not share file descriptor flags (e.g. `FD_CLOEXEC`).
143153
#[inline]
144154
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
145155
let res = unsafe { libc::dup(oldfd) };
146156

147157
Errno::result(res)
148158
}
149159

160+
/// Create a copy of the specified file descriptor using the specified fd (see
161+
/// [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
162+
///
163+
/// This function behaves similar to `dup()` except that it will try to use the
164+
/// specified fd instead of allocating a new one. See the man pages for more
165+
/// detail on the exact behavior of this function.
150166
#[inline]
151167
pub fn dup2(oldfd: RawFd, newfd: RawFd) -> Result<RawFd> {
152168
let res = unsafe { libc::dup2(oldfd, newfd) };
153169

154170
Errno::result(res)
155171
}
156172

173+
/// Create a new copy of the specified file descriptor using the specified fd
174+
/// and flags (see [dup(2)](http://man7.org/linux/man-pages/man2/dup.2.html)).
175+
///
176+
/// This function behaves similar to `dup2()` but allows for flags to be
177+
/// specified.
157178
pub fn dup3(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
158179
dup3_polyfill(oldfd, newfd, flags)
159180
}

0 commit comments

Comments
 (0)