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