diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2020-10-02 09:05:34 +0200 |
---|---|---|
committer | Giuseppe Scrivano <gscrivan@redhat.com> | 2020-10-02 09:05:34 +0200 |
commit | 07546cca18c28f444deacaf7f001c5bcc5d3219a (patch) | |
tree | c5c06d8963dc0ca314e5bb0a4dc604e16b923371 /pkg/rootless/rootless.go | |
parent | baef6eff36775dd3d1762e5821703fec22168ad7 (diff) | |
download | podman-07546cca18c28f444deacaf7f001c5bcc5d3219a.tar.gz podman-07546cca18c28f444deacaf7f001c5bcc5d3219a.tar.bz2 podman-07546cca18c28f444deacaf7f001c5bcc5d3219a.zip |
rootless: use sync.Once for GetAvailableGids()
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Diffstat (limited to 'pkg/rootless/rootless.go')
-rw-r--r-- | pkg/rootless/rootless.go | 28 |
1 files changed, 19 insertions, 9 deletions
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 } |