| 1 | //! This file is meant to house (OS specific) implementations on how to kill |
| 2 | //! processes. |
| 3 | |
| 4 | use anyhow::bail; |
| 5 | #[cfg(target_os = "windows")] |
| 6 | use windows::Win32::{ |
| 7 | Foundation::{CloseHandle, HANDLE}, |
| 8 | System::Threading::{ |
| 9 | OpenProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE, TerminateProcess, |
| 10 | }, |
| 11 | }; |
| 12 | |
| 13 | use crate::collection::processes::Pid; |
| 14 | |
| 15 | /// Based from [this SO answer](https://stackoverflow.com/a/55231715). |
| 16 | #[cfg(target_os = "windows")] |
| 17 | struct Process(HANDLE); |
| 18 | |
| 19 | #[cfg(target_os = "windows")] |
| 20 | impl Process { |
| 21 | fn open(pid: u32) -> anyhow::Result<Process> { |
| 22 | // SAFETY: Windows API call, tread carefully with the args. |
| 23 | match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } { |
| 24 | Ok(process) => Ok(Process(process)), |
| 25 | Err(_) => bail!("process may have already been terminated."), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn kill(self) -> anyhow::Result<()> { |
| 30 | // SAFETY: Windows API call, this is safe as we are passing in the handle. |
| 31 | let result = unsafe { TerminateProcess(self.0, 1) }; |
| 32 | if result.is_err() { |
| 33 | bail!("process may have already been terminated."); |
| 34 | } |
| 35 | |
| 36 | Ok(()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[cfg(target_os = "windows")] |
| 41 | impl Drop for Process { |
| 42 | fn drop(&mut self) { |
| 43 | // SAFETY: Windows API call, this is safe as we are passing in the handle. |
| 44 | unsafe { |
| 45 | let _ = CloseHandle(self.0); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Kills a process, given a PID, for windows. |
| 51 | #[cfg(target_os = "windows")] |
| 52 | pub fn kill_process_given_pid(pid: Pid) -> anyhow::Result<()> { |
| 53 | let process = Process::open(pid as u32)?; |
| 54 | process.kill()?; |
| 55 | |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | /// Kills a process, given a PID, for UNIX. |
| 60 | #[cfg(unix)] |
| 61 | pub fn kill_process_given_pid(pid: Pid, signal: usize) -> anyhow::Result<()> { |
| 62 | // SAFETY: the signal should be valid, and we act properly on an error (exit |
| 63 | // code not 0). |
| 64 | let output = unsafe { libc::kill(pid, signal as i32) }; |
| 65 | |
| 66 | if output != 0 { |
| 67 | // We had an error... |
| 68 | let err_code = std::io::Error::last_os_error().raw_os_error(); |
| 69 | let err = match err_code { |
| 70 | Some(libc::ESRCH) => "the target process did not exist.", |
| 71 | Some(libc::EPERM) => { |
| 72 | "the calling process does not have the permissions to terminate the target process(es)." |
| 73 | } |
| 74 | Some(libc::EINVAL) => "an invalid signal was specified.", |
| 75 | _ => "Unknown error occurred.", |
| 76 | }; |
| 77 | |
| 78 | if let Some(err_code) = err_code { |
| 79 | bail!(format!("Error code {err_code} - {err}")) |
| 80 | } else { |
| 81 | bail!(format!("Error code unknown - {err}")) |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | Ok(()) |
| 86 | } |