Skip to content

Commit

Permalink
Merge pull request docker-archive#161 from crosbymichael/return-notfo…
Browse files Browse the repository at this point in the history
…und-err

Return NotFound error for cgroups abs paths
  • Loading branch information
Michael Crosby committed Aug 20, 2014
2 parents edfe81a + a117d3a commit db65c35
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 34 deletions.
29 changes: 24 additions & 5 deletions cgroups/cgroups.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package cgroups

import (
"errors"
"fmt"

"github.com/docker/libcontainer/devices"
)

var (
ErrNotFound = errors.New("mountpoint not found")
)

type FreezerState string

const (
Expand All @@ -18,6 +14,29 @@ const (
Thawed FreezerState = "THAWED"
)

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
}

type Cgroup struct {
Name string `json:"name,omitempty"`
Parent string `json:"parent,omitempty"` // name of parent cgroup or slice
Expand Down
24 changes: 22 additions & 2 deletions cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GetStats(c *cgroups.Cgroup) (*cgroups.Stats, error) {
path, err := d.path(sysname)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if err == cgroups.ErrNotFound {
if cgroups.IsNotFound(err) {
continue
}

Expand Down Expand Up @@ -155,25 +155,45 @@ func (raw *data) parent(subsystem string) (string, error) {

func (raw *data) Paths() (map[string]string, error) {
paths := make(map[string]string)

for sysname := range subsystems {
path, err := raw.path(sysname)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if cgroups.IsNotFound(err) {
continue
}

return nil, err
}

paths[sysname] = path
}

return paths, nil
}

func (raw *data) path(subsystem string) (string, error) {
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
if filepath.IsAbs(raw.cgroup) {
return filepath.Join(raw.root, subsystem, raw.cgroup), nil
path := filepath.Join(raw.root, subsystem, raw.cgroup)

if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return "", cgroups.NewNotFoundError(subsystem)
}

return "", err
}

return path, nil
}

parent, err := raw.parent(subsystem)
if err != nil {
return "", err
}

return filepath.Join(parent, raw.cgroup), nil
}

Expand Down
3 changes: 2 additions & 1 deletion cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type BlkioGroup struct {

func (s *BlkioGroup) Set(d *data) error {
// we just want to join this group even though we don't set anything
if _, err := d.join("blkio"); err != nil && err != cgroups.ErrNotFound {
if _, err := d.join("blkio"); err != nil && !cgroups.IsNotFound(err) {
return err
}

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type CpuacctGroup struct {

func (s *CpuacctGroup) Set(d *data) error {
// we just want to join this group even though we don't set anything
if _, err := d.join("cpuacct"); err != nil && err != cgroups.ErrNotFound {
if _, err := d.join("cpuacct"); err != nil && !cgroups.IsNotFound(err) {
return err
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cgroups/fs/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *FreezerGroup) Set(d *data) error {
time.Sleep(1 * time.Millisecond)
}
default:
if _, err := d.join("freezer"); err != nil && err != cgroups.ErrNotFound {
if _, err := d.join("freezer"); err != nil && !cgroups.IsNotFound(err) {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/fs/perf_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type PerfEventGroup struct {

func (s *PerfEventGroup) Set(d *data) error {
// we just want to join this group even though we don't set anything
if _, err := d.join("perf_event"); err != nil && err != cgroups.ErrNotFound {
if _, err := d.join("perf_event"); err != nil && !cgroups.IsNotFound(err) {
return err
}
return nil
Expand Down
19 changes: 0 additions & 19 deletions cgroups/fs/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"
"path/filepath"
"testing"

"github.com/docker/libcontainer/cgroups"
)

const (
Expand Down Expand Up @@ -68,20 +66,3 @@ func TestGetCgroupParamsInt(t *testing.T) {
t.Fatal("Expecting error, got none")
}
}

func TestAbsolutePathHandling(t *testing.T) {
testCgroup := cgroups.Cgroup{
Name: "bar",
Parent: "/foo",
}
cgroupData := data{
root: "/sys/fs/cgroup",
cgroup: "/foo/bar",
c: &testCgroup,
pid: 1,
}
expectedPath := filepath.Join(cgroupData.root, "cpu", testCgroup.Parent, testCgroup.Name)
if path, err := cgroupData.path("cpu"); path != expectedPath || err != nil {
t.Fatalf("expected path %s but got %s %s", expectedPath, path, err)
}
}
4 changes: 2 additions & 2 deletions cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (c *systemdCgroup) Paths() (map[string]string, error) {
subsystemPath, err := getSubsystemPath(c.cgroup, sysname)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if err == cgroups.ErrNotFound {
if cgroups.IsNotFound(err) {
continue
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func GetStats(c *cgroups.Cgroup) (*cgroups.Stats, error) {
subsystemPath, err := getSubsystemPath(c, sysname)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if err == cgroups.ErrNotFound {
if cgroups.IsNotFound(err) {
continue
}

Expand Down
9 changes: 7 additions & 2 deletions cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func FindCgroupMountpoint(subsystem string) (string, error) {
}
}
}
return "", ErrNotFound

return "", NewNotFoundError(subsystem)
}

type Mount struct {
Expand Down Expand Up @@ -153,19 +154,23 @@ func ReadProcsFile(dir string) ([]int, error) {

func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
s := bufio.NewScanner(r)

for s.Scan() {
if err := s.Err(); err != nil {
return "", err
}

text := s.Text()
parts := strings.Split(text, ":")

for _, subs := range strings.Split(parts[1], ",") {
if subs == subsystem {
return parts[2], nil
}
}
}
return "", ErrNotFound

return "", NewNotFoundError(subsystem)
}

func pathExists(path string) bool {
Expand Down

0 comments on commit db65c35

Please sign in to comment.