From 7bc5f1d68a9711146760128f4ad69ff6192c3b9b Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 8 Jan 2021 13:11:59 +0000
Subject: [PATCH] Implement the container indicator from systemd's
 CONTAINER_INTERFACE

This is a container-manager-agnostic version of probing /.flatpak-info
and /run/pressure-vessel.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap.c                  | 18 +++++++++++++++++-
 tests/pressure-vessel/inside-runtime.py |  5 +++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index 6a17c2d52..2aa1bac8a 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -4,7 +4,7 @@
  * Contains code taken from Flatpak.
  *
  * Copyright © 2014-2019 Red Hat, Inc
- * Copyright © 2017-2020 Collabora Ltd.
+ * Copyright © 2017-2021 Collabora Ltd.
  *
  * SPDX-License-Identifier: LGPL-2.1-or-later
  *
@@ -2233,6 +2233,22 @@ main (int argc,
 
   /* TODO: In future we will not do this when using Flatpak sub-sandboxing */
     {
+      /* Tell the application that it's running under a container manager
+       * in a generic way (based on https://systemd.io/CONTAINER_INTERFACE/,
+       * although a lot of that document is intended for "system"
+       * containers and is less suitable for "app" containers like
+       * Flatpak and pressure-vessel). */
+      flatpak_bwrap_add_args (bwrap,
+                              "--setenv", "container", "pressure-vessel",
+                              NULL);
+      if (!flatpak_bwrap_add_args_data (bwrap,
+                                        "container-manager",
+                                        "pressure-vessel\n", -1,
+                                        "/run/host/container-manager",
+                                        error))
+        return FALSE;
+
+
       if (opt_verbose)
         {
           g_message ("%s options before bundling:", bwrap_executable);
diff --git a/tests/pressure-vessel/inside-runtime.py b/tests/pressure-vessel/inside-runtime.py
index b305f3ea4..3914843e7 100755
--- a/tests/pressure-vessel/inside-runtime.py
+++ b/tests/pressure-vessel/inside-runtime.py
@@ -159,6 +159,11 @@ class TestInsideRuntime(BaseTest):
         elif os.environ.get('TEST_INSIDE_RUNTIME_IS_SOLDIER'):
             self.assertEqual(os.environ.get('STEAM_RUNTIME'), None)
 
+        self.assertEqual(os.environ.get('container'), 'pressure-vessel')
+
+        with open('/run/host/container-manager', 'r') as reader:
+            self.assertEqual(reader.read(), 'pressure-vessel\n')
+
     def test_overrides(self) -> None:
         if os.getenv('TEST_INSIDE_RUNTIME_IS_COPY'):
             target = os.readlink('/overrides')
-- 
GitLab