From d0c89dfac3d4892f35ba7ba4156a694d3cefcc06 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Sat, 26 Mar 2022 17:47:34 -0700 Subject: [PATCH] libct/cg: IsCgroup2HybridMode: don't panic In case statfs("/sys/fs/cgroup/unified") fails with any error other than ENOENT, current code panics. As IsCgroup2HybridMode is called from libcontainer/cgroups/fs's init function, this means that any user of libcontainer may panic during initialization, which is ugly. Avoid panicking; instead, do not enable hybrid hierarchy support and report the error (under debug level, not to confuse anyone). Basically, replace the panic with "turn off hybrid mode support" (which makes total sense since we were unable to statfs its root). Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/utils.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 13ebf52ab..b32af4ee5 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -55,12 +55,12 @@ func IsCgroup2HybridMode() bool { var st unix.Statfs_t err := unix.Statfs(hybridMountpoint, &st) if err != nil { - if os.IsNotExist(err) { - // ignore the "not found" error - isHybrid = false - return + isHybrid = false + if !os.IsNotExist(err) { + // Report unexpected errors. + logrus.WithError(err).Debugf("statfs(%q) failed", hybridMountpoint) } - panic(fmt.Sprintf("cannot statfs cgroup root: %s", err)) + return } isHybrid = st.Type == unix.CGROUP2_SUPER_MAGIC })