Debian Codegraph @

src/libsysprof/sysprof-mount-namespace.c

raw
1 /* sysprof-mount-namespace.c
2 *
3 * Copyright 2023 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 #include "config.h"
22
23 #include <gio/gio.h>
24
25 #include "timsort/gtktimsortprivate.h"
26
27 #include "sysprof-mount-namespace-private.h"
28
29 struct _SysprofMountNamespace
30 {
31 GObject parent_instance;
32 GPtrArray *devices;
33 GPtrArray *mounts;
34 guint mounts_dirty : 1;
35 };
36
37 static GType
38 sysprof_mount_namespace_get_item_type (GListModel *model)
39 {
40 return SYSPROF_TYPE_MOUNT;
41 }
42
43 static guint
44 sysprof_mount_namespace_get_n_items (GListModel *model)
45 {
46 return SYSPROF_MOUNT_NAMESPACE (model)->mounts->len;
47 }
48
49 static gpointer
50 sysprof_mount_namespace_get_item (GListModel *model,
51 guint position)
52 {
53 SysprofMountNamespace *self = SYSPROF_MOUNT_NAMESPACE (model);
54
55 if (position < self->mounts->len)
56 return g_object_ref (g_ptr_array_index (self->mounts, position));
57
58 return NULL;
59 }
60
61 static void
62 list_model_iface_init (GListModelInterface *iface)
63 {
64 iface->get_item_type = sysprof_mount_namespace_get_item_type;
65 iface->get_item = sysprof_mount_namespace_get_item;
66 iface->get_n_items = sysprof_mount_namespace_get_n_items;
67 }
68
69 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofMountNamespace, sysprof_mount_namespace, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
71
72 static void
73 sysprof_mount_namespace_finalize (GObject *object)
74 {
75 SysprofMountNamespace *self = (SysprofMountNamespace *)object;
76
77 g_clear_pointer (&self->devices, g_ptr_array_unref);
78 g_clear_pointer (&self->mounts, g_ptr_array_unref);
79
80 G_OBJECT_CLASS (sysprof_mount_namespace_parent_class)->finalize (object);
81 }
82
83 static void
84 sysprof_mount_namespace_class_init (SysprofMountNamespaceClass *klass)
85 {
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87
88 object_class->finalize = sysprof_mount_namespace_finalize;
89 }
90
91 static void
92 sysprof_mount_namespace_init (SysprofMountNamespace *self)
93 {
94 self->devices = g_ptr_array_new_with_free_func (g_object_unref);
95 self->mounts = g_ptr_array_new_with_free_func (g_object_unref);
96 }
97
98 SysprofMountNamespace *
99 sysprof_mount_namespace_new (void)
100 {
101 return g_object_new (SYSPROF_TYPE_MOUNT_NAMESPACE, NULL);
102 }
103
104 SysprofMountNamespace *
105 sysprof_mount_namespace_copy (SysprofMountNamespace *self)
106 {
107 SysprofMountNamespace *copy;
108
109 g_return_val_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self), NULL);
110
111 copy = sysprof_mount_namespace_new ();
112
113 for (guint i = 0; i < self->devices->len; i++)
114 sysprof_mount_namespace_add_device (copy, g_object_ref (g_ptr_array_index (self->devices, i)));
115
116 return copy;
117 }
118
119 /**
120 * sysprof_mount_namespace_add_device:
121 * @self: a #SysprofMountNamespace
122 * @device: (transfer full): a #SysprofMountDevice
123 *
124 * Adds information about where a device is mounted on the host for resolving
125 * paths to binaries.
126 */
127 void
128 sysprof_mount_namespace_add_device (SysprofMountNamespace *self,
129 SysprofMountDevice *device)
130 {
131 g_return_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self));
132 g_return_if_fail (SYSPROF_IS_MOUNT_DEVICE (device));
133
134 g_ptr_array_add (self->devices, device);
135 }
136
137 /**
138 * sysprof_mount_namespace_add_mount:
139 * @self: a #SysprofMountNamespace
140 * @mount: (transfer full): a #SysprofMount
141 *
142 */
143 void
144 sysprof_mount_namespace_add_mount (SysprofMountNamespace *self,
145 SysprofMount *mount)
146 {
147 g_return_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self));
148 g_return_if_fail (SYSPROF_IS_MOUNT (mount));
149
150 g_ptr_array_add (self->mounts, mount);
151
152 self->mounts_dirty = TRUE;
153 }
154
155 static SysprofMountDevice *
156 sysprof_mount_namespace_find_device (SysprofMountNamespace *self,
157 SysprofMount *mount,
158 const char *relative_path)
159 {
160 const char *mount_source;
161 g_autofree char *subvolume = NULL;
162
163 g_assert (SYSPROF_IS_MOUNT_NAMESPACE (self));
164 g_assert (SYSPROF_IS_MOUNT (mount));
165
166 while (relative_path[0] == '/')
167 relative_path++;
168
169 mount_source = sysprof_mount_get_mount_source (mount);
170 subvolume = sysprof_mount_get_superblock_option (mount, "subvol");
171
172 for (guint i = 0; i < self->devices->len; i++)
173 {
174 SysprofMountDevice *device = g_ptr_array_index (self->devices, i);
175 const char *fs_spec = sysprof_mount_device_get_fs_spec (device);
176
177 if (g_strcmp0 (fs_spec, mount_source) != 0)
178 continue;
179
180 if (subvolume != NULL)
181 {
182 const char *device_subvolume = sysprof_mount_device_get_subvolume (device);
183 const char *mount_point = sysprof_mount_device_get_mount_point (device);
184
185 if (g_strcmp0 (subvolume, device_subvolume) != 0)
186 continue;
187
188 /* Just ignore /sysroot, as it seems to be a convention on systems like
189 * Silverblue or GNOME OS.
190 */
191 if (g_strcmp0 (mount_point, "/sysroot") == 0)
192 continue;
193 }
194
195 return device;
196 }
197
198 return NULL;
199 }
200
201 static int
202 compare_mount (gconstpointer a,
203 gconstpointer b)
204 {
205 SysprofMount *mount_a = *(SysprofMount * const *)a;
206 SysprofMount *mount_b = *(SysprofMount * const *)b;
207 gsize alen = strlen (sysprof_mount_get_mount_point (mount_a));
208 gsize blen = strlen (sysprof_mount_get_mount_point (mount_b));
209
210 if (mount_a->is_overlay && !mount_b->is_overlay)
211 return -1;
212 else if (!mount_a->is_overlay && mount_b->is_overlay)
213 return 1;
214
215 if (alen > blen)
216 return -1;
217 else if (blen > alen)
218 return 1;
219
220 if (mount_a->layer < mount_b->layer)
221 return -1;
222 else if (mount_a->layer > mount_b->layer)
223 return 1;
224
225 return 0;
226 }
227
228 /**
229 * sysprof_mount_namespace_translate:
230 * @self: a #SysprofMountNamespace
231 * @file: the path within the mount namespace to translate
232 *
233 * Attempts to translate a path within the mount namespace into a
234 * path available in our current mount namespace.
235 *
236 * As overlays are involved, multiple paths may be returned which
237 * could contain the target file. You should check these starting
238 * from the first element in the resulting array to the last.
239 *
240 * Returns: (transfer full) (nullable): a UTF-8 encoded string array
241 * if successful; otherwise %NULL and @error is set.
242 */
243 char **
244 sysprof_mount_namespace_translate (SysprofMountNamespace *self,
245 const char *file)
246 {
247 g_autoptr(GArray) strv = NULL;
248
249 g_return_val_if_fail (SYSPROF_IS_MOUNT_NAMESPACE (self), NULL);
250 g_return_val_if_fail (file != NULL, NULL);
251
252 if G_UNLIKELY (self->mounts_dirty)
253 {
254 gtk_tim_sort (self->mounts->pdata,
255 self->mounts->len,
256 sizeof (gpointer),
257 (GCompareDataFunc)compare_mount,
258 NULL);
259 self->mounts_dirty = FALSE;
260 }
261
262 strv = g_array_new (TRUE, FALSE, sizeof (char *));
263
264 for (guint i = 0; i < self->mounts->len; i++)
265 {
266 SysprofMount *mount = g_ptr_array_index (self->mounts, i);
267 SysprofMountDevice *device;
268 const char *device_mount_point;
269 const char *fs_type;
270 const char *relative;
271 char *translated;
272
273 if (!(relative = _sysprof_mount_get_relative_path (mount, file)))
274 continue;
275
276 fs_type = sysprof_mount_get_filesystem_type (mount);
277
278 if (mount->is_overlay)
279 {
280 translated = g_build_filename (mount->mount_source, relative, NULL);
281 }
282 else if (g_strcmp0 (fs_type, "overlay") == 0)
283 {
284 g_autofree char *lowerdir_str = sysprof_mount_get_superblock_option (mount, "lowerdir");
285 g_autofree char *upperdir_str = sysprof_mount_get_superblock_option (mount, "upperdir");
286 g_auto(GStrv) lowerdirs = lowerdir_str ? g_strsplit (lowerdir_str, ":", 0) : NULL;
287 g_auto(GStrv) upperdirs = upperdir_str ? g_strsplit (upperdir_str, ":", 0) : NULL;
288
289 if (upperdirs != NULL)
290 {
291 for (guint j = 0; upperdirs[j]; j++)
292 {
293 translated = g_build_filename (upperdirs[j], relative, NULL);
294 g_array_append_val (strv, translated);
295 }
296 }
297
298 if (lowerdirs != NULL)
299 {
300 for (guint j = 0; lowerdirs[j]; j++)
301 {
302 translated = g_build_filename (lowerdirs[j], relative, NULL);
303 g_array_append_val (strv, translated);
304 }
305 }
306
307 continue;
308 }
309 else
310 {
311 const char *root;
312 const char *subvolume;
313
314 if (!(device = sysprof_mount_namespace_find_device (self, mount, relative)))
315 continue;
316
317 device_mount_point = sysprof_mount_device_get_mount_point (device);
318 root = sysprof_mount_get_root (mount);
319 subvolume = sysprof_mount_device_get_subvolume (device);
320
321 if (root != NULL && subvolume != NULL)
322 {
323 if (g_strcmp0 (root, subvolume) == 0)
324 root = "/";
325 else if (g_str_has_prefix (root, subvolume) && root[strlen (subvolume)] == '/')
326 root += strlen (subvolume);
327 }
328
329 translated = g_build_filename (device_mount_point, root, relative, NULL);
330 }
331
332 g_array_append_val (strv, translated);
333 }
334
335 if (strv->len == 0)
336 {
337 /* Try to passthrough the path in case we had no matches just to give
338 * things a chance to decode. This can happen if we never recorded
339 * the contents of /proc/$$/mountinfo.
340 */
341 char *copy = g_strdup (file);
342 g_array_append_val (strv, copy);
343 }
344
345 return (char **)g_array_free (g_steal_pointer (&strv), FALSE);
346 }