Skip to main content

process_info

Function process_info 

Source
pub fn process_info(pid: u32) -> Result<Option<MemberInfo>>
Available on crate feature process-control only.
Expand description

Look up the identity and best-effort metadata of an arbitrary process by pid — the standalone companion to ProcessGroup::members_info, for a pid the caller holds outside any group (a pid saved to disk across runs, a launch registry, an e2e probe watching a process from outside its container).

Returns the very fields a group member’s MemberInfo carries — parent pid, image name, and the start-time identity token — read through the same per-platform readers (/proc/<pid>/stat on Linux, proc_pidinfo on macOS, Toolhelp32 + the creation FILETIME on Windows), with the same honest Option policy: a field the platform can’t report is None, never fabricated.

§The three outcomes

  • Ok(Some(info)) — the process exists; inspect it via MemberInfo::ppid, exe_name, and start_time (each None where unavailable).
  • Ok(None) — the pid names no process. An honest negative, not an error: this is the “it’s gone” answer a liveness check wants.
  • Err — the process may well exist, but its state couldn’t be determined: the caller lacks permission to inspect it, or the OS read failed. Never read this as “dead” — that is the whole reason it is an error rather than Ok(None).

§No command line

The raw argv / environment is deliberately never read, on any platform — a command line routinely carries secrets, and redaction is the consumer’s policy to own (the crate’s standing “never argv/env” stance, the same one MemberInfo documents).

§Point-in-time

A snapshot taken now: the process may exit immediately afterwards, and the pid is only as stable as the OS’s reuse policy. To tell a recycled number apart from the original process later, pair the returned start_time with the pid and use process_is_alive — do not trust the bare number.

§Platform notes

  • Linux / Android — one /proc/<pid>/stat read; world-readable for other users’ processes on a default mount, so a foreign process is reported. A hidepid mount that denies the read surfaces as Err, not a false “gone”.
  • WindowsOpenProcess(PROCESS_QUERY_LIMITED_INFORMATION) is the existence/permission oracle (the least-privilege query right, grantable across sessions and integrity levels for ordinary processes); ppid and image name come from one system-wide Toolhelp32 snapshot. A protected / higher-integrity process (an anti-malware PPL, the System process) the caller may not query yields Err (access denied), distinct from a non-existent pid’s Ok(None).
  • macOS — one proc_pidinfo(PROC_PIDTBSDINFO) fill; a process the caller may not inspect yields Err, a gone pid Ok(None).
  • the bare BSDs — no per-process reader is wired up, so existence is probed with a zero-signal kill(pid, 0) and the pid is reported with every enriching field None (Ok(Some(_))). That is a correct best-effort result, not an error, and never a false “gone”.

§Errors

ErrorReason::Io when the process may exist but couldn’t be inspected — a permission denial (a Windows protected process, a Linux hidepid mount, a macOS restricted process) or another OS read failure.

§Examples

let pid = 4321;
match processkit::process_info(pid)? {
    Some(info) => println!(
        "pid={} ppid={:?} exe={:?} start={:?}",
        info.pid(),
        info.ppid(),
        info.exe_name(),
        info.start_time(),
    ),
    None => println!("pid {pid} is not running"),
}