From 60eaed2ed61e71ad5ec521de88a29ac42fa1f7bb Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Sun, 5 Apr 2020 20:39:25 -0700 Subject: [PATCH] cgroupv2: move sanity path check to common code The fs2 cgroup driver has a sanity check for path. Since systemd driver is relying on the same path, it makes sense to move this check to the common code. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs2/fs2.go | 6 +----- libcontainer/factory_linux.go | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libcontainer/cgroups/fs2/fs2.go b/libcontainer/cgroups/fs2/fs2.go index 4ab291b1889..4f047bd5c52 100644 --- a/libcontainer/cgroups/fs2/fs2.go +++ b/libcontainer/cgroups/fs2/fs2.go @@ -30,11 +30,7 @@ func NewManager(config *configs.Cgroup, dirPath string, rootless bool) (cgroups. if config == nil { config = &configs.Cgroup{} } - if dirPath != "" { - if filepath.Clean(dirPath) != dirPath || !filepath.IsAbs(dirPath) { - return nil, errors.Errorf("invalid dir path %q", dirPath) - } - } else { + if dirPath == "" { var err error dirPath, err = defaultDirPath(config) if err != nil { diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index fa446c03d6f..03c6ec45b31 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -51,16 +51,22 @@ func InitArgs(args ...string) func(*LinuxFactory) error { } func getUnifiedPath(paths map[string]string) string { - unifiedPath := "" + path := "" for k, v := range paths { - if unifiedPath == "" { - unifiedPath = v - } else if v != unifiedPath { - panic(errors.Errorf("expected %q path to be unified path %q, got %q", k, unifiedPath, v)) + if path == "" { + path = v + } else if v != path { + panic(errors.Errorf("expected %q path to be unified path %q, got %q", k, path, v)) } } // can be empty - return unifiedPath + if path != "" { + if filepath.Clean(path) != path || !filepath.IsAbs(path) { + panic(errors.Errorf("invalid dir path %q", path)) + } + } + + return path } func systemdCgroupV2(l *LinuxFactory, rootless bool) error {