linux_keyutils/
errors.rs

1use core::fmt::Debug;
2use core::fmt::Display;
3use core::fmt::Formatter;
4use core::fmt::Result;
5
6#[cfg(feature = "std")]
7use std::error::Error;
8
9/// Error type for this library, optionally implements `std::error::Error`.
10#[allow(dead_code)]
11#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum KeyError {
13    /// The keyring wasn't available for modification by the user.
14    AccessDenied,
15
16    /// The key quota for this user would be exceeded by creating
17    /// this key or linking it to the keyring.
18    QuotaExceeded,
19
20    /// One or more of type, description, and payload points outside
21    /// process's accessible address space.
22    BadAddress,
23
24    /// Provided bad arguments
25    InvalidArguments,
26
27    /// The keyring has expired.
28    KeyExpired,
29
30    /// The keyring has been revoked.
31    KeyRevoked,
32
33    /// The attempt to generate a new key was rejected.
34    KeyRejected,
35
36    /// The keyring doesn't exist.
37    KeyringDoesNotExist,
38
39    /// They key does not exist
40    KeyDoesNotExist,
41
42    /// Insufficient memory to create a key.
43    OutOfMemory,
44
45    /// Invalid Description
46    InvalidDescription,
47
48    /// An invalid identifier was returned
49    InvalidIdentifier,
50
51    /// Operation not supported
52    OperationNotSupported,
53
54    /// Write to destination failed
55    WriteError,
56
57    /// Unknown - catch all, return this instead of panicing
58    Unknown(i32),
59}
60
61impl Display for KeyError {
62    #[inline(always)]
63    fn fmt(&self, f: &mut Formatter) -> Result {
64        <KeyError as Debug>::fmt(self, f)
65    }
66}
67
68#[cfg(feature = "std")]
69impl Error for KeyError {}
70
71impl KeyError {
72    /// Obtain the KeyError derived from checking errno
73    pub fn from_errno() -> KeyError {
74        match unsafe { *libc::__errno_location() } {
75            // Create Errors
76            libc::EACCES => KeyError::AccessDenied,
77            libc::EDQUOT => KeyError::QuotaExceeded,
78            libc::EFAULT => KeyError::BadAddress,
79            libc::EINVAL => KeyError::InvalidArguments,
80            libc::EKEYEXPIRED => KeyError::KeyExpired,
81            libc::EKEYREVOKED => KeyError::KeyRevoked,
82            libc::EKEYREJECTED => KeyError::KeyRejected,
83            libc::ENOMEM => KeyError::OutOfMemory,
84            libc::ENOKEY => KeyError::KeyDoesNotExist,
85            libc::ENOTSUP => KeyError::OperationNotSupported,
86
87            // Unknown, provide error code for debugging
88            x => KeyError::Unknown(x),
89        }
90    }
91}