Debian Codegraph @

src/sysprofd/ipc-service-impl.c

raw
1 /* ipc-service-impl.c
2 *
3 * Copyright 2019 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #define G_LOG_DOMAIN "ipc-service-impl"
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <gio/gunixfdlist.h>
28 #include <polkit/polkit.h>
29 #include <string.h>
30 #include <sys/syscall.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 #include "helpers.h"
35 #include "ipc-service-impl.h"
36
37 struct _IpcServiceImpl
38 {
39 IpcServiceSkeleton parent_instance;
40 };
41
42 enum {
43 ACTIVITY,
44 N_SIGNALS
45 };
46
47 static guint signals [N_SIGNALS];
48
49 static gboolean
50 file_set_contents_no_backup (const gchar *path,
51 const gchar *contents,
52 gssize len,
53 GError **error)
54 {
55 int fd;
56
57 g_return_val_if_fail (path != NULL, FALSE);
58
59 if (contents == NULL)
60 contents = "";
61
62 if (len < 0)
63 len = strlen (contents);
64
65 /* This is only for setting files in /sys, which need to be a single
66 * write anyway, so don't try to incrementally write at all.
67 */
68
69 fd = open (path, O_WRONLY, 0644);
70
71 if (fd == -1)
72 {
73 int errsv = errno;
74 g_set_error (error,
75 G_FILE_ERROR,
76 g_file_error_from_errno (errsv),
77 "%s", g_strerror (errsv));
78 return FALSE;
79 }
80
81 ftruncate (fd, 0);
82
83 if (write (fd, contents, len) != len)
84 {
85 int errsv = errno;
86 g_set_error (error,
87 G_FILE_ERROR,
88 g_file_error_from_errno (errsv),
89 "%s", g_strerror (errsv));
90 close (fd);
91 return FALSE;
92 }
93
94 close (fd);
95
96 return TRUE;
97 }
98
99 static gboolean
100 ipc_service_impl_handle_list_processes (IpcService *service,
101 GDBusMethodInvocation *invocation)
102 {
103 g_autofree gint32 *processes = NULL;
104 gsize n_processes = 0;
105
106 g_assert (IPC_IS_SERVICE_IMPL (service));
107 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
108
109 if (!helpers_list_processes (&processes, &n_processes))
110 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
111 G_DBUS_ERROR,
112 G_DBUS_ERROR_NOT_SUPPORTED,
113 "Failed to access processes");
114 else
115 ipc_service_complete_list_processes (service,
116 g_steal_pointer (&invocation),
117 g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
118 processes,
119 n_processes,
120 sizeof (gint32)));
121
122 return TRUE;
123 }
124
125 static gboolean
126 ipc_service_impl_handle_get_proc_file (IpcService *service,
127 GDBusMethodInvocation *invocation,
128 const gchar *path)
129 {
130 g_autofree gchar *contents = NULL;
131 gsize len;
132
133 g_assert (IPC_IS_SERVICE_IMPL (service));
134 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
135
136 if (!helpers_get_proc_file (path, &contents, &len))
137 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
138 G_DBUS_ERROR,
139 G_DBUS_ERROR_ACCESS_DENIED,
140 "Failed to load proc file");
141 else
142 ipc_service_complete_get_proc_file (service,
143 g_steal_pointer (&invocation),
144 contents);
145
146 return TRUE;
147 }
148
149 static gboolean
150 ipc_service_impl_handle_get_proc_fd (IpcService *service,
151 GDBusMethodInvocation *invocation,
152 GUnixFDList *in_fd_list,
153 const gchar *path)
154 {
155 g_autoptr(GFile) file = NULL;
156 g_autoptr(GError) error = NULL;
157 g_autofree gchar *canon = NULL;
158
159 g_assert (IPC_IS_SERVICE_IMPL (service));
160 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
161
162 file = g_file_new_for_path (path);
163 canon = g_file_get_path (file);
164
165 if (g_str_has_prefix (canon, "/proc/") || g_str_has_prefix (canon, "/sys/"))
166 {
167 gint fd = open (canon, O_RDONLY | O_CLOEXEC);
168
169 if (fd != -1)
170 {
171 g_autoptr(GUnixFDList) fd_list = g_unix_fd_list_new ();
172 gint handle = g_unix_fd_list_append (fd_list, fd, &error);
173
174 close (fd);
175
176 if (handle != -1)
177 {
178 ipc_service_complete_get_proc_fd (service,
179 g_steal_pointer (&invocation),
180 fd_list,
181 g_variant_new ("h", handle));
182 return TRUE;
183 }
184 }
185 }
186
187 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
188 G_DBUS_ERROR,
189 G_DBUS_ERROR_ACCESS_DENIED,
190 "Failed to load proc fd");
191
192 return TRUE;
193 }
194
195 static gboolean
196 ipc_service_impl_handle_perf_event_open (IpcService *service,
197 GDBusMethodInvocation *invocation,
198 GUnixFDList *in_fd_list,
199 GVariant *options,
200 gint32 pid,
201 gint32 cpu,
202 GVariant *group_fdv,
203 guint64 flags)
204 {
205 gint group_fd = -1;
206 gint out_fd = -1;
207 gint handle;
208
209 g_assert (IPC_IS_SERVICE_IMPL (service));
210 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
211
212 g_message ("PerfEventOpen(pid=%d, cpu=%d)", pid, cpu);
213
214 /* Get the group_fd if provided */
215 if (in_fd_list != NULL &&
216 (handle = g_variant_get_handle (group_fdv)) > -1)
217 group_fd = g_unix_fd_list_get (in_fd_list, handle, NULL);
218
219 errno = 0;
220 if (!helpers_perf_event_open (options, pid, cpu, group_fd, flags, &out_fd))
221 {
222 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
223 G_DBUS_ERROR,
224 G_DBUS_ERROR_FAILED,
225 "Failed to create perf counter: %s",
226 g_strerror (errno));
227 }
228 else
229 {
230 g_autoptr(GUnixFDList) out_fd_list = g_unix_fd_list_new ();
231 g_autoptr(GError) error = NULL;
232
233 if (-1 == (handle = g_unix_fd_list_append (out_fd_list, out_fd, &error)))
234 {
235 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
236 G_DBUS_ERROR,
237 G_DBUS_ERROR_LIMITS_EXCEEDED,
238 "Failed to create file-descriptor for reply");
239 }
240 else
241 {
242 ipc_service_complete_perf_event_open (service,
243 g_steal_pointer (&invocation),
244 out_fd_list,
245 g_variant_new ("h", handle));
246 }
247 }
248
249 if (out_fd != -1)
250 close (out_fd);
251
252 if (group_fd != -1)
253 close (group_fd);
254
255 return TRUE;
256 }
257
258 static gboolean
259 ipc_service_impl_g_authorize_method (GDBusInterfaceSkeleton *skeleton,
260 GDBusMethodInvocation *invocation)
261 {
262 PolkitAuthorizationResult *res = NULL;
263 PolkitAuthority *authority = NULL;
264 PolkitSubject *subject = NULL;
265 const gchar *peer_name;
266 gboolean ret = TRUE;
267
268 g_assert (IPC_IS_SERVICE_IMPL (skeleton));
269 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
270
271 g_signal_emit (skeleton, signals [ACTIVITY], 0);
272
273 peer_name = g_dbus_method_invocation_get_sender (invocation);
274
275 if (!(authority = polkit_authority_get_sync (NULL, NULL)) ||
276 !(subject = polkit_system_bus_name_new (peer_name)) ||
277 !(res = polkit_authority_check_authorization_sync (authority,
278 POLKIT_SUBJECT (subject),
279 "org.gnome.sysprof3.profile",
280 NULL,
281 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
282 NULL,
283 NULL)) ||
284 !polkit_authorization_result_get_is_authorized (res))
285 {
286 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
287 G_DBUS_ERROR,
288 G_DBUS_ERROR_ACCESS_DENIED,
289 "Not authorized to make request");
290 ret = FALSE;
291 }
292
293 g_clear_object (&authority);
294 g_clear_object (&subject);
295 g_clear_object (&res);
296
297 return ret;
298 }
299
300 static gboolean
301 ipc_service_impl_handle_get_process_info (IpcService *service,
302 GDBusMethodInvocation *invocation,
303 const gchar *attributes)
304 {
305 g_autoptr(GVariant) res = NULL;
306
307 g_assert (IPC_IS_SERVICE (service));
308 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
309 g_assert (attributes != NULL);
310
311 res = helpers_get_process_info (attributes);
312 ipc_service_complete_get_process_info (service, invocation, res);
313
314 return TRUE;
315 }
316
317 static gboolean
318 ipc_service_impl_handle_set_governor (IpcService *service,
319 GDBusMethodInvocation *invocation,
320 const gchar *governor)
321 {
322 g_autofree gchar *available = NULL;
323 g_autofree gchar *previous = NULL;
324 gboolean had_error = FALSE;
325 guint n_procs;
326
327 g_assert (IPC_IS_SERVICE (service));
328 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
329 g_assert (governor != NULL);
330
331 if (!g_file_get_contents ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", &available, NULL, NULL))
332 {
333 g_warning ("Failed to discover available governors");
334 had_error = TRUE;
335 goto finish;
336 }
337
338 if (strstr (available, governor) == NULL)
339 {
340 /* No such governor */
341 g_warning ("No such governor \"%s\"", governor);
342 had_error = TRUE;
343 goto finish;
344 }
345
346 if (g_file_get_contents ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", &previous, NULL, NULL))
347 g_strstrip (previous);
348 else
349 previous = g_strdup ("unknown");
350
351 n_procs = g_get_num_processors ();
352
353 for (guint i = 0; i < n_procs; i++)
354 {
355 g_autofree gchar *path = g_strdup_printf ("/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor", i);
356 g_autoptr(GError) error = NULL;
357
358 if (!file_set_contents_no_backup (path, governor, -1, &error))
359 {
360 g_warning ("Failed to set governor on CPU %u: %s", i, error->message);
361 had_error = TRUE;
362 }
363 }
364
365 finish:
366 if (had_error)
367 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
368 G_DBUS_ERROR,
369 G_DBUS_ERROR_FAILED,
370 "Failed to set governor");
371 else
372 ipc_service_complete_set_governor (service,
373 g_steal_pointer (&invocation),
374 previous);
375
376 return TRUE;
377 }
378
379 static gboolean
380 ipc_service_impl_handle_set_paranoid (IpcService *service,
381 GDBusMethodInvocation *invocation,
382 int paranoid)
383 {
384 g_autoptr(GError) error = NULL;
385 g_autofree gchar *str = NULL;
386 int previous = G_MAXINT;
387
388 g_assert (IPC_IS_SERVICE (service));
389 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
390
391 paranoid = CLAMP (paranoid, -1, 2);
392
393 if (!g_file_get_contents ("/proc/sys/kernel/perf_event_paranoid", &str, NULL, &error))
394 {
395 g_warning ("Failed to discover previous perf_event_paranoid setting: %s", error->message);
396 goto finish;
397 }
398
399 previous = g_ascii_strtoll (str, NULL, 10);
400
401 if (previous != paranoid)
402 {
403 char paranoid_str[8];
404 gssize len = g_snprintf (paranoid_str, sizeof paranoid_str, "%d", paranoid);
405
406 if (!file_set_contents_no_backup ("/proc/sys/kernel/perf_event_paranoid", paranoid_str, len, &error))
407 g_warning ("Failed to set perf_event_paranoid: %s", error->message);
408 }
409
410 finish:
411 if (error != NULL)
412 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
413 G_DBUS_ERROR,
414 G_DBUS_ERROR_FAILED,
415 "Failed to set perf_event_paranoid: %s",
416 error->message);
417 else
418 ipc_service_complete_set_paranoid (service,
419 g_steal_pointer (&invocation),
420 previous);
421
422 return TRUE;
423 }
424
425 static void
426 init_service_iface (IpcServiceIface *iface)
427 {
428 iface->handle_list_processes = ipc_service_impl_handle_list_processes;
429 iface->handle_get_proc_file = ipc_service_impl_handle_get_proc_file;
430 iface->handle_get_proc_fd = ipc_service_impl_handle_get_proc_fd;
431 iface->handle_perf_event_open = ipc_service_impl_handle_perf_event_open;
432 iface->handle_get_process_info = ipc_service_impl_handle_get_process_info;
433 iface->handle_set_governor = ipc_service_impl_handle_set_governor;
434 iface->handle_set_paranoid = ipc_service_impl_handle_set_paranoid;
435 }
436
437 G_DEFINE_TYPE_WITH_CODE (IpcServiceImpl, ipc_service_impl, IPC_TYPE_SERVICE_SKELETON,
438 G_IMPLEMENT_INTERFACE (IPC_TYPE_SERVICE, init_service_iface))
439
440 static void
441 ipc_service_impl_class_init (IpcServiceImplClass *klass)
442 {
443 GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
444
445 skeleton_class->g_authorize_method = ipc_service_impl_g_authorize_method;
446
447 signals [ACTIVITY] =
448 g_signal_new ("activity",
449 G_TYPE_FROM_CLASS (klass),
450 G_SIGNAL_RUN_LAST,
451 0, NULL, NULL,
452 NULL,
453 G_TYPE_NONE, 0);
454 }
455
456 static void
457 ipc_service_impl_init (IpcServiceImpl *self)
458 {
459 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (self),
460 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
461 }
462
463 IpcService *
464 ipc_service_impl_new (void)
465 {
466 return g_object_new (IPC_TYPE_SERVICE_IMPL, NULL);
467 }