-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
65 lines (52 loc) · 1.12 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
package testutils
import (
"errors"
"github.com/isu-kim/dyn-uprobe/internal"
"os"
"strings"
"testing"
"github.com/isu-kim/dyn-uprobe/internal/unix"
)
var cgroup2Path = internal.Memoize(func() (string, error) {
mounts, err := os.ReadFile("/proc/mounts")
if err != nil {
return "", err
}
for _, line := range strings.Split(string(mounts), "\n") {
mount := strings.SplitN(line, " ", 3)
if mount[0] == "cgroup2" {
return mount[1], nil
}
continue
}
return "", errors.New("cgroup2 not mounted")
})
func CreateCgroup(tb testing.TB) *os.File {
tb.Helper()
cg2, err := cgroup2Path()
if err != nil {
tb.Fatal("Can't locate cgroup2 mount:", err)
}
cgdir, err := os.MkdirTemp(cg2, "ebpf-link")
if err != nil {
tb.Fatal("Can't create cgroupv2:", err)
}
cgroup, err := os.Open(cgdir)
if err != nil {
os.Remove(cgdir)
tb.Fatal(err)
}
tb.Cleanup(func() {
cgroup.Close()
os.Remove(cgdir)
})
return cgroup
}
func GetCgroupIno(t *testing.T, cgroup *os.File) uint64 {
cgroupStat := unix.Stat_t{}
err := unix.Fstat(int(cgroup.Fd()), &cgroupStat)
if err != nil {
t.Fatal(err)
}
return cgroupStat.Ino
}