summaryrefslogtreecommitdiffstats
path: root/src/app/process_killer.rs
blob: b6793f397bcae548a138531aa099ddf4c9c37241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// This file is meant to house (OS specific) implementations on how to kill processes.
use std::process::Command;

// TODO: Make it update process list on freeze.

// 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},
	},
};

#[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());
		}
		Ok(Process(pc))
	}

	fn kill(self) -> Result<(), String> {
		unsafe { TerminateProcess(self.0, 1) };
		Ok(())
	}
}

/// Kills a process, given a PID.
pub fn kill_process_given_pid(pid: u64) -> crate::utils::error::Result<()> {
	if cfg!(target_os = "linux") {
		// Linux
		Command::new("kill").arg(pid.to_string()).output()?;
	} else if cfg!(target_os = "windows") {
		#[cfg(target_os = "windows")]
		let process = Process::open(pid as DWORD)?;
		#[cfg(target_os = "windows")]
		process.kill()?;
	} else if cfg!(target_os = "macos") {
		// TODO: macOS
		// See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html
		debug!("Sorry, macOS support is not implemented yet!");
	} else {
		// TODO: Others?
		debug!("Sorry, other support this is not implemented yet!");
	}

	Ok(())
}