@@ -86,63 +86,37 @@ func init() {
8686}
8787
8888// getAvailableControllers get the available controllers.
89- func getAvailableControllers (exclude map [string ]controllerHandler , cgroup2 bool ) ([]controller , error ) {
90- if cgroup2 {
91- controllers := []controller {}
92- controllersFile := filepath .Join (cgroupRoot , "cgroup.controllers" )
93-
94- // rootless cgroupv2: check available controllers for current user, systemd or servicescope will inherit
95- if unshare .IsRootless () {
96- userSlice , err := getCgroupPathForCurrentProcess ()
97- if err != nil {
98- return controllers , err
99- }
100- // userSlice already contains '/' so not adding here
101- basePath := cgroupRoot + userSlice
102- controllersFile = filepath .Join (basePath , "cgroup.controllers" )
103- }
104- controllersFileBytes , err := os .ReadFile (controllersFile )
105- if err != nil {
106- return nil , fmt .Errorf ("failed while reading controllers for cgroup v2: %w" , err )
107- }
108- for controllerName := range strings .FieldsSeq (string (controllersFileBytes )) {
109- c := controller {
110- name : controllerName ,
111- symlink : false ,
112- }
113- controllers = append (controllers , c )
114- }
115- return controllers , nil
116- }
117-
118- subsystems , _ := cgroupV1GetAllSubsystems ()
89+ func getAvailableControllers () ([]controller , error ) {
11990 controllers := []controller {}
120- // cgroupv1 and rootless: No subsystem is available: delegation is unsafe.
121- if unshare .IsRootless () {
122- return controllers , nil
123- }
91+ controllersFile := filepath .Join (cgroupRoot , "cgroup.controllers" )
12492
125- for _ , name := range subsystems {
126- if _ , found := exclude [name ]; found {
127- continue
128- }
129- fileInfo , err := os .Stat (cgroupRoot + "/" + name )
93+ // rootless cgroupv2: check available controllers for current user, systemd or servicescope will inherit
94+ if unshare .IsRootless () {
95+ userSlice , err := getCgroupPathForCurrentProcess ()
13096 if err != nil {
131- continue
97+ return controllers , err
13298 }
99+ // userSlice already contains '/' so not adding here
100+ basePath := cgroupRoot + userSlice
101+ controllersFile = filepath .Join (basePath , "cgroup.controllers" )
102+ }
103+ controllersFileBytes , err := os .ReadFile (controllersFile )
104+ if err != nil {
105+ return nil , fmt .Errorf ("failed while reading controllers for cgroup v2: %w" , err )
106+ }
107+ for controllerName := range strings .FieldsSeq (string (controllersFileBytes )) {
133108 c := controller {
134- name : name ,
135- symlink : ! fileInfo . IsDir () ,
109+ name : controllerName ,
110+ symlink : false ,
136111 }
137112 controllers = append (controllers , c )
138113 }
139-
140114 return controllers , nil
141115}
142116
143117// AvailableControllers get string:bool map of all the available controllers.
144- func AvailableControllers (exclude map [ string ] controllerHandler , cgroup2 bool ) ([]string , error ) {
145- availableControllers , err := getAvailableControllers (exclude , cgroup2 )
118+ func AvailableControllers () ([]string , error ) {
119+ availableControllers , err := getAvailableControllers ()
146120 if err != nil {
147121 return nil , err
148122 }
@@ -154,31 +128,6 @@ func AvailableControllers(exclude map[string]controllerHandler, cgroup2 bool) ([
154128 return controllerList , nil
155129}
156130
157- func cgroupV1GetAllSubsystems () ([]string , error ) {
158- f , err := os .Open ("/proc/cgroups" )
159- if err != nil {
160- return nil , err
161- }
162- defer f .Close ()
163-
164- subsystems := []string {}
165-
166- s := bufio .NewScanner (f )
167- for s .Scan () {
168- text := s .Text ()
169- if text [0 ] != '#' {
170- parts := strings .Fields (text )
171- if len (parts ) >= 4 && parts [3 ] != "0" {
172- subsystems = append (subsystems , parts [0 ])
173- }
174- }
175- }
176- if err := s .Err (); err != nil {
177- return nil , err
178- }
179- return subsystems , nil
180- }
181-
182131func getCgroupPathForCurrentProcess () (string , error ) {
183132 path := fmt .Sprintf ("/proc/%d/cgroup" , os .Getpid ())
184133 f , err := os .Open (path )
@@ -607,7 +556,7 @@ func SystemCPUUsage() (uint64, error) {
607556 return total , nil
608557}
609558
610- // IsCgroup2UnifiedMode returns whether we are running in cgroup 2 unified mode.
559+ // IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
611560func IsCgroup2UnifiedMode () (bool , error ) {
612561 isUnifiedOnce .Do (func () {
613562 var st syscall.Statfs_t
0 commit comments