forked from apptainer/singularity
/
rlimit_linux.go
70 lines (57 loc) · 1.53 KB
/
rlimit_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package rlimit
import (
"fmt"
"syscall"
)
var resource = map[string]int{
"RLIMIT_CPU": 0,
"RLIMIT_FSIZE": 1,
"RLIMIT_DATA": 2,
"RLIMIT_STACK": 3,
"RLIMIT_CORE": 4,
"RLIMIT_RSS": 5,
"RLIMIT_NPROC": 6,
"RLIMIT_NOFILE": 7,
"RLIMIT_MEMLOCK": 8,
"RLIMIT_AS": 9,
"RLIMIT_LOCKS": 10,
"RLIMIT_SIGPENDING": 11,
"RLIMIT_MSGQUEUE": 12,
"RLIMIT_NICE": 13,
"RLIMIT_RTPRIO": 14,
"RLIMIT_RTTIME": 15,
}
// Set sets soft and hard resource limit
func Set(res string, cur uint64, max uint64) error {
var rlim syscall.Rlimit
resVal, ok := resource[res]
if !ok {
return fmt.Errorf("%s is not a valid resource type", res)
}
rlim.Cur = cur
rlim.Max = max
if err := syscall.Setrlimit(resVal, &rlim); err != nil {
return fmt.Errorf("failed to set resource limit %s: %s", res, err)
}
return nil
}
// Get retrieves soft and hard resource limit
func Get(res string) (cur uint64, max uint64, err error) {
var rlim syscall.Rlimit
resVal, ok := resource[res]
if !ok {
err = fmt.Errorf("%s is not a valid resource type", res)
return
}
if err = syscall.Getrlimit(resVal, &rlim); err != nil {
err = fmt.Errorf("failed to get resource limit %s: %s", res, err)
return
}
cur = rlim.Cur
max = rlim.Max
return
}