Skip to content

Commit e6a9f49

Browse files
committed
deprecate RunningInUserNS(), migrate to github.com/moby/sys/userns
The userns package in libcontainer was integrated into the moby/sys/user module at commit 3778ae603c706494fd1e2c2faf83b406e38d687d. The userns package is used in many places, and currently either depends on runc/libcontainer, or on containerd, both of which have a complex dependency tree. This patch is part of a series of patches to unify the implementations, and to migrate toward that implementation to simplify the dependency tree. [3778ae603c706494fd1e2c2faf83b406e38d687d]: opencontainers/runc@3778ae6 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 0c03de4 commit e6a9f49

File tree

7 files changed

+14
-33
lines changed

7 files changed

+14
-33
lines changed

cgroup1/subsystem.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"fmt"
2121
"os"
2222

23-
"github.com/containerd/cgroups/v3"
2423
v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
24+
"github.com/moby/sys/user/userns"
2525
specs "github.com/opencontainers/runtime-spec/specs-go"
2626
)
2727

@@ -60,7 +60,7 @@ func Subsystems() []Name {
6060
Blkio,
6161
Rdma,
6262
}
63-
if !cgroups.RunningInUserNS() {
63+
if !userns.RunningInUserNS() {
6464
n = append(n, Devices)
6565
}
6666
if _, err := os.Stat("/sys/kernel/mm/hugepages"); err == nil {

cgroup1/utils.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/containerd/cgroups/v3"
3030
units "github.com/docker/go-units"
31+
"github.com/moby/sys/user/userns"
3132
specs "github.com/opencontainers/runtime-spec/specs-go"
3233
)
3334

@@ -53,7 +54,7 @@ func defaults(root string) ([]Subsystem, error) {
5354
}
5455
// only add the devices cgroup if we are not in a user namespace
5556
// because modifications are not allowed
56-
if !cgroups.RunningInUserNS() {
57+
if !userns.RunningInUserNS() {
5758
s = append(s, NewDevices(root))
5859
}
5960
// add the hugetlb cgroup if error wasn't due to missing hugetlb

cmd/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
1616
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
1717
github.com/godbus/dbus/v5 v5.0.4 // indirect
18+
github.com/moby/sys/user v0.2.0 // indirect
1819
github.com/opencontainers/runtime-spec v1.0.2 // indirect
1920
github.com/russross/blackfriday/v2 v2.0.1 // indirect
2021
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect

cmd/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2020
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2121
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2222
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
23+
github.com/moby/sys/user v0.2.0 h1:OnpapJsRp25vkhw8TFG6OLJODNh/3rEwRWtJ3kakwRM=
24+
github.com/moby/sys/user v0.2.0/go.mod h1:RYstrcWOJpVh+6qzUqp2bU3eaRpdiQeKGlKitaH0PM8=
2325
github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0=
2426
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
2527
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/coreos/go-systemd/v22 v22.3.2
99
github.com/docker/go-units v0.5.0
1010
github.com/godbus/dbus/v5 v5.0.4
11+
github.com/moby/sys/user v0.2.0
1112
github.com/opencontainers/runtime-spec v1.0.2
1213
github.com/stretchr/testify v1.8.4
1314
go.uber.org/goleak v1.1.12

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2222
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2323
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2424
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
25+
github.com/moby/sys/user v0.2.0 h1:OnpapJsRp25vkhw8TFG6OLJODNh/3rEwRWtJ3kakwRM=
26+
github.com/moby/sys/user v0.2.0/go.mod h1:RYstrcWOJpVh+6qzUqp2bU3eaRpdiQeKGlKitaH0PM8=
2527
github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0=
2628
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
2729
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

utils.go

+4-30
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ import (
2525
"strings"
2626
"sync"
2727

28+
"github.com/moby/sys/user/userns"
2829
"golang.org/x/sys/unix"
2930
)
3031

3132
var (
32-
nsOnce sync.Once
33-
inUserNS bool
3433
checkMode sync.Once
3534
cgMode CGMode
3635
)
@@ -77,35 +76,10 @@ func Mode() CGMode {
7776

7877
// RunningInUserNS detects whether we are currently running in a user namespace.
7978
// Copied from github.com/lxc/lxd/shared/util.go
79+
//
80+
// Deprecated: use [userns.RunningInUserNS].
8081
func RunningInUserNS() bool {
81-
nsOnce.Do(func() {
82-
file, err := os.Open("/proc/self/uid_map")
83-
if err != nil {
84-
// This kernel-provided file only exists if user namespaces are supported
85-
return
86-
}
87-
defer file.Close()
88-
89-
buf := bufio.NewReader(file)
90-
l, _, err := buf.ReadLine()
91-
if err != nil {
92-
return
93-
}
94-
95-
line := string(l)
96-
var a, b, c int64
97-
fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
98-
99-
/*
100-
* We assume we are in the initial user namespace if we have a full
101-
* range - 4294967295 uids starting at uid 0.
102-
*/
103-
if a == 0 && b == 0 && c == 4294967295 {
104-
return
105-
}
106-
inUserNS = true
107-
})
108-
return inUserNS
82+
return userns.RunningInUserNS()
10983
}
11084

11185
// ParseCgroupFileUnified returns legacy subsystem paths as the first value,

0 commit comments

Comments
 (0)