mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
libcontainer: RunningInUserNS() use sync.Once
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -5,6 +5,7 @@ package system
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/user"
|
"github.com/opencontainers/runc/libcontainer/user"
|
||||||
@@ -86,15 +87,23 @@ func Setctty() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
inUserNS bool
|
||||||
|
nsOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
// RunningInUserNS detects whether we are currently running in a user namespace.
|
// RunningInUserNS detects whether we are currently running in a user namespace.
|
||||||
// Originally copied from github.com/lxc/lxd/shared/util.go
|
// Originally copied from github.com/lxc/lxd/shared/util.go
|
||||||
func RunningInUserNS() bool {
|
func RunningInUserNS() bool {
|
||||||
uidmap, err := user.CurrentProcessUIDMap()
|
nsOnce.Do(func() {
|
||||||
if err != nil {
|
uidmap, err := user.CurrentProcessUIDMap()
|
||||||
// This kernel-provided file only exists if user namespaces are supported
|
if err != nil {
|
||||||
return false
|
// This kernel-provided file only exists if user namespaces are supported
|
||||||
}
|
return
|
||||||
return UIDMapInUserNS(uidmap)
|
}
|
||||||
|
inUserNS = UIDMapInUserNS(uidmap)
|
||||||
|
})
|
||||||
|
return inUserNS
|
||||||
}
|
}
|
||||||
|
|
||||||
func UIDMapInUserNS(uidmap []user.IDMap) bool {
|
func UIDMapInUserNS(uidmap []user.IDMap) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user