userfaultfd/
error.rs

1use std::io;
2
3use crate::IoctlFlags;
4use nix::errno::Errno;
5use thiserror::Error;
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors for this crate.
10///
11/// Several of these errors contain an underlying `Errno` value; see
12/// [`userfaultfd(2)`](http://man7.org/linux/man-pages/man2/userfaultfd.2.html) and
13/// [`ioctl_userfaultfd(2)`](http://man7.org/linux/man-pages/man2/ioctl_userfaultfd.2.html) for more
14/// details on how to interpret these errors.
15#[derive(Debug, Error)]
16pub enum Error {
17    /// Copy ioctl failure with `errno` value.
18    #[error("Copy failed")]
19    CopyFailed(Errno),
20
21    /// Copy ioctl failure with copied length.
22    #[error("Copy partially succeeded")]
23    PartiallyCopied(usize),
24
25    /// Failure to read a full `uffd_msg` struct from the underlying file descriptor.
26    #[error("Incomplete uffd_msg; read only {read}/{expected} bytes")]
27    IncompleteMsg { read: usize, expected: usize },
28
29    /// Generic system error.
30    #[error("System error")]
31    SystemError(#[source] nix::Error),
32
33    /// End-of-file was read from the underlying file descriptor.
34    #[error("EOF when reading file descriptor")]
35    ReadEof,
36
37    /// An unrecognized event code was found in a `uffd_msg` struct.
38    #[error("Unrecognized event in uffd_msg: {0}")]
39    UnrecognizedEvent(u8),
40
41    /// Requested ioctls were not available when initializing the API.
42    #[error("Requested ioctls unsupported; supported: {0:?}")]
43    UnsupportedIoctls(IoctlFlags),
44
45    /// Zeropage ioctl failure with `errno` value.
46    #[error("Zeropage failed: {0}")]
47    ZeropageFailed(Errno),
48
49    /// Could not open /dev/userfaultfd even though it exists
50    #[error("Error accessing /dev/userfaultfd: {0}")]
51    OpenDevUserfaultfd(io::Error),
52}
53
54impl From<nix::Error> for Error {
55    fn from(e: nix::Error) -> Error {
56        Error::SystemError(e)
57    }
58}