mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-25 14:44:39 +02:00
refactor: remove kill command, use libc
Removes the kill command call and instead uses libc to manage killing processes.
This commit is contained in:
parent
7475f24a4e
commit
ff15649be7
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -2,12 +2,16 @@
|
|||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
"DWORD",
|
"DWORD",
|
||||||
"Deque",
|
"Deque",
|
||||||
|
"EINVAL",
|
||||||
|
"EPERM",
|
||||||
|
"ESRCH",
|
||||||
"MSRV",
|
"MSRV",
|
||||||
"Mahmoud",
|
"Mahmoud",
|
||||||
"Marcin",
|
"Marcin",
|
||||||
"Nonexhaustive",
|
"Nonexhaustive",
|
||||||
"PKGBUILD",
|
"PKGBUILD",
|
||||||
"Qudsi",
|
"Qudsi",
|
||||||
|
"SIGTERM",
|
||||||
"Tebibytes",
|
"Tebibytes",
|
||||||
"Ungrouped",
|
"Ungrouped",
|
||||||
"WASD",
|
"WASD",
|
||||||
|
@ -87,6 +87,7 @@ pub struct DataCollector {
|
|||||||
widgets_to_harvest: UsedWidgets,
|
widgets_to_harvest: UsedWidgets,
|
||||||
battery_manager: Option<Manager>,
|
battery_manager: Option<Manager>,
|
||||||
battery_list: Option<Vec<Battery>>,
|
battery_list: Option<Vec<Battery>>,
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
page_file_size_kb: u64,
|
page_file_size_kb: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,15 +112,8 @@ impl Default for DataCollector {
|
|||||||
widgets_to_harvest: UsedWidgets::default(),
|
widgets_to_harvest: UsedWidgets::default(),
|
||||||
battery_manager: None,
|
battery_manager: None,
|
||||||
battery_list: None,
|
battery_list: None,
|
||||||
page_file_size_kb: {
|
#[cfg(target_os = "linux")]
|
||||||
#[cfg(target_os = "linux")]
|
page_file_size_kb: unsafe { libc::sysconf(libc::_SC_PAGESIZE) as u64 / 1024 },
|
||||||
unsafe {
|
|
||||||
libc::sysconf(libc::_SC_PAGESIZE) as u64 / 1024
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
|
||||||
0
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
use std::process::Command;
|
|
||||||
|
|
||||||
// Copied from SO: https://stackoverflow.com/a/55231715
|
// Copied from SO: https://stackoverflow.com/a/55231715
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
use winapi::{
|
use winapi::{
|
||||||
@ -35,12 +33,25 @@ impl Process {
|
|||||||
/// Kills a process, given a PID.
|
/// Kills a process, given a PID.
|
||||||
pub fn kill_process_given_pid(pid: u32) -> crate::utils::error::Result<()> {
|
pub fn kill_process_given_pid(pid: u32) -> crate::utils::error::Result<()> {
|
||||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||||
// FIXME: Use libc instead of a command...
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||||
let output = Command::new("kill").arg(pid.to_string()).output()?;
|
{
|
||||||
if !(output.status).success() {
|
let output = unsafe { libc::kill(pid as i32, libc::SIGTERM) };
|
||||||
return Err(BottomError::GenericError(
|
if output != 0 {
|
||||||
std::str::from_utf8(&output.stderr)?.to_string(),
|
// We had an error...
|
||||||
));
|
let err_code = std::io::Error::last_os_error().raw_os_error();
|
||||||
|
let err = match err_code {
|
||||||
|
Some(libc::ESRCH) => "the target process did not exist.",
|
||||||
|
Some(libc::EPERM) => "the calling process does not have the permissions to terminate the target process(es).",
|
||||||
|
Some(libc::EINVAL) => "an invalid signal was specified.",
|
||||||
|
_ => "Unknown error occurred."
|
||||||
|
};
|
||||||
|
|
||||||
|
return Err(BottomError::GenericError(format!(
|
||||||
|
"Error code {} - {}",
|
||||||
|
err_code.unwrap_or(-1),
|
||||||
|
err,
|
||||||
|
)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if cfg!(target_os = "windows") {
|
} else if cfg!(target_os = "windows") {
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
@ -24,7 +24,8 @@ impl KillDialog for Painter {
|
|||||||
if let Some(dd_err) = &app_state.dd_err {
|
if let Some(dd_err) = &app_state.dd_err {
|
||||||
return Some(vec![
|
return Some(vec![
|
||||||
Text::raw("\n"),
|
Text::raw("\n"),
|
||||||
Text::raw(format!("Failure to properly kill the process - {}", dd_err)),
|
Text::raw(format!("Failed to kill process.\n{}\n", dd_err)),
|
||||||
|
Text::raw("Please press ENTER or ESC to close this dialog."),
|
||||||
]);
|
]);
|
||||||
} else if let Some(to_kill_processes) = app_state.get_to_delete_processes() {
|
} else if let Some(to_kill_processes) = app_state.get_to_delete_processes() {
|
||||||
if let Some(first_pid) = to_kill_processes.1.first() {
|
if let Some(first_pid) = to_kill_processes.1.first() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user