hierr/
unix.rs

1use super::format_message_utf8;
2use core::slice;
3
4extern "C" {
5    #[cfg(any(
6        target_os = "linux",
7        target_os = "redox",
8        target_os = "dragonfly",
9        target_os = "fuchsia"
10    ))]
11    #[link_name = "__errno_location"]
12    fn errno_location() -> *mut i32;
13
14    #[cfg(any(target_os = "android", target_os = "netbsd", target_os = "openbsd"))]
15    #[link_name = "__errno"]
16    fn errno_location() -> *mut i32;
17
18    #[cfg(any(target_os = "ios", target_os = "macos", target_os = "freebsd"))]
19    #[link_name = "__error"]
20    fn errno_location() -> *mut i32;
21
22    #[cfg(any(target_os = "illumos", target_os = "solaris"))]
23    #[link_name = "___errno"]
24    fn errno_location() -> *mut i32;
25
26    #[cfg(target_os = "haiku")]
27    #[link_name = "_errnop"]
28    fn errno_location() -> *mut i32;
29
30    // mingw
31    #[cfg(all(target_os = "windows", target_env = "gnu"))]
32    #[link_name = "_errno"]
33    fn errno_location() -> *mut i32;
34
35    fn strerror(errno: i32) -> *const u8;
36    fn strlen(s: *const u8) -> usize;
37}
38
39pub fn errno() -> i32 {
40    // # Safety
41    // errno_location总是返回有效地址
42    unsafe { *errno_location() }
43}
44
45pub fn set_errno(errno: i32) {
46    // # Safety
47    // errno_location总是返回有效地址
48    unsafe { *errno_location() = errno };
49}
50
51pub fn errmsg(errno: i32, msg: &mut [u8]) -> &str {
52    let s = unsafe { strerror(errno) };
53    let len = unsafe { strlen(s) };
54    let info = unsafe { slice::from_raw_parts(s, len) };
55    return format_message_utf8(info, msg);
56}
57
58#[cfg(unix)]
59mod errors {
60    pub const EPERM: i32 = 1; /* Operation not permitted */
61    pub const ENOENT: i32 = 2; /* No such file or directory */
62    pub const ESRCH: i32 = 3; /* No such process */
63    pub const EINTR: i32 = 4; /* Interrupted system call */
64    pub const EIO: i32 = 5; /* I/O error */
65    pub const ENXIO: i32 = 6; /* No such device or address */
66    pub const E2BIG: i32 = 7; /* Argument list too long */
67    pub const ENOEXEC: i32 = 8; /* Exec format error */
68    pub const EBADF: i32 = 9; /* Bad file number */
69    pub const ECHILD: i32 = 10; /* No child processes */
70    pub const EAGAIN: i32 = 11; /* Try again */
71    pub const ENOMEM: i32 = 12; /* Out of memory */
72    pub const EACCES: i32 = 13; /* Permission denied */
73    pub const EFAULT: i32 = 14; /* Bad address */
74    pub const ENOTBLK: i32 = 15; /* Block device required */
75    pub const EBUSY: i32 = 16; /* Device or resource busy */
76    pub const EEXIST: i32 = 17; /* File exists */
77    pub const EXDEV: i32 = 18; /* Cross-device link */
78    pub const ENODEV: i32 = 19; /* No such device */
79    pub const ENOTDIR: i32 = 20; /* Not a directory */
80    pub const EISDIR: i32 = 21; /* Is a directory */
81    pub const EINVAL: i32 = 22; /* Invalid argument */
82    pub const ENFILE: i32 = 23; /* File table overflow */
83    pub const EMFILE: i32 = 24; /* Too many open files */
84    pub const ENOTTY: i32 = 25; /* Not a typewriter */
85    pub const ETXTBSY: i32 = 26; /* Text file busy */
86    pub const EFBIG: i32 = 27; /* File too large */
87    pub const ENOSPC: i32 = 28; /* No space left on device */
88    pub const ESPIPE: i32 = 29; /* Illegal seek */
89    pub const EROFS: i32 = 30; /* Read-only file system */
90    pub const EMLINK: i32 = 31; /* Too many links */
91    pub const EPIPE: i32 = 32; /* Broken pipe */
92    pub const EDOM: i32 = 33; /* Math argument out of domain of func */
93    pub const ERANGE: i32 = 34; /* Math result not representable */
94    pub const EDEADLK: i32 = 35; /* Resource deadlock would occur */
95    pub const ENAMETOOLONG: i32 = 36; /* File name too long */
96    pub const ENOLCK: i32 = 37; /* No record locks available */
97
98    /*
99     * This error code is special: arch syscall entry code will return
100     * -ENOSYS if users try to call a syscall that doesn't exist.  To keep
101     * failures of syscalls that really do exist distinguishable from
102     * failures due to attempts to use a nonexistent syscall, syscall
103     * implementations should refrain from returning -ENOSYS.
104     */
105    pub const ENOSYS: i32 = 38; /* Invalid system call number */
106
107    pub const ENOTEMPTY: i32 = 39; /* Directory not empty */
108    pub const ELOOP: i32 = 40; /* Too many symbolic links encountered */
109    pub const EWOULDBLOCK: i32 = EAGAIN; /* Operation would block */
110    pub const ENOMSG: i32 = 42; /* No message of desired type */
111    pub const EIDRM: i32 = 43; /* Identifier removed */
112    pub const ECHRNG: i32 = 44; /* Channel number out of range */
113    pub const EL2NSYNC: i32 = 45; /* Level 2 not synchronized */
114    pub const EL3HLT: i32 = 46; /* Level 3 halted */
115    pub const EL3RST: i32 = 47; /* Level 3 reset */
116    pub const ELNRNG: i32 = 48; /* Link number out of range */
117    pub const EUNATCH: i32 = 49; /* Protocol driver not attached */
118    pub const ENOCSI: i32 = 50; /* No CSI structure available */
119    pub const EL2HLT: i32 = 51; /* Level 2 halted */
120    pub const EBADE: i32 = 52; /* Invalid exchange */
121    pub const EBADR: i32 = 53; /* Invalid request descriptor */
122    pub const EXFULL: i32 = 54; /* Exchange full */
123    pub const ENOANO: i32 = 55; /* No anode */
124    pub const EBADRQC: i32 = 56; /* Invalid request code */
125    pub const EBADSLT: i32 = 57; /* Invalid slot */
126
127    pub const EDEADLOCK: i32 = EDEADLK;
128
129    pub const EBFONT: i32 = 59; /* Bad font file format */
130    pub const ENOSTR: i32 = 60; /* Device not a stream */
131    pub const ENODATA: i32 = 61; /* No data available */
132    pub const ETIME: i32 = 62; /* Timer expired */
133    pub const ENOSR: i32 = 63; /* Out of streams resources */
134    pub const ENONET: i32 = 64; /* Machine is not on the network */
135    pub const ENOPKG: i32 = 65; /* Package not installed */
136    pub const EREMOTE: i32 = 66; /* Object is remote */
137    pub const ENOLINK: i32 = 67; /* Link has been severed */
138    pub const EADV: i32 = 68; /* Advertise error */
139    pub const ESRMNT: i32 = 69; /* Srmount error */
140    pub const ECOMM: i32 = 70; /* Communication error on send */
141    pub const EPROTO: i32 = 71; /* Protocol error */
142    pub const EMULTIHOP: i32 = 72; /* Multihop attempted */
143    pub const EDOTDOT: i32 = 73; /* RFS specific error */
144    pub const EBADMSG: i32 = 74; /* Not a data message */
145    pub const EOVERFLOW: i32 = 75; /* Value too large for defined data type */
146    pub const ENOTUNIQ: i32 = 76; /* Name not unique on network */
147    pub const EBADFD: i32 = 77; /* File descriptor in bad state */
148    pub const EREMCHG: i32 = 78; /* Remote address changed */
149    pub const ELIBACC: i32 = 79; /* Can not access a needed shared library */
150    pub const ELIBBAD: i32 = 80; /* Accessing a corrupted shared library */
151    pub const ELIBSCN: i32 = 81; /* .lib section in a.out corrupted */
152    pub const ELIBMAX: i32 = 82; /* Attempting to link in too many shared libraries */
153    pub const ELIBEXEC: i32 = 83; /* Cannot exec a shared library directly */
154    pub const EILSEQ: i32 = 84; /* Illegal byte sequence */
155    pub const ERESTART: i32 = 85; /* Interrupted system call should be restarted */
156    pub const ESTRPIPE: i32 = 86; /* Streams pipe error */
157    pub const EUSERS: i32 = 87; /* Too many users */
158    pub const ENOTSOCK: i32 = 88; /* Socket operation on non-socket */
159    pub const EDESTADDRREQ: i32 = 89; /* Destination address required */
160    pub const EMSGSIZE: i32 = 90; /* Message too long */
161    pub const EPROTOTYPE: i32 = 91; /* Protocol wrong type for socket */
162    pub const ENOPROTOOPT: i32 = 92; /* Protocol not available */
163    pub const EPROTONOSUPPORT: i32 = 93; /* Protocol not supported */
164    pub const ESOCKTNOSUPPORT: i32 = 94; /* Socket type not supported */
165    pub const EOPNOTSUPP: i32 = 95; /* Operation not supported on transport endpoint */
166    pub const EPFNOSUPPORT: i32 = 96; /* Protocol family not supported */
167    pub const EAFNOSUPPORT: i32 = 97; /* Address family not supported by protocol */
168    pub const EADDRINUSE: i32 = 98; /* Address already in use */
169    pub const EADDRNOTAVAIL: i32 = 99; /* Cannot assign requested address */
170    pub const ENETDOWN: i32 = 100; /* Network is down */
171    pub const ENETUNREACH: i32 = 101; /* Network is unreachable */
172    pub const ENETRESET: i32 = 102; /* Network dropped connection because of reset */
173    pub const ECONNABORTED: i32 = 103; /* Software caused connection abort */
174    pub const ECONNRESET: i32 = 104; /* Connection reset by peer */
175    pub const ENOBUFS: i32 = 105; /* No buffer space available */
176    pub const EISCONN: i32 = 106; /* Transport endpoint is already connected */
177    pub const ENOTCONN: i32 = 107; /* Transport endpoint is not connected */
178    pub const ESHUTDOWN: i32 = 108; /* Cannot send after transport endpoint shutdown */
179    pub const ETOOMANYREFS: i32 = 109; /* Too many references: cannot splice */
180    pub const ETIMEDOUT: i32 = 110; /* Connection timed out */
181    pub const ECONNREFUSED: i32 = 111; /* Connection refused */
182    pub const EHOSTDOWN: i32 = 112; /* Host is down */
183    pub const EHOSTUNREACH: i32 = 113; /* No route to host */
184    pub const EALREADY: i32 = 114; /* Operation already in progress */
185    pub const EINPROGRESS: i32 = 115; /* Operation now in progress */
186    pub const ESTALE: i32 = 116; /* Stale file handle */
187    pub const EUCLEAN: i32 = 117; /* Structure needs cleaning */
188    pub const ENOTNAM: i32 = 118; /* Not a XENIX named type file */
189    pub const ENAVAIL: i32 = 119; /* No XENIX semaphores available */
190    pub const EISNAM: i32 = 120; /* Is a named type file */
191    pub const EREMOTEIO: i32 = 121; /* Remote I/O error */
192    pub const EDQUOT: i32 = 122; /* Quota exceeded */
193    pub const ENOMEDIUM: i32 = 123; /* No medium found */
194    pub const EMEDIUMTYPE: i32 = 124; /* Wrong medium type */
195    pub const ECANCELED: i32 = 125; /* Operation Canceled */
196    pub const ENOKEY: i32 = 126; /* Required key not available */
197    pub const EKEYEXPIRED: i32 = 127; /* Key has expired */
198    pub const EKEYREVOKED: i32 = 128; /* Key has been revoked */
199    pub const EKEYREJECTED: i32 = 129; /* Key was rejected by service */
200
201    /* for robust mutexes */
202    pub const EOWNERDEAD: i32 = 130; /* Owner died */
203    pub const ENOTRECOVERABLE: i32 = 131; /* State not recoverable */
204
205    pub const ERFKILL: i32 = 132; /* Operation not possible due to RF-kill */
206
207    pub const EHWPOISON: i32 = 133; /* Memory page has hardware error */
208}
209
210#[cfg(all(target_os = "windows", target_env = "gnu"))]
211mod errors {
212    pub const EPERM: i32 = 1;
213    pub const ENOENT: i32 = 2;
214    pub const ENOFILE: i32 = ENOENT;
215    pub const ESRCH: i32 = 3;
216    pub const EINTR: i32 = 4;
217    pub const EIO: i32 = 5;
218    pub const ENXIO: i32 = 6;
219    pub const E2BIG: i32 = 7;
220    pub const ENOEXEC: i32 = 8;
221    pub const EBADF: i32 = 9;
222    pub const ECHILD: i32 = 10;
223    pub const EAGAIN: i32 = 11;
224    pub const ENOMEM: i32 = 12;
225    pub const EACCES: i32 = 13;
226    pub const EFAULT: i32 = 14;
227    pub const EBUSY: i32 = 16;
228    pub const EEXIST: i32 = 17;
229    pub const EXDEV: i32 = 18;
230    pub const ENODEV: i32 = 19;
231    pub const ENOTDIR: i32 = 20;
232    pub const EISDIR: i32 = 21;
233    pub const ENFILE: i32 = 23;
234    pub const EMFILE: i32 = 24;
235    pub const ENOTTY: i32 = 25;
236    pub const EFBIG: i32 = 27;
237    pub const ENOSPC: i32 = 28;
238    pub const ESPIPE: i32 = 29;
239    pub const EROFS: i32 = 30;
240    pub const EMLINK: i32 = 31;
241    pub const EPIPE: i32 = 32;
242    pub const EDOM: i32 = 33;
243    pub const EDEADLK: i32 = 36;
244    pub const EDEADLOCK: i32 = EDEADLK;
245    pub const ENAMETOOLONG: i32 = 38;
246    pub const ENOLCK: i32 = 39;
247    pub const ENOSYS: i32 = 40;
248    pub const ENOTEMPTY: i32 = 41;
249    pub const EINVAL: i32 = 22;
250    pub const ERANGE: i32 = 34;
251    pub const EILSEQ: i32 = 42;
252    pub const ENOTSUP: i32 = 129;
253    pub const EAFNOSUPPORT: i32 = 102;
254    pub const EADDRINUSE: i32 = 100;
255    pub const EADDRNOTAVAIL: i32 = 101;
256    pub const EISCONN: i32 = 113;
257    pub const ENOBUFS: i32 = 119;
258    pub const ECONNABORTED: i32 = 106;
259    pub const EALREADY: i32 = 103;
260    pub const ECONNREFUSED: i32 = 107;
261    pub const ECONNRESET: i32 = 108;
262    pub const EDESTADDRREQ: i32 = 109;
263    pub const EHOSTUNREACH: i32 = 110;
264    pub const EMSGSIZE: i32 = 115;
265    pub const ENETDOWN: i32 = 116;
266    pub const ENETRESET: i32 = 117;
267    pub const ENETUNREACH: i32 = 118;
268    pub const ENOPROTOOPT: i32 = 123;
269    pub const ENOTSOCK: i32 = 128;
270    pub const ENOTCONN: i32 = 126;
271    pub const ECANCELED: i32 = 105;
272    pub const EINPROGRESS: i32 = 112;
273    pub const EOPNOTSUPP: i32 = 130;
274    pub const EWOULDBLOCK: i32 = 140;
275    pub const EOWNERDEAD: i32 = 133;
276    pub const EPROTO: i32 = 134;
277    pub const EPROTONOSUPPORT: i32 = 135;
278    pub const EBADMSG: i32 = 104;
279    pub const EIDRM: i32 = 111;
280    pub const ENODATA: i32 = 120;
281    pub const ENOLINK: i32 = 121;
282    pub const ENOMSG: i32 = 122;
283    pub const ENOSR: i32 = 124;
284    pub const ENOSTR: i32 = 125;
285    pub const ENOTRECOVERABLE: i32 = 127;
286    pub const ETIME: i32 = 137;
287    pub const ETXTBSY: i32 = 139;
288    pub const ETIMEDOUT: i32 = 138;
289    pub const ELOOP: i32 = 114;
290    pub const EPROTOTYPE: i32 = 136;
291    pub const EOVERFLOW: i32 = 132;
292}
293
294pub use errors::*;