use std::io::{PipeReader, PipeWriter};
use std::os::fd::{AsFd, OwnedFd};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
use rustix::io::Errno;
use super::outcome::{classify, AcceptorFailure, KillError, KillOutcome, LinkOutcome, NotStarted, NotStartedCause};
use super::probe::{DropReason, LinkEvent, Probe};
use super::sys::{self, Read};
use crate::elevation::shim::protocol::Command;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StartState {
Pending,
Live,
Refused,
}
const FRAME_LEN: usize = 5;
pub(super) struct Inner {
start: StartState,
hello_seen: bool,
frame: [u8; FRAME_LEN],
frame_len: usize,
outcome: Option<LinkOutcome>,
failure: Option<AcceptorFailure>,
unlinked: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Observed {
pub(crate) start: StartState,
pub(crate) outcome: Option<LinkOutcome>,
pub(crate) acceptor_failure: Option<AcceptorFailure>,
}
pub(super) struct Shared {
inner: Mutex<Inner>,
pub(super) conn: OnceLock<UnixStream>,
pub(super) settled: (PipeReader, PipeWriter),
pub(super) sock_path: PathBuf,
dir: OwnedFd,
pub(super) peer_euid: u32,
pub(super) probe: Probe,
stopping: AtomicBool,
}
impl Shared {
pub(super) fn new(
sock_path: PathBuf,
dir: OwnedFd,
peer_euid: u32,
probe: Probe,
settled: (PipeReader, PipeWriter),
) -> Self {
Shared {
inner: Mutex::new(Inner {
start: StartState::Pending,
hello_seen: false,
frame: [0; FRAME_LEN],
frame_len: 0,
outcome: None,
failure: None,
unlinked: false,
}),
conn: OnceLock::new(),
settled,
sock_path,
dir,
peer_euid,
probe,
stopping: AtomicBool::new(false),
}
}
pub(super) fn lock(&self) -> MutexGuard<'_, Inner> {
self.inner.lock().unwrap_or_else(PoisonError::into_inner)
}
fn unlink(&self, inner: &mut Inner) {
if std::mem::replace(&mut inner.unlinked, true) {
return;
}
match sys::unlink_socket(self.dir.as_fd(), super::SOCKET_NAME) {
Ok(()) => {}
Err(Errno::NOENT) => {
log::debug!("the socket {} was already gone", self.sock_path.display());
}
Err(e) => log::warn!("cannot remove the socket {}: {e}", self.sock_path.display()),
}
}
pub(super) fn request_stop(&self) {
self.stopping.store(true, Ordering::SeqCst);
}
pub(super) fn is_stopping(&self) -> bool {
self.stopping.load(Ordering::SeqCst)
}
pub(super) fn refuse_pending_in_memory(&self) -> bool {
let Ok(mut inner) = self.inner.try_lock() else {
return false;
};
if inner.start == StartState::Pending {
inner.start = StartState::Refused;
}
true
}
pub(super) fn refuse_pending(&self, inner: &mut Inner) {
if inner.start == StartState::Pending {
inner.start = StartState::Refused;
self.unlink(inner);
}
}
pub(super) fn fail_closed(&self, failure: AcceptorFailure) {
let mut inner = self.lock();
match inner.start {
StartState::Pending | StartState::Refused => {
log::error!(
"the acceptor for {} failed ({failure:?}); the start is refused",
self.sock_path.display()
);
inner.failure.get_or_insert(failure);
self.refuse_pending(&mut inner);
}
StartState::Live => {
log::warn!(
"the acceptor for {} stopped ({failure:?}); the running shim is unaffected",
self.sock_path.display()
);
self.unlink(&mut inner);
}
}
}
pub(super) fn answer_hello(&self, conn: UnixStream) {
let mut inner = self.lock();
let further = std::mem::replace(&mut inner.hello_seen, true);
let path = self.sock_path.display();
if inner.start == StartState::Pending {
match sys::send_byte(conn.as_fd(), Command::Allow.encode()) {
Ok(()) => {
inner.start = StartState::Live;
let set = self.conn.set(conn);
debug_assert!(set.is_ok(), "the connection is set once, at the first Live");
self.unlink(&mut inner);
self.probe.event(|| LinkEvent::Answered(Command::Allow));
}
Err(e) => {
log::debug!("cannot answer the shim at {path}: {e}; the start is refused");
self.refuse_pending(&mut inner);
self.probe.event(|| LinkEvent::Dropped(DropReason::AnswerFailed));
}
}
return;
}
if further {
log::warn!("a further root peer at {path} said hello; answered N");
} else {
log::debug!("a root peer at {path} said hello after the start was refused; answered N");
}
self.deny(&conn);
}
pub(super) fn deny(&self, conn: &UnixStream) {
if let Err(e) = sys::send_byte(conn.as_fd(), Command::Deny.encode()) {
log::debug!("cannot answer N at {}: {e}", self.sock_path.display());
}
self.probe.event(|| LinkEvent::Answered(Command::Deny));
}
fn set_outcome(&self, inner: &mut Inner, outcome: LinkOutcome) {
debug_assert!(inner.outcome.is_none(), "the outcome is set once");
let path = self.sock_path.display();
match outcome {
LinkOutcome::Exited(_) => {}
LinkOutcome::NotStarted(n) => log::debug!("the program at {path} never ran: {n:?}"),
LinkOutcome::SupervisionLost(ws) => {
log::warn!("supervision of the program at {path} was lost; the shim killed it (status {ws:#x})");
}
LinkOutcome::StatusLost => {
log::warn!("the program at {path} has exited and its status is lost");
}
LinkOutcome::ShimLost => {
log::warn!("the shim at {path} was lost; the program may still be running");
}
}
inner.outcome = Some(outcome);
let written = match self.probe.settled_write_error() {
Some(injected) => Err(injected),
None => sys::write_byte(self.settled.1.as_fd()),
};
if let Err(e) = written {
if e != Errno::AGAIN {
debug_assert!(false, "writing the settled byte failed: {e}");
log::warn!(
"cannot signal the outcome of the shim at {}: {e}",
self.sock_path.display()
);
}
}
}
fn read_frame(&self, inner: &mut Inner) {
let conn = self.conn.get().expect("Live has a connection");
while inner.outcome.is_none() {
let len = inner.frame_len;
let (settled, more) = match sys::read_some(conn.as_fd(), &mut inner.frame[len..]) {
Read::Bytes(n) => {
inner.frame_len += n;
self.probe.event(|| LinkEvent::Read(n, std::thread::current().id()));
(classify(&inner.frame[..inner.frame_len], false), true)
}
Read::Eof => (classify(&inner.frame[..len], true), false),
Read::Empty => (None, false),
Read::Failed(e) => {
log::warn!("reading from the shim at {}: {e}", self.sock_path.display());
(Some(LinkOutcome::ShimLost), false)
}
};
if let Some(outcome) = settled {
self.set_outcome(inner, outcome);
} else if !more {
break;
}
}
}
pub(super) fn settle(&self) -> Option<LinkOutcome> {
let mut inner = self.lock();
if inner.outcome.is_none() {
self.refuse_pending(&mut inner);
if inner.start == StartState::Refused {
let cause = inner
.failure
.map_or(NotStartedCause::Withheld, NotStartedCause::AcceptorFailed);
let shim_connected = inner.hello_seen;
self.set_outcome(
&mut inner,
LinkOutcome::NotStarted(NotStarted { shim_connected, cause }),
);
} else {
self.read_frame(&mut inner);
}
}
inner.outcome
}
pub(super) fn wait(&self) -> Result<LinkOutcome, Errno> {
loop {
if let Some(outcome) = self.settle() {
return Ok(outcome);
}
let conn = self.conn.get().expect("Live has a connection");
self.probe.event(|| LinkEvent::Parked(std::thread::current().id()));
self.probe.waiter_gate();
let armed = |fds| {
self.probe.event(|| LinkEvent::Polling {
fds,
settled_readable: sys::is_readable(&self.settled.0),
});
};
let waited = match self.probe.wait_poll_error() {
Some(injected) => Err(injected),
None => sys::wait_for_frame_or_outcome(conn.as_fd(), &self.settled.0, armed),
};
if let Err(e) = waited {
log::warn!("waiting for the shim at {}: {e}", self.sock_path.display());
return Err(e);
}
}
}
pub(super) fn observe(&self) -> Observed {
let mut inner = self.lock();
if inner.start == StartState::Live && inner.outcome.is_none() {
self.read_frame(&mut inner);
}
Observed {
start: inner.start,
outcome: inner.outcome,
acceptor_failure: inner.failure,
}
}
pub(super) fn kill(&self) -> Result<KillOutcome, KillError> {
let mut inner = self.lock();
if inner.start != StartState::Live {
self.refuse_pending(&mut inner);
return Ok(KillOutcome::RefusedStart);
}
match inner.outcome {
Some(LinkOutcome::ShimLost) => return Err(KillError::ShimLost),
Some(_) => return Ok(KillOutcome::AlreadyEnded),
None => {}
}
let conn = self.conn.get().expect("Live has a connection");
let sent = match self.probe.send_error() {
Some(injected) => Err(injected),
None => sys::send_byte(conn.as_fd(), Command::Kill.encode()),
};
match sent {
Ok(()) => Ok(KillOutcome::Delivered),
Err(Errno::PIPE | Errno::CONNRESET | Errno::NOTCONN) => {
self.read_frame(&mut inner);
match inner.outcome {
Some(outcome) if outcome.program_is_gone() => Ok(KillOutcome::AlreadyEnded),
_ => Err(KillError::ShimLost),
}
}
Err(Errno::AGAIN | Errno::NOBUFS) => Err(KillError::Unkillable),
Err(e) => {
log::warn!("sending K to the shim at {}: {e}", self.sock_path.display());
Err(KillError::Io(e.into()))
}
}
}
}