Merge pull request #4330 from thaJeztah/better_alias

libct/userns: assorted (godoc) improvements
This commit is contained in:
Kir Kolyshkin
2024-07-01 12:39:28 -07:00
committed by GitHub
4 changed files with 22 additions and 33 deletions
+6 -2
View File
@@ -1,4 +1,8 @@
package userns
// RunningInUserNS detects whether we are currently running in a user namespace.
var RunningInUserNS = runningInUserNS
// RunningInUserNS detects whether we are currently running in a Linux
// user namespace and memoizes the result. It returns false on non-Linux
// platforms.
func RunningInUserNS() bool {
return inUserNS()
}
+13 -19
View File
@@ -7,32 +7,26 @@ import (
"sync"
)
var (
inUserNS bool
nsOnce sync.Once
)
var inUserNS = sync.OnceValue(runningInUserNS)
// runningInUserNS detects whether we are currently running in a user namespace.
//
// Originally copied from https://github.com/lxc/incus/blob/e45085dd42f826b3c8c3228e9733c0b6f998eafe/shared/util.go#L678-L700.
func runningInUserNS() bool {
nsOnce.Do(func() {
file, err := os.Open("/proc/self/uid_map")
if err != nil {
// This kernel-provided file only exists if user namespaces are supported.
return
}
defer file.Close()
file, err := os.Open("/proc/self/uid_map")
if err != nil {
// This kernel-provided file only exists if user namespaces are supported.
return false
}
defer file.Close()
buf := bufio.NewReader(file)
l, _, err := buf.ReadLine()
if err != nil {
return
}
buf := bufio.NewReader(file)
l, _, err := buf.ReadLine()
if err != nil {
return false
}
inUserNS = uidMapInUserNS(string(l))
})
return inUserNS
return uidMapInUserNS(string(l))
}
func uidMapInUserNS(uidMap string) bool {
@@ -1,4 +1,4 @@
//go:build gofuzz
//go:build linux && gofuzz
package userns
+2 -11
View File
@@ -2,14 +2,5 @@
package userns
// runningInUserNS is a stub for non-Linux systems
// Always returns false
func runningInUserNS() bool {
return false
}
// uidMapInUserNS is a stub for non-Linux systems
// Always returns false
func uidMapInUserNS(uidMap string) bool {
return false
}
// inUserNS is a stub for non-Linux systems. Always returns false.
func inUserNS() bool { return false }