return errors from system module

This commit is contained in:
Bruce Leidl 2019-09-26 22:40:54 -04:00
parent df4ab4c0c9
commit 2b69ce2c08
5 changed files with 22 additions and 11 deletions

View File

@ -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<u32> {
}
}
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)
}

View File

@ -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,
}

View File

@ -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<io::Error> for Error {
@ -32,13 +36,19 @@ impl Display for Error {
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
io::Error::from_raw_os_error(err.0)
}
}
pub fn errno_result<T>() -> Result<T> {
Err(Error::last_os_error())
}
pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
pub fn cvt<T: IsMinusOne>(t: T) -> Result<T> {
if t.is_minus_one() {
Err(io::Error::last_os_error())
Err(Error::last_os_error())
} else {
Ok(t)
}

View File

@ -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;

View File

@ -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<u32> {
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<T>(fd: RawFd, request: c_ulong, arg: &T) -> Result<u32> {
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<T>(fd: RawFd, request: c_ulong, arg: &T) -> Result<
pub unsafe fn ioctl_with_mut_ref<T>(fd: RawFd, request: c_ulong, arg: &mut T) -> Result<u32> {
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)
}