Skip to content

Commit 6d777e1

Browse files
committed
Add Verify function to check if Ulimit max value is too big
When parsing the ulimit, we should check if the ulimit max value is greater then the processes max value. If yes then we should return an error. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
1 parent 519db1e commit 6d777e1

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

ulimit_linux.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package units
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/sys/unix"
7+
)
8+
9+
// Verify that ulimit values work with current kernel
10+
func (u *Ulimit) Verify() error {
11+
var rlimit unix.Rlimit
12+
if err := unix.Getrlimit(ulimitNameMapping[u.Name], &rlimit); err == nil {
13+
if u.Hard > int64(rlimit.Max) {
14+
return fmt.Errorf("ulimit hard limit (%d) must be less than or equal to hard limit for the current process %d", u.Hard, rlimit.Max)
15+
}
16+
}
17+
return nil
18+
}

ulimit_linux_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// +build linux
2+
3+
package units
4+
5+
import (
6+
"fmt"
7+
8+
"golang.org/x/sys/unix"
9+
"testing"
10+
)
11+
12+
func TestParseUlimitTooBig(t *testing.T) {
13+
var rlimit unix.Rlimit
14+
if err := unix.Getrlimit(rlimitNofile, &rlimit); err != nil {
15+
t.Fatalf("Failed to get rlimit of current process %q", err)
16+
}
17+
u, err := ParseUlimit(fmt.Sprintf("nofile=512:%d", rlimit.Max+1))
18+
if err != nil {
19+
t.Fatalf("expected valid value got %q", err)
20+
}
21+
if err := u.Verify(); err == nil {
22+
t.Fatalf("expected invalid hard limit")
23+
}
24+
}

ulimit_unsupported.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build !linux
2+
3+
package units
4+
5+
// Verify that ulimit values work with current kernel
6+
func (u *Ulimit) Verify() error {
7+
return nil
8+
}

0 commit comments

Comments
 (0)