diff --git a/utils/utils_supported.go b/utils/utils_supported.go index f8e49b7527..85ed88efaa 100644 --- a/utils/utils_supported.go +++ b/utils/utils_supported.go @@ -64,7 +64,7 @@ func GetPidCgroupv2(pid int) (string, error) { return "", err } if !unified { - return "", errors.New("move under subtree supported only on cgroup v2") +// return "", errors.New("move under subtree supported only on cgroup v2") } procFile := fmt.Sprintf("/proc/%d/cgroup", pid) @@ -78,8 +78,8 @@ func GetPidCgroupv2(pid int) (string, error) { cgroup := "" for scanner.Scan() { line := scanner.Text() - if strings.HasPrefix(line, "0::") { - cgroup = line[3:] + if strings.HasPrefix(line, "3:pids:") { + cgroup = line[7:] break } } @@ -93,31 +93,50 @@ func GetPidCgroupv2(pid int) (string, error) { // MoveUnderCgroupSubtree moves the PID under a cgroup subtree. func MoveUnderCgroup2Subtree(subtree string) error { cgroup, err := GetPidCgroupv2(0) + fmt.Println(cgroup); if err != nil { return err } cgroupRoot := "/sys/fs/cgroup" - processes, err := ioutil.ReadFile(filepath.Join(cgroupRoot, cgroup, "cgroup.procs")) + ctrls, err := ioutil.ReadDir(cgroupRoot) if err != nil { return err } - newCgroup := filepath.Join(cgroupRoot, cgroup, subtree) - if err := os.Mkdir(newCgroup, 0755); err != nil { - return err - } + for _, ctrl := range ctrls { + if !ctrl.IsDir() { + continue; + } - f, err := os.OpenFile(filepath.Join(newCgroup, "cgroup.procs"), os.O_RDWR, 0755) - if err != nil { - return err - } - defer f.Close() + if ctrl.Mode()&os.ModeSymlink != 0 { + continue; + } + + fmt.Println(ctrl.Name()); + processes, err := ioutil.ReadFile(filepath.Join(cgroupRoot, ctrl.Name(), cgroup, "cgroup.procs")) + if err != nil { + return err + } + + newCgroup := filepath.Join(filepath.Join(cgroupRoot, ctrl.Name(), cgroup, subtree)) + if _, err2 := os.Stat(newCgroup); os.IsNotExist(err2) { + if err := os.Mkdir(newCgroup, 0755); err != nil { + return err + } + } + + f, err := os.OpenFile(filepath.Join(newCgroup, "cgroup.procs"), os.O_RDWR, 0755) + if err != nil { + return err + } + defer f.Close() - for _, pid := range bytes.Split(processes, []byte("\n")) { - if _, err := f.Write(pid); err != nil { - logrus.Warnf("Cannot move process %s to cgroup %q", pid, newCgroup) + for _, pid := range bytes.Split(processes, []byte("\n")) { + if _, err := f.Write(pid); err != nil { + logrus.Warnf("Cannot move process %s to cgroup %q", pid, newCgroup) + } } } return nil