diff --git a/src/kvm/ioctl.rs b/src/kvm/ioctl.rs index 9c7ba05..832bfde 100644 --- a/src/kvm/ioctl.rs +++ b/src/kvm/ioctl.rs @@ -6,6 +6,7 @@ use std::fmt; use crate::system::ioctl::{ioctl_with_val,ioctl_with_ref,ioctl_with_mut_ref}; use crate::vm::{Result,Error,ErrorKind}; +use crate::system; const KVMIO: u64 = 0xAE; @@ -602,9 +603,9 @@ pub fn kvm_run(cpufd: &VcpuFd) -> Result { } } -pub fn ioctl_err(ioctl_name: &'static str, e: Error) -> Error { +pub fn ioctl_err(ioctl_name: &'static str, e: system::Error) -> Error { if e.is_interrupted() { - e + Error::new(ErrorKind::Interrupted, e) } else { Error::new(ErrorKind::IoctlFailed(ioctl_name), e) } diff --git a/src/memory/mod.rs b/src/memory/mod.rs index f949c8a..2139ab3 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -31,7 +31,7 @@ pub enum Error { GbmCreateDevice(system::Error), GbmCreateBuffer(system::Error), OpenRenderNode(io::Error), - PrimeHandleToFD(VmError), + PrimeHandleToFD(system::Error), CreateBuffer(io::Error), NoDrmAllocator, } diff --git a/src/system/errno.rs b/src/system/errno.rs index 8b13c25..809bb30 100644 --- a/src/system/errno.rs +++ b/src/system/errno.rs @@ -16,6 +16,10 @@ impl Error { pub fn last_os_error() -> Error { Error(unsafe { *__errno_location() }) } + + pub fn is_interrupted(&self) -> bool { + self.0 == libc::EINTR + } } impl From for Error { @@ -32,13 +36,19 @@ impl Display for Error { } } +impl From for io::Error { + fn from(err: Error) -> io::Error { + io::Error::from_raw_os_error(err.0) + } +} + pub fn errno_result() -> Result { Err(Error::last_os_error()) } -pub fn cvt(t: T) -> io::Result { +pub fn cvt(t: T) -> Result { if t.is_minus_one() { - Err(io::Error::last_os_error()) + Err(Error::last_os_error()) } else { Ok(t) } diff --git a/src/system/filedesc.rs b/src/system/filedesc.rs index eb1457d..f6b43f1 100644 --- a/src/system/filedesc.rs +++ b/src/system/filedesc.rs @@ -1,6 +1,6 @@ use std::os::unix::io::{IntoRawFd,AsRawFd,RawFd}; use std::{mem, io}; -use crate::system::cvt; +use crate::system::errno::cvt; use std::os::raw::c_void; use libc::c_int; use std::io::SeekFrom; diff --git a/src/system/ioctl.rs b/src/system/ioctl.rs index a12ca92..bfdffc3 100644 --- a/src/system/ioctl.rs +++ b/src/system/ioctl.rs @@ -1,6 +1,6 @@ use libc::{self, c_ulong, c_void}; use std::os::unix::io::RawFd; -use crate::vm::{Error,Result}; +use crate::system::{Error,Result}; pub const IOC_SIZEBITS: u64 = 14; pub const IOC_DIRBITS: u64 = 2; @@ -26,7 +26,7 @@ macro_rules! ioc { ((($dir as u64 & $crate::system::ioctl::IOC_DIRMASK) << $crate::system::ioctl::IOC_DIRSHIFT) | (($ty as u64 & $crate::system::ioctl::IOC_TYPEMASK) << $crate::system::ioctl::IOC_TYPESHIFT) | (($nr as u64 & $crate::system::ioctl::IOC_NRMASK) << $crate::system::ioctl::IOC_NRSHIFT) | - (($sz as u64 & $crate::system::ioctl::IOC_SIZEMASK) << $crate::system::ioctl::IOC_SIZESHIFT)) as c_ulong) + (($sz as u64 & $crate::system::ioctl::IOC_SIZEMASK) << $crate::system::ioctl::IOC_SIZESHIFT)) as ::libc::c_ulong) } macro_rules! io { @@ -48,7 +48,7 @@ macro_rules! iorw { pub unsafe fn ioctl_with_val(fd: RawFd, request: c_ulong, val: c_ulong) -> Result { let ret = libc::ioctl(fd, request, val); if ret < 0 { - return Err(Error::from_last_errno()); + return Err(Error::last_os_error()); } Ok(ret as u32) } @@ -56,7 +56,7 @@ pub unsafe fn ioctl_with_val(fd: RawFd, request: c_ulong, val: c_ulong) -> Resul pub unsafe fn ioctl_with_ref(fd: RawFd, request: c_ulong, arg: &T) -> Result { let ret = libc::ioctl(fd, request, arg as *const T as *const c_void); if ret < 0 { - return Err(Error::from_last_errno()); + return Err(Error::last_os_error()); } Ok(ret as u32) } @@ -64,7 +64,7 @@ pub unsafe fn ioctl_with_ref(fd: RawFd, request: c_ulong, arg: &T) -> Result< pub unsafe fn ioctl_with_mut_ref(fd: RawFd, request: c_ulong, arg: &mut T) -> Result { let ret = libc::ioctl(fd, request, arg as *mut T as *mut c_void); if ret < 0 { - return Err(Error::from_last_errno()); + return Err(Error::last_os_error()); } Ok(ret as u32) }