From fcc04fbabaf656553fcec9195cbf219c451d4c5a Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Wed, 23 Dec 2020 10:21:24 +0100 Subject: rootless: add function to retrieve gid mappings Signed-off-by: Giuseppe Scrivano --- pkg/rootless/rootless.go | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'pkg') diff --git a/pkg/rootless/rootless.go b/pkg/rootless/rootless.go index 799c793d8..2ba0f8e81 100644 --- a/pkg/rootless/rootless.go +++ b/pkg/rootless/rootless.go @@ -50,24 +50,36 @@ func TryJoinPauseProcess(pausePidPath string) (bool, int, error) { } var ( - availableGids int64 - availableGidsErr error - availableGidsOnce sync.Once + gidMap []user.IDMap + gidMapError error + gidMapOnce sync.Once ) -// GetAvailableGids returns how many GIDs are available in the +// GetAvailableGidMap returns the GID mappings in the // current user namespace. -func GetAvailableGids() (int64, error) { - availableGidsOnce.Do(func() { - idMap, err := user.ParseIDMapFile("/proc/self/gid_map") +func GetAvailableGidMap() ([]user.IDMap, error) { + gidMapOnce.Do(func() { + var err error + gidMap, err = user.ParseIDMapFile("/proc/self/gid_map") if err != nil { - availableGidsErr = err + gidMapError = err return } - availableGids = int64(0) - for _, r := range idMap { - availableGids += r.Count - } }) - return availableGids, availableGidsErr + return gidMap, gidMapError +} + +// GetAvailableGids returns how many GIDs are available in the +// current user namespace. +func GetAvailableGids() (int64, error) { + gids, err := GetAvailableGidMap() + if err != nil { + return -1, err + } + + availableGids := int64(0) + for _, r := range gids { + availableGids += r.Count + } + return availableGids, nil } -- cgit v1.2.3-54-g00ecf