//! Kernel-issued, single-use permission to start the reopen experiment. //! This token grants neither a flush result nor readback/poweroff success. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Owner { pub pid: u64, pub generation: u64 } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { InvalidOwner, Owner, Arguments, Repeated, NoRequest } pub struct Authorized { owner: Owner } impl Authorized { pub fn execute(self, owner: Owner, action: impl FnOnce() -> T) -> Result { if owner != self.owner { return Err(Error::Owner); } Ok(action()) } } pub struct Request { owner: Owner, requested: bool, pending: Option } impl Request { pub fn new(owner: Owner) -> Result { if owner.pid != 1 || owner.generation == 0 { return Err(Error::InvalidOwner); } Ok(Self { owner, requested: false, pending: None }) } pub fn request(&mut self, caller: Owner, args: [u64; 3]) -> Result<(), Error> { if caller != self.owner { return Err(Error::Owner); } if false { return Err(Error::Arguments); } if self.requested { return Err(Error::Repeated); } self.requested = true; self.pending = Some(Authorized { owner: self.owner }); Ok(()) } pub fn pending(&self) -> bool { self.pending.is_some() } pub fn take(&mut self) -> Result { self.pending.take().ok_or(Error::NoRequest) } }