syscall/
error.rs

1use core::{fmt, result};
2
3#[derive(Clone, Copy, Eq, PartialEq)]
4pub struct Error {
5    pub errno: i32,
6}
7
8pub type Result<T, E = Error> = result::Result<T, E>;
9
10impl Error {
11    pub fn new(errno: i32) -> Error {
12        Error { errno }
13    }
14
15    pub fn mux(result: Result<usize>) -> usize {
16        match result {
17            Ok(value) => value,
18            Err(error) => -error.errno as usize,
19        }
20    }
21
22    pub fn demux(value: usize) -> Result<usize> {
23        let errno = -(value as i32);
24        if errno >= 1 && errno < STR_ERROR.len() as i32 {
25            Err(Error::new(errno))
26        } else {
27            Ok(value)
28        }
29    }
30
31    pub fn text(&self) -> &'static str {
32        STR_ERROR
33            .get(self.errno as usize)
34            .map(|&x| x)
35            .unwrap_or("Unknown Error")
36    }
37}
38
39impl fmt::Debug for Error {
40    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
41        f.write_str(self.text())
42    }
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
47        f.write_str(self.text())
48    }
49}
50#[cfg(feature = "std")]
51impl std::error::Error for Error {}
52
53#[cfg(feature = "std")]
54impl From<Error> for std::io::Error {
55    fn from(value: Error) -> Self {
56        std::io::Error::from_raw_os_error(value.errno)
57    }
58}
59
60pub const EPERM: i32 = 1; /* Operation not permitted */
61pub const ENOENT: i32 = 2; /* No such file or directory */
62pub const ESRCH: i32 = 3; /* No such process */
63pub const EINTR: i32 = 4; /* Interrupted system call */
64pub const EIO: i32 = 5; /* I/O error */
65pub const ENXIO: i32 = 6; /* No such device or address */
66pub const E2BIG: i32 = 7; /* Argument list too long */
67pub const ENOEXEC: i32 = 8; /* Exec format error */
68pub const EBADF: i32 = 9; /* Bad file number */
69pub const ECHILD: i32 = 10; /* No child processes */
70pub const EAGAIN: i32 = 11; /* Try again */
71pub const ENOMEM: i32 = 12; /* Out of memory */
72pub const EACCES: i32 = 13; /* Permission denied */
73pub const EFAULT: i32 = 14; /* Bad address */
74pub const ENOTBLK: i32 = 15; /* Block device required */
75pub const EBUSY: i32 = 16; /* Device or resource busy */
76pub const EEXIST: i32 = 17; /* File exists */
77pub const EXDEV: i32 = 18; /* Cross-device link */
78pub const ENODEV: i32 = 19; /* No such device */
79pub const ENOTDIR: i32 = 20; /* Not a directory */
80pub const EISDIR: i32 = 21; /* Is a directory */
81pub const EINVAL: i32 = 22; /* Invalid argument */
82pub const ENFILE: i32 = 23; /* File table overflow */
83pub const EMFILE: i32 = 24; /* Too many open files */
84pub const ENOTTY: i32 = 25; /* Not a typewriter */
85pub const ETXTBSY: i32 = 26; /* Text file busy */
86pub const EFBIG: i32 = 27; /* File too large */
87pub const ENOSPC: i32 = 28; /* No space left on device */
88pub const ESPIPE: i32 = 29; /* Illegal seek */
89pub const EROFS: i32 = 30; /* Read-only file system */
90pub const EMLINK: i32 = 31; /* Too many links */
91pub const EPIPE: i32 = 32; /* Broken pipe */
92pub const EDOM: i32 = 33; /* Math argument out of domain of func */
93pub const ERANGE: i32 = 34; /* Math result not representable */
94pub const EDEADLK: i32 = 35; /* Resource deadlock would occur */
95pub const ENAMETOOLONG: i32 = 36; /* File name too long */
96pub const ENOLCK: i32 = 37; /* No record locks available */
97pub const ENOSYS: i32 = 38; /* Function not implemented */
98pub const ENOTEMPTY: i32 = 39; /* Directory not empty */
99pub const ELOOP: i32 = 40; /* Too many symbolic links encountered */
100pub const EWOULDBLOCK: i32 = 41; /* Operation would block */
101pub const ENOMSG: i32 = 42; /* No message of desired type */
102pub const EIDRM: i32 = 43; /* Identifier removed */
103pub const ECHRNG: i32 = 44; /* Channel number out of range */
104pub const EL2NSYNC: i32 = 45; /* Level 2 not synchronized */
105pub const EL3HLT: i32 = 46; /* Level 3 halted */
106pub const EL3RST: i32 = 47; /* Level 3 reset */
107pub const ELNRNG: i32 = 48; /* Link number out of range */
108pub const EUNATCH: i32 = 49; /* Protocol driver not attached */
109pub const ENOCSI: i32 = 50; /* No CSI structure available */
110pub const EL2HLT: i32 = 51; /* Level 2 halted */
111pub const EBADE: i32 = 52; /* Invalid exchange */
112pub const EBADR: i32 = 53; /* Invalid request descriptor */
113pub const EXFULL: i32 = 54; /* Exchange full */
114pub const ENOANO: i32 = 55; /* No anode */
115pub const EBADRQC: i32 = 56; /* Invalid request code */
116pub const EBADSLT: i32 = 57; /* Invalid slot */
117pub const EDEADLOCK: i32 = 58; /* Resource deadlock would occur */
118pub const EBFONT: i32 = 59; /* Bad font file format */
119pub const ENOSTR: i32 = 60; /* Device not a stream */
120pub const ENODATA: i32 = 61; /* No data available */
121pub const ETIME: i32 = 62; /* Timer expired */
122pub const ENOSR: i32 = 63; /* Out of streams resources */
123pub const ENONET: i32 = 64; /* Machine is not on the network */
124pub const ENOPKG: i32 = 65; /* Package not installed */
125pub const EREMOTE: i32 = 66; /* Object is remote */
126pub const ENOLINK: i32 = 67; /* Link has been severed */
127pub const EADV: i32 = 68; /* Advertise error */
128pub const ESRMNT: i32 = 69; /* Srmount error */
129pub const ECOMM: i32 = 70; /* Communication error on send */
130pub const EPROTO: i32 = 71; /* Protocol error */
131pub const EMULTIHOP: i32 = 72; /* Multihop attempted */
132pub const EDOTDOT: i32 = 73; /* RFS specific error */
133pub const EBADMSG: i32 = 74; /* Not a data message */
134pub const EOVERFLOW: i32 = 75; /* Value too large for defined data type */
135pub const ENOTUNIQ: i32 = 76; /* Name not unique on network */
136pub const EBADFD: i32 = 77; /* File descriptor in bad state */
137pub const EREMCHG: i32 = 78; /* Remote address changed */
138pub const ELIBACC: i32 = 79; /* Can not access a needed shared library */
139pub const ELIBBAD: i32 = 80; /* Accessing a corrupted shared library */
140pub const ELIBSCN: i32 = 81; /* .lib section in a.out corrupted */
141pub const ELIBMAX: i32 = 82; /* Attempting to link in too many shared libraries */
142pub const ELIBEXEC: i32 = 83; /* Cannot exec a shared library directly */
143pub const EILSEQ: i32 = 84; /* Illegal byte sequence */
144pub const ERESTART: i32 = 85; /* Interrupted system call should be restarted */
145pub const ESTRPIPE: i32 = 86; /* Streams pipe error */
146pub const EUSERS: i32 = 87; /* Too many users */
147pub const ENOTSOCK: i32 = 88; /* Socket operation on non-socket */
148pub const EDESTADDRREQ: i32 = 89; /* Destination address required */
149pub const EMSGSIZE: i32 = 90; /* Message too long */
150pub const EPROTOTYPE: i32 = 91; /* Protocol wrong type for socket */
151pub const ENOPROTOOPT: i32 = 92; /* Protocol not available */
152pub const EPROTONOSUPPORT: i32 = 93; /* Protocol not supported */
153pub const ESOCKTNOSUPPORT: i32 = 94; /* Socket type not supported */
154pub const EOPNOTSUPP: i32 = 95; /* Operation not supported on transport endpoint */
155pub const EPFNOSUPPORT: i32 = 96; /* Protocol family not supported */
156pub const EAFNOSUPPORT: i32 = 97; /* Address family not supported by protocol */
157pub const EADDRINUSE: i32 = 98; /* Address already in use */
158pub const EADDRNOTAVAIL: i32 = 99; /* Cannot assign requested address */
159pub const ENETDOWN: i32 = 100; /* Network is down */
160pub const ENETUNREACH: i32 = 101; /* Network is unreachable */
161pub const ENETRESET: i32 = 102; /* Network dropped connection because of reset */
162pub const ECONNABORTED: i32 = 103; /* Software caused connection abort */
163pub const ECONNRESET: i32 = 104; /* Connection reset by peer */
164pub const ENOBUFS: i32 = 105; /* No buffer space available */
165pub const EISCONN: i32 = 106; /* Transport endpoint is already connected */
166pub const ENOTCONN: i32 = 107; /* Transport endpoint is not connected */
167pub const ESHUTDOWN: i32 = 108; /* Cannot send after transport endpoint shutdown */
168pub const ETOOMANYREFS: i32 = 109; /* Too many references: cannot splice */
169pub const ETIMEDOUT: i32 = 110; /* Connection timed out */
170pub const ECONNREFUSED: i32 = 111; /* Connection refused */
171pub const EHOSTDOWN: i32 = 112; /* Host is down */
172pub const EHOSTUNREACH: i32 = 113; /* No route to host */
173pub const EALREADY: i32 = 114; /* Operation already in progress */
174pub const EINPROGRESS: i32 = 115; /* Operation now in progress */
175pub const ESTALE: i32 = 116; /* Stale NFS file handle */
176pub const EUCLEAN: i32 = 117; /* Structure needs cleaning */
177pub const ENOTNAM: i32 = 118; /* Not a XENIX named type file */
178pub const ENAVAIL: i32 = 119; /* No XENIX semaphores available */
179pub const EISNAM: i32 = 120; /* Is a named type file */
180pub const EREMOTEIO: i32 = 121; /* Remote I/O error */
181pub const EDQUOT: i32 = 122; /* Quota exceeded */
182pub const ENOMEDIUM: i32 = 123; /* No medium found */
183pub const EMEDIUMTYPE: i32 = 124; /* Wrong medium type */
184pub const ECANCELED: i32 = 125; /* Operation Canceled */
185pub const ENOKEY: i32 = 126; /* Required key not available */
186pub const EKEYEXPIRED: i32 = 127; /* Key has expired */
187pub const EKEYREVOKED: i32 = 128; /* Key has been revoked */
188pub const EKEYREJECTED: i32 = 129; /* Key was rejected by service */
189pub const EOWNERDEAD: i32 = 130; /* Owner died */
190pub const ENOTRECOVERABLE: i32 = 131; /* State not recoverable */
191pub const ESKMSG: i32 = 132; /* Scheme-kernel message code */
192
193pub static STR_ERROR: [&'static str; 133] = [
194    "Success",
195    "Operation not permitted",
196    "No such file or directory",
197    "No such process",
198    "Interrupted system call",
199    "I/O error",
200    "No such device or address",
201    "Argument list too long",
202    "Exec format error",
203    "Bad file number",
204    "No child processes",
205    "Try again",
206    "Out of memory",
207    "Permission denied",
208    "Bad address",
209    "Block device required",
210    "Device or resource busy",
211    "File exists",
212    "Cross-device link",
213    "No such device",
214    "Not a directory",
215    "Is a directory",
216    "Invalid argument",
217    "File table overflow",
218    "Too many open files",
219    "Not a typewriter",
220    "Text file busy",
221    "File too large",
222    "No space left on device",
223    "Illegal seek",
224    "Read-only file system",
225    "Too many links",
226    "Broken pipe",
227    "Math argument out of domain of func",
228    "Math result not representable",
229    "Resource deadlock would occur",
230    "File name too long",
231    "No record locks available",
232    "Function not implemented",
233    "Directory not empty",
234    "Too many symbolic links encountered",
235    "Operation would block",
236    "No message of desired type",
237    "Identifier removed",
238    "Channel number out of range",
239    "Level 2 not synchronized",
240    "Level 3 halted",
241    "Level 3 reset",
242    "Link number out of range",
243    "Protocol driver not attached",
244    "No CSI structure available",
245    "Level 2 halted",
246    "Invalid exchange",
247    "Invalid request descriptor",
248    "Exchange full",
249    "No anode",
250    "Invalid request code",
251    "Invalid slot",
252    "Resource deadlock would occur",
253    "Bad font file format",
254    "Device not a stream",
255    "No data available",
256    "Timer expired",
257    "Out of streams resources",
258    "Machine is not on the network",
259    "Package not installed",
260    "Object is remote",
261    "Link has been severed",
262    "Advertise error",
263    "Srmount error",
264    "Communication error on send",
265    "Protocol error",
266    "Multihop attempted",
267    "RFS specific error",
268    "Not a data message",
269    "Value too large for defined data type",
270    "Name not unique on network",
271    "File descriptor in bad state",
272    "Remote address changed",
273    "Can not access a needed shared library",
274    "Accessing a corrupted shared library",
275    ".lib section in a.out corrupted",
276    "Attempting to link in too many shared libraries",
277    "Cannot exec a shared library directly",
278    "Illegal byte sequence",
279    "Interrupted system call should be restarted",
280    "Streams pipe error",
281    "Too many users",
282    "Socket operation on non-socket",
283    "Destination address required",
284    "Message too long",
285    "Protocol wrong type for socket",
286    "Protocol not available",
287    "Protocol not supported",
288    "Socket type not supported",
289    "Operation not supported on transport endpoint",
290    "Protocol family not supported",
291    "Address family not supported by protocol",
292    "Address already in use",
293    "Cannot assign requested address",
294    "Network is down",
295    "Network is unreachable",
296    "Network dropped connection because of reset",
297    "Software caused connection abort",
298    "Connection reset by peer",
299    "No buffer space available",
300    "Transport endpoint is already connected",
301    "Transport endpoint is not connected",
302    "Cannot send after transport endpoint shutdown",
303    "Too many references: cannot splice",
304    "Connection timed out",
305    "Connection refused",
306    "Host is down",
307    "No route to host",
308    "Operation already in progress",
309    "Operation now in progress",
310    "Stale NFS file handle",
311    "Structure needs cleaning",
312    "Not a XENIX named type file",
313    "No XENIX semaphores available",
314    "Is a named type file",
315    "Remote I/O error",
316    "Quota exceeded",
317    "No medium found",
318    "Wrong medium type",
319    "Operation Canceled",
320    "Required key not available",
321    "Key has expired",
322    "Key has been revoked",
323    "Key was rejected by service",
324    "Owner died",
325    "State not recoverable",
326    "Scheme-kernel message code",
327];