Skip to main content

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    // Insufficient permissions
58    PermissionDenied,
59
60    // Missing file or directory (ENOENT)
61    //
62    // For request_key this could be due to a missing /sbin/request-key
63    // binary. I.e. keyutils utilities are not installed.
64    MissingFileOrDirectory,
65
66    /// Unknown - catch all, return this instead of panicing
67    Unknown(i32),
68}
69
70impl Display for KeyError {
71    #[inline(always)]
72    fn fmt(&self, f: &mut Formatter) -> Result {
73        <KeyError as Debug>::fmt(self, f)
74    }
75}
76
77#[cfg(feature = "std")]
78impl Error for KeyError {}
79
80impl KeyError {
81    /// Obtain the KeyError derived from checking errno
82    pub fn from_errno() -> KeyError {
83        match unsafe { *libc::__errno_location() } {
84            // Create Errors
85            libc::ENOENT => KeyError::MissingFileOrDirectory,
86            libc::EPERM => KeyError::PermissionDenied,
87            libc::EACCES => KeyError::AccessDenied,
88            libc::EDQUOT => KeyError::QuotaExceeded,
89            libc::EFAULT => KeyError::BadAddress,
90            libc::EINVAL => KeyError::InvalidArguments,
91            libc::EKEYEXPIRED => KeyError::KeyExpired,
92            libc::EKEYREVOKED => KeyError::KeyRevoked,
93            libc::EKEYREJECTED => KeyError::KeyRejected,
94            libc::ENOMEM => KeyError::OutOfMemory,
95            libc::ENOKEY => KeyError::KeyDoesNotExist,
96            libc::ENOTSUP => KeyError::OperationNotSupported,
97
98            // Unknown, provide error code for debugging
99            x => KeyError::Unknown(x),
100        }
101    }
102}