mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-12-05 14:31:49 +00:00
233 lines
6.0 KiB
C
233 lines
6.0 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
/* Whole capacity of the disk backing the boot filesystem. */
|
|
#define _XOPEN_SOURCE 700
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/sysmacros.h>
|
|
#include <glib.h>
|
|
#include "../sysinfo-backend.h"
|
|
#include "storage.h"
|
|
|
|
static gboolean
|
|
read_capacity (const char *entry, guint64 *capacity)
|
|
{
|
|
char *path = g_build_filename (entry, "size", NULL);
|
|
char *contents = NULL;
|
|
char *end;
|
|
guint64 sectors;
|
|
gboolean valid = FALSE;
|
|
|
|
if (g_file_get_contents (path, &contents, NULL, NULL))
|
|
{
|
|
g_strstrip (contents);
|
|
if (g_ascii_isdigit (contents[0]))
|
|
{
|
|
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)
|
|
{
|
|
*capacity = sectors * 512;
|
|
valid = TRUE;
|
|
}
|
|
}
|
|
}
|
|
g_free (contents);
|
|
g_free (path);
|
|
return valid;
|
|
}
|
|
|
|
static gboolean
|
|
has_entry (const char *entry, const char *name)
|
|
{
|
|
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;
|
|
gboolean valid = FALSE;
|
|
|
|
if (!resolved || depth >= 32)
|
|
goto done;
|
|
if (has_entry (resolved, "partition"))
|
|
{
|
|
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
|
|
{
|
|
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;
|
|
}
|
|
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);
|
|
return valid;
|
|
}
|
|
|
|
static guint
|
|
boot_mount_priority (const char *mountpoint)
|
|
{
|
|
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;
|
|
}
|
|
|
|
static gboolean
|
|
valid_device_number (const char *id)
|
|
{
|
|
const char *p = id;
|
|
if (!g_ascii_isdigit (*p))
|
|
return FALSE;
|
|
while (g_ascii_isdigit (*p))
|
|
p++;
|
|
if (*p++ != ':' || !g_ascii_isdigit (*p))
|
|
return FALSE;
|
|
while (g_ascii_isdigit (*p))
|
|
p++;
|
|
return *p == '\0';
|
|
}
|
|
|
|
static char *
|
|
resolve_mount_device (const char *sysfs, const char *id, const char *source)
|
|
{
|
|
char *entry = NULL;
|
|
struct stat st;
|
|
|
|
/* 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))
|
|
{
|
|
entry = g_build_filename (sysfs, "dev", "block", id, NULL);
|
|
if (g_file_test (entry, G_FILE_TEST_EXISTS))
|
|
return entry;
|
|
g_free (entry);
|
|
}
|
|
if (g_str_has_prefix (source, "/dev/") && stat (source, &st) == 0 && S_ISBLK (st.st_mode))
|
|
{
|
|
char *number = g_strdup_printf ("%u:%u", (unsigned int) major (st.st_rdev),
|
|
(unsigned int) minor (st.st_rdev));
|
|
entry = g_build_filename (sysfs, "dev", "block", number, NULL);
|
|
g_free (number);
|
|
return entry;
|
|
}
|
|
/* 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;
|
|
}
|
|
|
|
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;
|
|
|
|
for (i = 0; lines[i]; i++)
|
|
{
|
|
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)
|
|
{
|
|
g_strfreev (fields);
|
|
continue;
|
|
}
|
|
for (j = 6; j + 3 < count; j++)
|
|
if (strcmp (fields[j], "-") == 0)
|
|
{
|
|
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;
|
|
}
|
|
g_strfreev (fields);
|
|
}
|
|
g_strfreev (lines);
|
|
|
|
if (!ambiguous && entry && resolve_disk (entry, &disk, 0) &&
|
|
read_capacity (disk, &capacity))
|
|
result = g_format_size (capacity);
|
|
g_free (disk);
|
|
g_free (entry);
|
|
return result;
|
|
}
|
|
|
|
char *
|
|
sysinfo_backend_get_disk (void)
|
|
{
|
|
char *mountinfo = NULL;
|
|
char *result;
|
|
|
|
/* 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))
|
|
return NULL;
|
|
result = sysinfo_linux_boot_disk ("/sys", mountinfo);
|
|
g_free (mountinfo);
|
|
return result;
|
|
}
|