| 1 | //! This file is meant to house (OS specific) implementations on how to kill |
| 2 | //! processes. |
| 3 | |
| 4 | use anyhowrust-analyzer cargo anyhow 1.0.104 crate/::bailrust-analyzer cargo anyhow 1.0.104 macros/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 craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::processesrust-analyzer cargo bottom 0.14.9 collection/processes/::Pidrust-analyzer cargo bottom 0.14.9 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) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Process> { |
| 22 | // SAFETY: Windows API call, tread carefully with the args. |
| 23 | match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } { |
| 24 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(process) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Process(process)), |
| 25 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!("process may have already been terminated."), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn kill(self) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 30 | // SAFETY: Windows API call, this is safe as we are passing in the handle. |
| 31 | let result = unsafe { TerminateProcess(selfrust-analyzer cargo bottom 0.14.9 utils/process_killer/.0, 1) }; |
| 32 | if result.is_err() { |
| 33 | bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!("process may have already been terminated."); |
| 34 | } |
| 35 | |
| 36 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[cfg(target_os = "windows")] |
| 41 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/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(selfrust-analyzer cargo bottom 0.14.9 utils/process_killer/.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: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 53 | let process = Process::open(pid as u32)?; |
| 54 | process.kill()?; |
| 55 | |
| 56 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 57 | } |
| 58 | |
| 59 | /// Kills a process, given a PID, for UNIX. |
| 60 | #[cfg(unix)] |
| 61 | pub fn kill_process_given_pidrust-analyzer cargo bottom 0.14.9 utils/process_killer/kill_process_given_pid().(pid: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#, signal: usize) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 62 | // SAFETY: the signal should be valid, and we act properly on an error (exit |
| 63 | // code not 0). |
| 64 | let output = unsafe { libcrust-analyzer cargo libc 0.2.189 crate/::killrust-analyzer cargo libc 0.2.189 kill().(pid, signal as i32) }; |
| 65 | |
| 66 | if output rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0 { |
| 67 | // We had an error... |
| 68 | let err_code = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().().raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]raw_os_error().(); |
| 69 | let err = match err_code { |
| 70 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.189 crate/::ESRCHrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/ESRCH.) => "the target process did not exist.", |
| 71 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.189 crate/::EPERMrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EPERM.) => { |
| 72 | "the calling process does not have the permissions to terminate the target process(es)." |
| 73 | } |
| 74 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.189 crate/::EINVALrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EINVAL.) => "an invalid signal was specified.", |
| 75 | _ => "Unknown error occurred.", |
| 76 | }; |
| 77 | |
| 78 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(err_code) = err_code { |
| 79 | bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Error code {err_code} - {err}")) |
| 80 | } else { |
| 81 | bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Error code unknown - {err}")) |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 86 | } |