Skip to content

Commit 54411dd

Browse files
ppennaCopilot
andcommitted
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>
1 parent ebaffcb commit 54411dd

4 files changed

Lines changed: 119 additions & 109 deletions

File tree

‎nvx/src/host/vm.rs‎

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ use crate::protocol::CAPABILITY_LEN;
5555
use crate::{Error, Result};
5656

5757
mod connection;
58+
mod handle;
5859

5960
pub use connection::{Connection, ConnectionError, ConnectionErrorKind, Console};
61+
pub use handle::InheritedHandle;
6062

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

212-
/// An owned descriptor on Unix.
213-
#[cfg(unix)]
214-
type Owned = std::os::fd::OwnedFd;
215-
/// An owned handle on Windows.
216-
#[cfg(windows)]
217-
type Owned = std::os::windows::io::OwnedHandle;
218-
/// An owned file on hosts without descriptors or handles.
219-
#[cfg(not(any(unix, windows)))]
220-
type Owned = File;
221-
222-
/// An already opened resource that a VMM inherits, owned until dropped or transferred.
223-
///
224-
/// Conversions move the same descriptor or handle; they neither duplicate nor reopen it.
225-
/// Borrowed raw descriptors and handles cannot transfer ownership into this type.
226-
#[derive(Debug)]
227-
pub struct InheritedHandle {
228-
/// The owned platform resource.
229-
handle: Owned,
230-
}
231-
232-
impl From<File> for InheritedHandle {
233-
fn from(file: File) -> Self {
234-
Self {
235-
handle: file.into(),
236-
}
237-
}
238-
}
239-
240-
#[cfg(unix)]
241-
impl From<std::os::fd::OwnedFd> for InheritedHandle {
242-
fn from(handle: std::os::fd::OwnedFd) -> Self {
243-
Self { handle }
244-
}
245-
}
246-
247-
#[cfg(unix)]
248-
impl From<InheritedHandle> for std::os::fd::OwnedFd {
249-
fn from(resource: InheritedHandle) -> Self {
250-
resource.handle
251-
}
252-
}
253-
254-
#[cfg(unix)]
255-
impl std::os::fd::AsFd for InheritedHandle {
256-
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
257-
std::os::fd::AsFd::as_fd(&self.handle)
258-
}
259-
}
260-
261-
#[cfg(windows)]
262-
impl From<std::os::windows::io::OwnedHandle> for InheritedHandle {
263-
fn from(handle: std::os::windows::io::OwnedHandle) -> Self {
264-
Self { handle }
265-
}
266-
}
267-
268-
#[cfg(windows)]
269-
impl From<InheritedHandle> for std::os::windows::io::OwnedHandle {
270-
fn from(resource: InheritedHandle) -> Self {
271-
resource.handle
272-
}
273-
}
274-
275-
#[cfg(windows)]
276-
impl std::os::windows::io::AsHandle for InheritedHandle {
277-
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
278-
std::os::windows::io::AsHandle::as_handle(&self.handle)
279-
}
280-
}
281-
282214
/// A complete launch handed to the caller's launcher, with all its resources owned.
283215
///
284216
/// The launcher supplies the capability on stdin through a pipe closed to writers before

‎nvx/src/host/vm/handle.rs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! An owned descriptor or handle that a VMM inherits, and its platform conversions.
2+
3+
use std::fs::File;
4+
5+
/// An owned descriptor on Unix.
6+
#[cfg(unix)]
7+
type Owned = std::os::fd::OwnedFd;
8+
/// An owned handle on Windows.
9+
#[cfg(windows)]
10+
type Owned = std::os::windows::io::OwnedHandle;
11+
/// An owned file on hosts without descriptors or handles.
12+
#[cfg(not(any(unix, windows)))]
13+
type Owned = File;
14+
15+
/// An already opened resource that a VMM inherits, owned until dropped or transferred.
16+
///
17+
/// Conversions move the same descriptor or handle; they neither duplicate nor reopen it.
18+
/// Borrowed raw descriptors and handles cannot transfer ownership into this type.
19+
#[derive(Debug)]
20+
pub struct InheritedHandle {
21+
/// The owned platform resource.
22+
handle: Owned,
23+
}
24+
25+
impl From<File> for InheritedHandle {
26+
fn from(file: File) -> Self {
27+
Self {
28+
handle: file.into(),
29+
}
30+
}
31+
}
32+
33+
#[cfg(unix)]
34+
impl From<std::os::fd::OwnedFd> for InheritedHandle {
35+
fn from(handle: std::os::fd::OwnedFd) -> Self {
36+
Self { handle }
37+
}
38+
}
39+
40+
#[cfg(unix)]
41+
impl From<InheritedHandle> for std::os::fd::OwnedFd {
42+
fn from(resource: InheritedHandle) -> Self {
43+
resource.handle
44+
}
45+
}
46+
47+
#[cfg(unix)]
48+
impl std::os::fd::AsFd for InheritedHandle {
49+
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
50+
std::os::fd::AsFd::as_fd(&self.handle)
51+
}
52+
}
53+
54+
#[cfg(windows)]
55+
impl From<std::os::windows::io::OwnedHandle> for InheritedHandle {
56+
fn from(handle: std::os::windows::io::OwnedHandle) -> Self {
57+
Self { handle }
58+
}
59+
}
60+
61+
#[cfg(windows)]
62+
impl From<InheritedHandle> for std::os::windows::io::OwnedHandle {
63+
fn from(resource: InheritedHandle) -> Self {
64+
resource.handle
65+
}
66+
}
67+
68+
#[cfg(windows)]
69+
impl std::os::windows::io::AsHandle for InheritedHandle {
70+
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
71+
std::os::windows::io::AsHandle::as_handle(&self.handle)
72+
}
73+
}
74+
75+
#[cfg(test)]
76+
pub(super) mod tests;

