Skip to content

Commit

Permalink
no-conmon-delegate for cgroups v1
Browse files Browse the repository at this point in the history
  • Loading branch information
goochjj committed Jun 18, 2020
1 parent 173e414 commit 3d6ec20
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions utils/utils_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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
Expand Down

0 comments on commit 3d6ec20

Please sign in to comment.