From 3cdf12b0419149e5b687c851b45dffd611b6e9e5 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 20 Aug 2014 10:32:01 -0700 Subject: [PATCH] Change not found error to be typed with cgroup subsystem Signed-off-by: Michael Crosby --- cgroups/cgroups.go | 29 ++++++++++++++++++++++++----- cgroups/fs/apply_raw.go | 4 ++-- cgroups/fs/blkio.go | 3 ++- cgroups/fs/cpuacct.go | 3 ++- cgroups/fs/freezer.go | 2 +- cgroups/fs/perf_event.go | 2 +- cgroups/systemd/apply_systemd.go | 4 ++-- cgroups/utils.go | 9 +++++++-- 8 files changed, 41 insertions(+), 15 deletions(-) diff --git a/cgroups/cgroups.go b/cgroups/cgroups.go index 59845486..567e9a6c 100644 --- a/cgroups/cgroups.go +++ b/cgroups/cgroups.go @@ -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 ( @@ -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 diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 64ad8c84..63f7441f 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -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 } @@ -172,7 +172,7 @@ func (raw *data) path(subsystem string) (string, error) { if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - return "", cgroups.ErrNotFound + return "", cgroups.NewNotFoundError(subsystem) } return "", err diff --git a/cgroups/fs/blkio.go b/cgroups/fs/blkio.go index 38b87577..f784d011 100644 --- a/cgroups/fs/blkio.go +++ b/cgroups/fs/blkio.go @@ -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 } diff --git a/cgroups/fs/cpuacct.go b/cgroups/fs/cpuacct.go index 7761d4c2..853ab6bf 100644 --- a/cgroups/fs/cpuacct.go +++ b/cgroups/fs/cpuacct.go @@ -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 } diff --git a/cgroups/fs/freezer.go b/cgroups/fs/freezer.go index db2a41ca..c6b677fa 100644 --- a/cgroups/fs/freezer.go +++ b/cgroups/fs/freezer.go @@ -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 } } diff --git a/cgroups/fs/perf_event.go b/cgroups/fs/perf_event.go index 5f45678f..813274d8 100644 --- a/cgroups/fs/perf_event.go +++ b/cgroups/fs/perf_event.go @@ -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 diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index 252fb60e..7af4818e 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -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 } @@ -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 } diff --git a/cgroups/utils.go b/cgroups/utils.go index 6688ff71..5516c5a2 100644 --- a/cgroups/utils.go +++ b/cgroups/utils.go @@ -29,7 +29,8 @@ func FindCgroupMountpoint(subsystem string) (string, error) { } } } - return "", ErrNotFound + + return "", NewNotFoundError(subsystem) } type Mount struct { @@ -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 {