...
1
16
17 package fs2
18
19 import (
20 "bufio"
21 "errors"
22 "fmt"
23 "io"
24 "os"
25 "path/filepath"
26 "strings"
27
28 "github.com/opencontainers/runc/libcontainer/configs"
29 "github.com/opencontainers/runc/libcontainer/utils"
30 )
31
32 const UnifiedMountpoint = "/sys/fs/cgroup"
33
34 func defaultDirPath(c *configs.Cgroup) (string, error) {
35 if (c.Name != "" || c.Parent != "") && c.Path != "" {
36 return "", fmt.Errorf("cgroup: either Path or Name and Parent should be used, got %+v", c)
37 }
38
39 return _defaultDirPath(UnifiedMountpoint, c.Path, c.Parent, c.Name)
40 }
41
42 func _defaultDirPath(root, cgPath, cgParent, cgName string) (string, error) {
43 if (cgName != "" || cgParent != "") && cgPath != "" {
44 return "", errors.New("cgroup: either Path or Name and Parent should be used")
45 }
46
47
48 innerPath := utils.CleanPath(cgPath)
49 if innerPath == "" {
50 cgParent := utils.CleanPath(cgParent)
51 cgName := utils.CleanPath(cgName)
52 innerPath = filepath.Join(cgParent, cgName)
53 }
54 if filepath.IsAbs(innerPath) {
55 return filepath.Join(root, innerPath), nil
56 }
57
58 ownCgroup, err := parseCgroupFile("/proc/self/cgroup")
59 if err != nil {
60 return "", err
61 }
62
63
64
65 ownCgroup = filepath.Dir(ownCgroup)
66
67 return filepath.Join(root, ownCgroup, innerPath), nil
68 }
69
70
71 func parseCgroupFile(path string) (string, error) {
72 f, err := os.Open(path)
73 if err != nil {
74 return "", err
75 }
76 defer f.Close()
77 return parseCgroupFromReader(f)
78 }
79
80 func parseCgroupFromReader(r io.Reader) (string, error) {
81 s := bufio.NewScanner(r)
82 for s.Scan() {
83 var (
84 text = s.Text()
85 parts = strings.SplitN(text, ":", 3)
86 )
87 if len(parts) < 3 {
88 return "", fmt.Errorf("invalid cgroup entry: %q", text)
89 }
90
91 if parts[0] == "0" && parts[1] == "" {
92 return parts[2], nil
93 }
94 }
95 if err := s.Err(); err != nil {
96 return "", err
97 }
98 return "", errors.New("cgroup path not found")
99 }
100
View as plain text