1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#![deny(unsafe_op_in_unsafe_fn)]
use std::os::raw::c_int;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
#[derive(Debug)]
/// Thin wrapper around an open file descriptor.
///
/// The wrapped file descriptor will be closed
/// when the wrapper is dropped.
pub struct FileDesc {
fd: OwnedFd,
}
impl FileDesc {
/// Create [`FileDesc`] from an owned file descriptor.
///
/// This does not do anything to the file descriptor other than wrap it.
/// Notably, it does not set the `close-on-exec` flag.
pub fn new(fd: OwnedFd) -> Self {
Self { fd }
}
/// Wrap a raw file descriptor in a [`FileDesc`].
///
/// This does not do anything to the file descriptor other than wrapping it.
/// Notably, it does not set the `close-on-exec` flag.
///
/// # Safety
/// The input must be a valid file descriptor.
/// The file descriptor must not be closed as long as it is managed by the created [`FileDesc`].
pub unsafe fn from_raw_fd(fd: RawFd) -> Self {
unsafe {
Self::new(OwnedFd::from_raw_fd(fd))
}
}
/// Duplicate a file descriptor from an object that has a file descriptor.
///
/// The new file descriptor will have the `close-on-exec` flag set.
/// If the platform supports it, the flag will be set atomically.
///
/// The duplicated [`FileDesc`] will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
pub fn duplicate_from<T: AsFd>(other: T) -> std::io::Result<Self> {
Ok(Self::new(other.as_fd().try_clone_to_owned()?))
}
/// Duplicate a raw file descriptor and wrap it in a [`FileDesc`].
///
/// The new file descriptor will have the `close-on-exec` flag set.
/// If the platform supports it, the flag will be set atomically.
///
/// The duplicated [`FileDesc`] will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
///
/// # Safety
/// The file descriptor must be valid,
/// and duplicating it must not violate the safety requirements of any object already using the file descriptor.
pub unsafe fn duplicate_raw_fd(fd: RawFd) -> std::io::Result<Self> {
unsafe {
Self::duplicate_from(BorrowedFd::borrow_raw(fd))
}
}
/// Get the file descriptor.
///
/// This function does not release ownership of the underlying file descriptor.
/// The file descriptor will still be closed when the [`FileDesc`] is dropped.
pub fn as_fd(&self) -> BorrowedFd {
self.fd.as_fd()
}
/// Release and get the raw file descriptor.
///
/// This function releases ownership of the underlying file descriptor.
/// The file descriptor will not be closed.
pub fn into_fd(self) -> OwnedFd {
self.fd
}
/// Get the raw file descriptor.
///
/// This function does not release ownership of the underlying file descriptor.
/// The file descriptor will still be closed when the [`FileDesc`] is dropped.
pub fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}
/// Release and get the raw file descriptor.
///
/// This function releases ownership of the underlying file descriptor.
/// The file descriptor will not be closed.
pub fn into_raw_fd(self) -> RawFd {
self.fd.into_raw_fd()
}
/// Try to duplicate the file descriptor.
///
/// The duplicated [`FileDesc`] will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
///
/// The new file descriptor will have the `close-on-exec` flag set.
/// If the platform supports it, the flag will be set atomically.
pub fn duplicate(&self) -> std::io::Result<Self> {
Self::duplicate_from(self)
}
/// Change the close-on-exec flag of the file descriptor.
///
/// You should always try to create file descriptors with the close-on-exec flag already set atomically instead of changing it later with this function.
/// Setting the flag later on introduces a race condition if another thread forks before the call to `set_close_on_exec` finishes.
///
/// You can use this without any race condition to disable the `close-on-exec` flag *after* forking but before executing a new program.
pub fn set_close_on_exec(&self, close_on_exec: bool) -> std::io::Result<()> {
unsafe {
// TODO: Are there platforms where we need to preserve other bits?
let arg = if close_on_exec { libc::FD_CLOEXEC } else { 0 };
check_ret(libc::fcntl(self.fd.as_raw_fd(), libc::F_SETFD, arg))?;
Ok(())
}
}
/// Check the close-on-exec flag of the file descriptor.
pub fn get_close_on_exec(&self) -> std::io::Result<bool> {
unsafe {
let ret = check_ret(libc::fcntl(self.fd.as_raw_fd(), libc::F_GETFD, 0))?;
Ok(ret & libc::FD_CLOEXEC != 0)
}
}
}
impl AsFd for FileDesc {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}
impl From<OwnedFd> for FileDesc {
fn from(value: OwnedFd) -> Self {
Self::new(value)
}
}
impl From<FileDesc> for OwnedFd {
fn from(value: FileDesc) -> Self {
value.fd
}
}
impl FromRawFd for FileDesc {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
unsafe {
Self::from_raw_fd(fd)
}
}
}
impl AsRawFd for FileDesc {
fn as_raw_fd(&self) -> RawFd {
self.as_raw_fd()
}
}
impl AsRawFd for &'_ FileDesc {
fn as_raw_fd(&self) -> RawFd {
(*self).as_raw_fd()
}
}
impl IntoRawFd for FileDesc {
fn into_raw_fd(self) -> RawFd {
self.into_raw_fd()
}
}
/// Wrap the return value of a libc function in an [`std::io::Result`].
///
/// If the return value is -1, [`last_os_error()`](std::io::Error::last_os_error) is returned.
/// Otherwise, the return value is returned wrapped as [`Ok`].
fn check_ret(ret: c_int) -> std::io::Result<c_int> {
if ret == -1 {
Err(std::io::Error::last_os_error())
} else {
Ok(ret)
}
}