Skip to main content

dope_sys/kernel/
mod.rs

1use std::{fmt, io::stderr, marker, mem, num, os::fd::AsFd as _, process};
2
3use crate::io::fd;
4
5pub use returned::Returned;
6pub use source::Source;
7
8pub mod errno;
9pub mod raw;
10
11mod returned;
12mod source;
13
14/// Exact value observed in the thread-local errno slot after a rejected syscall.
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16#[repr(transparent)]
17pub struct Errno<O>(crate::Errno, marker::PhantomData<fn() -> O>);
18
19impl<O> Errno<O> {
20    pub const fn code(&self) -> num::NonZero<i32> {
21        self.0.code()
22    }
23
24    pub const fn is_code(self, code: crate::Errno) -> bool {
25        self.0.code().get() == code.code().get()
26    }
27}
28
29impl<O> From<crate::Errno> for Errno<O> {
30    fn from(code: crate::Errno) -> Self {
31        Self(code, marker::PhantomData)
32    }
33}
34
35impl<O> From<Errno<O>> for crate::Errno {
36    fn from(error: Errno<O>) -> Self {
37        error.0
38    }
39}
40
41/// The exact scalar rejected by one native result domain.
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub struct Invalid {
44    pub actual: libc::ssize_t,
45    pub minimum: libc::ssize_t,
46    pub maximum: libc::ssize_t,
47}
48
49const _: () = {
50    assert!(mem::size_of::<Errno<()>>() == mem::size_of::<i32>());
51    assert!(mem::size_of::<Result<(), Errno<()>>>() == mem::size_of::<i32>());
52};
53
54/// A nonnegative result admitted at the syscall boundary.
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56#[repr(transparent)]
57pub struct Value(libc::c_int);
58
59impl Value {
60    pub const fn get(self) -> libc::c_int {
61        self.0
62    }
63}
64
65/// Canonical kernel-operation and descriptor-table failures for every backend.
66/// Diagnostics retain the backend's exact failure without a second error domain.
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
68pub enum Diagnostic<S> {
69    Setup(crate::Errno),
70    Enter(crate::Errno),
71    FileUpdate(crate::Errno),
72    Probe(crate::Errno),
73    FileTable(crate::Errno),
74    RingRegister(crate::Errno),
75    BufferRegister(crate::Errno),
76    BufferUnregister(crate::Errno),
77    Cancel(crate::Errno),
78    FileRange(crate::Errno),
79    Map(crate::Errno),
80    Unmap(crate::Errno),
81    Fault(crate::Errno),
82    FileIndex(S),
83    FileOccupied(S),
84}
85
86impl<S> From<crate::Errno> for Diagnostic<S> {
87    fn from(cause: crate::Errno) -> Self {
88        Self::Fault(cause)
89    }
90}
91
92/// Native failures that prevent releasing worker or driver storage, or an armed watchdog.
93#[derive(Debug)]
94pub enum Fatal<'a> {
95    Join(crate::Errno),
96    TimerDelete(crate::Errno),
97    TimerRefresh(crate::Errno),
98    /// Native retirement failed while borrowed driver storage remains live.
99    Retirement(&'a dyn fmt::Debug),
100}
101
102impl Fatal<'_> {
103    /// Stops before workers, kernel borrows, or live watchdogs can outlive their owners.
104    #[cold]
105    pub fn terminate(self) -> ! {
106        let message = format!("fatal native: {self:?}\n");
107        let stderr = stderr();
108        let _ = fd::raw::write(stderr.as_fd(), message.as_bytes());
109        process::abort()
110    }
111}