Debian Codegraph @

src/open-file.cpp

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include "config.h"
6
7 #include <glib/gi18n.h>
8
9 #include <glibtop/procopenfiles.h>
10 #include <sys/stat.h>
11 #include <netdb.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 #include "procinfo.h"
18
19 #include "open-file.h"
20
21
22 #ifndef NI_IDN
23 const int NI_IDN = 0;
24 #endif
25
26
27 struct _GsmOpenFile {
28 GObject parent_instance;
29
30 ProcInfo *info;
31 glibtop_open_files_entry *entry;
32
33 pid_t pid;
34 char *process;
35 GdkPaintable *icon;
36
37 int fd;
38 char *type;
39 char *object;
40 };
41
42
43 G_DEFINE_FINAL_TYPE (GsmOpenFile, gsm_open_file, G_TYPE_OBJECT)
44
45
46 enum {
47 PROP_0,
48 PROP_INFO,
49 PROP_ENTRY,
50 PROP_PID,
51 PROP_PROCESS,
52 PROP_ICON,
53 PROP_FD,
54 PROP_TYPE,
55 PROP_OBJECT,
56 N_PROPS
57 };
58 static GParamSpec *properties[N_PROPS];
59
60
61 static void
62 gsm_open_file_dispose (GObject *object)
63 {
64 GsmOpenFile *self = GSM_OPEN_FILE (object);
65
66 g_clear_pointer (&self->entry, g_free);
67 g_clear_pointer (&self->process, g_free);
68 g_clear_object (&self->icon);
69 g_clear_pointer (&self->type, g_free);
70 g_clear_pointer (&self->object, g_free);
71
72 G_OBJECT_CLASS (gsm_open_file_parent_class)->dispose (object);
73 }
74
75
76 static void
77 gsm_open_file_get_property (GObject *object,
78 guint property_id,
79 GValue *value,
80 GParamSpec *pspec)
81 {
82 GsmOpenFile *self = GSM_OPEN_FILE (object);
83
84 switch (property_id) {
85 case PROP_INFO:
86 g_value_set_pointer (value, self->info);
87 break;
88 case PROP_ENTRY:
89 g_value_set_boxed (value, self->entry);
90 break;
91 case PROP_PID:
92 g_value_set_int (value, self->pid);
93 break;
94 case PROP_PROCESS:
95 g_value_set_string (value, self->process);
96 break;
97 case PROP_ICON:
98 g_value_set_object (value, self->icon);
99 break;
100 case PROP_FD:
101 g_value_set_int (value, self->fd);
102 break;
103 case PROP_TYPE:
104 g_value_set_string (value, self->type);
105 break;
106 case PROP_OBJECT:
107 g_value_set_string (value, self->object);
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111 }
112 }
113
114
115 static inline void
116 set_info (GsmOpenFile *self, ProcInfo *info)
117 {
118 g_return_if_fail (GSM_IS_OPEN_FILE (self));
119
120 if (info == NULL) {
121 self->pid = 0;
122 g_clear_pointer (&self->process, g_free);
123 g_clear_object (&self->icon);
124 self->info = NULL;
125 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PID]);
126 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PROCESS]);
127 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ICON]);
128 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INFO]);
129 return;
130 }
131
132 g_object_freeze_notify (G_OBJECT (self));
133
134 if (self->pid != info->pid) {
135 self->pid = info->pid;
136 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PID]);
137 }
138
139 if (g_set_str (&self->process, info->name.c_str ())) {
140 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PROCESS]);
141 }
142
143 if (g_set_object (&self->icon, GDK_PAINTABLE (info->icon->gobj ()))) {
144 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ICON]);
145 }
146
147 self->info = info;
148 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INFO]);
149
150 g_object_thaw_notify (G_OBJECT (self));
151 }
152
153
154 static void
155 gsm_open_file_set_property (GObject *object,
156 guint property_id,
157 const GValue *value,
158 GParamSpec *pspec)
159 {
160 GsmOpenFile *self = GSM_OPEN_FILE (object);
161
162 switch (property_id) {
163 case PROP_INFO:
164 set_info (self, static_cast<ProcInfo *>(g_value_get_pointer (value)));
165 break;
166 case PROP_ENTRY:
167 gsm_open_file_set_entry (self,
168 static_cast<glibtop_open_files_entry *>(g_value_get_boxed (value)));
169 break;
170 default:
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
172 }
173 }
174
175
176 static void
177 gsm_open_file_class_init (GsmOpenFileClass *klass)
178 {
179 GObjectClass *object_class = G_OBJECT_CLASS (klass);
180
181 object_class->dispose = gsm_open_file_dispose;
182 object_class->get_property = gsm_open_file_get_property;
183 object_class->set_property = gsm_open_file_set_property;
184
185 properties[PROP_INFO] =
186 g_param_spec_pointer ("info", NULL, NULL,
187 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
188 properties[PROP_ENTRY] =
189 g_param_spec_boxed ("entry", NULL, NULL,
190 glibtop_open_files_entry_get_type (),
191 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
192 properties[PROP_PID] =
193 g_param_spec_int ("pid", NULL, NULL,
194 0, G_MAXINT, 0,
195 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
196 properties[PROP_PROCESS] =
197 g_param_spec_string ("process", NULL, NULL,
198 NULL,
199 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
200 properties[PROP_ICON] =
201 g_param_spec_object ("icon", NULL, NULL,
202 GDK_TYPE_PAINTABLE,
203 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
204 properties[PROP_FD] =
205 g_param_spec_int ("fd", NULL, NULL,
206 0, G_MAXINT, 0,
207 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
208 properties[PROP_TYPE] =
209 g_param_spec_string ("type", NULL, NULL,
210 NULL,
211 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
212 properties[PROP_OBJECT] =
213 g_param_spec_string ("object", NULL, NULL,
214 NULL,
215 (GParamFlags) (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
216
217 g_object_class_install_properties (object_class, N_PROPS, properties);
218 }
219
220
221 static void
222 gsm_open_file_init (GsmOpenFile *)
223 {
224 }
225
226
227 GsmOpenFile *
228 gsm_open_file_new (ProcInfo *info,
229 glibtop_open_files_entry *entry)
230 {
231 return GSM_OPEN_FILE (g_object_new (GSM_TYPE_OPEN_FILE,
232 "info", info,
233 "entry", entry,
234 NULL));
235 }
236
237
238 static const char *
239 get_type_name (enum glibtop_file_type t)
240 {
241 switch (t) {
242 case GLIBTOP_FILE_TYPE_FILE:
243 return _("file");
244 case GLIBTOP_FILE_TYPE_PIPE:
245 return _("pipe");
246 case GLIBTOP_FILE_TYPE_INET6SOCKET:
247 return _("IPv6 network connection");
248 case GLIBTOP_FILE_TYPE_INETSOCKET:
249 return _("IPv4 network connection");
250 case GLIBTOP_FILE_TYPE_LOCALSOCKET:
251 return _("local socket");
252 default:
253 return _("unknown type");
254 }
255 }
256
257
258 static char *
259 friendlier_hostname (const char *addr_str,
260 int port)
261 {
262 struct addrinfo hints = { };
263 struct addrinfo *res = NULL;
264 char hostname[NI_MAXHOST];
265 char service[NI_MAXSERV];
266 char port_str[6];
267
268 if (!addr_str[0])
269 return g_strdup ("");
270
271 snprintf (port_str, sizeof port_str, "%d", port);
272
273 hints.ai_family = AF_UNSPEC;
274 hints.ai_socktype = SOCK_STREAM;
275
276 if (getaddrinfo (addr_str, port_str, &hints, &res))
277 goto failsafe;
278
279 if (getnameinfo (res->ai_addr, res->ai_addrlen, hostname,
280 sizeof hostname, service, sizeof service, NI_IDN))
281 goto failsafe;
282
283 if (res)
284 freeaddrinfo (res);
285 return g_strdup_printf ("%s, TCP port %d (%s)", hostname, port, service);
286
287 failsafe:
288 if (res)
289 freeaddrinfo (res);
290 return g_strdup_printf ("%s, TCP port %d", addr_str, port);
291 }
292
293
294 static char *
295 describe_entry (glibtop_open_files_entry *entry)
296 {
297 switch (entry->type) {
298 case GLIBTOP_FILE_TYPE_FILE:
299 return g_strdup (entry->info.file.name);
300 case GLIBTOP_FILE_TYPE_INET6SOCKET:
301 case GLIBTOP_FILE_TYPE_INETSOCKET:
302 return friendlier_hostname (entry->info.sock.dest_host,
303 entry->info.sock.dest_port);
304 case GLIBTOP_FILE_TYPE_LOCALSOCKET:
305 return g_strdup (entry->info.localsock.name);
306 }
307
308 return NULL;
309 }
310
311
312 void
313 gsm_open_file_set_entry (GsmOpenFile *self,
314 glibtop_open_files_entry *entry)
315 {
316 g_autofree char *description = NULL;
317
318 g_return_if_fail (GSM_IS_OPEN_FILE (self));
319
320 if (entry == NULL && self->entry == NULL) {
321 return;
322 }
323
324 if (entry == NULL) {
325 self->fd = 0;
326 g_clear_pointer (&self->entry, g_free);
327 g_clear_pointer (&self->type, g_free);
328 g_clear_pointer (&self->object, g_free);
329 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FD]);
330 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TYPE]);
331 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_OBJECT]);
332 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENTRY]);
333 return;
334 }
335
336 description = describe_entry (entry);
337
338 g_object_freeze_notify (G_OBJECT (self));
339
340 if (self->fd != entry->fd) {
341 self->fd = entry->fd;
342 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FD]);
343 }
344
345 if (g_set_str (&self->type,
346 get_type_name (static_cast<glibtop_file_type>(entry->type)))) {
347 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TYPE]);
348 }
349
350 if (g_set_str (&self->object, description)) {
351 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_OBJECT]);
352 }
353
354 g_free (self->entry);
355 self->entry =
356 static_cast<glibtop_open_files_entry *>(g_boxed_copy (glibtop_open_files_entry_get_type (),
357 static_cast<gconstpointer>(entry)));
358 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENTRY]);
359
360 g_object_thaw_notify (G_OBJECT (self));
361 }