From 9b23a34697b68241831df78eb07da54995fab2bb Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Wed, 29 May 2024 09:11:36 +0200 Subject: [PATCH] new(userspace/libsinsp): support lxc 4.0 cgroup layout. Signed-off-by: Federico Di Pierro --- userspace/libsinsp/container_engine/lxc.cpp | 39 ++++++++++----------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/userspace/libsinsp/container_engine/lxc.cpp b/userspace/libsinsp/container_engine/lxc.cpp index f217b7749d..ce161ad962 100644 --- a/userspace/libsinsp/container_engine/lxc.cpp +++ b/userspace/libsinsp/container_engine/lxc.cpp @@ -21,6 +21,12 @@ limitations under the License. using namespace libsinsp::container_engine; +constexpr const std::string_view LXC_CGROUP_LAYOUT[] = { + "/lxc/", // non-systemd + "/lxc.payload/", // systemd + "/lxc.payload.", // lxc4.0 layout: https://linuxcontainers.org/lxc/news/2020_03_25_13_03.html +}; + bool lxc::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) { auto container = sinsp_container_info(); @@ -28,29 +34,22 @@ bool lxc::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info) for(const auto& it : tinfo->cgroups()) { - // - // Non-systemd LXC - // - const auto& cgroup = it.second; - size_t pos = cgroup.find("/lxc/"); - if(pos != std::string::npos) + const auto &cgroup = it.second; + for(const auto &cgroup_layout : LXC_CGROUP_LAYOUT) { - auto id_start = pos + sizeof("/lxc/") - 1; - auto id_end = cgroup.find('/', id_start); - container.m_type = CT_LXC; - container.m_id = cgroup.substr(id_start, id_end - id_start); - matches = true; - break; + size_t pos = cgroup.find(cgroup_layout); + if(pos != std::string::npos) + { + auto id_start = pos + cgroup_layout.length(); + auto id_end = cgroup.find('/', id_start); + container.m_type = CT_LXC; + container.m_id = cgroup.substr(id_start, id_end - id_start); + matches = true; + break; + } } - - pos = cgroup.find("/lxc.payload/"); - if(pos != std::string::npos) + if (matches) { - auto id_start = pos + sizeof("/lxc.payload/") - 1; - auto id_end = cgroup.find('/', id_start); - container.m_type = CT_LXC; - container.m_id = cgroup.substr(id_start, id_end - id_start); - matches = true; break; } }