From 07546cca18c28f444deacaf7f001c5bcc5d3219a Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Fri, 2 Oct 2020 09:05:34 +0200 Subject: rootless: use sync.Once for GetAvailableGids() Signed-off-by: Giuseppe Scrivano --- pkg/rootless/rootless.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'pkg') diff --git a/pkg/rootless/rootless.go b/pkg/rootless/rootless.go index 3c33597b6..799c793d8 100644 --- a/pkg/rootless/rootless.go +++ b/pkg/rootless/rootless.go @@ -2,6 +2,7 @@ package rootless import ( "os" + "sync" "github.com/containers/storage" "github.com/opencontainers/runc/libcontainer/user" @@ -48,16 +49,25 @@ func TryJoinPauseProcess(pausePidPath string) (bool, int, error) { return became, ret, err } +var ( + availableGids int64 + availableGidsErr error + availableGidsOnce sync.Once +) + // GetAvailableGids returns how many GIDs are available in the // current user namespace. func GetAvailableGids() (int64, error) { - idMap, err := user.ParseIDMapFile("/proc/self/gid_map") - if err != nil { - return 0, err - } - count := int64(0) - for _, r := range idMap { - count += r.Count - } - return count, nil + availableGidsOnce.Do(func() { + idMap, err := user.ParseIDMapFile("/proc/self/gid_map") + if err != nil { + availableGidsErr = err + return + } + availableGids = int64(0) + for _, r := range idMap { + availableGids += r.Count + } + }) + return availableGids, availableGidsErr } -- cgit v1.2.3-54-g00ecf