Skip to content

Commit 50f05aa

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 d36096e commit 50f05aa

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

ulimit.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,15 @@ func ParseUlimit(val string) (*Ulimit, error) {
109109
return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
110110
}
111111

112-
// ParseUlimitAndVerify parses the value and verifyies that values work
113-
// with current kernel, returns a Ulimit from the specified string.
114-
func ParseUlimitAndVerify(val string) (*Ulimit, error) {
115-
limit, err := ParseUlimit(val)
116-
if err != nil {
117-
return nil, err
118-
}
119-
120-
parts := strings.Split(val, "=")
121-
if len(parts) < 1 {
122-
// this should never happen ParseUlimit should have caught this
123-
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
124-
}
125-
112+
// Verify that ulimit values work with current kernel
113+
func (u *Ulimit) Verify(limit Ulimit) error {
126114
var rlimit syscall.Rlimit
127115
if err := syscall.Getrlimit(ulimitNameMapping[parts[0]], &rlimit); err == nil {
128-
if limit.Hard > int64(rlimit.Max) {
129-
return nil, fmt.Errorf("ulimit hard limit must be less than or equal to hard limit for the current process, hard: %d", limit.Hard)
116+
if u.Hard > int64(rlimit.Max) {
117+
return fmt.Errorf("ulimit hard limit must be less than or equal to hard limit for the current process, hard: %d", limit.Hard)
130118
}
131119
}
132-
return limit, nil
120+
return nil
133121
}
134122

135123
// GetRlimit returns the RLimit corresponding to Ulimit.

ulimit_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ func TestParseUlimitValid(t *testing.T) {
2222
}
2323

2424
func TestParseUlimitTooBig(t *testing.T) {
25-
if _, err := ParseUlimitAndVerify("nofile=512:1000024"); err == nil {
26-
t.Fatalf("expected invalid value got %q", err)
25+
if u, err := ParseUlimit("nofile=512:1000024"); err != nil {
26+
t.Fatalf("expected valid value got %q", err)
27+
}
28+
if err := u.Verify(); err == nil {
29+
t.Fatalf("expected invalid hard limit")
2730
}
2831
}
2932

0 commit comments

Comments
 (0)