From 6dbc26eec38857008d13bb3a29c63fab9336fd91 Mon Sep 17 00:00:00 2001 From: Vikram Auradkar Date: Fri, 22 Apr 2022 03:49:15 +0000 Subject: [PATCH] base: Export windows ioctl interfaces Minor refactor to rename variables from 'handle' to 'descriptor' BUG=b:213153157 TEST=presubmit Change-Id: Ic4c51368f53b3dfab15a12f2bd07be7f1bc277a6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3602922 Commit-Queue: Vikram Auradkar Reviewed-by: Dennis Kempin Tested-by: kokoro --- base/src/lib.rs | 7 ++-- base/src/sys/windows/win/ioctl.rs | 55 +++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/base/src/lib.rs b/base/src/lib.rs index 45eba2e30b..0314ffc934 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -20,6 +20,10 @@ pub use alloc::LayoutAllocation; pub use errno::{errno_result, Error, Result}; pub use external_mapping::{Error as ExternalMappingError, Result as ExternalMappingResult, *}; pub use notifiers::*; +pub use platform::ioctl::{ + ioctl, ioctl_with_mut_ptr, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref, ioctl_with_val, + *, +}; pub use scoped_event_macro::*; pub use timer::{FakeTimer, Timer}; pub use tube::{Error as TubeError, RecvTube, Result as TubeResult, SendTube, Tube}; @@ -36,9 +40,6 @@ cfg_if::cfg_if! { pub use unix::net::*; pub use event::{Event, EventReadResult, ScopedEvent}; - pub use unix::ioctl::{*, - ioctl, ioctl_with_mut_ptr, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref, ioctl_with_val, - }; pub use mmap::{ MemoryMapping, MemoryMappingBuilder, MemoryMappingBuilderUnix, Unix as MemoryMappingUnix, }; diff --git a/base/src/sys/windows/win/ioctl.rs b/base/src/sys/windows/win/ioctl.rs index bf52d2107e..b7a96aa698 100644 --- a/base/src/sys/windows/win/ioctl.rs +++ b/base/src/sys/windows/win/ioctl.rs @@ -4,7 +4,12 @@ //! Macros and wrapper functions for dealing with ioctls. -use std::{mem::size_of, os::raw::*, ptr::null_mut}; +use std::{ + mem::size_of, + os::raw::*, + os::raw::{c_int, c_ulong}, + ptr::null_mut, +}; use crate::descriptor::AsRawDescriptor; pub use winapi::um::winioctl::{CTL_CODE, FILE_ANY_ACCESS, METHOD_BUFFERED}; @@ -128,12 +133,13 @@ pub type IoctlNr = c_ulong; // remove it right now until we re-implement all the code that calls // this funciton for windows. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// This method should be safe as `DeviceIoControl` will handle error cases /// and it does size checking. -pub unsafe fn ioctl(handle: &F, nr: IoctlNr) -> c_int { +pub unsafe fn ioctl(descriptor: &F, nr: IoctlNr) -> c_int { let mut byte_ret: c_ulong = 0; let ret = DeviceIoControl( - handle.as_raw_descriptor(), + descriptor.as_raw_descriptor(), nr, null_mut(), 0, @@ -150,15 +156,20 @@ pub unsafe fn ioctl(handle: &F, nr: IoctlNr) -> c_int { GetLastError() as i32 } -/// Run an ioctl with a single value argument +/// Run an ioctl with a single value argument. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// This method should be safe as `DeviceIoControl` will handle error cases /// and it does size checking. -pub unsafe fn ioctl_with_val(handle: &dyn AsRawDescriptor, nr: IoctlNr, mut arg: c_ulong) -> c_int { +pub unsafe fn ioctl_with_val( + descriptor: &dyn AsRawDescriptor, + nr: IoctlNr, + mut arg: c_ulong, +) -> c_int { let mut byte_ret: c_ulong = 0; let ret = DeviceIoControl( - handle.as_raw_descriptor(), + descriptor.as_raw_descriptor(), nr, &mut arg as *mut c_ulong as *mut c_void, size_of::() as u32, @@ -177,20 +188,22 @@ pub unsafe fn ioctl_with_val(handle: &dyn AsRawDescriptor, nr: IoctlNr, mut arg: /// Run an ioctl with an immutable reference. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// Look at `ioctl_with_ptr` comments. -pub unsafe fn ioctl_with_ref(handle: &dyn AsRawDescriptor, nr: IoctlNr, arg: &T) -> c_int { - ioctl_with_ptr(handle, nr, arg) +pub unsafe fn ioctl_with_ref(descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: &T) -> c_int { + ioctl_with_ptr(descriptor, nr, arg) } /// Run an ioctl with a mutable reference. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// Look at `ioctl_with_ptr` comments. pub unsafe fn ioctl_with_mut_ref( - handle: &dyn AsRawDescriptor, + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: &mut T, ) -> c_int { - ioctl_with_mut_ptr(handle, nr, arg) + ioctl_with_mut_ptr(descriptor, nr, arg) } /// Run an ioctl with a raw pointer, specifying the size of the buffer. @@ -198,7 +211,7 @@ pub unsafe fn ioctl_with_mut_ref( /// This method should be safe as `DeviceIoControl` will handle error cases /// and it does size checking. Also The caller should make sure `T` is valid. pub unsafe fn ioctl_with_ptr_sized( - handle: &dyn AsRawDescriptor, + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: *const T, size: usize, @@ -209,7 +222,7 @@ pub unsafe fn ioctl_with_ptr_sized( // to the input buffer. Just because it's a *const does not prevent // the unsafe call from writing to it. let ret = DeviceIoControl( - handle.as_raw_descriptor(), + descriptor.as_raw_descriptor(), nr, arg as *mut c_void, size as u32, @@ -231,25 +244,31 @@ pub unsafe fn ioctl_with_ptr_sized( /// Run an ioctl with a raw pointer. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// This method should be safe as `DeviceIoControl` will handle error cases /// and it does size checking. Also The caller should make sure `T` is valid. -pub unsafe fn ioctl_with_ptr(handle: &dyn AsRawDescriptor, nr: IoctlNr, arg: *const T) -> c_int { - ioctl_with_ptr_sized(handle, nr, arg, size_of::()) +pub unsafe fn ioctl_with_ptr( + descriptor: &dyn AsRawDescriptor, + nr: IoctlNr, + arg: *const T, +) -> c_int { + ioctl_with_ptr_sized(descriptor, nr, arg, size_of::()) } /// Run an ioctl with a mutable raw pointer. /// # Safety +/// The caller is responsible for determining the safety of the particular ioctl. /// This method should be safe as `DeviceIoControl` will handle error cases /// and it does size checking. Also The caller should make sure `T` is valid. pub unsafe fn ioctl_with_mut_ptr( - handle: &dyn AsRawDescriptor, + descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: *mut T, ) -> c_int { let mut byte_ret: c_ulong = 0; let ret = DeviceIoControl( - handle.as_raw_descriptor(), + descriptor.as_raw_descriptor(), nr, arg as *mut c_void, size_of::() as u32, @@ -272,7 +291,7 @@ pub unsafe fn ioctl_with_mut_ptr( /// for invalid paramters and takes input buffer and output buffer size /// arguments. Also The caller should make sure `T` is valid. pub unsafe fn device_io_control( - handle: &F, + descriptor: &F, nr: IoctlNr, input: &T, inputsize: u32, @@ -282,7 +301,7 @@ pub unsafe fn device_io_control( let mut byte_ret: c_ulong = 0; let ret = DeviceIoControl( - handle.as_raw_descriptor(), + descriptor.as_raw_descriptor(), nr, input as *const T as *mut c_void, inputsize,