mirror of
https://github.com/containers/podman.git
synced 2025-06-18 07:28:57 +08:00
pkg/cgroups/createCgroupv2Path: nits
1. Check the path validity before trying to read the cgroup.controllers. 2. Do not hardcode "/sys/fs/cgroup". 3. Simplify creating the "+this +that" string. 4. Do not wrap ioutil.WriteFile error. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@ -2,6 +2,7 @@ package cgroups
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
@ -155,23 +156,15 @@ func (c *CgroupControl) getCgroupv1Path(name string) string {
|
|||||||
|
|
||||||
// createCgroupv2Path creates the cgroupv2 path and enables all the available controllers
|
// createCgroupv2Path creates the cgroupv2 path and enables all the available controllers
|
||||||
func createCgroupv2Path(path string) (deferredError error) {
|
func createCgroupv2Path(path string) (deferredError error) {
|
||||||
content, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers")
|
if !strings.HasPrefix(path, cgroupRoot+"/") {
|
||||||
|
return fmt.Errorf("invalid cgroup path %s", path)
|
||||||
|
}
|
||||||
|
content, err := ioutil.ReadFile(cgroupRoot + "/cgroup.controllers")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(path, "/sys/fs/cgroup/") {
|
ctrs := bytes.Fields(content)
|
||||||
return fmt.Errorf("invalid cgroup path %s", path)
|
res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...)
|
||||||
}
|
|
||||||
|
|
||||||
res := ""
|
|
||||||
for i, c := range strings.Split(strings.TrimSpace(string(content)), " ") {
|
|
||||||
if i == 0 {
|
|
||||||
res = fmt.Sprintf("+%s", c)
|
|
||||||
} else {
|
|
||||||
res += fmt.Sprintf(" +%s", c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resByte := []byte(res)
|
|
||||||
|
|
||||||
current := "/sys/fs"
|
current := "/sys/fs"
|
||||||
elements := strings.Split(path, "/")
|
elements := strings.Split(path, "/")
|
||||||
@ -194,8 +187,8 @@ func createCgroupv2Path(path string) (deferredError error) {
|
|||||||
// We enable the controllers for all the path components except the last one. It is not allowed to add
|
// We enable the controllers for all the path components except the last one. It is not allowed to add
|
||||||
// PIDs if there are already enabled controllers.
|
// PIDs if there are already enabled controllers.
|
||||||
if i < len(elements[3:])-1 {
|
if i < len(elements[3:])-1 {
|
||||||
if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), resByte, 0755); err != nil {
|
if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), res, 0755); err != nil {
|
||||||
return errors.Wrapf(err, "write %s", filepath.Join(current, "cgroup.subtree_control"))
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user