mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 22:37:14 +08:00
4316df8b53
Moving these utilities to a separate package, so that consumers of this package don't have to pull in the whole "system" package. Looking at uses of these utilities (outside of runc itself); `RunningInUserNS()` is used by [various external consumers][1], so adding a "Deprecated" alias for this. [1]: https://grep.app/search?current=2&q=.RunningInUserNS Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
38 lines
848 B
Go
38 lines
848 B
Go
package userns
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/user"
|
|
)
|
|
|
|
var (
|
|
inUserNS bool
|
|
nsOnce sync.Once
|
|
)
|
|
|
|
// runningInUserNS detects whether we are currently running in a user namespace.
|
|
// Originally copied from github.com/lxc/lxd/shared/util.go
|
|
func runningInUserNS() bool {
|
|
nsOnce.Do(func() {
|
|
uidmap, err := user.CurrentProcessUIDMap()
|
|
if err != nil {
|
|
// This kernel-provided file only exists if user namespaces are supported
|
|
return
|
|
}
|
|
inUserNS = uidMapInUserNS(uidmap)
|
|
})
|
|
return inUserNS
|
|
}
|
|
|
|
func uidMapInUserNS(uidmap []user.IDMap) bool {
|
|
/*
|
|
* We assume we are in the initial user namespace if we have a full
|
|
* range - 4294967295 uids starting at uid 0.
|
|
*/
|
|
if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|