From ee957ed1d4fa13676efd14bda9f9e89e4557d550 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 5 May 2023 12:44:31 -0700 Subject: [PATCH] libsysprof-analyze: start on mount namespace APIs The goal here is that we can try to reproduce to some degree the mount namespace as the PID saw it, but also convert that to paths where we can find the files on the host system. --- src/libsysprof-analyze/meson.build | 2 + .../sysprof-mount-device-private.h | 42 ++++ src/libsysprof-analyze/sysprof-mount-device.c | 195 ++++++++++++++++++ .../sysprof-mount-namespace-private.h | 46 +++++ .../sysprof-mount-namespace.c | 94 +++++++++ 5 files changed, 379 insertions(+) create mode 100644 src/libsysprof-analyze/sysprof-mount-device-private.h create mode 100644 src/libsysprof-analyze/sysprof-mount-device.c create mode 100644 src/libsysprof-analyze/sysprof-mount-namespace-private.h create mode 100644 src/libsysprof-analyze/sysprof-mount-namespace.c diff --git a/src/libsysprof-analyze/meson.build b/src/libsysprof-analyze/meson.build index d0ba82a3..bbe14fe5 100644 --- a/src/libsysprof-analyze/meson.build +++ b/src/libsysprof-analyze/meson.build @@ -22,6 +22,8 @@ libsysprof_analyze_public_sources = [ libsysprof_analyze_private_sources = [ 'sysprof-mapped-file.c', 'sysprof-memory-map.c', + 'sysprof-mount-device.c', + 'sysprof-mount-namespace.c', ] libsysprof_analyze_public_headers = [ diff --git a/src/libsysprof-analyze/sysprof-mount-device-private.h b/src/libsysprof-analyze/sysprof-mount-device-private.h new file mode 100644 index 00000000..cfebc3bd --- /dev/null +++ b/src/libsysprof-analyze/sysprof-mount-device-private.h @@ -0,0 +1,42 @@ +/* sysprof-mount-device-private.h + * + * Copyright 2023 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 + +#define SYSPROF_TYPE_MOUNT_DEVICE (sysprof_mount_device_get_type()) + +G_DECLARE_FINAL_TYPE (SysprofMountDevice, sysprof_mount_device, SYSPROF, MOUNT_DEVICE, GObject) + +SysprofMountDevice *sysprof_mount_device_new (void); +const char *sysprof_mount_device_get_id (SysprofMountDevice *self); +void sysprof_mount_device_set_id (SysprofMountDevice *self, + const char *id); +const char *sysprof_mount_device_get_mount_path (SysprofMountDevice *self); +void sysprof_mount_device_set_mount_path (SysprofMountDevice *self, + const char *mount_path); +const char *sysprof_mount_device_get_subvolume (SysprofMountDevice *self); +void sysprof_mount_device_set_subvolume (SysprofMountDevice *self, + const char *subvolume); + +G_END_DECLS diff --git a/src/libsysprof-analyze/sysprof-mount-device.c b/src/libsysprof-analyze/sysprof-mount-device.c new file mode 100644 index 00000000..e93002b1 --- /dev/null +++ b/src/libsysprof-analyze/sysprof-mount-device.c @@ -0,0 +1,195 @@ +/* sysprof-mount-device.c + * + * Copyright 2023 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-mount-device-private.h" + +struct _SysprofMountDevice +{ + GObject parent_instance; + char *id; + char *mount_path; + char *subvolume; +}; + +enum { + PROP_0, + PROP_ID, + PROP_MOUNT_PATH, + PROP_SUBVOLUME, + N_PROPS +}; + +G_DEFINE_FINAL_TYPE (SysprofMountDevice, sysprof_mount_device, G_TYPE_OBJECT) + +static GParamSpec *properties [N_PROPS]; + +static void +sysprof_mount_device_finalize (GObject *object) +{ + SysprofMountDevice *self = (SysprofMountDevice *)object; + + g_clear_pointer (&self->id, g_free); + g_clear_pointer (&self->mount_path, g_free); + g_clear_pointer (&self->subvolume, g_free); + + G_OBJECT_CLASS (sysprof_mount_device_parent_class)->finalize (object); +} + +static void +sysprof_mount_device_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofMountDevice *self = SYSPROF_MOUNT_DEVICE (object); + + switch (prop_id) + { + case PROP_ID: + g_value_set_string (value, sysprof_mount_device_get_id (self)); + break; + + case PROP_MOUNT_PATH: + g_value_set_string (value, sysprof_mount_device_get_mount_path (self)); + break; + + case PROP_SUBVOLUME: + g_value_set_string (value, sysprof_mount_device_get_subvolume (self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_mount_device_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + SysprofMountDevice *self = SYSPROF_MOUNT_DEVICE (object); + + switch (prop_id) + { + case PROP_ID: + sysprof_mount_device_set_id (self, g_value_get_string (value)); + break; + + case PROP_MOUNT_PATH: + sysprof_mount_device_set_mount_path (self, g_value_get_string (value)); + break; + + case PROP_SUBVOLUME: + sysprof_mount_device_set_subvolume (self, g_value_get_string (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_mount_device_class_init (SysprofMountDeviceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = sysprof_mount_device_finalize; + object_class->get_property = sysprof_mount_device_get_property; + object_class->set_property = sysprof_mount_device_set_property; + + properties [PROP_ID] = + g_param_spec_string ("id", NULL, NULL, + NULL, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + properties [PROP_MOUNT_PATH] = + g_param_spec_string ("mount-path", NULL, NULL, + NULL, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + properties [PROP_SUBVOLUME] = + g_param_spec_string ("subvolume", NULL, NULL, + NULL, + (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_mount_device_init (SysprofMountDevice *self) +{ +} + +const char * +sysprof_mount_device_get_id (SysprofMountDevice *self) +{ + g_return_val_if_fail (SYSPROF_IS_MOUNT_DEVICE (self), NULL); + + return self->id; +} + +void +sysprof_mount_device_set_id (SysprofMountDevice *self, + const char *id) +{ + g_return_if_fail (SYSPROF_IS_MOUNT_DEVICE (self)); + + if (g_set_str (&self->id, id)) + g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ID]); +} + +const char * +sysprof_mount_device_get_mount_path (SysprofMountDevice *self) +{ + g_return_val_if_fail (SYSPROF_IS_MOUNT_DEVICE (self), NULL); + + return self->mount_path; +} + +void +sysprof_mount_device_set_mount_path (SysprofMountDevice *self, + const char *mount_path) +{ + g_return_if_fail (SYSPROF_IS_MOUNT_DEVICE (self)); + + if (g_set_str (&self->mount_path, mount_path)) + g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MOUNT_PATH]); +} + +const char * +sysprof_mount_device_get_subvolume (SysprofMountDevice *self) +{ + g_return_val_if_fail (SYSPROF_IS_MOUNT_DEVICE (self), NULL); + + return self->subvolume; +} + +void +sysprof_mount_device_set_subvolume (SysprofMountDevice *self, + const char *subvolume) +{ + g_return_if_fail (SYSPROF_IS_MOUNT_DEVICE (self)); + + if (g_set_str (&self->subvolume, subvolume)) + g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SUBVOLUME]); +} diff --git a/src/libsysprof-analyze/sysprof-mount-namespace-private.h b/src/libsysprof-analyze/sysprof-mount-namespace-private.h new file mode 100644 index 00000000..1a5cea0b --- /dev/null +++ b/src/libsysprof-analyze/sysprof-mount-namespace-private.h @@ -0,0 +1,46 @@ +/* sysprof-mount-namespace-private.h + * + * Copyright 2023 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 + +#include "sysprof-mount-device-private.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_MOUNT_NAMESPACE (sysprof_mount_namespace_get_type()) + +G_DECLARE_FINAL_TYPE (SysprofMountNamespace, sysprof_mount_namespace, SYSPROF, MOUNT_NAMESPACE, GObject) + +SysprofMountNamespace *sysprof_mount_namespace_new (void); +SysprofMountNamespace *sysprof_mount_namespace_copy (SysprofMountNamespace *self); +void sysprof_mount_namespace_add_device (SysprofMountNamespace *self, + SysprofMountDevice *mount); +void sysprof_mount_namespace_add_mount (SysprofMountNamespace *self, + const char *path, + const char *host_path, + int layer); +char **sysprof_mount_namespace_translate (SysprofMountNamespace *self, + const char *path); +GMappedFile *sysprof_mount_namespace_open (SysprofMountNamespace *self, + const char *path); + +G_END_DECLS diff --git a/src/libsysprof-analyze/sysprof-mount-namespace.c b/src/libsysprof-analyze/sysprof-mount-namespace.c new file mode 100644 index 00000000..c660b5ed --- /dev/null +++ b/src/libsysprof-analyze/sysprof-mount-namespace.c @@ -0,0 +1,94 @@ +/* sysprof-mount-namespace.c + * + * Copyright 2023 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-mount-namespace-private.h" + +struct _SysprofMountNamespace +{ + GObject parent_instance; + GPtrArray *devices; +}; + +G_DEFINE_FINAL_TYPE (SysprofMountNamespace, sysprof_mount_namespace, G_TYPE_OBJECT) + +static void +sysprof_mount_namespace_finalize (GObject *object) +{ + SysprofMountNamespace *self = (SysprofMountNamespace *)object; + + g_clear_pointer (&self->devices, g_ptr_array_unref); + + G_OBJECT_CLASS (sysprof_mount_namespace_parent_class)->finalize (object); +} + +static void +sysprof_mount_namespace_class_init (SysprofMountNamespaceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = sysprof_mount_namespace_finalize; +} + +static void +sysprof_mount_namespace_init (SysprofMountNamespace *self) +{ + self->devices = g_ptr_array_new_with_free_func (g_object_unref); +} + +SysprofMountNamespace * +sysprof_mount_namespace_new (void) +{ + return g_object_new (SYSPROF_TYPE_MOUNT_NAMESPACE, NULL); +} + +SysprofMountNamespace * +sysprof_mount_namespace_copy (SysprofMountNamespace *self) +{ + SysprofMountNamespace *copy; + + g_return_val_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self), NULL); + + copy = sysprof_mount_namespace_new (); + + for (guint i = 0; i < self->devices->len; i++) + sysprof_mount_namespace_add_device (copy, g_object_ref (g_ptr_array_index (self->devices, i))); + + return copy; +} + +/** + * sysprof_mount_namespace_add_device: + * @self: a #SysprofMountNamespace + * @device: (transfer full): a #SysprofMountDevice + * + * Adds information about where a device is mounted on the host for resolving + * paths to binaries. + */ +void +sysprof_mount_namespace_add_device (SysprofMountNamespace *self, + SysprofMountDevice *device) +{ + g_return_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self)); + g_return_if_fail (SYSPROF_IS_MOUNT_DEVICE (device)); + + g_ptr_array_add (self->devices, device); +}