diff options
author | Daniel Verkamp <dverkamp@chromium.org> | 2024-06-11 14:17:54 -0700 |
---|---|---|
committer | crosvm LUCI <crosvm-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-06-11 22:28:40 +0000 |
commit | 247ed73ed1ef3cac0a8bdda4f40a4b6298df4f93 (patch) | |
tree | 3fa50aedefe5397edef1e0d41961f29c03b3267b | |
parent | 38d1ac5b1b73f0219059bb59f225255f24b2b098 (diff) | |
download | crosvm-247ed73ed1ef3cac0a8bdda4f40a4b6298df4f93.tar.gz |
crosvm_control: remove all conditional compilation
The crosvm_control library's API and ABI should not depend on crosvm
configuration options. If functionality is not available in a particular
build, the library should just provide a stub version of the function
that returns an error, rather than changing the exported symbols or
function prototypes.
BUG=None
TEST=tools/dev_container tools/presubmit
TEST=cargo build --no-default-features -p crosvm_control
Change-Id: I4428d3efe8d52a16ec78998fd83cd7d16b6b7773
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5623295
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
-rw-r--r-- | crosvm_control/src/lib.rs | 34 | ||||
-rw-r--r-- | vm_control/src/client.rs | 1 | ||||
-rw-r--r-- | vm_control/src/lib.rs | 10 | ||||
-rw-r--r-- | vm_control/src/sys.rs | 2 | ||||
-rw-r--r-- | vm_control/src/sys/windows.rs | 14 |
5 files changed, 25 insertions, 36 deletions
diff --git a/crosvm_control/src/lib.rs b/crosvm_control/src/lib.rs index a5df661d1..0bfefc8ea 100644 --- a/crosvm_control/src/lib.rs +++ b/crosvm_control/src/lib.rs @@ -9,7 +9,13 @@ //! //! Downstream projects rely on this library maintaining a stable API surface. //! Do not make changes to this library without consulting the crosvm externalization team. -//! Email: crosvm-dev@chromium.org +//! Email: <crosvm-dev@chromium.org> +//! +//! The API of this library should remain the same regardless of which crosvm features are enabled. +//! Any missing functionality should be handled by returning an error at runtime, not conditional +//! compilation, so that users can rely on the the same set of functions with the same prototypes +//! regardless of how crosvm is configured. +//! //! For more information see: //! <https://crosvm.dev/book/running_crosvm/programmatic_interaction.html#usage> @@ -19,7 +25,6 @@ use std::ffi::CStr; use std::panic::catch_unwind; use std::path::Path; use std::path::PathBuf; -#[cfg(any(target_os = "android", target_os = "linux"))] use std::time::Duration; use libc::c_char; @@ -33,14 +38,12 @@ use vm_control::client::do_usb_attach; use vm_control::client::do_usb_detach; use vm_control::client::do_usb_list; use vm_control::client::handle_request; -#[cfg(any(target_os = "android", target_os = "linux"))] use vm_control::client::handle_request_with_timeout; use vm_control::client::vms_request; use vm_control::BalloonControlCommand; use vm_control::BalloonStats; use vm_control::BalloonWS; use vm_control::DiskControlCommand; -#[cfg(feature = "registered_events")] use vm_control::RegisteredEvent; use vm_control::SwapCommand; use vm_control::UsbControlAttachedDevice; @@ -183,7 +186,6 @@ pub unsafe extern "C" fn crosvm_client_balloon_vms( /// Function is unsafe due to raw pointer usage - a null pointer could be passed in. Usage of /// !raw_pointer.is_null() checks should prevent unsafe behavior but the caller should ensure no /// null pointers are passed. -#[cfg(any(target_os = "android", target_os = "linux"))] #[no_mangle] pub unsafe extern "C" fn crosvm_client_balloon_vms_wait_with_timeout( socket_path: *const c_char, @@ -783,13 +785,7 @@ pub unsafe extern "C" fn crosvm_client_balloon_stats( stats: *mut BalloonStatsFfi, actual: *mut u64, ) -> bool { - crosvm_client_balloon_stats_impl( - socket_path, - #[cfg(any(target_os = "android", target_os = "linux"))] - None, - stats, - actual, - ) + crosvm_client_balloon_stats_impl(socket_path, None, stats, actual) } /// See crosvm_client_balloon_stats. @@ -799,7 +795,6 @@ pub unsafe extern "C" fn crosvm_client_balloon_stats( /// Function is unsafe due to raw pointer usage - a null pointer could be passed in. Usage of /// !raw_pointer.is_null() checks should prevent unsafe behavior but the caller should ensure no /// null pointers are passed. -#[cfg(any(target_os = "android", target_os = "linux"))] #[no_mangle] pub unsafe extern "C" fn crosvm_client_balloon_stats_with_timeout( socket_path: *const c_char, @@ -817,16 +812,13 @@ pub unsafe extern "C" fn crosvm_client_balloon_stats_with_timeout( fn crosvm_client_balloon_stats_impl( socket_path: *const c_char, - #[cfg(any(target_os = "android", target_os = "linux"))] timeout_ms: Option<Duration>, + timeout_ms: Option<Duration>, stats: *mut BalloonStatsFfi, actual: *mut u64, ) -> bool { catch_unwind(|| { if let Some(socket_path) = validate_socket_path(socket_path) { let request = &VmRequest::BalloonCommand(BalloonControlCommand::Stats {}); - #[cfg(not(unix))] - let resp = handle_request(request, socket_path); - #[cfg(any(target_os = "android", target_os = "linux"))] let resp = handle_request_with_timeout(request, socket_path, timeout_ms); if let Ok(VmResponse::BalloonStats { stats: ref balloon_stats, @@ -989,19 +981,14 @@ pub unsafe extern "C" fn crosvm_client_balloon_working_set( /// Publically exposed version of RegisteredEvent enum, implemented as an /// integral newtype for FFI safety. -#[cfg(feature = "registered_events")] #[repr(C)] #[derive(Copy, Clone, PartialEq, Eq)] pub struct RegisteredEventFfi(u32); -#[cfg(feature = "registered_events")] pub const REGISTERED_EVENT_VIRTIO_BALLOON_WS_REPORT: RegisteredEventFfi = RegisteredEventFfi(0); -#[cfg(feature = "registered_events")] pub const REGISTERED_EVENT_VIRTIO_BALLOON_RESIZE: RegisteredEventFfi = RegisteredEventFfi(1); -#[cfg(feature = "registered_events")] pub const REGISTERED_EVENT_VIRTIO_BALLOON_OOM_DEFLATION: RegisteredEventFfi = RegisteredEventFfi(2); -#[cfg(feature = "registered_events")] impl TryFrom<RegisteredEventFfi> for RegisteredEvent { type Error = &'static str; @@ -1024,7 +1011,6 @@ impl TryFrom<RegisteredEventFfi> for RegisteredEvent { /// Function is unsafe due to raw pointer usage - a null pointer could be passed in. Usage of /// !raw_pointer.is_null() checks should prevent unsafe behavior but the caller should ensure no /// null pointers are passed. -#[cfg(feature = "registered_events")] #[no_mangle] pub unsafe extern "C" fn crosvm_client_register_events_listener( socket_path: *const c_char, @@ -1062,7 +1048,6 @@ pub unsafe extern "C" fn crosvm_client_register_events_listener( /// Function is unsafe due to raw pointer usage - a null pointer could be passed in. Usage of /// !raw_pointer.is_null() checks should prevent unsafe behavior but the caller should ensure no /// null pointers are passed. -#[cfg(feature = "registered_events")] #[no_mangle] pub unsafe extern "C" fn crosvm_client_unregister_events_listener( socket_path: *const c_char, @@ -1100,7 +1085,6 @@ pub unsafe extern "C" fn crosvm_client_unregister_events_listener( /// Function is unsafe due to raw pointer usage - a null pointer could be passed in. Usage of /// !raw_pointer.is_null() checks should prevent unsafe behavior but the caller should ensure no /// null pointers are passed. -#[cfg(feature = "registered_events")] #[no_mangle] pub unsafe extern "C" fn crosvm_client_unregister_listener( socket_path: *const c_char, diff --git a/vm_control/src/client.rs b/vm_control/src/client.rs index 4e0bdd0a1..73577e76c 100644 --- a/vm_control/src/client.rs +++ b/vm_control/src/client.rs @@ -24,7 +24,6 @@ pub use crate::gpu::do_gpu_set_display_mouse_mode; #[cfg(feature = "gpu")] pub use crate::gpu::ModifyGpuResult; pub use crate::sys::handle_request; -#[cfg(any(target_os = "android", target_os = "linux"))] pub use crate::sys::handle_request_with_timeout; use crate::BatControlCommand; use crate::BatControlResult; diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs index fffccb308..b53f81c98 100644 --- a/vm_control/src/lib.rs +++ b/vm_control/src/lib.rs @@ -1337,19 +1337,16 @@ pub enum VmRequest { /// Command to Snapshot devices Snapshot(SnapshotCommand), /// Register for event notification - #[cfg(feature = "registered_events")] RegisterListener { socket_addr: String, event: RegisteredEvent, }, /// Unregister for notifications for event - #[cfg(feature = "registered_events")] UnregisterListener { socket_addr: String, event: RegisteredEvent, }, /// Unregister for all event notification - #[cfg(feature = "registered_events")] Unregister { socket_addr: String }, /// Suspend VM VCPUs and Devices until resume. SuspendVm, @@ -1359,7 +1356,6 @@ pub enum VmRequest { /// NOTE: when making any changes to this enum please also update /// RegisteredEventFfi in crosvm_control/src/lib.rs -#[cfg(feature = "registered_events")] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)] pub enum RegisteredEvent { VirtioBalloonWsReport, @@ -1367,7 +1363,6 @@ pub enum RegisteredEvent { VirtioBalloonOOMDeflation, } -#[cfg(feature = "registered_events")] #[derive(Serialize, Deserialize, Debug)] pub enum RegisteredEventWithData { VirtioBalloonWsReport { @@ -1378,7 +1373,6 @@ pub enum RegisteredEventWithData { VirtioBalloonOOMDeflation, } -#[cfg(feature = "registered_events")] impl RegisteredEventWithData { pub fn into_event(&self) -> RegisteredEvent { match self { @@ -1388,6 +1382,7 @@ impl RegisteredEventWithData { } } + #[cfg(feature = "registered_events")] pub fn into_proto(&self) -> registered_events::RegisteredEvent { match self { Self::VirtioBalloonWsReport { @@ -2005,17 +2000,14 @@ impl VmRequest { } } } - #[cfg(feature = "registered_events")] VmRequest::RegisterListener { socket_addr: _, event: _, } => VmResponse::Ok, - #[cfg(feature = "registered_events")] VmRequest::UnregisterListener { socket_addr: _, event: _, } => VmResponse::Ok, - #[cfg(feature = "registered_events")] VmRequest::Unregister { socket_addr: _ } => VmResponse::Ok, } } diff --git a/vm_control/src/sys.rs b/vm_control/src/sys.rs index 8b868392b..29bfdb6ef 100644 --- a/vm_control/src/sys.rs +++ b/vm_control/src/sys.rs @@ -9,7 +9,6 @@ cfg_if::cfg_if! { pub use platform::{VmMemoryMappingRequest, VmMemoryMappingResponse, FsMappingRequest}; #[cfg(feature = "gpu")] pub use platform::gpu::UnixDisplayMode as DisplayMode; - pub use platform::handle_request_with_timeout; #[cfg(feature = "gpu")] pub use platform::gpu::UnixMouseMode as MouseMode; } else if #[cfg(windows)] { @@ -26,5 +25,6 @@ cfg_if::cfg_if! { } pub use platform::handle_request; +pub use platform::handle_request_with_timeout; pub use platform::prepare_shared_memory_region; pub use platform::should_prepare_memory_region; diff --git a/vm_control/src/sys/windows.rs b/vm_control/src/sys/windows.rs index d1020b7c3..04acfa727 100644 --- a/vm_control/src/sys/windows.rs +++ b/vm_control/src/sys/windows.rs @@ -8,6 +8,7 @@ pub(crate) mod gpu; use std::io::Result; use std::mem::size_of; use std::path::Path; +use std::time::Duration; use base::error; use base::named_pipes::BlockingMode; @@ -68,6 +69,19 @@ pub fn handle_request<T: AsRef<Path> + std::fmt::Debug>( } } +pub fn handle_request_with_timeout<T: AsRef<Path> + std::fmt::Debug>( + request: &VmRequest, + socket_path: T, + timeout: Option<Duration>, +) -> HandleRequestResult { + if timeout.is_none() { + handle_request(request, socket_path) + } else { + error!("handle_request_with_timeout() not implemented for Windows"); + Err(()) + } +} + /// Send the size header first and then the protbuf message. /// /// A helper function to keep communication with service consistent across crosvm code. |