forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
61 lines (49 loc) · 1.15 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
package main
import (
"bufio"
"io/ioutil"
"os"
"path"
"strings"
)
func getInitCgroupPath(controller string) string {
f, err := os.Open("/proc/1/cgroup")
if err != nil {
return "/"
}
defer f.Close()
scan := bufio.NewScanner(f)
for scan.Scan() {
line := scan.Text()
fields := strings.Split(line, ":")
if len(fields) != 3 {
return "/"
}
if fields[2] != controller {
continue
}
initPath := string(fields[3])
// ignore trailing /init.scope if it is there
dir, file := path.Split(initPath)
if file == "init.scope" {
return dir
} else {
return initPath
}
}
return "/"
}
func cGroupGet(controller, cgroup, file string) (string, error) {
initPath := getInitCgroupPath(controller)
path := path.Join("/sys/fs/cgroup", controller, initPath, cgroup, file)
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.Trim(string(contents), "\n"), nil
}
func cGroupSet(controller, cgroup, file string, value string) error {
initPath := getInitCgroupPath(controller)
path := path.Join("/sys/fs/cgroup", controller, initPath, cgroup, file)
return ioutil.WriteFile(path, []byte(value), 0755)
}