Files
zoitechat/plugins/sysinfo/unix/storage.c
T

232 lines
6.0 KiB
C
Raw Normal View History

2026-12-04 18:26:41 -06:00
/* SPDX-License-Identifier: GPL-2.0-or-later */
2026-12-04 19:23:49 -06:00
/* Whole capacity of the disk backing the boot filesystem. */
#define _XOPEN_SOURCE 700
2026-12-04 18:26:41 -06:00
#include <errno.h>
2026-12-04 19:23:49 -06:00
#include <stdlib.h>
2026-12-04 18:26:41 -06:00
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <glib.h>
#include "../sysinfo-backend.h"
#include "storage.h"
2026-12-04 19:23:49 -06:00
static gboolean
read_capacity (const char *entry, guint64 *capacity)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *path = g_build_filename (entry, "size", NULL);
char *contents = NULL;
char *end;
guint64 sectors;
gboolean valid = FALSE;
2026-12-04 18:26:41 -06:00
2026-12-04 19:23:49 -06:00
if (g_file_get_contents (path, &contents, NULL, NULL))
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
g_strstrip (contents);
if (g_ascii_isdigit (contents[0]))
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
errno = 0;
sectors = g_ascii_strtoull (contents, &end, 10);
/* Linux reports size in 512-byte sectors, including 4Kn disks. */
if (!errno && *end == '\0' && sectors && sectors <= G_MAXUINT64 / 512)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
*capacity = sectors * 512;
valid = TRUE;
2026-12-04 18:26:41 -06:00
}
}
}
2026-12-04 19:23:49 -06:00
g_free (contents);
g_free (path);
return valid;
2026-12-04 18:26:41 -06:00
}
2026-12-04 19:23:49 -06:00
static gboolean
has_entry (const char *entry, const char *name)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *path = g_build_filename (entry, name, NULL);
gboolean present = g_file_test (path, G_FILE_TEST_EXISTS);
g_free (path);
return present;
}
/* Resolve the kernel device link before walking from a partition to its
* parent. Never infer the parent by stripping digits from a device name. */
static gboolean
resolve_disk (const char *entry, char **disk, guint depth)
{
char *resolved = realpath (entry, NULL);
char *slaves = NULL;
GDir *dir = NULL;
const char *name;
2026-12-04 18:26:41 -06:00
gboolean valid = FALSE;
2026-12-04 19:23:49 -06:00
if (!resolved || depth >= 32)
goto done;
if (has_entry (resolved, "partition"))
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *parent = g_path_get_dirname (resolved);
valid = resolve_disk (parent, disk, depth + 1);
g_free (parent);
goto done;
}
/* Follow LUKS/LVM/MD layers. Multiple distinct underlying disks are
* ambiguous, so don't arbitrarily choose one of them. */
slaves = g_build_filename (resolved, "slaves", NULL);
dir = g_dir_open (slaves, 0, NULL);
if (dir && (name = g_dir_read_name (dir)) != NULL)
{
do
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *child = g_build_filename (slaves, name, NULL);
valid = resolve_disk (child, disk, depth + 1);
g_free (child);
if (!valid)
goto done;
} while ((name = g_dir_read_name (dir)) != NULL);
goto done;
2026-12-04 18:26:41 -06:00
}
2026-12-04 19:23:49 -06:00
name = strrchr (resolved, '/');
name = name ? name + 1 : resolved;
if (g_str_has_prefix (name, "loop") || g_str_has_prefix (name, "ram") ||
g_str_has_prefix (name, "zram") || g_str_has_prefix (name, "sr") ||
g_str_has_prefix (name, "fd") || g_str_has_prefix (name, "dm-") ||
g_str_has_prefix (name, "md") || !has_entry (resolved, "device"))
goto done;
if (!*disk)
*disk = g_strdup (resolved);
valid = strcmp (*disk, resolved) == 0;
done:
if (dir)
g_dir_close (dir);
g_free (slaves);
free (resolved);
2026-12-04 18:26:41 -06:00
return valid;
}
2026-12-04 19:23:49 -06:00
static guint
boot_mount_priority (const char *mountpoint)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
if (strcmp (mountpoint, "/boot") == 0)
return 4;
if (strcmp (mountpoint, "/boot/efi") == 0)
return 3;
if (strcmp (mountpoint, "/efi") == 0)
return 2;
if (strcmp (mountpoint, "/") == 0)
return 1;
return 0;
2026-12-04 18:26:41 -06:00
}
static gboolean
2026-12-04 19:23:49 -06:00
valid_device_number (const char *id)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
const char *p = id;
if (!g_ascii_isdigit (*p))
2026-12-04 18:26:41 -06:00
return FALSE;
2026-12-04 19:23:49 -06:00
while (g_ascii_isdigit (*p))
p++;
if (*p++ != ':' || !g_ascii_isdigit (*p))
2026-12-04 18:26:41 -06:00
return FALSE;
2026-12-04 19:23:49 -06:00
while (g_ascii_isdigit (*p))
p++;
return *p == '\0';
2026-12-04 18:26:41 -06:00
}
static char *
2026-12-04 19:23:49 -06:00
resolve_mount_device (const char *sysfs, const char *id, const char *source)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *entry = NULL;
2026-12-04 18:26:41 -06:00
struct stat st;
2026-12-04 19:23:49 -06:00
/* Btrfs and some other filesystems expose a synthetic mount device
* number. Prefer a verified source block device in that case. */
if (valid_device_number (id))
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
entry = g_build_filename (sysfs, "dev", "block", id, NULL);
if (g_file_test (entry, G_FILE_TEST_EXISTS))
return entry;
g_free (entry);
2026-12-04 18:26:41 -06:00
}
if (g_str_has_prefix (source, "/dev/") && stat (source, &st) == 0 && S_ISBLK (st.st_mode))
{
2026-12-04 19:23:49 -06:00
char *number = g_strdup_printf ("%u:%u", (unsigned int) major (st.st_rdev),
2026-12-04 18:26:41 -06:00
(unsigned int) minor (st.st_rdev));
2026-12-04 19:23:49 -06:00
entry = g_build_filename (sysfs, "dev", "block", number, NULL);
g_free (number);
return entry;
2026-12-04 18:26:41 -06:00
}
2026-12-04 19:23:49 -06:00
/* A sandbox may hide /dev nodes but still expose kernel block names.
* Only accept a direct /dev/<name>, never guess a mapper/UUID alias. */
if (g_str_has_prefix (source, "/dev/") && source[5] &&
strcmp (source + 5, ".") != 0 && strcmp (source + 5, "..") != 0 &&
!strchr (source + 5, '/') && !strchr (source + 5, '\\'))
return g_build_filename (sysfs, "class", "block", source + 5, NULL);
return NULL;
}
2026-12-04 18:26:41 -06:00
2026-12-04 19:23:49 -06:00
char *
sysinfo_linux_boot_disk (const char *sysfs, const char *mountinfo)
{
char **lines = g_strsplit (mountinfo, "\n", -1);
char *entry = NULL, *disk = NULL, *result = NULL;
guint priority = 0, i;
guint64 capacity;
gboolean ambiguous = FALSE;
2026-12-04 18:26:41 -06:00
2026-12-04 19:23:49 -06:00
for (i = 0; lines[i]; i++)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char **fields = g_strsplit (lines[i], " ", -1);
guint count = g_strv_length (fields), rank, j;
if (count < 10 || !(rank = boot_mount_priority (fields[4])) || rank < priority)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
g_strfreev (fields);
continue;
}
for (j = 6; j + 3 < count; j++)
if (strcmp (fields[j], "-") == 0)
2026-12-04 18:26:41 -06:00
{
2026-12-04 19:23:49 -06:00
char *candidate = resolve_mount_device (sysfs, fields[2], fields[j + 2]);
if (rank > priority)
{
g_free (entry);
entry = candidate;
priority = rank;
ambiguous = FALSE;
}
else
{
if (g_strcmp0 (entry, candidate) != 0)
ambiguous = TRUE;
g_free (candidate);
}
break;
2026-12-04 18:26:41 -06:00
}
2026-12-04 19:23:49 -06:00
g_strfreev (fields);
2026-12-04 18:26:41 -06:00
}
2026-12-04 19:23:49 -06:00
g_strfreev (lines);
2026-12-04 18:26:41 -06:00
2026-12-04 19:23:49 -06:00
if (!ambiguous && entry && resolve_disk (entry, &disk, 0) &&
read_capacity (disk, &capacity))
result = g_format_size (capacity);
g_free (disk);
g_free (entry);
2026-12-04 18:26:41 -06:00
return result;
}
char *
sysinfo_backend_get_disk (void)
{
char *mountinfo = NULL;
2026-12-04 19:23:49 -06:00
char *result;
2026-12-04 18:26:41 -06:00
2026-12-04 19:23:49 -06:00
/* The sandbox root is not the host boot filesystem. Without a trusted
* host mount table there is no reliable way to identify its boot disk. */
if (g_file_test ("/.flatpak-info", G_FILE_TEST_IS_REGULAR) ||
!g_file_get_contents ("/proc/self/mountinfo", &mountinfo, NULL, NULL))
2026-12-04 18:26:41 -06:00
return NULL;
2026-12-04 19:23:49 -06:00
result = sysinfo_linux_boot_disk ("/sys", mountinfo);
g_free (mountinfo);
return result;
2026-12-04 18:26:41 -06:00
}