summaryrefslogtreecommitdiffstats
path: root/src
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 /src
parent4b392c96f7a3a8917c77b47812d2b43b8a99ed64 (diff)
other: switch to windows-rs for process killing (#985)
Diffstat (limited to 'src')
-rw-r--r--src/app/process_killer.rs55
-rw-r--r--src/utils/error.rs2
2 files changed, 28 insertions, 29 deletions
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}")]