‎nvx/src/host/vm/handle/tests.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use super::*;
2+
3+
pub(in crate::host::vm) fn inherited_file(resource: InheritedHandle) -> File {
4+
#[cfg(any(unix, windows))]
5+
return File::from(Owned::from(resource));
6+
#[cfg(not(any(unix, windows)))]
7+
resource.handle
8+
}
9+
10+
#[cfg(unix)]
11+
#[test]
12+
fn an_owned_descriptor_moves_into_and_out_of_an_inherited_handle() {
13+
use std::io::Write;
14+
use std::os::fd::{AsFd, AsRawFd, OwnedFd};
15+
16+
let file = tempfile::tempfile().unwrap();
17+
let number = file.as_raw_fd();
18+
let descriptor: OwnedFd = file.into();
19+
let resource: InheritedHandle = descriptor.into();
20+
assert_eq!(resource.as_fd().as_raw_fd(), number);
21+
let descriptor: OwnedFd = resource.into();
22+
assert_eq!(descriptor.as_raw_fd(), number);
23+
File::from(descriptor).write_all(b"still open").unwrap();
24+
}
25+
26+
#[cfg(windows)]
27+
#[test]
28+
fn an_owned_windows_handle_moves_into_and_out_of_an_inherited_handle() {
29+
use std::io::Write;
30+
use std::os::windows::io::{AsHandle, AsRawHandle, OwnedHandle};
31+
32+
let file = tempfile::tempfile().unwrap();
33+
let number = file.as_raw_handle();
34+
let handle: OwnedHandle = file.into();
35+
let resource: InheritedHandle = handle.into();
36+
assert_eq!(resource.as_handle().as_raw_handle(), number);
37+
let handle: OwnedHandle = resource.into();
38+
assert_eq!(handle.as_raw_handle(), number);
39+
File::from(handle).write_all(b"still open").unwrap();
40+
}

‎nvx/src/host/vm/tests.rs‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::process::{Command, Stdio};
55
#[cfg(any(target_os = "linux", windows))]
66
use std::time::{Duration, Instant};
77

8+
use super::handle::tests::inherited_file;
89
use super::*;
910
use crate::ErrorKind;
1011
use crate::guest_restore::{Network, Publication};
@@ -1730,13 +1731,6 @@ fn the_log_and_inherited_resources_stay_open_without_reopening_their_paths() {
17301731
assert_eq!(fs::read(&input).unwrap(), b"replacement input");
17311732
}
17321733

1733-
fn inherited_file(resource: InheritedHandle) -> File {
1734-
#[cfg(any(unix, windows))]
1735-
return File::from(Owned::from(resource));
1736-
#[cfg(not(any(unix, windows)))]
1737-
resource.handle
1738-
}
1739-
17401734
#[test]
17411735
fn a_command_moves_the_checked_launch_and_the_same_opened_resources() {
17421736
use std::io::Write;
@@ -1793,38 +1787,6 @@ fn a_command_moves_the_checked_launch_and_the_same_opened_resources() {
17931787
assert!(!outputs.outcome.exists());
17941788
}
17951789

1796-
#[cfg(unix)]
1797-
#[test]
1798-
fn an_owned_descriptor_moves_into_and_out_of_an_inherited_handle() {
1799-
use std::io::Write;
1800-
use std::os::fd::{AsFd, AsRawFd, OwnedFd};
1801-
1802-
let file = tempfile::tempfile().unwrap();
1803-
let number = file.as_raw_fd();
1804-
let descriptor: OwnedFd = file.into();
1805-
let resource: InheritedHandle = descriptor.into();
1806-
assert_eq!(resource.as_fd().as_raw_fd(), number);
1807-
let descriptor: OwnedFd = resource.into();
1808-
assert_eq!(descriptor.as_raw_fd(), number);
1809-
File::from(descriptor).write_all(b"still open").unwrap();
1810-
}
1811-
1812-
#[cfg(windows)]
1813-
#[test]
1814-
fn an_owned_windows_handle_moves_into_and_out_of_an_inherited_handle() {
1815-
use std::io::Write;
1816-
use std::os::windows::io::{AsHandle, AsRawHandle, OwnedHandle};
1817-
1818-
let file = tempfile::tempfile().unwrap();
1819-
let number = file.as_raw_handle();
1820-
let handle: OwnedHandle = file.into();
1821-
let resource: InheritedHandle = handle.into();
1822-
assert_eq!(resource.as_handle().as_raw_handle(), number);
1823-
let handle: OwnedHandle = resource.into();
1824-
assert_eq!(handle.as_raw_handle(), number);
1825-
File::from(handle).write_all(b"still open").unwrap();
1826-
}
1827-
18281790
#[test]
18291791
fn creation_checks_the_complete_actual_command_line_at_the_windows_boundary() {
18301792
let (_directory, vm) = vm_dir();

0 commit comments

Comments
 (0)