From fc95b44255465bed78316632046e605d31ff5a39 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 29 Jul 2020 12:32:44 +0100
Subject: [PATCH] os: Look at /run/host/os-release if we are inspecting a
 container

Recent versions of Flatpak and systemd-nspawn can mount the host's
os-release at /run/host/os-release, even if the rest of the host's
/etc and /usr are not exposed to the container.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 steam-runtime-tools/os.c   | 26 +++++++++++++++++---------
 tests/generate-sysroots.py |  8 ++++++--
 tests/system-info.c        |  9 +++++++--
 3 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/steam-runtime-tools/os.c b/steam-runtime-tools/os.c
index 4fba109c5..0d9f11552 100644
--- a/steam-runtime-tools/os.c
+++ b/steam-runtime-tools/os.c
@@ -48,10 +48,17 @@ _srt_os_release_init (SrtOsRelease *self)
   self->populated = FALSE;
 }
 
-static const char * const os_release_paths[] =
+static const struct
 {
-  "/etc/os-release",
-  "/usr/lib/os-release"
+  const char *path;
+  gboolean only_in_run_host;
+}
+os_release_paths[] =
+{
+  { "/etc/os-release", FALSE },
+  { "/usr/lib/os-release", FALSE },
+  /* https://github.com/flatpak/flatpak/pull/3733 */
+  { "/os-release", TRUE }
 };
 
 static void
@@ -150,23 +157,24 @@ _srt_os_release_populate (SrtOsRelease *self,
 
   for (i = 0; i < G_N_ELEMENTS (os_release_paths); i++)
     {
+      const char *path = os_release_paths[i].path;
+      gboolean only_in_run_host = os_release_paths[i].only_in_run_host;
       gchar *built_path = NULL;
       gchar *contents = NULL;
-      const char *path = NULL;
       char *beginning_of_line;
       GError *local_error = NULL;
       gsize len;
       gsize j;
 
+      if (only_in_run_host
+          && (sysroot == NULL || !g_str_has_suffix (sysroot, "/run/host")))
+        continue;
+
       if (sysroot != NULL)
         {
-          built_path = g_build_filename (sysroot, os_release_paths[i], NULL);
+          built_path = g_build_filename (sysroot, path, NULL);
           path = built_path;
         }
-      else
-        {
-          path = os_release_paths[i];
-        }
 
       if (!g_file_get_contents (path, &contents, &len, &local_error))
         {
diff --git a/tests/generate-sysroots.py b/tests/generate-sysroots.py
index 2cdb98f4f..b65a5ca94 100755
--- a/tests/generate-sysroots.py
+++ b/tests/generate-sysroots.py
@@ -253,8 +253,12 @@ BUG_REPORT_URL="https://bugs.debian.org/"
 with open('debian10/run/systemd/container', 'w') as writer:
     writer.write('whatever\n')
 
-with open('debian-unstable/etc/os-release', 'w') as writer:
-    writer.write('''\
+for name in (
+    'debian-unstable/etc/os-release',
+    'flatpak-example/run/host/os-release',
+):
+    with open(name, 'w') as writer:
+        writer.write('''\
 PRETTY_NAME="Debian GNU/Linux bullseye/sid"
 NAME="Debian GNU/Linux"
 ID=debian
diff --git a/tests/system-info.c b/tests/system-info.c
index 127a708e5..99a56453e 100644
--- a/tests/system-info.c
+++ b/tests/system-info.c
@@ -1344,12 +1344,13 @@ static void
 os_debian_unstable (Fixture *f,
                     gconstpointer context)
 {
+  const char *sysroot_name = context;
   SrtSystemInfo *info;
   gchar **strv;
   gchar *sysroot;
   gchar *s;
 
-  sysroot = g_build_filename (f->sysroots, "debian-unstable", NULL);
+  sysroot = g_build_filename (f->sysroots, sysroot_name, NULL);
 
   info = srt_system_info_new (NULL);
   srt_system_info_set_sysroot (info, sysroot);
@@ -3231,7 +3232,11 @@ main (int argc,
 
   g_test_add ("/system-info/os/debian10", Fixture, NULL,
               setup, os_debian10, teardown);
-  g_test_add ("/system-info/os/debian-unstable", Fixture, NULL,
+  g_test_add ("/system-info/os/debian-unstable", Fixture,
+              "debian-unstable",
+              setup, os_debian_unstable, teardown);
+  g_test_add ("/system-info/os/flatpak-on-debian-unstable", Fixture,
+              "flatpak-example/run/host",
               setup, os_debian_unstable, teardown);
   g_test_add ("/system-info/os/steamrt", Fixture, NULL,
               setup, os_steamrt, teardown);
-- 
GitLab