| 1 | /* Procman process actions |
| 2 | * Copyright (C) 2001 Kevin Vandersloot |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include <glib/gi18n.h> |
| 22 | |
| 23 | #include "application.h" |
| 24 | #include "gsm-actions.h" |
| 25 | #include "procdialogs.h" |
| 26 | #include "procinfo.h" |
| 27 | #include "proctable.h" |
| 28 | #include "settings-keys.h" |
| 29 | |
| 30 | #include "procactions.h" |
| 31 | |
| 32 | |
| 33 | typedef struct _PriorityData PriorityData; |
| 34 | struct _PriorityData { |
| 35 | GPtrArray *infos; |
| 36 | int priority; |
| 37 | GtkWidget *parent; |
| 38 | GsmApplication *app; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 43 | static void |
| 44 | build_priority_list (GtkTreeModel *model, |
| 45 | G_GNUC_UNUSED GtkTreePath *path, |
| 46 | GtkTreeIter *iter, |
| 47 | gpointer user_data) |
| 48 | { |
| 49 | PriorityData *data = static_cast<PriorityData *>(user_data); |
| 50 | ProcInfo *info; |
| 51 | |
| 52 | gtk_tree_model_get (model, iter, COL_POINTER, &info, -1); |
| 53 | |
| 54 | if (!info) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | g_ptr_array_add (data->infos, info); |
| 59 | } |
| 60 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | did_priority (GObject *source, GAsyncResult *result, gpointer user_data) |
| 65 | { |
| 66 | g_autoptr (GError) error = NULL; |
| 67 | PriorityData *data = static_cast<PriorityData *>(user_data); |
| 68 | |
| 69 | gsm_actions_set_priority_finish (GSM_ACTIONS (source), result, &error); |
| 70 | |
| 71 | if (error) { |
| 72 | AdwDialog *dialog = |
| 73 | adw_alert_dialog_new (_("Cannot Change Priority"), |
| 74 | error->message); |
| 75 | |
| 76 | adw_alert_dialog_add_response (ADW_ALERT_DIALOG (dialog), |
| 77 | "close", |
| 78 | _("_Close")); |
| 79 | |
| 80 | adw_dialog_present (dialog, data->parent); |
| 81 | } |
| 82 | |
| 83 | if (data->infos->len > 0) { |
| 84 | ProcInfo *info = |
| 85 | static_cast<ProcInfo *>(g_ptr_array_steal_index (data->infos, 0)); |
| 86 | |
| 87 | gsm_actions_set_priority (GSM_ACTIONS (source), |
| 88 | info->pid, |
| 89 | data->priority, |
| 90 | NULL, |
| 91 | did_priority, |
| 92 | data); |
| 93 | |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | proctable_thaw (data->app); |
| 98 | proctable_update (data->app); |
| 99 | |
| 100 | g_clear_pointer (&data->infos, g_ptr_array_unref); |
| 101 | g_clear_object (&data->parent); |
| 102 | g_free (data); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void |
| 107 | renice (GsmApplication *app, |
| 108 | int nice) |
| 109 | { |
| 110 | g_autoptr (GsmActions) actions = |
| 111 | GSM_ACTIONS (g_object_new (GSM_TYPE_ACTIONS, NULL)); |
| 112 | PriorityData *data = g_new0 (PriorityData, 1); |
| 113 | ProcInfo *info = NULL; |
| 114 | |
| 115 | /* EEEK - ugly hack - make sure the table is not updated as a crash |
| 116 | ** occurs if you first kill a process and the tree node is removed while |
| 117 | ** still in the foreach function |
| 118 | */ |
| 119 | proctable_freeze (app); |
| 120 | |
| 121 | data->priority = nice; |
| 122 | data->infos = g_ptr_array_new_null_terminated (10, NULL, TRUE); |
| 123 | g_set_object (&data->parent, GTK_WIDGET (app->main_window)); |
| 124 | data->app = app; |
| 125 | |
| 126 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 127 | gtk_tree_selection_selected_foreach (app->selection, |
| 128 | build_priority_list, |
| 129 | data); |
| 130 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 131 | |
| 132 | info = static_cast<ProcInfo *>(g_ptr_array_steal_index (data->infos, 0)); |
| 133 | gsm_actions_set_priority (actions, |
| 134 | info->pid, |
| 135 | nice, |
| 136 | NULL, |
| 137 | did_priority, |
| 138 | data); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void |
| 143 | on_activate_send_signal (GSimpleAction *action, |
| 144 | GVariant *value, |
| 145 | gpointer data) |
| 146 | { |
| 147 | GsmApplication *app = (GsmApplication *) data; |
| 148 | |
| 149 | const gchar *action_name = g_action_get_name (G_ACTION (action)); |
| 150 | gint32 proc = -1; |
| 151 | |
| 152 | if (value) |
| 153 | proc = g_variant_get_int32 (value); |
| 154 | |
| 155 | if (g_strcmp0 (action_name, "send-signal-cont") == 0) |
| 156 | kill_process (app, SIGCONT, proc); |
| 157 | else if (g_strcmp0 (action_name, "send-signal-stop") == 0) |
| 158 | kill_process_with_confirmation (app, SIGSTOP, proc); |
| 159 | else if (g_strcmp0 (action_name, "send-signal-term") == 0) |
| 160 | kill_process_with_confirmation (app, SIGTERM, proc); |
| 161 | else if (g_strcmp0 (action_name, "send-signal-kill") == 0) |
| 162 | kill_process_with_confirmation (app, SIGKILL, proc); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | kill_process_with_confirmation (GsmApplication *app, |
| 167 | int signal, |
| 168 | gint32 proc) |
| 169 | { |
| 170 | gboolean kill_dialog = app->settings->get_boolean (GSM_SETTING_SHOW_KILL_DIALOG); |
| 171 | |
| 172 | if (kill_dialog) |
| 173 | procdialog_create_kill_dialog (app, signal, proc); |
| 174 | else |
| 175 | kill_process (app, signal, proc); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | typedef struct _KillData KillData; |
| 180 | struct _KillData { |
| 181 | GPtrArray *infos; |
| 182 | int signal; |
| 183 | GtkWidget *parent; |
| 184 | GsmApplication *app; |
| 185 | }; |
| 186 | |
| 187 | |
| 188 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 189 | static void |
| 190 | build_kill_list (GtkTreeModel *model, |
| 191 | G_GNUC_UNUSED GtkTreePath *path, |
| 192 | GtkTreeIter *iter, |
| 193 | gpointer user_data) |
| 194 | { |
| 195 | KillData *data = static_cast<KillData *>(user_data); |
| 196 | ProcInfo *info; |
| 197 | |
| 198 | gtk_tree_model_get (model, iter, COL_POINTER, &info, -1); |
| 199 | |
| 200 | if (!info) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | g_ptr_array_add (data->infos, info); |
| 205 | } |
| 206 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 207 | |
| 208 | |
| 209 | static void |
| 210 | did_kill (GObject *source, GAsyncResult *result, gpointer user_data) |
| 211 | { |
| 212 | g_autoptr (GError) error = NULL; |
| 213 | KillData *data = static_cast<KillData *>(user_data); |
| 214 | |
| 215 | gsm_actions_kill_finish (GSM_ACTIONS (source), result, &error); |
| 216 | |
| 217 | if (error) { |
| 218 | AdwDialog *dialog = |
| 219 | adw_alert_dialog_new (_("Cannot Kill Process"), |
| 220 | error->message); |
| 221 | |
| 222 | adw_alert_dialog_add_response (ADW_ALERT_DIALOG (dialog), |
| 223 | "close", |
| 224 | _("_Close")); |
| 225 | |
| 226 | adw_dialog_present (dialog, data->parent); |
| 227 | } |
| 228 | |
| 229 | if (data->infos->len > 0) { |
| 230 | ProcInfo *info = |
| 231 | static_cast<ProcInfo *>(g_ptr_array_steal_index (data->infos, 0)); |
| 232 | |
| 233 | gsm_actions_kill (GSM_ACTIONS (source), |
| 234 | info->pid, |
| 235 | data->signal, |
| 236 | NULL, |
| 237 | did_kill, |
| 238 | data); |
| 239 | |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | proctable_thaw (data->app); |
| 244 | proctable_update (data->app); |
| 245 | |
| 246 | g_clear_pointer (&data->infos, g_ptr_array_unref); |
| 247 | g_clear_object (&data->parent); |
| 248 | g_free (data); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void |
| 253 | kill_process (GsmApplication *app, |
| 254 | int sig, |
| 255 | gint32 proc) |
| 256 | { |
| 257 | g_autoptr (GsmActions) actions = |
| 258 | GSM_ACTIONS (g_object_new (GSM_TYPE_ACTIONS, NULL)); |
| 259 | KillData *data = g_new0 (KillData, 1); |
| 260 | ProcInfo *info = NULL; |
| 261 | |
| 262 | /* EEEK - ugly hack - make sure the table is not updated as a crash |
| 263 | ** occurs if you first kill a process and the tree node is removed while |
| 264 | ** still in the foreach function |
| 265 | */ |
| 266 | proctable_freeze (app); |
| 267 | |
| 268 | data->signal = sig; |
| 269 | data->infos = g_ptr_array_new_null_terminated (10, NULL, TRUE); |
| 270 | g_set_object (&data->parent, GTK_WIDGET (app->main_window)); |
| 271 | data->app = app; |
| 272 | |
| 273 | if (proc == -1) { |
| 274 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 275 | gtk_tree_selection_selected_foreach (app->selection, |
| 276 | build_kill_list, |
| 277 | data); |
| 278 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 279 | } else { |
| 280 | g_ptr_array_add (data->infos, app->processes.find (proc)); |
| 281 | } |
| 282 | |
| 283 | info = static_cast<ProcInfo *>(g_ptr_array_steal_index (data->infos, 0)); |
| 284 | gsm_actions_kill (actions, |
| 285 | info->pid, |
| 286 | sig, |
| 287 | NULL, |
| 288 | did_kill, |
| 289 | data); |
| 290 | } |