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)]
pub(crate) struct StartToken(u64);
impl StartToken {
pub(crate) 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> {
Self::of_explained(pid).0
}
pub(crate) fn of_explained(pid: RawPid) -> (Resolved<ProcessId>, UnknownCause) {
#[cfg(all(test, target_os = "macos"))]
seams::note_by_pid_read();
let (read, cause) = start_token_explained(pid);
(read.map(|start| ProcessId { pid, start }), cause)
}
#[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/self/stat must be readable)");
ProcessId {
pid: std::process::id(),
start,
}
}
pub fn exists(&self) -> Existence {
self.exists_explained().0
}
pub(crate) fn exists_explained(&self) -> (Existence, UnknownCause) {
let (read, cause) = start_token_explained(self.pid);
let existence = match read {
Resolved::Found(t) if t == self.start => Existence::Present,
Resolved::Found(_) | Resolved::Gone => Existence::Gone,
Resolved::Unknown => Existence::Unknown,
};
(existence, cause)
}
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 = "linux")]
pub(crate) use backend::pid_stat;
#[cfg(target_os = "linux")]
pub(crate) use backend::proc_view::{pidfd_pid_in_view, proc_view, PidfdTarget, ProcDir, ProcView, ViewUnreadable};
#[derive(Debug, Default)]
pub(crate) struct UnknownCause {
#[cfg(target_os = "linux")]
view: Option<ViewUnreadable>,
}
impl UnknownCause {
pub(crate) fn into_error(self, subject: &str) -> Option<crate::error::Error> {
#[cfg(target_os = "linux")]
{
self.view.map(|why| backend::view_error(subject, why))
}
#[cfg(not(target_os = "linux"))]
{
let _ = (self, subject);
None
}
}
}
#[cfg(target_os = "linux")]
fn start_token_explained(pid: RawPid) -> (Resolved<StartToken>, UnknownCause) {
let (read, view) = backend::start_token_explained(pid);
(read, UnknownCause { view })
}
#[cfg(not(target_os = "linux"))]
fn start_token_explained(pid: RawPid) -> (Resolved<StartToken>, UnknownCause) {
(backend::start_token(pid), UnknownCause::default())
}
#[cfg(target_os = "linux")]
pub(crate) fn unknown_identity_error(subject: &str) -> Option<crate::error::Error> {
backend::unknown_identity_error(subject)
}
#[cfg(not(target_os = "linux"))]
pub(crate) fn unknown_identity_error(_subject: &str) -> Option<crate::error::Error> {
None
}
#[cfg(all(target_os = "linux", test))]
pub(crate) use backend::fault;
#[cfg(all(target_os = "macos", test))]
pub(crate) mod seams {
use std::cell::Cell;
thread_local! {
static BY_PID_READS: Cell<usize> = const { Cell::new(0) };
}
pub(super) fn note_by_pid_read() {
BY_PID_READS.with(|n| n.set(n.get() + 1));
}
pub(crate) fn by_pid_reads() -> usize {
BY_PID_READS.with(Cell::get)
}
}
#[cfg(all(target_os = "linux", test))]
pub(crate) use backend::proc_view::{fault as proc_view_fault, openat2_refused_message};
#[cfg(target_os = "linux")]
impl ProcessId {
pub(crate) fn exists_in(&self, proc_dir: &ProcDir) -> Existence {
match backend::start_token_in(proc_dir, self.pid) {
Resolved::Found(t) if t == self.start => Existence::Present,
Resolved::Found(_) | Resolved::Gone => Existence::Gone,
Resolved::Unknown => Existence::Unknown,
}
}
pub(crate) fn is_alive_in(&self, proc_dir: &ProcDir) -> Liveness {
backend::is_running_in(proc_dir, self.pid, self.start)
}
pub(crate) fn signal_says_no_such_process(&self) -> bool {
backend::signal_says_no_such_process(self.pid)
}
}
#[cfg(target_os = "macos")]
pub(crate) use backend::kinfo;
#[cfg(all(test, target_os = "linux"))]
pub(crate) fn force_unknown_identity(pid: RawPid) -> impl Drop {
proc_view_fault::force_identity_stat_errno(pid, libc::EACCES)
}
#[cfg(all(test, target_os = "macos"))]
pub(crate) fn force_unknown_identity(pid: RawPid) -> impl Drop {
backend::fault::force_unknown(pid)
}
#[cfg(all(target_os = "macos", test))]
pub(crate) use backend::UniqInfo;
#[cfg(target_os = "macos")]
pub(crate) use backend::{held_by, own_unique_id, uniq_info, Held, ReadPurpose, UniqRead, LAUNCHD};
#[cfg(all(target_os = "macos", test))]
pub(crate) use backend::{ppid_fault, uniq_fault, ReadErr};
#[cfg(target_os = "macos")]
pub(crate) use backend::ppid_of as macos_ppid_of;
#[cfg(all(test, target_os = "macos"))]
pub(crate) use backend::{fault as macos_fault, PpidRead};
#[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;