mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-07 13:54:44 +02:00
other: switch to windows-rs for process killing (#985)
This commit is contained in:
parent
4b392c96f7
commit
639c93b4c8
55
Cargo.lock
generated
55
Cargo.lock
generated
@ -252,6 +252,7 @@ dependencies = [
|
|||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"winapi",
|
"winapi",
|
||||||
|
"windows",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1899,6 +1900,15 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
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]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.42.0"
|
version = "0.42.0"
|
||||||
@ -1915,46 +1925,61 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows-targets"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
|
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.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
|
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
|
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
|
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
|
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
|
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.42.0"
|
version = "0.42.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
|
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wrapcenum-derive"
|
name = "wrapcenum-derive"
|
||||||
|
@ -109,6 +109,7 @@ mach2 = "0.4.1"
|
|||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
heim = { version = "0.1.0-rc.1", features = ["cpu", "disk", "memory"] }
|
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"
|
winapi = "0.3.9"
|
||||||
|
|
||||||
[target.'cfg(target_os = "freebsd")'.dependencies]
|
[target.'cfg(target_os = "freebsd")'.dependencies]
|
||||||
|
@ -1,41 +1,51 @@
|
|||||||
// Copied from SO: https://stackoverflow.com/a/55231715
|
//! This file is meant to house (OS specific) implementations on how to kill processes.
|
||||||
#[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.
|
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
use crate::utils::error::BottomError;
|
use crate::utils::error::BottomError;
|
||||||
use crate::Pid;
|
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")]
|
#[cfg(target_os = "windows")]
|
||||||
struct Process(HANDLE);
|
struct Process(HANDLE);
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
impl Process {
|
impl Process {
|
||||||
fn open(pid: DWORD) -> Result<Process, String> {
|
fn open(pid: u32) -> Result<Process, String> {
|
||||||
let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) };
|
match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } {
|
||||||
if pc.is_null() {
|
Ok(process) => Ok(Process(process)),
|
||||||
return Err("OpenProcess".to_string());
|
Err(_) => Err("process may have already been terminated.".to_string()),
|
||||||
}
|
}
|
||||||
Ok(Process(pc))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kill(self) -> Result<(), String> {
|
fn kill(self) -> Result<(), String> {
|
||||||
let result = unsafe { TerminateProcess(self.0, 1) };
|
let result = unsafe { TerminateProcess(self.0, 1) };
|
||||||
if result == 0 {
|
if result.0 == 0 {
|
||||||
return Err("Failed to kill process".to_string());
|
return Err("process may have already been terminated.".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
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.
|
/// Kills a process, given a PID, for unix.
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> {
|
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(())
|
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(())
|
|
||||||
}
|
|
||||||
|
@ -21,7 +21,7 @@ pub enum BottomError {
|
|||||||
#[error("Error caused by Crossterm, {0}")]
|
#[error("Error caused by Crossterm, {0}")]
|
||||||
CrosstermError(String),
|
CrosstermError(String),
|
||||||
/// An error to represent generic errors.
|
/// An error to represent generic errors.
|
||||||
#[error("Generic error, {0}")]
|
#[error("Error, {0}")]
|
||||||
GenericError(String),
|
GenericError(String),
|
||||||
/// An error to represent errors with fern.
|
/// An error to represent errors with fern.
|
||||||
#[error("Fern error, {0}")]
|
#[error("Fern error, {0}")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user