pub unsafe fn dup2_raw<Fd1: AsFd, Fd2: IntoRawFd>(
oldfd: Fd1,
newfd: Fd2,
) -> Result<OwnedFd>
Expand description
Create a copy of oldfd
with any fd value you want.
§Safety
Since this function returns an OwnedFd
, you have to ensure that the returned
OwnedFd
is the ONLY owner of the file descriptor specified newfd
. Otherwise,
double close could happen.
let oldfd: OwnedFd = open("foo", OFlag::O_RDONLY, Mode::empty()).unwrap();
let newfd: OwnedFd = open("bar", OFlag::O_RDONLY, Mode::empty()).unwrap();
// SAFETY:
// it is NOT safe.
// NOTE that we are passing a RawFd to `newfd`
let duplicated_fd: OwnedFd = unsafe { dup2_raw(&oldfd, newfd.as_raw_fd()) }.unwrap();
// `newfd` and `duplicated_fd` refer to the same file descriptor, and
// they are both owned, double close will happen here.
§Examples
Duplicate a file descriptor with a descriptor that is still not open:
let oldfd = open("foo", OFlag::O_RDONLY, Mode::empty()).unwrap();
// SAFETY:
// It is safe given that we are sure that fd 100 is not open, and the returned
// OwnedFd will be its only owner.
let duplicated_fd = unsafe { dup2_raw(&oldfd, 100) }.unwrap();
// do something with `duplicated_fd`
The code demonstrating double close can be fixed by passing newfd
by value:
let oldfd: OwnedFd = open("foo", OFlag::O_RDONLY, Mode::empty()).unwrap();
let newfd: OwnedFd = open("bar", OFlag::O_RDONLY, Mode::empty()).unwrap();
// SAFETY:
// it is safe since `duplicated_fd` is the only owner of the fd it refers to.
// NOTE that we are passing `newfd` by value, i.e., transfer the ownership
let duplicated_fd: OwnedFd = unsafe { dup2_raw(&oldfd, newfd) }.unwrap();