Debian Codegraph @

src/libsysprof/sysprof-mount.c

raw
1 /* sysprof-mount.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 <stdio.h>
24
25 #include "sysprof-mount-private.h"
26
27 enum {
28 PROP_0,
29 PROP_DEVICE_MAJOR,
30 PROP_DEVICE_MINOR,
31 PROP_FILESYSTEM_TYPE,
32 PROP_MOUNT_ID,
33 PROP_MOUNT_POINT,
34 PROP_MOUNT_SOURCE,
35 PROP_PARENT_MOUNT_ID,
36 PROP_ROOT,
37 PROP_SUPERBLOCK_OPTIONS,
38 N_PROPS
39 };
40
41 G_DEFINE_FINAL_TYPE (SysprofMount, sysprof_mount, G_TYPE_OBJECT)
42
43 static GParamSpec *properties [N_PROPS];
44
45 static void
46 sysprof_mount_finalize (GObject *object)
47 {
48 SysprofMount *self = (SysprofMount *)object;
49
50 g_clear_pointer (&self->root, g_ref_string_release);
51 g_clear_pointer (&self->mount_point, g_ref_string_release);
52 g_clear_pointer (&self->mount_source, g_ref_string_release);
53 g_clear_pointer (&self->filesystem_type, g_ref_string_release);
54 g_clear_pointer (&self->superblock_options, g_ref_string_release);
55
56 G_OBJECT_CLASS (sysprof_mount_parent_class)->finalize (object);
57 }
58
59 static void
60 sysprof_mount_get_property (GObject *object,
61 guint prop_id,
62 GValue *value,
63 GParamSpec *pspec)
64 {
65 SysprofMount *self = SYSPROF_MOUNT (object);
66
67 switch (prop_id)
68 {
69 case PROP_DEVICE_MAJOR:
70 g_value_set_int (value, sysprof_mount_get_device_major (self));
71 break;
72
73 case PROP_DEVICE_MINOR:
74 g_value_set_int (value, sysprof_mount_get_device_minor (self));
75 break;
76
77 case PROP_ROOT:
78 g_value_set_string (value, sysprof_mount_get_root (self));
79 break;
80
81 case PROP_MOUNT_ID:
82 g_value_set_int (value, sysprof_mount_get_mount_id (self));
83 break;
84
85 case PROP_MOUNT_POINT:
86 g_value_set_string (value, sysprof_mount_get_mount_point (self));
87 break;
88
89 case PROP_MOUNT_SOURCE:
90 g_value_set_string (value, sysprof_mount_get_mount_source (self));
91 break;
92
93 case PROP_PARENT_MOUNT_ID:
94 g_value_set_int (value, sysprof_mount_get_parent_mount_id (self));
95 break;
96
97 case PROP_FILESYSTEM_TYPE:
98 g_value_set_string (value, sysprof_mount_get_filesystem_type (self));
99 break;
100
101 case PROP_SUPERBLOCK_OPTIONS:
102 g_value_set_string (value, sysprof_mount_get_superblock_options (self));
103 break;
104
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 }
108 }
109
110 static void
111 sysprof_mount_class_init (SysprofMountClass *klass)
112 {
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
115 object_class->finalize = sysprof_mount_finalize;
116 object_class->get_property = sysprof_mount_get_property;
117
118 properties[PROP_DEVICE_MAJOR] =
119 g_param_spec_int ("device-major", NULL, NULL,
120 G_MININT, G_MAXINT, 0,
121 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
122
123 properties[PROP_DEVICE_MINOR] =
124 g_param_spec_int ("device-minor", NULL, NULL,
125 G_MININT, G_MAXINT, 0,
126 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
127
128 properties[PROP_ROOT] =
129 g_param_spec_string ("root", NULL, NULL,
130 NULL,
131 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
132
133 properties[PROP_MOUNT_ID] =
134 g_param_spec_int ("mount-id", NULL, NULL,
135 G_MININT, G_MAXINT, 0,
136 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
137
138 properties[PROP_PARENT_MOUNT_ID] =
139 g_param_spec_int ("parent-mount-id", NULL, NULL,
140 G_MININT, G_MAXINT, 0,
141 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
142
143 properties[PROP_MOUNT_POINT] =
144 g_param_spec_string ("mount-point", NULL, NULL,
145 NULL,
146 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
147
148 properties[PROP_MOUNT_SOURCE] =
149 g_param_spec_string ("mount-source", NULL, NULL,
150 NULL,
151 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
152
153 properties[PROP_FILESYSTEM_TYPE] =
154 g_param_spec_string ("filesystem-type", NULL, NULL,
155 NULL,
156 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157
158 properties[PROP_SUPERBLOCK_OPTIONS] =
159 g_param_spec_string ("superblock-options", NULL, NULL,
160 NULL,
161 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
162
163 g_object_class_install_properties (object_class, N_PROPS, properties);
164 }
165
166 static void
167 sysprof_mount_init (SysprofMount *self)
168 {
169 }
170
171 SysprofMount *
172 _sysprof_mount_new_for_mountinfo (SysprofStrings *strings,
173 const char *mountinfo)
174 {
175 g_autoptr(SysprofMount) self = NULL;
176 g_auto(GStrv) parts = NULL;
177 gsize n_parts;
178 guint i;
179
180 g_return_val_if_fail (strings != NULL, NULL);
181 g_return_val_if_fail (mountinfo != NULL, NULL);
182
183 parts = g_strsplit (mountinfo, " ", 20);
184 n_parts = g_strv_length (parts);
185 if (n_parts < 10)
186 return NULL;
187
188 self = g_object_new (SYSPROF_TYPE_MOUNT, NULL);
189
190 self->mount_id = g_ascii_strtoll (parts[0], NULL, 10);
191 self->parent_mount_id = g_ascii_strtoll (parts[1], NULL, 10);
192 sscanf (parts[2], "%d:%d", &self->device_major, &self->device_minor);
193 self->root = sysprof_strings_get (strings, parts[3]);
194 self->mount_point = sysprof_strings_get (strings, parts[4]);
195
196 /* Skip forward to end of optional fields */
197 for (i = 5; parts[i]; i++)
198 {
199 if (strcmp (parts[i], "-") == 0)
200 break;
201 }
202
203 if (parts[i] == NULL)
204 goto finish;
205
206 /* filesystem-type column */
207 i++;
208 if (parts[i] == NULL)
209 goto finish;
210 self->filesystem_type = sysprof_strings_get (strings, parts[i]);
211
212 /* mount-source column */
213 i++;
214 if (parts[i] == NULL)
215 goto finish;
216 self->mount_source = sysprof_strings_get (strings, parts[i]);
217
218 /* superblock options column */
219 i++;
220 if (parts[i] == NULL)
221 goto finish;
222 self->superblock_options = sysprof_strings_get (strings, parts[i]);
223
224 finish:
225 return g_steal_pointer (&self);
226 }
227
228 SysprofMount *
229 _sysprof_mount_new_for_overlay (SysprofStrings *strings,
230 const char *mount_point,
231 const char *host_path,
232 int layer)
233 {
234 SysprofMount *self;
235
236 g_return_val_if_fail (strings != NULL, NULL);
237 g_return_val_if_fail (mount_point != NULL, NULL);
238 g_return_val_if_fail (host_path != NULL, NULL);
239
240 self = g_object_new (SYSPROF_TYPE_MOUNT, NULL);
241 self->mount_point = sysprof_strings_get (strings, mount_point);
242 self->root = sysprof_strings_get (strings, "/");
243 self->mount_source = sysprof_strings_get (strings, host_path);
244 self->is_overlay = TRUE;
245
246 return self;
247 }
248
249 int
250 sysprof_mount_get_device_major (SysprofMount *self)
251 {
252 return self->device_major;
253 }
254
255 int
256 sysprof_mount_get_device_minor (SysprofMount *self)
257 {
258 return self->device_minor;
259 }
260
261 const char *
262 sysprof_mount_get_root (SysprofMount *self)
263 {
264 return self->root;
265 }
266
267 const char *
268 sysprof_mount_get_mount_point (SysprofMount *self)
269 {
270 return self->mount_point;
271 }
272
273 const char *
274 sysprof_mount_get_mount_source (SysprofMount *self)
275 {
276 return self->mount_source;
277 }
278
279 const char *
280 sysprof_mount_get_filesystem_type (SysprofMount *self)
281 {
282 return self->filesystem_type;
283 }
284
285 const char *
286 sysprof_mount_get_superblock_options (SysprofMount *self)
287 {
288 return self->superblock_options;
289 }
290
291 char *
292 sysprof_mount_get_superblock_option (SysprofMount *self,
293 const char *option)
294 {
295 gsize option_len;
296
297 g_return_val_if_fail (SYSPROF_IS_MOUNT (self), NULL);
298 g_return_val_if_fail (option != NULL, NULL);
299
300 if (self->superblock_options == NULL)
301 return NULL;
302
303 option_len = strlen (option);
304
305 for (const char *c = strstr (self->superblock_options, option);
306 c != NULL;
307 c = strstr (c+1, option))
308 {
309 if ((c == self->superblock_options || c[-1] == ',') &&
310 (c[option_len] == 0 || c[option_len] == '='))
311 {
312 const char *value;
313 const char *endptr;
314
315 if (c[option_len] == 0)
316 return g_strdup ("");
317
318 value = &c[option_len+1];
319
320 if (!(endptr = strchr (value, ',')))
321 return g_strdup (value);
322
323 return g_strndup (value, endptr - value);
324 }
325 }
326
327 return NULL;
328 }
329
330 int
331 sysprof_mount_get_mount_id (SysprofMount *self)
332 {
333 return self->mount_id;
334 }
335
336 int
337 sysprof_mount_get_parent_mount_id (SysprofMount *self)
338 {
339 return self->parent_mount_id;
340 }
341
342 static inline gboolean
343 mount_is_root (SysprofMount *self)
344 {
345 return self->mount_point[0] == '/' && self->mount_point[1] == 0;
346 }
347
348 const char *
349 _sysprof_mount_get_relative_path (SysprofMount *self,
350 const char *path)
351 {
352 gsize len;
353
354 g_return_val_if_fail (SYSPROF_IS_MOUNT (self), NULL);
355
356 if (path == NULL || self->mount_point == NULL)
357 return NULL;
358
359 len = g_ref_string_length (self->mount_point);
360
361 /* We don't care about directory paths, so ensure that we both
362 * have the proper prefix and that it is in a subdirectory of this
363 * mount point.
364 */
365 if (!mount_is_root (self) &&
366 (!g_str_has_prefix (path, self->mount_point) || path[len] != '/'))
367 return NULL;
368
369 return &path[len];
370 }