use std::io;
use std::path::PathBuf;
use std::time::Duration;
use super::Signal;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum LifecycleError {
#[error("invalid daemon descriptor: {reason}")]
InvalidDescriptor {
reason: String,
},
#[error("no install directory: neither CARGO_HOME nor a home directory is set")]
NoInstallDir,
#[error("cannot tell whether {} is served (connect timed out or failed)", socket.display())]
ProbeInconclusive {
socket: PathBuf,
},
#[error("{} is served, but SO_PEERCRED reported no peer pid", socket.display())]
NoPeerPid {
socket: PathBuf,
},
#[error(
"{} is not served but {} is locked; the holder cannot be identified",
socket.display(),
lock.display()
)]
Unidentified {
socket: PathBuf,
lock: PathBuf,
},
#[error("pid {pid} serves the socket but runs `{found}`, not `{expected}`; not signalled")]
IdentityMismatch {
pid: u32,
expected: String,
found: String,
},
#[error("cannot read the executable of pid {pid}: {source}")]
IdentityUnreadable {
pid: u32,
source: io::Error,
},
#[error("sending {signal:?} to pid {pid} failed: {source}")]
Signal {
pid: u32,
signal: Signal,
source: io::Error,
},
#[error("pid {pid} survived SIGKILL; the daemon is not stopped")]
Survivor {
pid: u32,
},
#[error("pid {pid} exited, but {signal} still does not hold")]
StoppedSignalNotReached {
pid: u32,
signal: String,
},
#[error("daemon binary {} is not installed", path.display())]
BinaryMissing {
path: PathBuf,
},
#[error("{} is already served{}", socket.display(), pid.map(|p| format!(" by pid {p}")).unwrap_or_default())]
AlreadyRunning {
socket: PathBuf,
pid: Option<u32>,
},
#[error("could not spawn {}: {message}", program.display())]
Spawn {
program: PathBuf,
message: String,
},
#[error("pid {pid} exited before serving the socket")]
ChildExited {
pid: u32,
},
#[error("pid {pid} did not serve the socket within {timeout:?}; {}", reap_note(*killed))]
NotReady {
pid: u32,
timeout: Duration,
killed: bool,
},
#[error("could not pin spawned pid {pid} with a pidfd ({source}); {}", reap_note(*killed))]
ChildUnpinned {
pid: u32,
killed: bool,
source: io::Error,
},
#[error("{context}: {source}")]
Io {
context: String,
source: io::Error,
},
#[error(
"{} is not served, but pid(s) {} run `{binary}` with the daemon's arguments; not signalled",
socket.display(),
pid_list(pids)
)]
RunningUnserved {
socket: PathBuf,
binary: String,
pids: Vec<u32>,
},
#[error(
"pid {pid} runs `{binary}` with the daemon's arguments, but its environment cannot be \
read ({source}); cannot tell whether it is this instance; not signalled"
)]
InstanceUnreadable {
pid: u32,
binary: String,
source: io::Error,
},
#[error(
"pid(s) {} run `{binary}` for this instance, but pid(s) {} do as well; a start after \
the stop would refuse, so nothing was stopped",
pid_list(serving),
pid_list(others)
)]
RestartBlocked {
serving: Vec<u32>,
binary: String,
others: Vec<u32>,
},
#[error(
"pid {pid} runs `{binary}` but is not this instance (its arguments, environment or \
owner differ); not signalled"
)]
NotThisInstance {
pid: u32,
binary: String,
},
#[error(
"pid {pid} runs `{binary}` with the daemon's arguments, but its `{flag}` value is \
missing, relative or unresolvable; cannot tell whether it is this instance; not signalled"
)]
InstanceUndecided {
pid: u32,
binary: String,
flag: String,
},
#[error("pid {pid} is this instance, but pid {peer} serves {}; not signalled", socket.display())]
ServedByAnother {
pid: u32,
peer: u32,
socket: PathBuf,
},
}
fn pid_list(pids: &[u32]) -> String {
let list: Vec<String> = pids.iter().map(u32::to_string).collect();
list.join(", ")
}
fn reap_note(killed: bool) -> &'static str {
if killed {
"it was killed and reaped"
} else {
"it was sent SIGKILL but not reaped within the kill wait"
}
}