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
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::convert::TryFrom;
use std::fs::File;
use std::os::fd::AsFd;
use std::os::fd::BorrowedFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
use std::os::unix::io::OwnedFd;
use std::os::unix::io::RawFd;
use crate::rutabaga_os::descriptor::AsRawDescriptor;
use crate::rutabaga_os::descriptor::Descriptor;
use crate::rutabaga_os::descriptor::FromRawDescriptor;
use crate::rutabaga_os::descriptor::IntoRawDescriptor;
use crate::rutabaga_os::descriptor::SafeDescriptor;
type Error = std::io::Error;
type Result<T> = std::result::Result<T, Error>;
pub type RawDescriptor = RawFd;
/// Clones `fd`, returning a new file descriptor that refers to the same open file description as
/// `fd`. The cloned fd will have the `FD_CLOEXEC` flag set but will not share any other file
/// descriptor flags with `fd`.
fn clone_fd(fd: &dyn AsRawFd) -> Result<RawFd> {
// SAFETY:
// Safe because this doesn't modify any memory and we check the return value.
let ret = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 0) };
if ret < 0 {
Err(Error::last_os_error())
} else {
Ok(ret)
}
}
impl Drop for SafeDescriptor {
fn drop(&mut self) {
// SAFETY:
// Safe because we own the SafeDescriptor at this point.
let _ = unsafe { libc::close(self.descriptor) };
}
}
impl AsRawFd for SafeDescriptor {
fn as_raw_fd(&self) -> RawFd {
self.as_raw_descriptor()
}
}
impl TryFrom<&dyn AsRawFd> for SafeDescriptor {
type Error = std::io::Error;
fn try_from(fd: &dyn AsRawFd) -> Result<Self> {
Ok(SafeDescriptor {
descriptor: clone_fd(fd)?,
})
}
}
impl SafeDescriptor {
/// Clones this descriptor, internally creating a new descriptor. The new SafeDescriptor will
/// share the same underlying count within the kernel.
pub fn try_clone(&self) -> Result<SafeDescriptor> {
// SAFETY:
// Safe because this doesn't modify any memory and we check the return value.
let descriptor = unsafe { libc::fcntl(self.descriptor, libc::F_DUPFD_CLOEXEC, 0) };
if descriptor < 0 {
Err(Error::last_os_error())
} else {
Ok(SafeDescriptor { descriptor })
}
}
}
impl From<SafeDescriptor> for File {
fn from(s: SafeDescriptor) -> File {
// SAFETY:
// Safe because we own the SafeDescriptor at this point.
unsafe { File::from_raw_fd(s.into_raw_descriptor()) }
}
}
// AsRawFd for interoperability with interfaces that require it. Within crosvm,
// always use AsRawDescriptor when possible.
impl AsRawFd for Descriptor {
fn as_raw_fd(&self) -> RawFd {
self.0
}
}
impl AsFd for SafeDescriptor {
fn as_fd(&self) -> BorrowedFd {
// SAFETY: the `BorrowedFd` we return lives no longer than this `SafeDescriptor`, so the
// descriptor will remain open.
unsafe { BorrowedFd::borrow_raw(self.descriptor) }
}
}
macro_rules! AsRawDescriptor {
($name:ident) => {
impl AsRawDescriptor for $name {
fn as_raw_descriptor(&self) -> RawDescriptor {
self.as_raw_fd()
}
}
};
}
macro_rules! FromRawDescriptor {
($name:ident) => {
impl FromRawDescriptor for $name {
// SAFETY:
// It is caller's responsibility to ensure that the descriptor is valid and
// stays valid for the lifetime of Self
unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
$name::from_raw_fd(descriptor)
}
}
};
}
macro_rules! IntoRawDescriptor {
($name:ident) => {
impl IntoRawDescriptor for $name {
fn into_raw_descriptor(self) -> RawDescriptor {
self.into_raw_fd()
}
}
};
}
// Implementations for File. This enables the File-type to use
// RawDescriptor, but does not mean File should be used as a generic
// descriptor container. That should go to either SafeDescriptor or another more
// relevant container type.
AsRawDescriptor!(File);
FromRawDescriptor!(File);
IntoRawDescriptor!(File);
AsRawDescriptor!(OwnedFd);
FromRawDescriptor!(OwnedFd);
IntoRawDescriptor!(OwnedFd);