Skip to content

Commit

Permalink
bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Nov 15, 2019
1 parent a4c0779 commit f2ad05a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Cgroups) List() ([]string, error) {

err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if err != nil {
// continue walking
return nil
}
if f == nil {
return nil
Expand Down Expand Up @@ -108,7 +108,7 @@ func (c *Cgroups) List() ([]string, error) {
func (c *Cgroups) AttachedSubsystems(h string) []string {
enabled := []string{}
for _, s := range Subsystems {
path := fmt.Sprintf("%s/%s%s", c.FsPath, s, h)
path := filepath.Clean(filepath.Join(c.FsPath, s, h))
if _, err := os.Lstat(path); err != nil {
continue
}
Expand All @@ -119,8 +119,22 @@ func (c *Cgroups) AttachedSubsystems(h string) []string {

// IsAttachedSubsystem return subsystem is active or not in specific cgroup hierarchy
func (c *Cgroups) IsAttachedSubsystem(h string, sname string) bool {
path := fmt.Sprintf("%s/%s%s", c.FsPath, sname, h)
if _, err := os.Lstat(path); err != nil {
path := filepath.Clean(filepath.Join(c.FsPath, sname, h))
f, err := os.Lstat(path)
if err != nil {
return false
}
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
realpath, err := filepath.EvalSymlinks(path)
if err != nil {
return false
}
f, err = os.Lstat(realpath)
if err != nil {
return false
}
}
if !f.IsDir() {
return false
}
return true
Expand Down Expand Up @@ -153,7 +167,7 @@ func (c *Cgroups) listPids(h string) []int {
pids := []int{}

for _, s := range subsys {
path := fmt.Sprintf("%s/%s%s", c.FsPath, s, h)
path := filepath.Clean(filepath.Join(c.FsPath, s, h))
err := filepath.Walk(path, func(p string, f os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -195,8 +209,7 @@ func (c *Cgroups) listPids(h string) []int {

// ReadSimple read file and return value as string
func (c *Cgroups) ReadSimple(h string, sname string, stat string) (string, error) {
path := fmt.Sprintf("%s/%s%s/%s", c.FsPath, sname, h, stat)
val, err := ioutil.ReadFile(filepath.Clean(path))
val, err := ioutil.ReadFile(filepath.Clean(filepath.Join(c.FsPath, sname, h, stat)))
if err != nil {
return "", err
}
Expand Down

0 comments on commit f2ad05a

Please sign in to comment.