summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2023-01-18 01:43:08 -0500
committerGitHub <noreply@github.com>2023-01-18 01:43:08 -0500
commit639c93b4c80857d9090ec60cb49ed1f898163945 (patch)
tree21ca00baea528fa505a43f1dea9294da518d5cc3
parent4b392c96f7a3a8917c77b47812d2b43b8a99ed64 (diff)
other: switch to windows-rs for process killing (#985)
-rw-r--r--Cargo.lock53
-rw-r--r--Cargo.toml1
-rw-r--r--src/app/process_killer.rs55
-rw-r--r--src/utils/error.rs2
4 files changed, 68 insertions, 43 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 81939e0a..fecc7347 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -252,6 +252,7 @@ dependencies = [
"unicode-segmentation",
"unicode-width",
"winapi",
+ "windows",
]
[[package]]
@@ -1900,6 +1901,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
+name = "windows"
+version = "0.44.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9e745dab35a0c4c77aa3ce42d595e13d2003d6902d6b08c9ef5fc326d08da12b"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1915,46 +1925,61 @@ dependencies = [
]
[[package]]
+name = "windows-targets"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc",
+ "windows_i686_gnu",
+ "windows_i686_msvc",
+ "windows_x86_64_gnu",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc",
+]
+
+[[package]]
name = "windows_aarch64_gnullvm"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
+checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_msvc"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
+checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_i686_gnu"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
+checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_msvc"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
+checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_x86_64_gnu"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
+checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnullvm"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
+checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_msvc"
-version = "0.42.0"
+version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
+checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "wrapcenum-derive"
diff --git a/Cargo.toml b/Cargo.toml
index 1d8886bd..dc769019 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -109,6 +109,7 @@ mach2 = "0.4.1"
[target.'cfg(target_os = "windows")'.dependencies]
heim = { version = "0.1.0-rc.1", features = ["cpu", "disk", "memory"] }
+windows = { version = "0.44.0", features = ["Win32_System_Threading", "Win32_Foundation"] }
winapi = "0.3.9"
[target.'cfg(target_os = "freebsd")'.dependencies]
diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs
index f3cea471..d93c6f07 100644
--- a/src/app/process_killer.rs
+++ b/src/app/process_killer.rs
@@ -1,41 +1,51 @@
-// Copied from SO: https://stackoverflow.com/a/55231715
-#[cfg(target_os = "windows")]
-use winapi::{
- shared::{minwindef::DWORD, ntdef::HANDLE},
- um::{
- processthreadsapi::{OpenProcess, TerminateProcess},
- winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE},
- },
-};
+//! This file is meant to house (OS specific) implementations on how to kill processes.
-/// This file is meant to house (OS specific) implementations on how to kill processes.
#[cfg(target_family = "unix")]
use crate::utils::error::BottomError;
use crate::Pid;
#[cfg(target_os = "windows")]
+use windows::Win32::{
+ Foundation::HANDLE,
+ System::Threading::{
+ OpenProcess, TerminateProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE,
+ },
+};
+
+/// Based from [this SO answer](https://stackoverflow.com/a/55231715).
+#[cfg(target_os = "windows")]
struct Process(HANDLE);
#[cfg(target_os = "windows")]
impl Process {
- fn open(pid: DWORD) -> Result<Process, String> {
- let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) };
- if pc.is_null() {
- return Err("OpenProcess".to_string());
+ fn open(pid: u32) -> Result<Process, String> {
+ match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } {
+ Ok(process) => Ok(Process(process)),
+ Err(_) => Err("process may have already been terminated.".to_string()),
}
- Ok(Process(pc))
}
fn kill(self) -> Result<(), String> {
let result = unsafe { TerminateProcess(self.0, 1) };
- if result == 0 {
- return Err("Failed to kill process".to_string());
+ if result.0 == 0 {
+ return Err("process may have already been terminated.".to_string());
}
Ok(())
}
}
+/// Kills a process, given a PID, for windows.
+#[cfg(target_os = "windows")]
+pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> {
+ {
+ let process = Process::open(pid as u32)?;
+ process.kill()?;
+ }
+
+ Ok(())
+}
+
/// Kills a process, given a PID, for unix.
#[cfg(target_family = "unix")]
pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> {
@@ -65,14 +75,3 @@ pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::R
Ok(())
}
-
-/// Kills a process, given a PID, for windows.
-#[cfg(target_os = "windows")]
-pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> {
- {
- let process = Process::open(pid as DWORD)?;
- process.kill()?;
- }
-
- Ok(())
-}
diff --git a/src/utils/error.rs b/src/utils/error.rs
index 904f1392..1473a9ae 100644
--- a/src/utils/error.rs
+++ b/src/utils/error.rs
@@ -21,7 +21,7 @@ pub enum BottomError {
#[error("Error caused by Crossterm, {0}")]
CrosstermError(String),
/// An error to represent generic errors.
- #[error("Generic error, {0}")]
+ #[error("Error, {0}")]
GenericError(String),
/// An error to represent errors with fern.
#[error("Fern error, {0}")]