@@ -68,6 +68,56 @@ func NewManager(cg *configs.Cgroup, paths map[string]string, rootless bool) cgro
68
68
var cgroupRootLock sync.Mutex
69
69
var cgroupRoot string
70
70
71
+ const defaultCgroupRoot = "/sys/fs/cgroup"
72
+
73
+ func tryDefaultCgroupRoot () string {
74
+ var st , pst unix.Stat_t
75
+
76
+ // (1) it should be a directory...
77
+ err := unix .Lstat (defaultCgroupRoot , & st )
78
+ if err != nil || st .Mode & unix .S_IFDIR == 0 {
79
+ return ""
80
+ }
81
+
82
+ // (2) ... and a mount point ...
83
+ err = unix .Lstat (filepath .Dir (defaultCgroupRoot ), & pst )
84
+ if err != nil {
85
+ return ""
86
+ }
87
+
88
+ if st .Dev == pst .Dev {
89
+ // parent dir has the same dev -- not a mount point
90
+ return ""
91
+ }
92
+
93
+ // (3) ... of 'tmpfs' fs type.
94
+ var fst unix.Statfs_t
95
+ err = unix .Statfs (defaultCgroupRoot , & fst )
96
+ if err != nil || fst .Type != unix .TMPFS_MAGIC {
97
+ return ""
98
+ }
99
+
100
+ // (4) it should have at least 1 entry ...
101
+ dir , err := os .Open (defaultCgroupRoot )
102
+ if err != nil {
103
+ return ""
104
+ }
105
+ names , err := dir .Readdirnames (1 )
106
+ if err != nil {
107
+ return ""
108
+ }
109
+ if len (names ) < 1 {
110
+ return ""
111
+ }
112
+ // ... which is a cgroup mount point.
113
+ err = unix .Statfs (filepath .Join (defaultCgroupRoot , names [0 ]), & fst )
114
+ if err != nil || fst .Type != unix .CGROUP_SUPER_MAGIC {
115
+ return ""
116
+ }
117
+
118
+ return defaultCgroupRoot
119
+ }
120
+
71
121
// Gets the cgroupRoot.
72
122
func getCgroupRoot () (string , error ) {
73
123
cgroupRootLock .Lock ()
@@ -77,6 +127,14 @@ func getCgroupRoot() (string, error) {
77
127
return cgroupRoot , nil
78
128
}
79
129
130
+ // fast path
131
+ cgroupRoot = tryDefaultCgroupRoot ()
132
+ if cgroupRoot != "" {
133
+ return cgroupRoot , nil
134
+ }
135
+
136
+ // slow path: parse mountinfo, find the first mount where fs=cgroup
137
+ // (e.g. "/sys/fs/cgroup/memory"), use its parent.
80
138
f , err := os .Open ("/proc/self/mountinfo" )
81
139
if err != nil {
82
140
return "" , err
0 commit comments