File tree Expand file tree Collapse file tree 4 files changed +24
-35
lines changed Expand file tree Collapse file tree 4 files changed +24
-35
lines changed Original file line number Diff line number Diff line change 11package 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+ }
Original file line number Diff line number Diff 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.
1815func 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
3832func uidMapInUserNS (uidMap string ) bool {
Original file line number Diff line number Diff line change 1- //go:build gofuzz
1+ //go:build linux && gofuzz
22
33package userns
44
Original file line number Diff line number Diff line change 22
33package 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 }
You can’t perform that action at this time.
0 commit comments