From 62169fda03666bff7ce041e4e715a7455d997136 Mon Sep 17 00:00:00 2001 From: Maciej Date: Fri, 12 Nov 2021 17:28:04 +0100 Subject: [PATCH] SystemMonitor: Ask user before trying to kill/stop process These actions can be dangerous, especially for important system processes. Let's make accidental breaking of the system a bit harder. :^) --- Userland/Applications/SystemMonitor/main.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/SystemMonitor/main.cpp b/Userland/Applications/SystemMonitor/main.cpp index 8fe40ae721..bbf00015eb 100644 --- a/Userland/Applications/SystemMonitor/main.cpp +++ b/Userland/Applications/SystemMonitor/main.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -254,10 +255,20 @@ int main(int argc, char** argv) return pid_index.data().to_i32(); }; + auto selected_name = [&](ProcessModel::Column column) -> String { + if (process_table_view.selection().is_empty()) + return {}; + auto pid_index = process_table_view.model()->index(process_table_view.selection().first().row(), column); + return pid_index.data().to_string(); + }; + auto kill_action = GUI::Action::create( "&Kill Process", { Mod_Ctrl, Key_K }, { Key_Delete }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/kill.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) { pid_t pid = selected_id(ProcessModel::Column::PID); - if (pid != -1) + if (pid == -1) + return; + auto rc = GUI::MessageBox::show(window, String::formatted("Do you really want to kill \"{}\" (PID {})?", selected_name(ProcessModel::Column::Name), pid), "System Monitor", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); + if (rc == GUI::Dialog::ExecYes) kill(pid, SIGKILL); }, &process_table_view); @@ -265,7 +276,10 @@ int main(int argc, char** argv) auto stop_action = GUI::Action::create( "&Stop Process", { Mod_Ctrl, Key_S }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/stop-hand.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) { pid_t pid = selected_id(ProcessModel::Column::PID); - if (pid != -1) + if (pid == -1) + return; + auto rc = GUI::MessageBox::show(window, String::formatted("Do you really want to stop \"{}\" (PID {})?", selected_name(ProcessModel::Column::Name), pid), "System Monitor", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); + if (rc == GUI::Dialog::ExecYes) kill(pid, SIGSTOP); }, &process_table_view);