Skip to content

Commit

Permalink
Add Verify function to check if Ulimit max value is too big
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rhatdan committed Aug 4, 2020
1 parent 519db1e commit 850fcf1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ulimit_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package units

import (
"fmt"

"golang.org/x/sys/unix"
)

// Verify that ulimit values work with current kernel
func (u *Ulimit) Verify() error {
var rlimit unix.Rlimit
if err := unix.Getrlimit(ulimitNameMapping[u.Name], &rlimit); err == nil {
if u.Hard > int64(rlimit.Max) {
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)
}
}
return nil
}
17 changes: 17 additions & 0 deletions ulimit_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build linux

package units

import (
"testing"
)

func TestParseUlimitTooBig(t *testing.T) {
u, err := ParseUlimit("nofile=512:1000024")
if err != nil {
t.Fatalf("expected valid value got %q", err)
}
if err := u.Verify(); err == nil {
t.Fatalf("expected invalid hard limit")
}
}
8 changes: 8 additions & 0 deletions ulimit_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build !linux

package units

// Verify that ulimit values work with current kernel
func (u *Ulimit) Verify() error {
return nil
}

0 comments on commit 850fcf1

Please sign in to comment.