Skip to content

Commit 83eebdf

Browse files
committed
libcontainer:clean cached rlimit nofile in go runtime
As reported in issue opencontainers#4195, the new version of go runtime will cache rlimit-nofile. before executing exec, the rlimit-nofile of the process will be updated with the cache. in runc, this will cause the rlimit-nofile set by the parent process for the container to become invalid. this can be solved by clearing the cache. Signed-off-by: ls-ggg <[email protected]>
1 parent 4641f17 commit 83eebdf

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

libcontainer/setns_init_linux.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func (l *linuxSetnsInit) Init() error {
4949
}
5050
}
5151
}
52+
53+
if err := utils.CleanRlimitNoFileCache(); err != nil {
54+
return err
55+
}
56+
5257
if l.config.CreateConsole {
5358
if err := setupConsole(l.consoleSocket, l.config, false); err != nil {
5459
return err

libcontainer/standard_init_linux.go

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func (l *linuxStandardInit) Init() error {
7777
}
7878
}
7979

80+
if err := utils.CleanRlimitNoFileCache(); err != nil {
81+
return err
82+
}
83+
8084
if err := setupNetwork(l.config); err != nil {
8185
return err
8286
}

libcontainer/utils/utils.go

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"syscall"
1011
"unsafe"
1112

1213
"golang.org/x/sys/unix"
@@ -129,3 +130,13 @@ func Annotations(labels []string) (bundle string, userAnnotations map[string]str
129130
}
130131
return
131132
}
133+
134+
// Clean the cache of RLIMIT_NOFILE in go runtime
135+
// https://github.com/golang/go/commit/f5eef58e4381259cbd84b3f2074c79607fb5c821#diff-ec665e9789f8cf5cd1828ad7fa9f0ff4ebc1f5b5dd0fc82a296da5c07da7ece6
136+
func CleanRlimitNoFileCache() error {
137+
rlimit := syscall.Rlimit{}
138+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
139+
return err
140+
}
141+
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
142+
}

tests/integration/resources.bats

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function setup() {
6+
setup_busybox
7+
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n"]'
8+
}
9+
10+
@test "runc run with RLIMIT_NOFILE" {
11+
update_config '.process.capabilities.bounding = ["CAP_RESOURCE"]'
12+
update_config '.process.rlimits = [{"type": "RLIMIT_NOFILE", "hard": 10000, "soft": 10000}]'
13+
14+
runc run test_hello
15+
[ "$status" -eq 0 ]
16+
17+
[[ "${output}" == *"10000"* ]]
18+
}

0 commit comments

Comments
 (0)