diff options
author | Clement Tsang <clementjhtsang@gmail.com> | 2019-09-25 01:30:50 -0400 |
---|---|---|
committer | ClementTsang <clementjhtsang@gmail.com> | 2019-09-25 02:00:10 -0400 |
commit | 52c4234ed03f0205f41516536e1cebd8e6493eb5 (patch) | |
tree | 9a7228bebe618165ef49eb5c96970dc39b57bced | |
parent | a24e5dbbcfcecd6514a768d1104f7be6c2829630 (diff) |
Added dd for windows.
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | TODO.md | 2 | ||||
-rw-r--r-- | src/app/process_killer.rs | 35 | ||||
-rw-r--r-- | src/utils/error.rs | 11 |
4 files changed, 46 insertions, 3 deletions
@@ -27,6 +27,7 @@ log = "0.4" rayon = "1.2" sysinfo = "0.9.4" tokio = "0.2.0-alpha.4" +winapi = "0.3.8" [dependencies.tui-temp-fork] #git = "https://github.com/ClementTsang/tui-rs" @@ -24,6 +24,8 @@ Note this will probably migrate to GitHub's native Issues; this was mostly for p * Travis +* Refactoring! Please. + * Scaling in and out (zoom), may need to show zoom levels * More keybinds diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index 2e74de16..3e5feb76 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -1,5 +1,35 @@ /// This file is meant to house (OS specific) implementations on how to kill processes. use std::process::Command; +use std::ptr::null_mut; +use winapi::{ + shared::{minwindef::DWORD, ntdef::HANDLE}, + um::{ + processthreadsapi::{OpenProcess, TerminateProcess}, + winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE}, + }, +}; + +// Copied from SO: https://stackoverflow.com/a/55231715 +struct Process(HANDLE); +impl Process { + fn open(pid : DWORD) -> Result<Process, String> { + let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) }; + if pc == null_mut() { + return Err("!OpenProcess".to_string()); + } + Ok(Process(pc)) + } + + fn kill(self) -> Result<(), String> { + unsafe { TerminateProcess(self.0, 1) }; + Ok(()) + } +} +impl Drop for Process { + fn drop(&mut self) { + unsafe { winapi::um::handleapi::CloseHandle(self.0) }; + } +} /// Kills a process, given a PID. pub fn kill_process_given_pid(pid : u64) -> crate::utils::error::Result<()> { @@ -8,9 +38,8 @@ pub fn kill_process_given_pid(pid : u64) -> crate::utils::error::Result<()> { Command::new("kill").arg(pid.to_string()).output()?; } else if cfg!(target_os = "windows") { - // Windows - // See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html - debug!("Sorry, Windows support is not implemented yet!"); + let process = Process::open(pid as DWORD)?; + process.kill()?; } else if cfg!(target_os = "macos") { // TODO: macOS diff --git a/src/utils/error.rs b/src/utils/error.rs index eecda2d5..ed829c3e 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -27,6 +27,11 @@ pub enum RustopError { /// The data provided is the error found. #[fail(display = "ERROR: Invalid error due to Crossterm: {}", message)] CrosstermError { message : String }, + /// An error to represent generic errors + /// + /// The data provided is the error found. + #[fail(display = "ERROR: Invalid generic error: {}", message)] + GenericError { message : String }, } impl From<std::io::Error> for RustopError { @@ -52,3 +57,9 @@ impl From<std::num::ParseIntError> for RustopError { RustopError::InvalidArg { message : err.to_string() } } } + +impl From<std::string::String> for RustopError { + fn from(err : std::string::String) -> Self { + RustopError::GenericError { message : err.to_string() } + } +} |