| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::str::CStr;
|
| 8 |
|
| 9 | use alloc::{
|
| 10 | alloc::{AllocError, LayoutError},
|
| 11 | collections::TryReserveError,
|
| 12 | };
|
| 13 |
|
| 14 | use core::convert::From;
|
| 15 | use core::fmt;
|
| 16 | use core::num::TryFromIntError;
|
| 17 | use core::str::Utf8Error;
|
| 18 |
|
| 19 | |
| 20 | #[rustfmt::skip]
|
| 21 | pub mod code {
|
| 22 | macro_rules! declare_err {
|
| 23 | ($err:tt $(,)? $($doc:expr),+) => {
|
| 24 | $(
|
| 25 | #[doc = $doc]
|
| 26 | )*
|
| 27 | pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32));
|
| 28 | };
|
| 29 | }
|
| 30 |
|
| 31 | declare_err!(EPERM, "Operation not permitted.");
|
| 32 | declare_err!(ENOENT, "No such file or directory.");
|
| 33 | declare_err!(ESRCH, "No such process.");
|
| 34 | declare_err!(EINTR, "Interrupted system call.");
|
| 35 | declare_err!(EIO, "I/O error.");
|
| 36 | declare_err!(ENXIO, "No such device or address.");
|
| 37 | declare_err!(E2BIG, "Argument list too long.");
|
| 38 | declare_err!(ENOEXEC, "Exec format error.");
|
| 39 | declare_err!(EBADF, "Bad file number.");
|
| 40 | declare_err!(ECHILD, "Exec format error.");
|
| 41 | declare_err!(EAGAIN, "Try again.");
|
| 42 | declare_err!(ENOMEM, "Out of memory.");
|
| 43 | declare_err!(EACCES, "Permission denied.");
|
| 44 | declare_err!(EFAULT, "Bad address.");
|
| 45 | declare_err!(ENOTBLK, "Block device required.");
|
| 46 | declare_err!(EBUSY, "Device or resource busy.");
|
| 47 | declare_err!(EEXIST, "File exists.");
|
| 48 | declare_err!(EXDEV, "Cross-device link.");
|
| 49 | declare_err!(ENODEV, "No such device.");
|
| 50 | declare_err!(ENOTDIR, "Not a directory.");
|
| 51 | declare_err!(EISDIR, "Is a directory.");
|
| 52 | declare_err!(EINVAL, "Invalid argument.");
|
| 53 | declare_err!(ENFILE, "File table overflow.");
|
| 54 | declare_err!(EMFILE, "Too many open files.");
|
| 55 | declare_err!(ENOTTY, "Not a typewriter.");
|
| 56 | declare_err!(ETXTBSY, "Text file busy.");
|
| 57 | declare_err!(EFBIG, "File too large.");
|
| 58 | declare_err!(ENOSPC, "No space left on device.");
|
| 59 | declare_err!(ESPIPE, "Illegal seek.");
|
| 60 | declare_err!(EROFS, "Read-only file system.");
|
| 61 | declare_err!(EMLINK, "Too many links.");
|
| 62 | declare_err!(EPIPE, "Broken pipe.");
|
| 63 | declare_err!(EDOM, "Math argument out of domain of func.");
|
| 64 | declare_err!(ERANGE, "Math result not representable.");
|
| 65 | declare_err!(ERESTARTSYS, "Restart the system call.");
|
| 66 | declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted.");
|
| 67 | declare_err!(ERESTARTNOHAND, "Restart if no handler.");
|
| 68 | declare_err!(ENOIOCTLCMD, "No ioctl command.");
|
| 69 | declare_err!(ERESTART_RESTARTBLOCK, "Restart by calling sys_restart_syscall.");
|
| 70 | declare_err!(EPROBE_DEFER, "Driver requests probe retry.");
|
| 71 | declare_err!(EOPENSTALE, "Open found a stale dentry.");
|
| 72 | declare_err!(ENOPARAM, "Parameter not supported.");
|
| 73 | declare_err!(EBADHANDLE, "Illegal NFS file handle.");
|
| 74 | declare_err!(ENOTSYNC, "Update synchronization mismatch.");
|
| 75 | declare_err!(EBADCOOKIE, "Cookie is stale.");
|
| 76 | declare_err!(ENOTSUPP, "Operation is not supported.");
|
| 77 | declare_err!(ETOOSMALL, "Buffer or request is too small.");
|
| 78 | declare_err!(ESERVERFAULT, "An untranslatable error occurred.");
|
| 79 | declare_err!(EBADTYPE, "Type not supported by server.");
|
| 80 | declare_err!(EJUKEBOX, "Request initiated, but will not complete before timeout.");
|
| 81 | declare_err!(EIOCBQUEUED, "iocb queued, will get completion event.");
|
| 82 | declare_err!(ERECALLCONFLICT, "Conflict with recalled state.");
|
| 83 | declare_err!(ENOGRACE, "NFS file lock reclaim refused.");
|
| 84 | }
|
| 85 |
|
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 95 | pub struct Error(core::ffi::c_int);
|
| 96 |
|
| 97 | impl Error {
|
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | pub(crate) fn from_errno(errno: core::ffi::c_int) -> Error {
|
| 103 | if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
|
| 104 | |
| 105 | crate::pr_warn!(
|
| 106 | "attempted to create `Error` with out of range `errno`: {}",
|
| 107 | errno
|
| 108 | );
|
| 109 | return code::EINVAL;
|
| 110 | }
|
| 111 |
|
| 112 | |
| 113 | |
| 114 | Error(errno)
|
| 115 | }
|
| 116 |
|
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | unsafe fn from_errno_unchecked(errno: core::ffi::c_int) -> Error {
|
| 123 | |
| 124 | |
| 125 | Error(errno)
|
| 126 | }
|
| 127 |
|
| 128 | |
| 129 | pub fn to_errno(self) -> core::ffi::c_int {
|
| 130 | self.0
|
| 131 | }
|
| 132 |
|
| 133 | |
| 134 | #[allow(dead_code)]
|
| 135 | pub(crate) fn to_ptr<T>(self) -> *mut T {
|
| 136 | |
| 137 | unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ }
|
| 138 | }
|
| 139 |
|
| 140 | |
| 141 | #[cfg(not(testlib))]
|
| 142 | pub fn name(&self) -> Option<&'static CStr> {
|
| 143 | |
| 144 | let ptr = unsafe { bindings::errname(-self.0) };
|
| 145 | if ptr.is_null() {
|
| 146 | None
|
| 147 | } else {
|
| 148 | |
| 149 | Some(unsafe { CStr::from_char_ptr(ptr) })
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | #[cfg(testlib)]
|
| 159 | pub fn name(&self) -> Option<&'static CStr> {
|
| 160 | None
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | impl fmt::Debug for Error {
|
| 165 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 166 | match self.name() {
|
| 167 | |
| 168 | None => f.debug_tuple("Error").field(&-self.0).finish(),
|
| 169 | |
| 170 | Some(name) => f
|
| 171 | .debug_tuple(unsafe { core::str::from_utf8_unchecked(name) })
|
| 172 | .finish(),
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | impl From<AllocError> for Error {
|
| 178 | fn from(_: AllocError) -> Error {
|
| 179 | code::ENOMEM
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | impl From<TryFromIntError> for Error {
|
| 184 | fn from(_: TryFromIntError) -> Error {
|
| 185 | code::EINVAL
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | impl From<Utf8Error> for Error {
|
| 190 | fn from(_: Utf8Error) -> Error {
|
| 191 | code::EINVAL
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | impl From<TryReserveError> for Error {
|
| 196 | fn from(_: TryReserveError) -> Error {
|
| 197 | code::ENOMEM
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | impl From<LayoutError> for Error {
|
| 202 | fn from(_: LayoutError) -> Error {
|
| 203 | code::ENOMEM
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | impl From<core::fmt::Error> for Error {
|
| 208 | fn from(_: core::fmt::Error) -> Error {
|
| 209 | code::EINVAL
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | impl From<core::convert::Infallible> for Error {
|
| 214 | fn from(e: core::convert::Infallible) -> Error {
|
| 215 | match e {}
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | pub type Result<T = (), E = Error> = core::result::Result<T, E>;
|
| 240 |
|
| 241 | |
| 242 | |
| 243 | pub fn to_result(err: core::ffi::c_int) -> Result {
|
| 244 | if err < 0 {
|
| 245 | Err(Error::from_errno(err))
|
| 246 | } else {
|
| 247 | Ok(())
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | #[allow(dead_code)]
|
| 278 | pub(crate) fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
|
| 279 | |
| 280 | let const_ptr: *const core::ffi::c_void = ptr.cast();
|
| 281 | |
| 282 | if unsafe { bindings::IS_ERR(const_ptr) } {
|
| 283 | |
| 284 | let err = unsafe { bindings::PTR_ERR(const_ptr) };
|
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | #[allow(clippy::unnecessary_cast)]
|
| 295 | return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::c_int) });
|
| 296 | }
|
| 297 | Ok(ptr)
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | #[allow(dead_code)]
|
| 325 | pub(crate) fn from_result<T, F>(f: F) -> T
|
| 326 | where
|
| 327 | T: From<i16>,
|
| 328 | F: FnOnce() -> Result<T>,
|
| 329 | {
|
| 330 | match f() {
|
| 331 | Ok(v) => v,
|
| 332 | |
| 333 | |
| 334 | |
| 335 | Err(e) => T::from(e.to_errno() as i16),
|
| 336 | }
|
| 337 | }
|