#[cfg(windows)]
pub(super) fn is_running(pid: u32) -> bool {
use windows::Win32::Foundation::{CloseHandle, ERROR_INVALID_PARAMETER, STILL_ACTIVE};
use windows::Win32::System::Threading::{GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION};
let handle = match unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) } {
Ok(handle) => handle,
Err(error) => return error.code() != ERROR_INVALID_PARAMETER.to_hresult(),
};
let mut code = 0u32;
let queried = unsafe { GetExitCodeProcess(handle, &mut code) };
let _ = unsafe { CloseHandle(handle) };
match queried {
Ok(()) => code == STILL_ACTIVE.0 as u32,
Err(_) => true,
}
}
#[cfg(unix)]
pub(super) fn is_running(pid: u32) -> bool {
let Ok(pid) = libc::pid_t::try_from(pid) else {
return true;
};
if pid <= 0 {
return true;
}
if unsafe { libc::kill(pid, 0) } == 0 {
return true;
}
std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
}
#[cfg(not(any(windows, unix)))]
pub(super) fn is_running(_pid: u32) -> bool {
true
}