-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils_linux.go
184 lines (165 loc) · 4.28 KB
/
utils_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cgroup
import (
"errors"
"fmt"
"io/fs"
"math/rand/v2"
"os"
"path"
"strconv"
"strings"
"syscall"
"golang.org/x/sys/unix"
)
// EnsureDirExists creates directories if the path not exists
func EnsureDirExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, dirPerm)
}
return os.ErrExist
}
// CreateV1ControllerPath create path for controller with given group, prefix
func CreateV1ControllerPath(controller, prefix string) (string, error) {
p := path.Join(basePath, controller, prefix)
return p, EnsureDirExists(p)
}
const initPath = "init"
// EnableV2Nesting migrates all process in the container to nested /init path
// and enables all available controllers in the root cgroup
func EnableV2Nesting() error {
if DetectType() != TypeV2 {
return nil
}
p, err := readFile(path.Join(basePath, cgroupProcs))
if err != nil {
return err
}
procs := strings.Split(string(p), "\n")
if len(procs) == 0 {
return nil
}
// mkdir init
if err := os.Mkdir(path.Join(basePath, initPath), dirPerm); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
// move all process into init cgroup
procFile, err := os.OpenFile(path.Join(basePath, initPath, cgroupProcs), os.O_RDWR, filePerm)
if err != nil {
return err
}
for _, v := range procs {
if _, err := procFile.WriteString(v); err != nil {
continue
//return err
}
}
procFile.Close()
return nil
}
// ReadProcesses reads cgroup.procs file and return pids individually
func ReadProcesses(path string) ([]int, error) {
content, err := readFile(path)
if err != nil {
return nil, err
}
procs := strings.Split(string(content), "\n")
rt := make([]int, len(procs))
for i, x := range procs {
if len(x) == 0 {
continue
}
rt[i], err = strconv.Atoi(x)
if err != nil {
return nil, err
}
}
return rt, nil
}
// AddProcesses add processes into cgroup.procs file
func AddProcesses(path string, procs []int) error {
f, err := os.OpenFile(path, os.O_RDWR, filePerm)
if err != nil {
return err
}
defer f.Close()
for _, p := range procs {
if _, err := f.WriteString(strconv.Itoa(p)); err != nil {
return err
}
}
return nil
}
// DetectType detects current mounted cgroup type in systemd default path
func DetectType() Type {
// if /sys/fs/cgroup is mounted as CGROUPV2 or TMPFS (V1)
var st unix.Statfs_t
if err := unix.Statfs(basePath, &st); err != nil {
// ignore errors, defaulting to CgroupV1
return TypeV1
}
if st.Type == unix.CGROUP2_SUPER_MAGIC {
return TypeV2
}
return TypeV1
}
func remove(name string) error {
if name != "" {
return os.Remove(name)
}
return nil
}
var errPatternHasSeparator = errors.New("pattern contains path separator")
// prefixAndSuffix splits pattern by the last wildcard "*", if applicable,
// returning prefix as the part before "*" and suffix as the part after "*".
func prefixAndSuffix(pattern string) (prefix, suffix string, err error) {
for i := 0; i < len(pattern); i++ {
if os.IsPathSeparator(pattern[i]) {
return "", "", errPatternHasSeparator
}
}
if pos := strings.LastIndexByte(pattern, '*'); pos != -1 {
prefix, suffix = pattern[:pos], pattern[pos+1:]
} else {
prefix = pattern
}
return prefix, suffix, nil
}
func readFile(p string) ([]byte, error) {
data, err := os.ReadFile(p)
for err != nil && errors.Is(err, syscall.EINTR) {
data, err = os.ReadFile(p)
}
return data, err
}
func writeFile(p string, content []byte, perm fs.FileMode) error {
err := os.WriteFile(p, content, perm)
for err != nil && errors.Is(err, syscall.EINTR) {
err = os.WriteFile(p, content, perm)
}
return err
}
func nextRandom() string {
return strconv.Itoa(int(rand.Int32()))
}
// randomBuild creates a cgroup with random directory, similar to os.MkdirTemp
func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, error) {
prefix, suffix, err := prefixAndSuffix(pattern)
if err != nil {
return nil, fmt.Errorf("cgroup.builder: random %v", err)
}
try := 0
for {
name := prefix + nextRandom() + suffix
cg, err := build(name)
if err == nil {
return cg, nil
}
if errors.Is(err, os.ErrExist) || cg.Existing() {
if try++; try < 10000 {
continue
}
return nil, fmt.Errorf("cgroup.builder: tried 10000 times but failed")
}
return nil, fmt.Errorf("cgroup.builder: random %v", err)
}
}