Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 2 additions & 70 deletions nvx/src/host/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ use crate::protocol::CAPABILITY_LEN;
use crate::{Error, Result};

mod connection;
mod handle;

pub use connection::{Connection, ConnectionError, ConnectionErrorKind, Console};
pub use handle::InheritedHandle;

/// OpenVMM's snapshot files: a capture's destination and a restore's snapshot view.
const OPENVMM: &str = "openvmm";
Expand Down Expand Up @@ -209,76 +211,6 @@ pub(crate) fn attach_with_transport(
)?)
}

/// An owned descriptor on Unix.
#[cfg(unix)]
type Owned = std::os::fd::OwnedFd;
/// An owned handle on Windows.
#[cfg(windows)]
type Owned = std::os::windows::io::OwnedHandle;
/// An owned file on hosts without descriptors or handles.
#[cfg(not(any(unix, windows)))]
type Owned = File;

/// An already opened resource that a VMM inherits, owned until dropped or transferred.
///
/// Conversions move the same descriptor or handle; they neither duplicate nor reopen it.
/// Borrowed raw descriptors and handles cannot transfer ownership into this type.
#[derive(Debug)]
pub struct InheritedHandle {
/// The owned platform resource.
handle: Owned,
}

impl From<File> for InheritedHandle {
fn from(file: File) -> Self {
Self {
handle: file.into(),
}
}
}

#[cfg(unix)]
impl From<std::os::fd::OwnedFd> for InheritedHandle {
fn from(handle: std::os::fd::OwnedFd) -> Self {
Self { handle }
}
}

#[cfg(unix)]
impl From<InheritedHandle> for std::os::fd::OwnedFd {
fn from(resource: InheritedHandle) -> Self {
resource.handle
}
}

#[cfg(unix)]
impl std::os::fd::AsFd for InheritedHandle {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
std::os::fd::AsFd::as_fd(&self.handle)
}
}

#[cfg(windows)]
impl From<std::os::windows::io::OwnedHandle> for InheritedHandle {
fn from(handle: std::os::windows::io::OwnedHandle) -> Self {
Self { handle }
}
}

#[cfg(windows)]
impl From<InheritedHandle> for std::os::windows::io::OwnedHandle {
fn from(resource: InheritedHandle) -> Self {
resource.handle
}
}

#[cfg(windows)]
impl std::os::windows::io::AsHandle for InheritedHandle {
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
std::os::windows::io::AsHandle::as_handle(&self.handle)
}
}

/// A complete launch handed to the caller's launcher, with all its resources owned.
///
/// The launcher supplies the capability on stdin through a pipe closed to writers before
Expand Down
76 changes: 76 additions & 0 deletions nvx/src/host/vm/handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! An owned descriptor or handle that a VMM inherits, and its platform conversions.

use std::fs::File;

/// An owned descriptor on Unix.
#[cfg(unix)]
type Owned = std::os::fd::OwnedFd;
/// An owned handle on Windows.
#[cfg(windows)]
type Owned = std::os::windows::io::OwnedHandle;
/// An owned file on hosts without descriptors or handles.
#[cfg(not(any(unix, windows)))]
type Owned = File;

/// An already opened resource that a VMM inherits, owned until dropped or transferred.
///
/// Conversions move the same descriptor or handle; they neither duplicate nor reopen it.
/// Borrowed raw descriptors and handles cannot transfer ownership into this type.
#[derive(Debug)]
pub struct InheritedHandle {
/// The owned platform resource.
handle: Owned,
}

impl From<File> for InheritedHandle {
fn from(file: File) -> Self {
Self {
handle: file.into(),
}
}
}

#[cfg(unix)]
impl From<std::os::fd::OwnedFd> for InheritedHandle {
fn from(handle: std::os::fd::OwnedFd) -> Self {
Self { handle }
}
}

#[cfg(unix)]
impl From<InheritedHandle> for std::os::fd::OwnedFd {
fn from(resource: InheritedHandle) -> Self {
resource.handle
}
}

#[cfg(unix)]
impl std::os::fd::AsFd for InheritedHandle {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
std::os::fd::AsFd::as_fd(&self.handle)
}
}

#[cfg(windows)]
impl From<std::os::windows::io::OwnedHandle> for InheritedHandle {
fn from(handle: std::os::windows::io::OwnedHandle) -> Self {
Self { handle }
}
}

