Coverage Report

Created: 2025-12-31 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/crosvm/base/src/errno.rs
Line
Count
Source
1
// Copyright 2017 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
use std::convert::From;
6
use std::convert::TryInto;
7
use std::fmt;
8
use std::fmt::Display;
9
use std::io;
10
use std::result;
11
12
use serde::Deserialize;
13
use serde::Serialize;
14
use thiserror::Error;
15
16
/// A system error
17
/// In Unix systems, retrieved from errno (man 3 errno), set by a libc
18
/// function that returned an error.
19
/// On Windows, retrieved from GetLastError, set by a Windows function
20
/// that returned an error
21
#[derive(Error, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
22
#[serde(transparent)]
23
pub struct Error(i32);
24
pub type Result<T> = result::Result<T, Error>;
25
26
impl Error {
27
    /// Constructs a new error with the given error number.
28
671
    pub fn new<T: TryInto<i32>>(e: T) -> Error {
29
        // A value outside the bounds of an i32 will never be a valid
30
        // errno/GetLastError
31
671
        Error(e.try_into().unwrap_or_default())
32
671
    }
33
34
    /// Constructs an Error from the most recent system error.
35
    ///
36
    /// The result of this only has any meaning just after a libc/Windows call that returned
37
    /// a value indicating errno was set.
38
7.07k
    pub fn last() -> Error {
39
7.07k
        Error(io::Error::last_os_error().raw_os_error().unwrap())
40
7.07k
    }
41
42
    /// Gets the errno for this error
43
588
    pub fn errno(self) -> i32 {
44
588
        self.0
45
588
    }
46
}
47
48
impl From<io::Error> for Error {
49
128
    fn from(e: io::Error) -> Self {
50
128
        Error(e.raw_os_error().unwrap_or_default())
51
128
    }
52
}
53
54
impl From<Error> for io::Error {
55
0
    fn from(e: Error) -> io::Error {
56
0
        io::Error::from_raw_os_error(e.0)
57
0
    }
58
}
59
60
impl From<Error> for Box<dyn std::error::Error + Send> {
61
0
    fn from(e: Error) -> Self {
62
0
        Box::new(e)
63
0
    }
64
}
65
66
impl Display for Error {
67
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68
0
        Into::<io::Error>::into(*self).fmt(f)
69
0
    }
70
}
71
72
/// Returns the last errno as a Result that is always an error.
73
5.89k
pub fn errno_result<T>() -> Result<T> {
74
5.89k
    Err(Error::last())
75
5.89k
}
Unexecuted instantiation: base::errno::errno_result::<base::descriptor::SafeDescriptor>
Unexecuted instantiation: base::errno::errno_result::<base::shm::SharedMemory>
Unexecuted instantiation: base::errno::errno_result::<base::event::EventWaitResult>
Unexecuted instantiation: base::errno::errno_result::<base::timer::Timer>
Unexecuted instantiation: base::errno::errno_result::<core::time::Duration>
Unexecuted instantiation: base::errno::errno_result::<base::sys::unix::file_flags::FileFlags>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::shm::MemfdSeals>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::event::PlatformEvent>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::netlink::NetlinkGenericRead>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::netlink::NetlinkGenericSocket>
Unexecuted instantiation: base::errno::errno_result::<libc::unix::linux_like::linux::rlimit64>
Unexecuted instantiation: base::errno::errno_result::<libc::unix::linux_like::linux::gnu::b64::sigset_t>
Unexecuted instantiation: base::errno::errno_result::<(std::fs::File, std::fs::File)>
Unexecuted instantiation: base::errno::errno_result::<bool>
Unexecuted instantiation: base::errno::errno_result::<usize>
base::errno::errno_result::<i32>
Line
Count
Source
73
5.89k
pub fn errno_result<T>() -> Result<T> {
74
5.89k
    Err(Error::last())
75
5.89k
}
Unexecuted instantiation: base::errno::errno_result::<()>
Unexecuted instantiation: base::errno::errno_result::<u64>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::serial::Serial>::spawn_input_thread::{closure#0}::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::bat::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::pit::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::virtio::console::worker::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::vmwdt::Vmwdt>::vmwdt_worker_thread::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::p9::Worker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::rng::Worker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::vhost_user_frontend::worker::Worker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::vhost::worker::Worker<_>>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::pci::coiommu::PinWorker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::pci::coiommu::UnpinWorker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::pci::vfio_pci::VfioPciWorker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::fs::worker::Worker<_>>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<<devices::virtio::wl::Worker>::run::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::virtcpufreq_v2::vcpufreq_worker_thread::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::acpi::run_worker::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::cmos::run_cmos_worker::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::virtio::interrupt::interrupt_resample_thread::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<devices::virtio::console::sys::linux::read_input::Token>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<u32>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::serial::Serial>::spawn_input_thread::{closure#0}::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::bat::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::pit::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::virtio::console::worker::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::vmwdt::Vmwdt>::vmwdt_worker_thread::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::p9::Worker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::rng::Worker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::vhost_user_frontend::worker::Worker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::vhost::worker::Worker<_>>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::pci::coiommu::PinWorker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::pci::coiommu::UnpinWorker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::pci::vfio_pci::VfioPciWorker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::fs::worker::Worker<_>>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<<devices::virtio::wl::Worker>::run::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::virtcpufreq_v2::vcpufreq_worker_thread::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::acpi::run_worker::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::cmos::run_cmos_worker::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::virtio::interrupt::interrupt_resample_thread::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<devices::virtio::console::sys::linux::read_input::Token>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<u32>>
Unexecuted instantiation: base::errno::errno_result::<isize>
Unexecuted instantiation: base::errno::errno_result::<i64>
Unexecuted instantiation: base::errno::errno_result::<(core::option::Option<i32>, std::process::ExitStatus)>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<metrics::controller::MetricsControllerToken>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<metrics::controller::MetricsControllerToken>>
Unexecuted instantiation: base::errno::errno_result::<[u64; 4]>
Unexecuted instantiation: base::errno::errno_result::<alloc::vec::Vec<u32>>
Unexecuted instantiation: base::errno::errno_result::<alloc::collections::btree::map::BTreeMap<u32, u64>>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::ClockState>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::VcpuExit>
Unexecuted instantiation: base::errno::errno_result::<snapshot::any_snapshot::AnySnapshot>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::kvm::Kvm>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::kvm::KvmVm>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::kvm::KvmVcpu>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::x86_64::Fpu>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::x86_64::Regs>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::x86_64::Sregs>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::x86_64::Xsave>
Unexecuted instantiation: base::errno::errno_result::<hypervisor::x86_64::DebugRegs>
Unexecuted instantiation: base::errno::errno_result::<kvm_sys::x86::bindings::kvm_mp_state>
Unexecuted instantiation: base::errno::errno_result::<kvm_sys::x86::bindings::kvm_pic_state>
Unexecuted instantiation: base::errno::errno_result::<kvm_sys::x86::bindings::kvm_pit_state2>
Unexecuted instantiation: base::errno::errno_result::<kvm_sys::x86::bindings::kvm_lapic_state>
Unexecuted instantiation: base::errno::errno_result::<kvm_sys::x86::bindings::kvm_ioapic_state>
Unexecuted instantiation: base::errno::errno_result::<smallvec::SmallVec<[base::wait_context::TriggeredEvent<usize>; 16]>>
Unexecuted instantiation: base::errno::errno_result::<base::sys::linux::poll::EventContext<usize>>