From 41530325eca28154e10c092cf4d1cddad43b4aa2 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Tue, 9 May 2017 07:20:56 +0200 Subject: [PATCH 1/2] Read host os-release file when running inside the snap --- src/anbox/cmds/system_info.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/anbox/cmds/system_info.cpp b/src/anbox/cmds/system_info.cpp index ef90366..60f4bf9 100644 --- a/src/anbox/cmds/system_info.cpp +++ b/src/anbox/cmds/system_info.cpp @@ -34,6 +34,7 @@ namespace fs = boost::filesystem; namespace { constexpr const char *os_release_path{"/etc/os-release"}; +constexpr const char *host_os_release_path{"/var/lib/snapd/hostfs/etc/os-release"}; constexpr const char *proc_version_path{"/proc/version"}; constexpr const char *binder_path{"/dev/binder"}; constexpr const char *ashmem_path{"/dev/ashmem"}; @@ -94,14 +95,18 @@ class SystemInformation { private: void collect_os_info() { os_info_.snap_based = (getenv("SNAP") != nullptr); - if (fs::exists(os_release_path)) { + fs::path path = os_release_path; + // If we're running from within a snap the best we can do is to + // access the hostfs and read the os-release file from there. + if (os_info_.snap_based && fs::exists(host_os_release_path)) + path = host_os_release_path; + + // Double check that there aren't any permission errors when trying + // to access the file (e.g. because of snap confinement) + if (fs::exists(path)) { anbox::utils::EnvironmentFile os_release(os_release_path); os_info_.name = os_release.value("NAME"); os_info_.version = os_release.value("VERSION"); - } else if (os_info_.snap_based) { - // As we can't read /etc/os-release and we're snap-based this is the best we can guess - os_info_.name = "Ubuntu"; - os_info_.version = "16"; } } From 385bf94d51ebbdd5c6c96c475e81e15b7bc48253 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Tue, 9 May 2017 09:51:38 +0200 Subject: [PATCH 2/2] Integrate review feedback --- src/anbox/cmds/system_info.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/anbox/cmds/system_info.cpp b/src/anbox/cmds/system_info.cpp index 60f4bf9..9dd54c8 100644 --- a/src/anbox/cmds/system_info.cpp +++ b/src/anbox/cmds/system_info.cpp @@ -38,6 +38,8 @@ constexpr const char *host_os_release_path{"/var/lib/snapd/hostfs/etc/os-release constexpr const char *proc_version_path{"/proc/version"}; constexpr const char *binder_path{"/dev/binder"}; constexpr const char *ashmem_path{"/dev/ashmem"}; +constexpr const char *os_release_name{"NAME"}; +constexpr const char *os_release_version{"VERSION"}; class SystemInformation { public: @@ -94,7 +96,7 @@ class SystemInformation { private: void collect_os_info() { - os_info_.snap_based = (getenv("SNAP") != nullptr); + os_info_.snap_based = !anbox::utils::get_env_value("SNAP").empty(); fs::path path = os_release_path; // If we're running from within a snap the best we can do is to // access the hostfs and read the os-release file from there. @@ -104,9 +106,9 @@ class SystemInformation { // Double check that there aren't any permission errors when trying // to access the file (e.g. because of snap confinement) if (fs::exists(path)) { - anbox::utils::EnvironmentFile os_release(os_release_path); - os_info_.name = os_release.value("NAME"); - os_info_.version = os_release.value("VERSION"); + anbox::utils::EnvironmentFile os_release(path); + os_info_.name = os_release.value(os_release_name); + os_info_.version = os_release.value(os_release_version); } }