linux_api/
errno.rs

1use crate::bindings;
2
3#[derive(Copy, Clone, PartialEq, Eq)]
4// Defined in libc as an `int`, but u16 is sufficient
5// to represent all values, and is what is used in `linux_errno`.
6//
7// We want to allow unknown values, but only up to Errno::MAX, making
8// it difficult to use an `enum` here. e.g. if we used `num_enum` with a `catch_all` `Unknown`
9// variant, we wouldn't be able to prevent construction of `Errno::Unknown(Errno::MAX.into() + 1)`.
10pub struct Errno(u16);
11
12impl TryFrom<u16> for Errno {
13    type Error = ();
14
15    fn try_from(val: u16) -> Result<Self, Self::Error> {
16        Self::from_u16(val).ok_or(())
17    }
18}
19
20impl TryFrom<u32> for Errno {
21    type Error = ();
22
23    fn try_from(val: u32) -> Result<Self, Self::Error> {
24        u16::try_from(val).ok().and_then(Self::from_u16).ok_or(())
25    }
26}
27
28impl TryFrom<u64> for Errno {
29    type Error = ();
30
31    fn try_from(val: u64) -> Result<Self, Self::Error> {
32        u16::try_from(val).ok().and_then(Self::from_u16).ok_or(())
33    }
34}
35
36impl TryFrom<i16> for Errno {
37    type Error = ();
38
39    fn try_from(val: i16) -> Result<Self, Self::Error> {
40        u16::try_from(val).ok().and_then(Self::from_u16).ok_or(())
41    }
42}
43
44impl TryFrom<i32> for Errno {
45    type Error = ();
46
47    fn try_from(val: i32) -> Result<Self, Self::Error> {
48        u16::try_from(val).ok().and_then(Self::from_u16).ok_or(())
49    }
50}
51
52impl TryFrom<i64> for Errno {
53    type Error = ();
54
55    fn try_from(val: i64) -> Result<Self, Self::Error> {
56        u16::try_from(val).ok().and_then(Self::from_u16).ok_or(())
57    }
58}
59
60impl From<Errno> for u16 {
61    #[inline]
62    fn from(val: Errno) -> u16 {
63        val.0
64    }
65}
66
67impl From<Errno> for u32 {
68    #[inline]
69    fn from(val: Errno) -> u32 {
70        val.0.into()
71    }
72}
73
74impl From<Errno> for u64 {
75    #[inline]
76    fn from(val: Errno) -> u64 {
77        val.0.into()
78    }
79}
80
81impl From<Errno> for i32 {
82    #[inline]
83    fn from(val: Errno) -> i32 {
84        val.0.into()
85    }
86}
87
88impl From<Errno> for i64 {
89    #[inline]
90    fn from(val: Errno) -> i64 {
91        val.0.into()
92    }
93}
94
95const fn errno_to_str(e: Errno) -> Option<&'static str> {
96    match e {
97        Errno::ENODEV => Some("ENODEV"),
98        Errno::EINVAL => Some("EINVAL"),
99        Errno::EDEADLK => Some("EDEADLK"),
100        Errno::ENAMETOOLONG => Some("ENAMETOOLONG"),
101        Errno::ENOLCK => Some("ENOLCK"),
102        Errno::ENOSYS => Some("ENOSYS"),
103        Errno::ENOTEMPTY => Some("ENOTEMPTY"),
104        Errno::ELOOP => Some("ELOOP"),
105        Errno::EWOULDBLOCK => Some("EWOULDBLOCK"),
106        Errno::ENOMSG => Some("ENOMSG"),
107        Errno::EIDRM => Some("EIDRM"),
108        Errno::ECHRNG => Some("ECHRNG"),
109        Errno::EL2NSYNC => Some("EL2NSYNC"),
110        Errno::EL3HLT => Some("EL3HLT"),
111        Errno::EL3RST => Some("EL3RST"),
112        Errno::ELNRNG => Some("ELNRNG"),
113        Errno::EUNATCH => Some("EUNATCH"),
114        Errno::ENOCSI => Some("ENOCSI"),
115        Errno::EL2HLT => Some("EL2HLT"),
116        Errno::EBADE => Some("EBADE"),
117        Errno::EBADR => Some("EBADR"),
118        Errno::EXFULL => Some("EXFULL"),
119        Errno::ENOANO => Some("ENOANO"),
120        Errno::EBADRQC => Some("EBADRQC"),
121        Errno::EBADSLT => Some("EBADSLT"),
122        Errno::EBFONT => Some("EBFONT"),
123        Errno::ENOSTR => Some("ENOSTR"),
124        Errno::ENODATA => Some("ENODATA"),
125        Errno::ETIME => Some("ETIME"),
126        Errno::ENOSR => Some("ENOSR"),
127        Errno::ENONET => Some("ENONET"),
128        Errno::ENOPKG => Some("ENOPKG"),
129        Errno::EREMOTE => Some("EREMOTE"),
130        Errno::ENOLINK => Some("ENOLINK"),
131        Errno::EADV => Some("EADV"),
132        Errno::ESRMNT => Some("ESRMNT"),
133        Errno::ECOMM => Some("ECOMM"),
134        Errno::EPROTO => Some("EPROTO"),
135        Errno::EMULTIHOP => Some("EMULTIHOP"),
136        Errno::EDOTDOT => Some("EDOTDOT"),
137        Errno::EBADMSG => Some("EBADMSG"),
138        Errno::EOVERFLOW => Some("EOVERFLOW"),
139        Errno::ENOTUNIQ => Some("ENOTUNIQ"),
140        Errno::EBADFD => Some("EBADFD"),
141        Errno::EREMCHG => Some("EREMCHG"),
142        Errno::ELIBACC => Some("ELIBACC"),
143        Errno::ELIBBAD => Some("ELIBBAD"),
144        Errno::ELIBSCN => Some("ELIBSCN"),
145        Errno::ELIBMAX => Some("ELIBMAX"),
146        Errno::ELIBEXEC => Some("ELIBEXEC"),
147        Errno::EILSEQ => Some("EILSEQ"),
148        Errno::ERESTART => Some("ERESTART"),
149        Errno::ESTRPIPE => Some("ESTRPIPE"),
150        Errno::EUSERS => Some("EUSERS"),
151        Errno::ENOTSOCK => Some("ENOTSOCK"),
152        Errno::EDESTADDRREQ => Some("EDESTADDRREQ"),
153        Errno::EMSGSIZE => Some("EMSGSIZE"),
154        Errno::EPROTOTYPE => Some("EPROTOTYPE"),
155        Errno::ENOPROTOOPT => Some("ENOPROTOOPT"),
156        Errno::EPROTONOSUPPORT => Some("EPROTONOSUPPORT"),
157        Errno::ESOCKTNOSUPPORT => Some("ESOCKTNOSUPPORT"),
158        Errno::EOPNOTSUPP => Some("EOPNOTSUPP"),
159        Errno::EPFNOSUPPORT => Some("EPFNOSUPPORT"),
160        Errno::EAFNOSUPPORT => Some("EAFNOSUPPORT"),
161        Errno::EADDRINUSE => Some("EADDRINUSE"),
162        Errno::EADDRNOTAVAIL => Some("EADDRNOTAVAIL"),
163        Errno::ENETDOWN => Some("ENETDOWN"),
164        Errno::ENETUNREACH => Some("ENETUNREACH"),
165        Errno::ENETRESET => Some("ENETRESET"),
166        Errno::ECONNABORTED => Some("ECONNABORTED"),
167        Errno::ECONNRESET => Some("ECONNRESET"),
168        Errno::ENOBUFS => Some("ENOBUFS"),
169        Errno::EISCONN => Some("EISCONN"),
170        Errno::ENOTCONN => Some("ENOTCONN"),
171        Errno::ESHUTDOWN => Some("ESHUTDOWN"),
172        Errno::ETOOMANYREFS => Some("ETOOMANYREFS"),
173        Errno::ETIMEDOUT => Some("ETIMEDOUT"),
174        Errno::ECONNREFUSED => Some("ECONNREFUSED"),
175        Errno::EHOSTDOWN => Some("EHOSTDOWN"),
176        Errno::EHOSTUNREACH => Some("EHOSTUNREACH"),
177        Errno::EALREADY => Some("EALREADY"),
178        Errno::EINPROGRESS => Some("EINPROGRESS"),
179        Errno::ESTALE => Some("ESTALE"),
180        Errno::EUCLEAN => Some("EUCLEAN"),
181        Errno::ENOTNAM => Some("ENOTNAM"),
182        Errno::ENAVAIL => Some("ENAVAIL"),
183        Errno::EISNAM => Some("EISNAM"),
184        Errno::EREMOTEIO => Some("EREMOTEIO"),
185        Errno::EDQUOT => Some("EDQUOT"),
186        Errno::ENOMEDIUM => Some("ENOMEDIUM"),
187        Errno::EMEDIUMTYPE => Some("EMEDIUMTYPE"),
188        Errno::ECANCELED => Some("ECANCELED"),
189        Errno::ENOKEY => Some("ENOKEY"),
190        Errno::EKEYEXPIRED => Some("EKEYEXPIRED"),
191        Errno::EKEYREVOKED => Some("EKEYREVOKED"),
192        Errno::EKEYREJECTED => Some("EKEYREJECTED"),
193        Errno::EOWNERDEAD => Some("EOWNERDEAD"),
194        Errno::ENOTRECOVERABLE => Some("ENOTRECOVERABLE"),
195        Errno::ERFKILL => Some("ERFKILL"),
196        Errno::EHWPOISON => Some("EHWPOISON"),
197        Errno::EINTR => Some("EINTR"),
198        Errno::ENFILE => Some("ENFILE"),
199        Errno::EPIPE => Some("EPIPE"),
200        Errno::ESPIPE => Some("ESPIPE"),
201        Errno::EBADF => Some("EBADF"),
202        Errno::EPERM => Some("EPERM"),
203        Errno::EFAULT => Some("EFAULT"),
204        Errno::ESRCH => Some("ESRCH"),
205        Errno::ENOENT => Some("ENOENT"),
206        Errno::ENOTTY => Some("ENOTTY"),
207        Errno::EEXIST => Some("EEXIST"),
208        Errno::ECHILD => Some("ECHILD"),
209        Errno::EACCES => Some("EACCES"),
210        Errno::ENOEXEC => Some("ENOEXEC"),
211        Errno::ENOTDIR => Some("ENOTDIR"),
212        _ => None,
213    }
214}
215
216impl core::fmt::Debug for Errno {
217    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
218        match errno_to_str(*self) {
219            Some(s) => {
220                formatter.write_str("Errno::")?;
221                formatter.write_str(s)
222            }
223            None => write!(formatter, "Errno::<{}>", u16::from(*self)),
224        }
225    }
226}
227
228impl core::fmt::Display for Errno {
229    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
230        match errno_to_str(*self) {
231            Some(s) => formatter.write_str(s),
232            None => write!(formatter, "(unknown errno {})", u16::from(*self)),
233        }
234    }
235}
236
237impl Errno {
238    pub const ENODEV: Self = Self::from_u32_const(bindings::LINUX_ENODEV);
239    pub const EINVAL: Self = Self::from_u32_const(bindings::LINUX_EINVAL);
240    pub const EDEADLK: Self = Self::from_u32_const(bindings::LINUX_EDEADLK);
241    pub const ENAMETOOLONG: Self = Self::from_u32_const(bindings::LINUX_ENAMETOOLONG);
242    pub const ENOLCK: Self = Self::from_u32_const(bindings::LINUX_ENOLCK);
243    pub const ENOSYS: Self = Self::from_u32_const(bindings::LINUX_ENOSYS);
244    pub const ENOTEMPTY: Self = Self::from_u32_const(bindings::LINUX_ENOTEMPTY);
245    pub const ELOOP: Self = Self::from_u32_const(bindings::LINUX_ELOOP);
246    pub const EWOULDBLOCK: Self = Self::from_u32_const(bindings::LINUX_EWOULDBLOCK);
247    pub const ENOMSG: Self = Self::from_u32_const(bindings::LINUX_ENOMSG);
248    pub const EIDRM: Self = Self::from_u32_const(bindings::LINUX_EIDRM);
249    pub const ECHRNG: Self = Self::from_u32_const(bindings::LINUX_ECHRNG);
250    pub const EL2NSYNC: Self = Self::from_u32_const(bindings::LINUX_EL2NSYNC);
251    pub const EL3HLT: Self = Self::from_u32_const(bindings::LINUX_EL3HLT);
252    pub const EL3RST: Self = Self::from_u32_const(bindings::LINUX_EL3RST);
253    pub const ELNRNG: Self = Self::from_u32_const(bindings::LINUX_ELNRNG);
254    pub const EUNATCH: Self = Self::from_u32_const(bindings::LINUX_EUNATCH);
255    pub const ENOCSI: Self = Self::from_u32_const(bindings::LINUX_ENOCSI);
256    pub const EL2HLT: Self = Self::from_u32_const(bindings::LINUX_EL2HLT);
257    pub const EBADE: Self = Self::from_u32_const(bindings::LINUX_EBADE);
258    pub const EBADR: Self = Self::from_u32_const(bindings::LINUX_EBADR);
259    pub const EXFULL: Self = Self::from_u32_const(bindings::LINUX_EXFULL);
260    pub const ENOANO: Self = Self::from_u32_const(bindings::LINUX_ENOANO);
261    pub const EBADRQC: Self = Self::from_u32_const(bindings::LINUX_EBADRQC);
262    pub const EBADSLT: Self = Self::from_u32_const(bindings::LINUX_EBADSLT);
263    pub const EBFONT: Self = Self::from_u32_const(bindings::LINUX_EBFONT);
264    pub const ENOSTR: Self = Self::from_u32_const(bindings::LINUX_ENOSTR);
265    pub const ENODATA: Self = Self::from_u32_const(bindings::LINUX_ENODATA);
266    pub const ETIME: Self = Self::from_u32_const(bindings::LINUX_ETIME);
267    pub const ENOSR: Self = Self::from_u32_const(bindings::LINUX_ENOSR);
268    pub const ENONET: Self = Self::from_u32_const(bindings::LINUX_ENONET);
269    pub const ENOPKG: Self = Self::from_u32_const(bindings::LINUX_ENOPKG);
270    pub const EREMOTE: Self = Self::from_u32_const(bindings::LINUX_EREMOTE);
271    pub const ENOLINK: Self = Self::from_u32_const(bindings::LINUX_ENOLINK);
272    pub const EADV: Self = Self::from_u32_const(bindings::LINUX_EADV);
273    pub const ESRMNT: Self = Self::from_u32_const(bindings::LINUX_ESRMNT);
274    pub const ECOMM: Self = Self::from_u32_const(bindings::LINUX_ECOMM);
275    pub const EPROTO: Self = Self::from_u32_const(bindings::LINUX_EPROTO);
276    pub const EMULTIHOP: Self = Self::from_u32_const(bindings::LINUX_EMULTIHOP);
277    pub const EDOTDOT: Self = Self::from_u32_const(bindings::LINUX_EDOTDOT);
278    pub const EBADMSG: Self = Self::from_u32_const(bindings::LINUX_EBADMSG);
279    pub const EOVERFLOW: Self = Self::from_u32_const(bindings::LINUX_EOVERFLOW);
280    pub const ENOTUNIQ: Self = Self::from_u32_const(bindings::LINUX_ENOTUNIQ);
281    pub const EBADFD: Self = Self::from_u32_const(bindings::LINUX_EBADFD);
282    pub const EREMCHG: Self = Self::from_u32_const(bindings::LINUX_EREMCHG);
283    pub const ELIBACC: Self = Self::from_u32_const(bindings::LINUX_ELIBACC);
284    pub const ELIBBAD: Self = Self::from_u32_const(bindings::LINUX_ELIBBAD);
285    pub const ELIBSCN: Self = Self::from_u32_const(bindings::LINUX_ELIBSCN);
286    pub const ELIBMAX: Self = Self::from_u32_const(bindings::LINUX_ELIBMAX);
287    pub const ELIBEXEC: Self = Self::from_u32_const(bindings::LINUX_ELIBEXEC);
288    pub const EILSEQ: Self = Self::from_u32_const(bindings::LINUX_EILSEQ);
289    pub const ERESTART: Self = Self::from_u32_const(bindings::LINUX_ERESTART);
290    pub const ESTRPIPE: Self = Self::from_u32_const(bindings::LINUX_ESTRPIPE);
291    pub const EUSERS: Self = Self::from_u32_const(bindings::LINUX_EUSERS);
292    pub const ENOTSOCK: Self = Self::from_u32_const(bindings::LINUX_ENOTSOCK);
293    pub const EDESTADDRREQ: Self = Self::from_u32_const(bindings::LINUX_EDESTADDRREQ);
294    pub const EMSGSIZE: Self = Self::from_u32_const(bindings::LINUX_EMSGSIZE);
295    pub const EPROTOTYPE: Self = Self::from_u32_const(bindings::LINUX_EPROTOTYPE);
296    pub const ENOPROTOOPT: Self = Self::from_u32_const(bindings::LINUX_ENOPROTOOPT);
297    pub const EPROTONOSUPPORT: Self = Self::from_u32_const(bindings::LINUX_EPROTONOSUPPORT);
298    pub const ESOCKTNOSUPPORT: Self = Self::from_u32_const(bindings::LINUX_ESOCKTNOSUPPORT);
299    pub const EOPNOTSUPP: Self = Self::from_u32_const(bindings::LINUX_EOPNOTSUPP);
300    pub const EPFNOSUPPORT: Self = Self::from_u32_const(bindings::LINUX_EPFNOSUPPORT);
301    pub const EAFNOSUPPORT: Self = Self::from_u32_const(bindings::LINUX_EAFNOSUPPORT);
302    pub const EADDRINUSE: Self = Self::from_u32_const(bindings::LINUX_EADDRINUSE);
303    pub const EADDRNOTAVAIL: Self = Self::from_u32_const(bindings::LINUX_EADDRNOTAVAIL);
304    pub const ENETDOWN: Self = Self::from_u32_const(bindings::LINUX_ENETDOWN);
305    pub const ENETUNREACH: Self = Self::from_u32_const(bindings::LINUX_ENETUNREACH);
306    pub const ENETRESET: Self = Self::from_u32_const(bindings::LINUX_ENETRESET);
307    pub const ECONNABORTED: Self = Self::from_u32_const(bindings::LINUX_ECONNABORTED);
308    pub const ECONNRESET: Self = Self::from_u32_const(bindings::LINUX_ECONNRESET);
309    pub const ENOBUFS: Self = Self::from_u32_const(bindings::LINUX_ENOBUFS);
310    pub const EISCONN: Self = Self::from_u32_const(bindings::LINUX_EISCONN);
311    pub const ENOTCONN: Self = Self::from_u32_const(bindings::LINUX_ENOTCONN);
312    pub const ESHUTDOWN: Self = Self::from_u32_const(bindings::LINUX_ESHUTDOWN);
313    pub const ETOOMANYREFS: Self = Self::from_u32_const(bindings::LINUX_ETOOMANYREFS);
314    pub const ETIMEDOUT: Self = Self::from_u32_const(bindings::LINUX_ETIMEDOUT);
315    pub const ECONNREFUSED: Self = Self::from_u32_const(bindings::LINUX_ECONNREFUSED);
316    pub const EHOSTDOWN: Self = Self::from_u32_const(bindings::LINUX_EHOSTDOWN);
317    pub const EHOSTUNREACH: Self = Self::from_u32_const(bindings::LINUX_EHOSTUNREACH);
318    pub const EALREADY: Self = Self::from_u32_const(bindings::LINUX_EALREADY);
319    pub const EINPROGRESS: Self = Self::from_u32_const(bindings::LINUX_EINPROGRESS);
320    pub const ESTALE: Self = Self::from_u32_const(bindings::LINUX_ESTALE);
321    pub const EUCLEAN: Self = Self::from_u32_const(bindings::LINUX_EUCLEAN);
322    pub const ENOTNAM: Self = Self::from_u32_const(bindings::LINUX_ENOTNAM);
323    pub const ENAVAIL: Self = Self::from_u32_const(bindings::LINUX_ENAVAIL);
324    pub const EISNAM: Self = Self::from_u32_const(bindings::LINUX_EISNAM);
325    pub const EREMOTEIO: Self = Self::from_u32_const(bindings::LINUX_EREMOTEIO);
326    pub const EDQUOT: Self = Self::from_u32_const(bindings::LINUX_EDQUOT);
327    pub const ENOMEDIUM: Self = Self::from_u32_const(bindings::LINUX_ENOMEDIUM);
328    pub const EMEDIUMTYPE: Self = Self::from_u32_const(bindings::LINUX_EMEDIUMTYPE);
329    pub const ECANCELED: Self = Self::from_u32_const(bindings::LINUX_ECANCELED);
330    pub const ENOKEY: Self = Self::from_u32_const(bindings::LINUX_ENOKEY);
331    pub const EKEYEXPIRED: Self = Self::from_u32_const(bindings::LINUX_EKEYEXPIRED);
332    pub const EKEYREVOKED: Self = Self::from_u32_const(bindings::LINUX_EKEYREVOKED);
333    pub const EKEYREJECTED: Self = Self::from_u32_const(bindings::LINUX_EKEYREJECTED);
334    pub const EOWNERDEAD: Self = Self::from_u32_const(bindings::LINUX_EOWNERDEAD);
335    pub const ENOTRECOVERABLE: Self = Self::from_u32_const(bindings::LINUX_ENOTRECOVERABLE);
336    pub const ERFKILL: Self = Self::from_u32_const(bindings::LINUX_ERFKILL);
337    pub const EHWPOISON: Self = Self::from_u32_const(bindings::LINUX_EHWPOISON);
338    pub const EINTR: Self = Self::from_u32_const(bindings::LINUX_EINTR);
339    pub const ENFILE: Self = Self::from_u32_const(bindings::LINUX_ENFILE);
340    pub const EPIPE: Self = Self::from_u32_const(bindings::LINUX_EPIPE);
341    pub const ESPIPE: Self = Self::from_u32_const(bindings::LINUX_ESPIPE);
342    pub const EBADF: Self = Self::from_u32_const(bindings::LINUX_EBADF);
343    pub const EPERM: Self = Self::from_u32_const(bindings::LINUX_EPERM);
344    pub const EFAULT: Self = Self::from_u32_const(bindings::LINUX_EFAULT);
345    pub const ESRCH: Self = Self::from_u32_const(bindings::LINUX_ESRCH);
346    pub const ENOENT: Self = Self::from_u32_const(bindings::LINUX_ENOENT);
347    pub const ENOTTY: Self = Self::from_u32_const(bindings::LINUX_ENOTTY);
348    pub const EEXIST: Self = Self::from_u32_const(bindings::LINUX_EEXIST);
349    pub const ECHILD: Self = Self::from_u32_const(bindings::LINUX_ECHILD);
350    pub const EACCES: Self = Self::from_u32_const(bindings::LINUX_EACCES);
351    pub const ENOEXEC: Self = Self::from_u32_const(bindings::LINUX_ENOEXEC);
352    pub const ENOTDIR: Self = Self::from_u32_const(bindings::LINUX_ENOTDIR);
353    // NOTE: add new entries to `errno_to_str` above
354
355    // Aliases
356    pub const EDEADLOCK: Self = Self::from_u32_const(bindings::LINUX_EDEADLOCK);
357    pub const EAGAIN: Self = Self::from_u32_const(bindings::LINUX_EAGAIN);
358    pub const ENOTSUP: Self = Self::EOPNOTSUPP;
359
360    /// From MAX_ERRNO in include/linux/err.h in kernel source. This doesn't
361    /// seem to be exposed in the installed kernel headers from which we generate bindings.
362    /// <https://github.com/torvalds/linux/blob/a4d7d701121981e3c3fe69ade376fe9f26324161/include/linux/err.h#L18>
363    pub const MAX: Self = Self(4095);
364
365    #[inline]
366    pub const fn from_u16(val: u16) -> Option<Self> {
367        const MAX: u16 = Errno::MAX.0;
368        match val {
369            1..=MAX => Some(Self(val)),
370            _ => None,
371        }
372    }
373
374    /// For C interop.
375    #[inline]
376    pub const fn to_negated_i64(self) -> i64 {
377        let val: u16 = self.0;
378        -(val as i64)
379    }
380
381    /// For C interop.
382    #[inline]
383    pub const fn to_negated_i32(self) -> i32 {
384        let val: u16 = self.0;
385        -(val as i32)
386    }
387
388    /// Primarily for checked conversion of bindings constants.
389    const fn from_u32_const(val: u32) -> Self {
390        let Some(rv) = Self::from_u16(val as u16) else {
391            panic!("Could not construct an `Errno`");
392        };
393        // check for truncation
394        assert!(rv.0 as u32 == val);
395        rv
396    }
397
398    /// Get libc's errno (global for the current thread).
399    #[cfg(feature = "libc")]
400    pub fn from_libc_errno() -> Self {
401        // SAFETY: The safety requirements for calling this function are
402        // undocumented, and I'm not aware of any.
403        let ptr = unsafe { libc::__errno_location() };
404        assert!(!ptr.is_null());
405        // SAFETY: We verified that `ptr` is non-NULL, and can only assume that
406        // libc gives us a pointer that is safe to dereference.
407        //
408        // `errno(2)` guarantees that it's thread-local, so there shouldn't be any
409        // data-races from other threads.
410        //
411        // A signal handler could run on the current thread and change the `errno`
412        // value at `*ptr`. A well-behaved handler should always restore the original
413        // value before returning (`signal-safety(7)`) which should make this
414        // sound; nonetheless we try to make the dereference "as atomic" as
415        // possible, e.g.  by directly dereferencing the pointer and not
416        // converting it to a Rust reference.
417        let raw = unsafe { *ptr };
418
419        Self::from_libc_errnum(raw)
420            .unwrap_or_else(|| panic!("Unexpected bad errno from libc: {raw}"))
421    }
422
423    /// Get a Result from the return value of a libc function that uses a
424    /// sentinel error value, and stores the errors themselves in errno.
425    #[cfg(feature = "libc")]
426    pub fn result_from_libc_errno<T>(sentinel: T, x: T) -> Result<T, Self>
427    where
428        T: Eq,
429    {
430        if x == sentinel {
431            Err(Self::from_libc_errno())
432        } else {
433            Ok(x)
434        }
435    }
436
437    /// Get a Result from a libc `errnum` return value, where 0 is used to
438    /// indicate "no error", and non-zero is an errno value. An example of such
439    /// a function is `libc::posix_spawn`.
440    ///
441    /// Panics if `errnum` is out of range. This shouldn't be the case for any `errnum` obtained
442    /// from libc APIs.
443    #[cfg(feature = "libc")]
444    pub fn result_from_libc_errnum(errnum: i32) -> Result<(), Self> {
445        if errnum == 0 {
446            Ok(())
447        } else {
448            Err(Self::from_libc_errnum(errnum)
449                .unwrap_or_else(|| panic!("errnum out of range: {errnum}")))
450        }
451    }
452
453    /// Convert from a libc error value. Returns `None` if out of range.
454    #[cfg(feature = "libc")]
455    pub fn from_libc_errnum(errnum: i32) -> Option<Self> {
456        // For now we assume that libc uses the same errnum values
457        // as the kernel. This isn't explicitly guaranteed, but we don't
458        // know of any exceptions in practice. In case it turns out not to be true
459        // we can either change this to a full explicit mapping, or handle
460        // individual known exceptions here.
461        Self::from_u16(u16::try_from(errnum).ok()?)
462    }
463}
464
465impl core::convert::From<linux_errno::Error> for Errno {
466    fn from(value: linux_errno::Error) -> Self {
467        // linux_errno::Error values should always be in range.
468        Self::try_from(value.get()).unwrap()
469    }
470}
471
472#[cfg(feature = "std")]
473impl core::convert::From<Errno> for std::io::Error {
474    fn from(e: Errno) -> Self {
475        Self::from_raw_os_error(e.into())
476    }
477}
478
479#[cfg(feature = "std")]
480impl core::convert::TryFrom<std::io::Error> for Errno {
481    type Error = std::io::Error;
482
483    fn try_from(e: std::io::Error) -> Result<Self, Self::Error> {
484        e.raw_os_error()
485            .and_then(|x| u16::try_from(x).ok())
486            .and_then(|x| x.try_into().ok())
487            .ok_or(e)
488    }
489}
490
491#[cfg(feature = "std")]
492impl std::error::Error for Errno {}