From 54411ddd8ff099b3c46d8883b299bf2f9e704d1a Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Sat, 28 Nov 2026 02:08:19 -0800 Subject: [PATCH] nvx: move InheritedHandle into its own vm submodule vm.rs defined the Owned platform alias and the public InheritedHandle type with its platform conversions inline, between the attach helpers and LaunchCommand. Move them unchanged into host/vm/handle.rs, which vm re-exports so nvx::host::vm::InheritedHandle keeps its path, and move their conversion tests and the inherited_file helper into host/vm/handle/tests.rs, which vm's tests reuse. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- nvx/src/host/vm.rs | 72 +------------------------------ nvx/src/host/vm/handle.rs | 76 +++++++++++++++++++++++++++++++++ nvx/src/host/vm/handle/tests.rs | 40 +++++++++++++++++ nvx/src/host/vm/tests.rs | 40 +---------------- 4 files changed, 119 insertions(+), 109 deletions(-) create mode 100644 nvx/src/host/vm/handle.rs create mode 100644 nvx/src/host/vm/handle/tests.rs diff --git a/nvx/src/host/vm.rs b/nvx/src/host/vm.rs index 684bc8b14..8cb9309a2 100644 --- a/nvx/src/host/vm.rs +++ b/nvx/src/host/vm.rs @@ -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"; @@ -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 for InheritedHandle { - fn from(file: File) -> Self { - Self { - handle: file.into(), - } - } -} - -#[cfg(unix)] -impl From for InheritedHandle { - fn from(handle: std::os::fd::OwnedFd) -> Self { - Self { handle } - } -} - -#[cfg(unix)] -impl From 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 for InheritedHandle { - fn from(handle: std::os::windows::io::OwnedHandle) -> Self { - Self { handle } - } -} - -#[cfg(windows)] -impl From 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 diff --git a/nvx/src/host/vm/handle.rs b/nvx/src/host/vm/handle.rs new file mode 100644 index 000000000..8c86d4a24 --- /dev/null +++ b/nvx/src/host/vm/handle.rs @@ -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 for InheritedHandle { + fn from(file: File) -> Self { + Self { + handle: file.into(), + } + } +} + +#[cfg(unix)] +impl From for InheritedHandle { + fn from(handle: std::os::fd::OwnedFd) -> Self { + Self { handle } + } +} + +#[cfg(unix)] +impl From 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 for InheritedHandle { + fn from(handle: std::os::windows::io::OwnedHandle) -> Self { + Self { handle } + } +} + +#[cfg(windows)] +impl From 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; diff --git a/nvx/src/host/vm/handle/tests.rs b/nvx/src/host/vm/handle/tests.rs new file mode 100644 index 000000000..395059fbf --- /dev/null +++ b/nvx/src/host/vm/handle/tests.rs @@ -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(); +} diff --git a/nvx/src/host/vm/tests.rs b/nvx/src/host/vm/tests.rs index 203a9598e..ce9305f13 100644 --- a/nvx/src/host/vm/tests.rs +++ b/nvx/src/host/vm/tests.rs @@ -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}; @@ -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; @@ -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();