From 31f0f5b7e03a20c75760d235694ffbfd5f924514 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 22 Sep 2020 19:53:06 -0700 Subject: [PATCH] libct/cg/fscommon.GetCgroupParamUint: improve 1. Use GetCgroupParamString as the initial part of both functions are the same and we can reuse it. This also gives us whatever security measures GetCgroupParamString has (see previous commit). 2. Fix the error wrapping to not add the value, as it is already a part of the error returned by ParseUint. 3. Improve docstring. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fscommon/utils.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/libcontainer/cgroups/fscommon/utils.go b/libcontainer/cgroups/fscommon/utils.go index 99c31bb4a..aa281aca8 100644 --- a/libcontainer/cgroups/fscommon/utils.go +++ b/libcontainer/cgroups/fscommon/utils.go @@ -5,9 +5,7 @@ package fscommon import ( "errors" "fmt" - "io/ioutil" "math" - "path/filepath" "strconv" "strings" ) @@ -54,21 +52,21 @@ func GetCgroupParamKeyValue(t string) (string, uint64, error) { } } -// Gets a single uint64 value from the specified cgroup file. -func GetCgroupParamUint(cgroupPath, cgroupFile string) (uint64, error) { - fileName := filepath.Join(cgroupPath, cgroupFile) - contents, err := ioutil.ReadFile(fileName) +// GetCgroupParamUint reads a single uint64 value from the specified cgroup file. +// If the value read is "max", the math.MaxUint64 is returned. +func GetCgroupParamUint(path, file string) (uint64, error) { + contents, err := GetCgroupParamString(path, file) if err != nil { return 0, err } - trimmed := strings.TrimSpace(string(contents)) - if trimmed == "max" { + contents = strings.TrimSpace(contents) + if contents == "max" { return math.MaxUint64, nil } - res, err := ParseUint(trimmed, 10, 64) + res, err := ParseUint(contents, 10, 64) if err != nil { - return res, fmt.Errorf("unable to parse %q as a uint from Cgroup file %q", string(contents), fileName) + return res, fmt.Errorf("unable to parse file %q", path+"/"+file) } return res, nil }