hermit/
errno.rs

1/// Operation not permitted
2pub const EPERM: i32 = 1;
3
4/// No such file or directory
5pub const ENOENT: i32 = 2;
6
7/// No such process
8pub const ESRCH: i32 = 3;
9
10/// Interrupted system call
11pub const EINTR: i32 = 4;
12
13/// I/O error
14pub const EIO: i32 = 5;
15
16/// No such device or address
17pub const ENXIO: i32 = 6;
18
19/// Argument list too long
20pub const E2BIG: i32 = 7;
21
22/// Exec format error
23pub const ENOEXEC: i32 = 8;
24
25/// Bad file number
26pub const EBADF: i32 = 9;
27
28/// No child processes
29pub const ECHILD: i32 = 10;
30
31/// Try again
32pub const EAGAIN: i32 = 11;
33
34/// Out of memory
35pub const ENOMEM: i32 = 12;
36
37/// Permission denied
38pub const EACCES: i32 = 13;
39
40/// Bad address
41pub const EFAULT: i32 = 14;
42
43/// Block device required
44pub const ENOTBLK: i32 = 15;
45
46/// Device or resource busy
47pub const EBUSY: i32 = 16;
48
49/// File exists
50pub const EEXIST: i32 = 17;
51
52/// Cross-device link
53pub const EXDEV: i32 = 18;
54
55/// No such device
56pub const ENODEV: i32 = 19;
57
58/// Not a directory
59pub const ENOTDIR: i32 = 20;
60
61/// Is a directory
62pub const EISDIR: i32 = 21;
63
64/// Invalid argument
65pub const EINVAL: i32 = 22;
66
67/// File table overflow
68pub const ENFILE: i32 = 23;
69
70/// Too many open files
71pub const EMFILE: i32 = 24;
72
73/// Not a typewriter
74pub const ENOTTY: i32 = 25;
75
76/// Text file busy
77pub const ETXTBSY: i32 = 26;
78
79/// File too large
80pub const EFBIG: i32 = 27;
81
82/// No space left on device
83pub const ENOSPC: i32 = 28;
84
85/// Illegal seek
86pub const ESPIPE: i32 = 29;
87
88/// Read-only file system
89pub const EROFS: i32 = 30;
90
91/// Too many links
92pub const EMLINK: i32 = 31;
93
94/// Broken pipe
95pub const EPIPE: i32 = 32;
96
97/// Math argument out of domain of func
98pub const EDOM: i32 = 33;
99
100/// Math result not representable
101pub const ERANGE: i32 = 34;
102
103/// Resource deadlock would occur
104pub const EDEADLK: i32 = 35;
105
106/// File name too long
107pub const ENAMETOOLONG: i32 = 36;
108
109/// No record locks available
110pub const ENOLCK: i32 = 37;
111
112/// Function not implemented
113pub const ENOSYS: i32 = 38;
114
115/// Directory not empty
116pub const ENOTEMPTY: i32 = 39;
117
118/// Too many symbolic links encountered
119pub const ELOOP: i32 = 40;
120
121/// Operation would block
122pub const EWOULDBLOCK: i32 = EAGAIN;
123
124/// No message of desired type
125pub const ENOMSG: i32 = 42;
126
127/// Identifier removed
128pub const EIDRM: i32 = 43;
129
130/// Channel number out of range
131pub const ECHRNG: i32 = 44;
132
133/// Level 2 not synchronized
134pub const EL2NSYNC: i32 = 45;
135
136/// Level 3 halted
137pub const EL3HLT: i32 = 46;
138
139/// Level 3 reset
140pub const EL3RST: i32 = 47;
141
142/// Link number out of range
143pub const ELNRNG: i32 = 48;
144
145/// Protocol driver not attached
146pub const EUNATCH: i32 = 49;
147
148/// No CSI structure available
149pub const ENOCSI: i32 = 50;
150
151/// Level 2 halted
152pub const EL2HLT: i32 = 51;
153
154/// Invalid exchange
155pub const EBADE: i32 = 52;
156
157/// Invalid request descriptor
158pub const EBADR: i32 = 53;
159
160/// Exchange full
161pub const EXFULL: i32 = 54;
162
163/// No anode
164pub const ENOANO: i32 = 55;
165
166/// Invalid request code
167pub const EBADRQC: i32 = 56;
168
169/// Invalid slot
170pub const EBADSLT: i32 = 57;
171
172pub const EDEADLOCK: i32 = EDEADLK;
173
174/// Bad font file format
175pub const EBFONT: i32 = 59;
176
177/// Device not a stream
178pub const ENOSTR: i32 = 60;
179
180/// No data available
181pub const ENODATA: i32 = 61;
182
183/// Timer expired
184pub const ETIME: i32 = 62;
185
186/// Out of streams resources
187pub const ENOSR: i32 = 63;
188
189/// Machine is not on the network
190pub const ENONET: i32 = 64;
191
192/// Package not installed
193pub const ENOPKG: i32 = 65;
194
195/// Object is remote
196pub const EREMOTE: i32 = 66;
197
198/// Link has been severed
199pub const ENOLINK: i32 = 67;
200
201/// Advertise error
202pub const EADV: i32 = 68;
203
204/// Srmount error
205pub const ESRMNT: i32 = 69;
206
207/// Communication error on send
208pub const ECOMM: i32 = 70;
209
210/// Protocol error
211pub const EPROTO: i32 = 71;
212
213/// Multihop attempted
214pub const EMULTIHOP: i32 = 72;
215
216/// RFS specific error
217pub const EDOTDOT: i32 = 73;
218
219/// Not a data message
220pub const EBADMSG: i32 = 74;
221
222/// Value too large for defined data type
223pub const EOVERFLOW: i32 = 75;
224
225/// Name not unique on network
226pub const ENOTUNIQ: i32 = 76;
227
228/// File descriptor in bad state
229pub const EBADFD: i32 = 77;
230
231/// Remote address changed
232pub const EREMCHG: i32 = 78;
233
234/// Can not access a needed shared library
235pub const ELIBACC: i32 = 79;
236
237/// Accessing a corrupted shared library
238pub const ELIBBAD: i32 = 80;
239
240/// .lib section in a.out corrupted
241pub const ELIBSCN: i32 = 81;
242
243/// Attempting to link in too many shared libraries
244pub const ELIBMAX: i32 = 82;
245
246/// Cannot exec a shared library directly
247pub const ELIBEXEC: i32 = 83;
248
249/// Illegal byte sequence
250pub const EILSEQ: i32 = 84;
251
252/// Interrupted system call should be restarted
253pub const ERESTART: i32 = 85;
254
255/// Streams pipe error
256pub const ESTRPIPE: i32 = 86;
257
258/// Too many users
259pub const EUSERS: i32 = 87;
260
261/// Socket operation on non-socket
262pub const ENOTSOCK: i32 = 88;
263
264/// Destination address required
265pub const EDESTADDRREQ: i32 = 89;
266
267/// Message too long
268pub const EMSGSIZE: i32 = 90;
269
270/// Protocol wrong type for socket
271pub const EPROTOTYPE: i32 = 91;
272
273/// Protocol not available
274pub const ENOPROTOOPT: i32 = 92;
275
276/// Protocol not supported
277pub const EPROTONOSUPPORT: i32 = 93;
278
279/// Socket type not supported
280pub const ESOCKTNOSUPPORT: i32 = 94;
281
282/// Operation not supported on transport endpoint
283pub const EOPNOTSUPP: i32 = 95;
284
285/// Protocol family not supported
286pub const EPFNOSUPPORT: i32 = 96;
287
288/// Address family not supported by protocol
289pub const EAFNOSUPPORT: i32 = 97;
290
291/// Address already in use
292pub const EADDRINUSE: i32 = 98;
293
294/// Cannot assign requested address
295pub const EADDRNOTAVAIL: i32 = 99;
296
297/// Network is down
298pub const ENETDOWN: i32 = 100;
299
300/// Network is unreachable
301pub const ENETUNREACH: i32 = 101;
302
303/// Network dropped connection because of reset
304pub const ENETRESET: i32 = 102;
305
306/// Software caused connection abort
307pub const ECONNABORTED: i32 = 103;
308
309/// Connection reset by peer
310pub const ECONNRESET: i32 = 104;
311
312/// No buffer space available
313pub const ENOBUFS: i32 = 105;
314
315/// Transport endpoint is already connected
316pub const EISCONN: i32 = 106;
317
318/// Transport endpoint is not connected
319pub const ENOTCONN: i32 = 107;
320
321/// Cannot send after transport endpoint shutdown
322pub const ESHUTDOWN: i32 = 108;
323
324/// Too many references: cannot splice
325pub const ETOOMANYREFS: i32 = 109;
326
327/// Connection timed out
328pub const ETIMEDOUT: i32 = 110;
329
330/// Connection refused
331pub const ECONNREFUSED: i32 = 111;
332
333/// Host is down
334pub const EHOSTDOWN: i32 = 112;
335
336/// No route to host
337pub const EHOSTUNREACH: i32 = 113;
338
339/// Operation already in progress
340pub const EALREADY: i32 = 114;
341
342/// Operation now in progress
343pub const EINPROGRESS: i32 = 115;
344
345/// Stale file handle
346pub const ESTALE: i32 = 116;
347
348/// Structure needs cleaning
349pub const EUCLEAN: i32 = 117;
350
351/// Not a XENIX named type file
352pub const ENOTNAM: i32 = 118;
353
354/// No XENIX semaphores available
355pub const ENAVAIL: i32 = 119;
356
357/// Is a named type file
358pub const EISNAM: i32 = 120;
359
360/// Remote I/O error
361pub const EREMOTEIO: i32 = 121;
362
363/// Quota exceeded
364pub const EDQUOT: i32 = 122;
365
366/// No medium found
367pub const ENOMEDIUM: i32 = 123;
368
369/// Wrong medium type
370pub const EMEDIUMTYPE: i32 = 124;
371
372/// Operation Canceled
373pub const ECANCELED: i32 = 125;
374
375/// Required key not available
376pub const ENOKEY: i32 = 126;
377
378/// Key has expired
379pub const EKEYEXPIRED: i32 = 127;
380
381/// Key has been revoked
382pub const EKEYREVOKED: i32 = 128;
383
384/// Key was rejected by service
385pub const EKEYREJECTED: i32 = 129;
386
387/// Robust mutexes: Owner died
388pub const EOWNERDEAD: i32 = 130;
389
390/// Robust mutexes: State not recoverable
391pub const ENOTRECOVERABLE: i32 = 131;
392
393/// Robust mutexes: Operation not possible due to RF-kill
394pub const ERFKILL: i32 = 132;
395
396/// Robust mutexes: Memory page has hardware error
397pub const EHWPOISON: i32 = 133;
398
399/// Returns the pointer to `errno`.
400#[cfg(all(
401	not(any(feature = "common-os", feature = "nostd")),
402	not(target_arch = "riscv64")
403))]
404#[unsafe(no_mangle)]
405#[linkage = "weak"]
406pub extern "C" fn sys_errno_location() -> *mut i32 {
407	use core::cell::UnsafeCell;
408
409	#[thread_local]
410	static ERRNO: UnsafeCell<i32> = UnsafeCell::new(0);
411
412	ERRNO.get()
413}
414
415/// Get the error number from the thread local storage
416///
417/// Soft-deprecated in favor of using `sys_errno_location`.
418#[cfg(not(feature = "nostd"))]
419#[unsafe(no_mangle)]
420pub extern "C" fn sys_get_errno() -> i32 {
421	sys_errno()
422}
423
424/// Get the error number from the thread local storage
425///
426/// Soft-deprecated in favor of using `sys_errno_location`.
427#[cfg(not(feature = "nostd"))]
428#[unsafe(no_mangle)]
429pub extern "C" fn sys_errno() -> i32 {
430	cfg_if::cfg_if! {
431		if #[cfg(any(feature = "common-os", target_arch = "riscv64"))] {
432			0
433		} else {
434			unsafe { *sys_errno_location() }
435		}
436	}
437}
438
439pub(crate) trait ToErrno {
440	fn to_errno(&self) -> Option<i32> {
441		None
442	}
443
444	fn set_errno(self) -> Self
445	where
446		Self: Sized,
447	{
448		if let Some(errno) = self.to_errno() {
449			cfg_if::cfg_if! {
450				if #[cfg(any(feature = "common-os", feature = "nostd", target_arch = "riscv64"))] {
451					let _ = errno;
452				} else {
453					unsafe {
454						*sys_errno_location() = errno;
455					}
456				}
457			}
458		}
459		self
460	}
461}
462
463impl ToErrno for i32 {
464	fn to_errno(&self) -> Option<i32> {
465		(*self < 0).then_some(-self)
466	}
467}
468
469impl ToErrno for i64 {
470	fn to_errno(&self) -> Option<i32> {
471		(*self < 0).then(|| i32::try_from(-self).unwrap())
472	}
473}
474
475impl ToErrno for isize {
476	fn to_errno(&self) -> Option<i32> {
477		(*self < 0).then(|| i32::try_from(-self).unwrap())
478	}
479}
480
481impl ToErrno for u8 {}
482impl ToErrno for u16 {}
483impl ToErrno for u32 {}
484impl ToErrno for usize {}
485impl ToErrno for *mut u8 {}
486impl ToErrno for () {}
487impl ToErrno for ! {}