video-interop 0.1.3

Video frame, DMA-BUF, synchronization, and ownership types for Rust and Elixir
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use std::io;
use std::os::fd::{FromRawFd, OwnedFd};

pub(crate) fn duplicate_fd_cloexec(fd: i32) -> io::Result<OwnedFd> {
    // SAFETY: fcntl borrows the input descriptor. On success it returns a new,
    // independently owned descriptor with FD_CLOEXEC already set.
    let duplicated = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0) };
    if duplicated < 0 {
        return Err(io::Error::last_os_error());
    }

    // SAFETY: F_DUPFD_CLOEXEC returned a fresh descriptor which has not been
    // wrapped or transferred elsewhere.
    Ok(unsafe { OwnedFd::from_raw_fd(duplicated) })
}