#[cfg(windows)]
impl From<InheritedHandle> for std::os::windows::io::OwnedHandle {
fn from(resource: InheritedHandle) -> Self {
resource.handle
}
}

#[cfg(windows)]
impl std::os::windows::io::AsHandle for InheritedHandle {
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
std::os::windows::io::AsHandle::as_handle(&self.handle)
}
}

#[cfg(test)]
pub(super) mod tests;
40 changes: 40 additions & 0 deletions nvx/src/host/vm/handle/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::*;

pub(in crate::host::vm) fn inherited_file(resource: InheritedHandle) -> File {
#[cfg(any(unix, windows))]
return File::from(Owned::from(resource));
#[cfg(not(any(unix, windows)))]
resource.handle
}

#[cfg(unix)]
#[test]
fn an_owned_descriptor_moves_into_and_out_of_an_inherited_handle() {
use std::io::Write;
use std::os::fd::{AsFd, AsRawFd, OwnedFd};

let file = tempfile::tempfile().unwrap();
let number = file.as_raw_fd();
let descriptor: OwnedFd = file.into();
let resource: InheritedHandle = descriptor.into();
assert_eq!(resource.as_fd().as_raw_fd(), number);
let descriptor: OwnedFd = resource.into();
assert_eq!(descriptor.as_raw_fd(), number);
File::from(descriptor).write_all(b"still open").unwrap();
}

#[cfg(windows)]
#[test]
fn an_owned_windows_handle_moves_into_and_out_of_an_inherited_handle() {
use std::io::Write;
use std::os::windows::io::{AsHandle, AsRawHandle, OwnedHandle};

let file = tempfile::tempfile().unwrap();
let number = file.as_raw_handle();
let handle: OwnedHandle = file.into();
let resource: InheritedHandle = handle.into();
assert_eq!(resource.as_handle().as_raw_handle(), number);
let handle: OwnedHandle = resource.into();
assert_eq!(handle.as_raw_handle(), number);
File::from(handle).write_all(b"still open").unwrap();
}
40 changes: 1 addition & 39 deletions nvx/src/host/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::{Command, Stdio};
#[cfg(any(target_os = "linux", windows))]
use std::time::{Duration, Instant};

use super::handle::tests::inherited_file;
use super::*;
use crate::ErrorKind;
use crate::guest_restore::{Network, Publication};
Expand Down Expand Up @@ -1730,13 +1731,6 @@ fn the_log_and_inherited_resources_stay_open_without_reopening_their_paths() {
assert_eq!(fs::read(&input).unwrap(), b"replacement input");
}

fn inherited_file(resource: InheritedHandle) -> File {
#[cfg(any(unix, windows))]
return File::from(Owned::from(resource));
#[cfg(not(any(unix, windows)))]
resource.handle
}

#[test]
fn a_command_moves_the_checked_launch_and_the_same_opened_resources() {
use std::io::Write;
Expand Down Expand Up @@ -1793,38 +1787,6 @@ fn a_command_moves_the_checked_launch_and_the_same_opened_resources() {
assert!(!outputs.outcome.exists());
}

#[cfg(unix)]
#[test]
fn an_owned_descriptor_moves_into_and_out_of_an_inherited_handle() {
use std::io::Write;
use std::os::fd::{AsFd, AsRawFd, OwnedFd};

let file = tempfile::tempfile().unwrap();
let number = file.as_raw_fd();
let descriptor: OwnedFd = file.into();
let resource: InheritedHandle = descriptor.into();
assert_eq!(resource.as_fd().as_raw_fd(), number);
let descriptor: OwnedFd = resource.into();
assert_eq!(descriptor.as_raw_fd(), number);
File::from(descriptor).write_all(b"still open").unwrap();
}

#[cfg(windows)]
#[test]
fn an_owned_windows_handle_moves_into_and_out_of_an_inherited_handle() {
use std::io::Write;
use std::os::windows::io::{AsHandle, AsRawHandle, OwnedHandle};

let file = tempfile::tempfile().unwrap();
let number = file.as_raw_handle();
let handle: OwnedHandle = file.into();
let resource: InheritedHandle = handle.into();
assert_eq!(resource.as_handle().as_raw_handle(), number);
let handle: OwnedHandle = resource.into();
assert_eq!(handle.as_raw_handle(), number);
File::from(handle).write_all(b"still open").unwrap();
}

#[test]
fn creation_checks_the_complete_actual_command_line_at_the_windows_boundary() {
let (_directory, vm) = vm_dir();
Expand Down
Loading