Skip to content

Commit

Permalink
Merge pull request #2411 from kolyshkin/v1-specific
Browse files Browse the repository at this point in the history
libct/cgroups/utils: fix/separate cgroupv1 code
  • Loading branch information
Mrunal Patel authored Jun 17, 2020
2 parents 82d2fa4 + 8c5a19f commit 12a7c8f
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 318 deletions.
24 changes: 0 additions & 24 deletions libcontainer/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package cgroups

import (
"fmt"

"github.com/opencontainers/runc/libcontainer/configs"
)

Expand Down Expand Up @@ -51,25 +49,3 @@ type Manager interface {
// Whether the cgroup path exists or not
Exists() bool
}

type NotFoundError struct {
Subsystem string
}

func (e *NotFoundError) Error() string {
return fmt.Sprintf("mountpoint for %s not found", e.Subsystem)
}

func NewNotFoundError(sub string) error {
return &NotFoundError{
Subsystem: sub,
}
}

func IsNotFound(err error) bool {
if err == nil {
return false
}
_, ok := err.(*NotFoundError)
return ok
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package fs

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"sync"

"github.com/opencontainers/runc/libcontainer/cgroups"
Expand Down Expand Up @@ -88,10 +90,43 @@ func getCgroupRoot() (string, error) {
return cgroupRoot, nil
}

root, err := cgroups.FindCgroupMountpointDir()
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return "", err
}
defer f.Close()

var root string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
fields := strings.Split(text, " ")
// Safe as mountinfo encodes mountpoints with spaces as \040.
index := strings.Index(text, " - ")
postSeparatorFields := strings.Fields(text[index+3:])
numPostFields := len(postSeparatorFields)

// This is an error as we can't detect if the mount is for "cgroup"
if numPostFields == 0 {
return "", fmt.Errorf("mountinfo: found no fields post '-' in %q", text)
}

if postSeparatorFields[0] == "cgroup" {
// Check that the mount is properly formatted.
if numPostFields < 3 {
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
}

root = filepath.Dir(fields[4])
break
}
}
if err := scanner.Err(); err != nil {
return "", err
}
if root == "" {
return "", errors.New("no cgroup mount found in mountinfo")
}

if _, err := os.Stat(root); err != nil {
return "", err
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 12a7c8f

Please sign in to comment.