pub(crate) mod probe;
pub(crate) mod stat_parse;
#[path = "identity/state.rs"]
mod state;
pub use state::{Existence, Liveness, Resolved};
#[path = "identity/persist.rs"]
mod persist;
pub use persist::{Platform, ProcessIdRecord, RECORD_VERSION};
#[cfg_attr(windows, path = "identity/windows.rs")]
#[cfg_attr(target_os = "linux", path = "identity/linux.rs")]
#[cfg_attr(target_os = "macos", path = "identity/macos.rs")]
mod backend;
#[cfg(all(windows, test))]
#[path = "identity/windows_fixture.rs"]
pub(crate) mod windows_fixture;
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
compile_error!("cosca::identity is implemented only for Windows, Linux, and macOS");
pub type RawPid = u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct StartToken(u64);
impl StartToken {
fn from_raw(v: u64) -> StartToken {
StartToken(v)
}
fn raw(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProcessId {
pid: RawPid,
start: StartToken,
}
impl ProcessId {
pub fn pid(&self) -> RawPid {
self.pid
}
pub(crate) fn start_token_raw(&self) -> u64 {
self.start.raw()
}
pub fn of(pid: RawPid) -> Resolved<ProcessId> {
backend::start_token(pid).map(|start| ProcessId { pid, start })
}
#[cfg(any(unix, test))]
pub(crate) fn from_parts(pid: RawPid, token: u64) -> ProcessId {
ProcessId {
pid,
start: StartToken::from_raw(token),
}
}
#[cfg(test)]
pub(crate) fn from_parts_for_test(pid: RawPid, token: u64) -> ProcessId {
Self::from_parts(pid, token)
}
pub fn current() -> ProcessId {
let start = backend::current_token()
.found()
.expect("a process must be able to read its own start token (Linux: /proc must be mounted)");
ProcessId {
pid: std::process::id(),
start,
}
}
pub fn exists(&self) -> Existence {
match backend::start_token(self.pid) {
Resolved::Found(t) if t == self.start => Existence::Present,
Resolved::Found(_) | Resolved::Gone => Existence::Gone,
Resolved::Unknown => Existence::Unknown,
}
}
pub fn is_alive(&self) -> Liveness {
backend::is_running(self.pid, self.start)
}
pub fn created_at(&self) -> Option<std::time::SystemTime> {
backend::created_at(self.start)
}
}
#[cfg(windows)]
pub(crate) use backend::{close as windows_close, open_classified as windows_open_classified, Opened};
#[cfg(target_os = "macos")]
pub(crate) use backend::kinfo;
#[cfg(target_os = "macos")]
pub(crate) use backend::ppid_of as macos_ppid_of;
#[cfg(windows)]
#[derive(Debug)]
pub(crate) enum HandleIdentity {
Same,
Different,
Unreadable(windows::core::Error),
}
#[cfg(windows)]
pub(crate) fn windows_handle_identity(handle: windows::Win32::Foundation::HANDLE, id: ProcessId) -> HandleIdentity {
match backend::creation_token_result(handle) {
Ok(t) if t == id.start => HandleIdentity::Same,
Ok(_) => HandleIdentity::Different,
Err(e) => HandleIdentity::Unreadable(e),
}
}
#[cfg(windows)]
pub(crate) fn windows_identity_from_handle(
handle: windows::Win32::Foundation::HANDLE,
pid: RawPid,
) -> Option<ProcessId> {
backend::windows_identity_from_handle(handle, pid)
}
#[cfg(test)]
#[path = "identity_tests.rs"]
mod identity_tests;