host_cgroup.go 826 Bytes
Newer Older
“李磊”'s avatar
“李磊” committed
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
package cgroup

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"

	"linkfog.com/public/lib/l"
	"linkfog.com/public/option"
)

var HostCgroupCfg string

func SetHostCgroup() (err error) {
	if HostCgroupCfg != "" {
		return nil
	}
	for i := 1; i < 10; i++ {
		HostCgroupCfg, err = GetCgroup(i)
		if err != nil {
			l.Info(getCgroupPath(i), err)
			continue
		}
		return nil
	}
	return fmt.Errorf("set host cgroup err")
}

func IsContainerCgroup(cgroup string) bool {
	if HostCgroupCfg == "" || cgroup == "" {
		return false
	}
	return cgroup != HostCgroupCfg
}

func GetCgroup(pid int) (string, error) {
	cgroup, err := os.ReadFile(getCgroupPath(pid))
	if err != nil {
		return "", err
	}
	return string(cgroup), nil
}

func getCgroupPath(pid int) string {
	return filepath.Join(option.HostProc, strconv.Itoa(pid), "cgroup")
}