Skip to main content

macos_resolver/
util.rs

1//! Internal utilities.
2
3/// Checks whether the process with the given PID is still alive.
4///
5/// Uses `kill(pid, 0)` — signal 0 checks existence without delivering a signal.
6#[must_use]
7pub fn is_process_alive(pid: u32) -> bool {
8    // SAFETY: `kill(pid, 0)` is a standard POSIX existence check that does
9    // not deliver any signal.
10    #[allow(clippy::cast_possible_wrap)]
11    unsafe {
12        libc::kill(pid as libc::pid_t, 0) == 0
13    }
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[test]
21    fn current_process_is_alive() {
22        assert!(is_process_alive(std::process::id()));
23    }
24
25    #[test]
26    fn dead_pid_is_not_alive() {
27        assert!(!is_process_alive(999_999_999));
28    }
29}