summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKir Kolyshkin <kolyshkin@gmail.com>2020-10-05 12:41:28 -0700
committerKir Kolyshkin <kolyshkin@gmail.com>2020-10-05 15:57:57 -0700
commitd4aa89bb40b3a2c1730c9bff31a681007a3feb97 (patch)
tree10ba280d6da9babc3bfb7a2518dde85a90f2b689
parent684d0079d2cb5f84f65b3313a52ca3fbcbc40350 (diff)
downloadpodman-d4aa89bb40b3a2c1730c9bff31a681007a3feb97.tar.gz
podman-d4aa89bb40b3a2c1730c9bff31a681007a3feb97.tar.bz2
podman-d4aa89bb40b3a2c1730c9bff31a681007a3feb97.zip
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>
-rw-r--r--pkg/cgroups/cgroups.go25
1 files changed, 9 insertions, 16 deletions
diff --git a/pkg/cgroups/cgroups.go b/pkg/cgroups/cgroups.go
index 3cf736bdc..0d7ed05b2 100644
--- a/pkg/cgroups/cgroups.go
+++ b/pkg/cgroups/cgroups.go
@@ -2,6 +2,7 @@ package cgroups
import (
"bufio"
+ "bytes"
"fmt"
"io/ioutil"
"math"
@@ -155,23 +156,15 @@ func (c *CgroupControl) getCgroupv1Path(name string) string {
// createCgroupv2Path creates the cgroupv2 path and enables all the available controllers
func createCgroupv2Path(path string) (deferredError error) {
- content, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers")
- if err != nil {
- return err
- }
- if !strings.HasPrefix(path, "/sys/fs/cgroup/") {
+ if !strings.HasPrefix(path, cgroupRoot+"/") {
return fmt.Errorf("invalid cgroup path %s", path)
}
-
- 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)
- }
+ content, err := ioutil.ReadFile(cgroupRoot + "/cgroup.controllers")
+ if err != nil {
+ return err
}
- resByte := []byte(res)
+ ctrs := bytes.Fields(content)
+ res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...)
current := "/sys/fs"
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
// PIDs if there are already enabled controllers.
if i < len(elements[3:])-1 {
- if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), resByte, 0755); err != nil {
- return errors.Wrapf(err, "write %s", filepath.Join(current, "cgroup.subtree_control"))
+ if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), res, 0755); err != nil {
+ return err
}
}
}