From 2971f34541f8c3f496fec354080417ce6229c8aa Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 21 Mar 2025 12:00:14 -0700 Subject: [PATCH] libsysprof/util: add helper to get proc file This helper allows us to get the file locally or via sysprofd when the GDBusConnection is provided. Be careful about use as your local /proc may be different than the "system" /proc where sysprofd lives. --- src/libsysprof/meson.build | 1 + src/libsysprof/sysprof-util-private.h | 31 +++++++++++++ src/libsysprof/sysprof-util.c | 67 +++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/libsysprof/sysprof-util-private.h create mode 100644 src/libsysprof/sysprof-util.c diff --git a/src/libsysprof/meson.build b/src/libsysprof/meson.build index 2ae977ca..3a9c53b9 100644 --- a/src/libsysprof/meson.build +++ b/src/libsysprof/meson.build @@ -156,6 +156,7 @@ libsysprof_private_sources = [ 'sysprof-process-info.c', 'sysprof-strings.c', 'sysprof-symbol-cache.c', + 'sysprof-util.c', 'timsort/gtktimsort.c', gnome.gdbus_codegen('ipc-unwinder', diff --git a/src/libsysprof/sysprof-util-private.h b/src/libsysprof/sysprof-util-private.h new file mode 100644 index 00000000..f2ddfaf7 --- /dev/null +++ b/src/libsysprof/sysprof-util-private.h @@ -0,0 +1,31 @@ +/* + * sysprof-util-private.h + * + * Copyright 2025 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +DexFuture *sysprof_get_proc_file_bytes (GDBusConnection *connection, + const char *path); + +G_END_DECLS diff --git a/src/libsysprof/sysprof-util.c b/src/libsysprof/sysprof-util.c new file mode 100644 index 00000000..9f55dddd --- /dev/null +++ b/src/libsysprof/sysprof-util.c @@ -0,0 +1,67 @@ +/* + * sysprof-util.c + * + * Copyright 2025 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-util-private.h" + +DexFuture * +sysprof_get_proc_file_bytes (GDBusConnection *connection, + const char *path) +{ + g_autoptr(GError) error = NULL; + + dex_return_error_if_fail (!connection || G_IS_DBUS_CONNECTION (connection)); + dex_return_error_if_fail (path != NULL); + + if (connection != NULL) + { + g_autoptr(GVariant) reply = NULL; + g_autoptr(GBytes) bytes = NULL; + const char *contents = NULL; + + if (!(reply = dex_await_variant (dex_dbus_connection_call (connection, + "org.gnome.Sysprof3", + "/org/gnome/Sysprof3", + "org.gnome.Sysprof3.Service", + "GetProcFile", + g_variant_new ("(^ay)", path), + G_VARIANT_TYPE ("(ay)"), + G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, + G_MAXINT), + &error))) + return dex_future_new_for_error (g_steal_pointer (&error)); + + g_variant_get (reply, "(^&ay)", &contents); + + g_assert (contents != NULL); + + return dex_future_new_take_boxed (G_TYPE_BYTES, + g_bytes_new_with_free_func (contents, strlen (contents), + (GDestroyNotify) g_variant_unref, + g_steal_pointer (&reply))); + } + else + { + g_autoptr(GFile) file = g_file_new_for_path (path); + return dex_file_load_contents_bytes (file); + } +}