Skip to content

Commit cc06259

Browse files
authored
Merge pull request #4330 from thaJeztah/better_alias
libct/userns: assorted (godoc) improvements
2 parents 7bcb611 + 6980adb commit cc06259

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

libcontainer/userns/userns.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package userns
22

3-
// RunningInUserNS detects whether we are currently running in a user namespace.
4-
var RunningInUserNS = runningInUserNS
3+
// RunningInUserNS detects whether we are currently running in a Linux
4+
// user namespace and memoizes the result. It returns false on non-Linux
5+
// platforms.
6+
func RunningInUserNS() bool {
7+
return inUserNS()
8+
}

libcontainer/userns/userns_linux.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,26 @@ import (
77
"sync"
88
)
99

10-
var (
11-
inUserNS bool
12-
nsOnce sync.Once
13-
)
10+
var inUserNS = sync.OnceValue(runningInUserNS)
1411

1512
// runningInUserNS detects whether we are currently running in a user namespace.
1613
//
1714
// Originally copied from https://github.com/lxc/incus/blob/e45085dd42f826b3c8c3228e9733c0b6f998eafe/shared/util.go#L678-L700.
1815
func runningInUserNS() bool {
19-
nsOnce.Do(func() {
20-
file, err := os.Open("/proc/self/uid_map")
21-
if err != nil {
22-
// This kernel-provided file only exists if user namespaces are supported.
23-
return
24-
}
25-
defer file.Close()
26-
27-
buf := bufio.NewReader(file)
28-
l, _, err := buf.ReadLine()
29-
if err != nil {
30-
return
31-
}
32-
33-
inUserNS = uidMapInUserNS(string(l))
34-
})
35-
return inUserNS
16+
file, err := os.Open("/proc/self/uid_map")
17+
if err != nil {
18+
// This kernel-provided file only exists if user namespaces are supported.
19+
return false
20+
}
21+
defer file.Close()
22+
23+
buf := bufio.NewReader(file)
24+
l, _, err := buf.ReadLine()
25+
if err != nil {
26+
return false
27+
}
28+
29+
return uidMapInUserNS(string(l))
3630
}
3731

3832
func uidMapInUserNS(uidMap string) bool {

libcontainer/userns/userns_fuzzer.go renamed to libcontainer/userns/userns_linux_fuzzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build gofuzz
1+
//go:build linux && gofuzz
22

33
package userns
44

libcontainer/userns/userns_unsupported.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
22

33
package userns
44

5-
// runningInUserNS is a stub for non-Linux systems
6-
// Always returns false
7-
func runningInUserNS() bool {
8-
return false
9-
}
10-
11-
// uidMapInUserNS is a stub for non-Linux systems
12-
// Always returns false
13-
func uidMapInUserNS(uidMap string) bool {
14-
return false
15-
}
5+
// inUserNS is a stub for non-Linux systems. Always returns false.
6+
func inUserNS() bool { return false }

0 commit comments

Comments
 (0)