-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgroup.go
423 lines (350 loc) · 8.74 KB
/
cgroup.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package cgroup
import (
"bufio"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/nextlinux/tracee/pkg/errfmt"
"github.com/nextlinux/tracee/pkg/logger"
"github.com/nextlinux/tracee/pkg/mount"
)
// Constants
const (
CgroupV1FsType = "cgroup"
CgroupV2FsType = "cgroup2"
CgroupDefaultController = "cpuset"
CgroupControllersFile = "/sys/fs/cgroup/cgroup.controllers"
procCgroups = "/proc/cgroups"
sysFsCgroup = "/sys/fs/cgroup"
)
// Versions
type CgroupVersion int
func (v CgroupVersion) String() string {
switch v {
case CgroupVersion1:
return CgroupV1FsType
case CgroupVersion2:
return CgroupV2FsType
}
return ""
}
const (
CgroupVersion1 CgroupVersion = iota
CgroupVersion2
)
//
// Cgroups
//
type Cgroups struct {
cgroupv1 Cgroup
cgroupv2 Cgroup
cgroup *Cgroup // pointer to default cgroup version
hid int // default cgroup controller hierarchy ID
}
func NewCgroups() (*Cgroups, error) {
var err error
var cgrp *Cgroup
var cgroupv1, cgroupv2 Cgroup
// discover the default cgroup being used
defaultVersion, err := GetCgroupDefaultVersion()
if err != nil {
return nil, errfmt.WrapError(err)
}
// only start cgroupv1 if it is the OS default (or else it isn't needed)
if defaultVersion == CgroupVersion1 {
cgroupv1, err = NewCgroup(CgroupVersion1)
if err != nil {
if _, ok := err.(*VersionNotSupported); !ok {
return nil, errfmt.WrapError(err)
}
}
}
// start cgroupv2 (if supported)
cgroupv2, err = NewCgroup(CgroupVersion2)
if err != nil {
if _, ok := err.(*VersionNotSupported); !ok {
return nil, errfmt.WrapError(err)
}
}
// at least one (or both) has to be supported
if cgroupv1 == nil && cgroupv2 == nil {
return nil, NoCgroupSupport()
}
hid := 0
// adjust pointer to the default cgroup version
switch defaultVersion {
case CgroupVersion1:
if cgroupv1 == nil {
return nil, CouldNotFindOrMountDefaultCgroup(CgroupVersion1)
}
cgrp = &cgroupv1
// discover default cgroup controller hierarchy id for cgroupv1
hid, err = GetCgroupControllerHierarchy(CgroupDefaultController)
if err != nil {
return nil, errfmt.WrapError(err)
}
case CgroupVersion2:
if cgroupv2 == nil {
return nil, CouldNotFindOrMountDefaultCgroup(CgroupVersion2)
}
cgrp = &cgroupv2
}
cs := &Cgroups{
cgroupv1: cgroupv1,
cgroupv2: cgroupv2,
cgroup: cgrp,
hid: hid,
}
return cs, nil
}
func (cs *Cgroups) Destroy() error {
if cs.cgroupv1 != nil {
err := cs.cgroupv1.destroy()
if err != nil {
return errfmt.WrapError(err)
}
}
if cs.cgroupv2 != nil {
return cs.cgroupv2.destroy()
}
return nil
}
func (cs *Cgroups) GetDefaultCgroupHierarchyID() int {
return cs.hid
}
func (cs *Cgroups) GetDefaultCgroup() Cgroup {
return *cs.cgroup
}
func (cs *Cgroups) GetCgroup(ver CgroupVersion) Cgroup {
switch ver {
case CgroupVersion1:
return cs.cgroupv1
case CgroupVersion2:
return cs.cgroupv2
}
return nil
}
//
// Cgroup
//
type Cgroup interface {
init() error
destroy() error
GetMountPoint() string
getDefaultHierarchyID() int
GetVersion() CgroupVersion
}
func NewCgroup(ver CgroupVersion) (Cgroup, error) {
var c Cgroup
switch ver {
case CgroupVersion1:
c = &CgroupV1{}
case CgroupVersion2:
c = &CgroupV2{}
}
return c, c.init()
}
// cgroupv1
type CgroupV1 struct {
mounted *mount.MountOnce
mountpoint string
hid int
}
func (c *CgroupV1) init() error {
// 0. check if cgroup type is supported
supported, err := mount.IsFileSystemSupported(CgroupVersion1.String())
if err != nil {
return errfmt.WrapError(err)
}
if !supported {
return &VersionNotSupported{}
}
// 1. mount cgroup (if needed)
c.mounted, err = mount.NewMountOnce(
CgroupV1FsType,
CgroupV1FsType,
CgroupDefaultController,
sysFsCgroup, // where to check for already mounted cgroupfs
)
if err != nil {
return errfmt.WrapError(err)
}
// 2. discover where cgroup is mounted
c.mountpoint = c.mounted.GetMountpoint()
return nil
}
func (c *CgroupV1) destroy() error {
return c.mounted.Umount()
}
func (c *CgroupV1) GetMountPoint() string {
return c.mountpoint
}
func (c *CgroupV1) getDefaultHierarchyID() int {
return c.hid
}
func (c *CgroupV1) GetVersion() CgroupVersion {
return CgroupVersion1
}
// cgroupv2
type CgroupV2 struct {
mounted *mount.MountOnce
mountpoint string
hid int
}
func (c *CgroupV2) init() error {
// 0. check if cgroup type is supported
supported, err := mount.IsFileSystemSupported(CgroupVersion2.String())
if err != nil {
return errfmt.WrapError(err)
}
if !supported {
return &VersionNotSupported{}
}
// 1. mount cgroup (if needed)
c.mounted, err = mount.NewMountOnce(
CgroupV2FsType,
CgroupV2FsType,
"", // cgroupv2 has no default controller
sysFsCgroup, // where to check for already mounted cgroupfs
)
if err != nil {
return errfmt.WrapError(err)
}
// 2. discover where cgroup is mounted
c.mountpoint = c.mounted.GetMountpoint()
return nil
}
func (c *CgroupV2) destroy() error {
return c.mounted.Umount()
}
func (c *CgroupV2) GetMountPoint() string {
return c.mountpoint
}
func (c *CgroupV2) getDefaultHierarchyID() int {
return c.hid
}
func (c *CgroupV2) GetVersion() CgroupVersion {
return CgroupVersion2
}
//
// General
//
func GetCgroupDefaultVersion() (CgroupVersion, error) {
// 1st Method: already mounted cgroupv1 filesystem
if ok, _ := IsCgroupV2MountedAndDefault(); ok {
return CgroupVersion2, nil
}
//
// 2nd Method: From cgroup man page:
// ...
// 2. The unique ID of the cgroup hierarchy on which this
// controller is mounted. If multiple cgroups v1
// controllers are bound to the same hierarchy, then each
// will show the same hierarchy ID in this field. The
// value in this field will be 0 if:
//
// a) the controller is not mounted on a cgroups v1
// hierarchy;
// b) the controller is bound to the cgroups v2 single
// unified hierarchy; or
// c) the controller is disabled (see below).
// ...
var value int
file, err := os.Open(procCgroups)
if err != nil {
return -1, CouldNotOpenFile(procCgroups, err)
}
defer func() {
if err := file.Close(); err != nil {
logger.Errorw("Closing file", "error", err)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Fields(scanner.Text())
if line[0] != CgroupDefaultController {
continue
}
value, err = strconv.Atoi(line[1])
if err != nil || value < 0 {
return -1, ErrorParsingFile(procCgroups, err)
}
}
if value == 0 { // == (a), (b) or (c)
return CgroupVersion2, nil
}
return CgroupVersion1, nil
}
// IsCgroupV2MountedAndDefault tests if cgroup2 is mounted and is the default
// cgroup version being used by the running environment. It does so by checking
// the existence of a "cgroup.controllers" file in default cgroupfs mountpoint.
func IsCgroupV2MountedAndDefault() (bool, error) {
_, err := os.Stat(CgroupControllersFile)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, CouldNotOpenFile(CgroupControllersFile, err)
}
return true, nil
}
// Returns a cgroup controller hierarchy value
func GetCgroupControllerHierarchy(subsys string) (int, error) {
var value int
file, err := os.Open(procCgroups)
if err != nil {
return -1, CouldNotOpenFile(procCgroups, err)
}
defer func() {
if err := file.Close(); err != nil {
logger.Errorw("Closing file", "error", err)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Fields(scanner.Text())
if len(line) < 2 || line[0] != subsys {
continue
}
value, err = strconv.Atoi(line[1])
if err != nil || value < 0 {
return -1, ErrorParsingFile(procCgroups, err)
}
}
return value, nil
}
// GetCgroupPath walks the cgroup fs and provides the cgroup directory path of
// given cgroupId and subPath (related to cgroup fs root dir). If subPath is
// empty, then all directories from cgroup fs will be searched for the given
// cgroupId.
func GetCgroupPath(rootDir string, cgroupId uint64, subPath string) (string, error) {
entries, err := os.ReadDir(rootDir)
if err != nil {
return "", errfmt.WrapError(err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
entryPath := filepath.Join(rootDir, entry.Name())
if strings.HasSuffix(entryPath, subPath) {
// Lower 32 bits of the cgroup id == inode number of matching cgroupfs entry
var stat syscall.Stat_t
if err := syscall.Stat(entryPath, &stat); err == nil {
// Check if this cgroup path belongs to cgroupId
if (stat.Ino & 0xFFFFFFFF) == (cgroupId & 0xFFFFFFFF) {
return entryPath, nil
}
}
}
// No match at this dir level: continue recursively
path, err := GetCgroupPath(entryPath, cgroupId, subPath)
if err == nil {
return path, nil
}
}
return "", fs.ErrNotExist
}