| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | */ |
| 4 | |
| 5 | #include <config.h> |
| 6 | |
| 7 | #include <glib/gi18n.h> |
| 8 | #include <glibtop/procopenfiles.h> |
| 9 | |
| 10 | #include <sys/wait.h> |
| 11 | |
| 12 | #include "application.h" |
| 13 | #include "open-file.h" |
| 14 | #include "procinfo.h" |
| 15 | #include "util.h" |
| 16 | |
| 17 | #include "lsof.h" |
| 18 | |
| 19 | |
| 20 | struct _GsmLsof { |
| 21 | AdwWindow parent; |
| 22 | |
| 23 | GtkWidget *search_bar; |
| 24 | GtkWidget *search_entry; |
| 25 | |
| 26 | GListStore *lsof_store; |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | G_DEFINE_FINAL_TYPE (GsmLsof, gsm_lsof, ADW_TYPE_WINDOW) |
| 31 | |
| 32 | |
| 33 | static char * |
| 34 | format_title (G_GNUC_UNUSED GObject *object, |
| 35 | guint n_items, |
| 36 | const char *search) |
| 37 | { |
| 38 | if (search && search[0]) { |
| 39 | return g_strdup_printf (g_dngettext (G_LOG_DOMAIN, "%d Matching Open File", "%d Matching Open Files", n_items), n_items); |
| 40 | } |
| 41 | |
| 42 | return g_strdup_printf (g_dngettext (G_LOG_DOMAIN, "%d Open File", "%d Open Files", n_items), n_items); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static inline void |
| 47 | load_process_files (GPtrArray *files, ProcInfo *info) |
| 48 | { |
| 49 | g_autofree glibtop_open_files_entry *entries = NULL; |
| 50 | glibtop_proc_open_files buf; |
| 51 | |
| 52 | entries = glibtop_get_proc_open_files (&buf, info->pid); |
| 53 | |
| 54 | for (unsigned i = 0; i != buf.number; ++i) { |
| 55 | if (entries[i].type & GLIBTOP_FILE_TYPE_FILE) { |
| 56 | g_ptr_array_add (files, gsm_open_file_new (info, entries + i)); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | typedef struct _LoadFilesData LoadFilesData; |
| 63 | struct _LoadFilesData { |
| 64 | GPtrArray *processes; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | static void |
| 69 | load_files_data_free (gpointer data) |
| 70 | { |
| 71 | LoadFilesData *self = static_cast<LoadFilesData *>(data); |
| 72 | |
| 73 | g_clear_pointer (&self->processes, g_ptr_array_unref); |
| 74 | |
| 75 | g_free (self); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
| 80 | load_files_thread (GTask *task, |
| 81 | gpointer source_object, |
| 82 | gpointer task_data, |
| 83 | GCancellable *cancellable) |
| 84 | { |
| 85 | LoadFilesData *data = static_cast<LoadFilesData *>(task_data); |
| 86 | g_autoptr (GPtrArray) files = |
| 87 | g_ptr_array_new_null_terminated (data->processes->len * 10, |
| 88 | g_object_unref, |
| 89 | TRUE); |
| 90 | |
| 91 | for (size_t i = 0; i < data->processes->len; i++) { |
| 92 | load_process_files (files, |
| 93 | static_cast<ProcInfo *>(g_ptr_array_index (data->processes, i))); |
| 94 | } |
| 95 | |
| 96 | g_task_return_pointer (task, |
| 97 | g_steal_pointer (&files), |
| 98 | (GDestroyNotify) g_ptr_array_unref); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static GPtrArray * |
| 103 | load_files_finish (GsmLsof *self, GAsyncResult *result, GError **error) |
| 104 | { |
| 105 | g_return_val_if_fail (g_task_is_valid (result, self), NULL); |
| 106 | |
| 107 | return static_cast<GPtrArray *>(g_task_propagate_pointer (G_TASK (result), error)); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | load_files (GsmLsof *self, |
| 113 | GAsyncReadyCallback callback, |
| 114 | gpointer callback_data) |
| 115 | { |
| 116 | g_autoptr (GTask) task = g_task_new (self, NULL, callback, callback_data); |
| 117 | LoadFilesData *data = g_new0 (LoadFilesData, 1); |
| 118 | |
| 119 | data->processes = |
| 120 | g_ptr_array_new_null_terminated (1000, NULL, TRUE); |
| 121 | |
| 122 | g_task_set_name (task, "load_files"); |
| 123 | g_task_set_task_data (task, data, load_files_data_free); |
| 124 | |
| 125 | for (auto &v : GsmApplication::get().processes) { |
| 126 | g_ptr_array_add (data->processes, &v.second); |
| 127 | } |
| 128 | |
| 129 | g_task_run_in_thread (task, load_files_thread); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static void |
| 134 | did_load (GObject *source, GAsyncResult *result, gpointer user_data) |
| 135 | { |
| 136 | GsmLsof *self = GSM_LSOF (source); |
| 137 | g_autoptr (GError) error = NULL; |
| 138 | g_autoptr (GPtrArray) files = load_files_finish (self, result, &error); |
| 139 | guint old_length = g_list_model_get_n_items (G_LIST_MODEL (self->lsof_store)); |
| 140 | |
| 141 | if (error) { |
| 142 | g_warning ("Failed to refresh lsof: %s", error->message); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | g_list_store_splice (self->lsof_store, |
| 147 | 0, |
| 148 | old_length, |
| 149 | files->pdata, |
| 150 | files->len); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static void |
| 155 | refresh (GtkWidget *widget, |
| 156 | G_GNUC_UNUSED const char *action_name, |
| 157 | G_GNUC_UNUSED GVariant *target) |
| 158 | { |
| 159 | load_files (GSM_LSOF (widget), did_load, NULL); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | static void |
| 164 | gsm_lsof_class_init (GsmLsofClass *klass) |
| 165 | { |
| 166 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
| 167 | |
| 168 | gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/gnome-system-monitor/data/lsof.ui"); |
| 169 | |
| 170 | gtk_widget_class_bind_template_child (widget_class, GsmLsof, search_bar); |
| 171 | gtk_widget_class_bind_template_child (widget_class, GsmLsof, search_entry); |
| 172 | gtk_widget_class_bind_template_child (widget_class, GsmLsof, lsof_store); |
| 173 | |
| 174 | gtk_widget_class_bind_template_callback (widget_class, format_title); |
| 175 | |
| 176 | gtk_widget_class_install_action (widget_class, "lsof.refresh", NULL, refresh); |
| 177 | |
| 178 | g_type_ensure (GSM_TYPE_OPEN_FILE); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static void |
| 183 | gsm_lsof_init (GsmLsof *self) |
| 184 | { |
| 185 | gtk_widget_init_template (GTK_WIDGET (self)); |
| 186 | |
| 187 | gtk_search_bar_connect_entry (GTK_SEARCH_BAR (self->search_bar), |
| 188 | GTK_EDITABLE (self->search_entry)); |
| 189 | gtk_search_bar_set_key_capture_widget (GTK_SEARCH_BAR (self->search_bar), |
| 190 | GTK_WIDGET (self)); |
| 191 | |
| 192 | gtk_widget_activate_action (GTK_WIDGET (self), "lsof.refresh", NULL); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | GsmLsof * |
| 197 | gsm_lsof_new (GtkWindow *transient_for) |
| 198 | { |
| 199 | return GSM_LSOF (g_object_new (GSM_TYPE_LSOF, |
| 200 | "transient-for", transient_for, |
| 201 | NULL)); |
| 202 | } |