From 4e65e0e90a588489be02b3c70981f95eb12e0413 Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Fri, 20 Mar 2015 15:29:14 -0400 Subject: [PATCH] Throw an error if cgroup tries to set cpu-shares more/less than the maximum/minimum permissible value. Signed-off-by: Shishir Mahajan --- cgroups/fs/apply_raw.go | 28 ++++++++++++++++++++++++++++ cgroups/systemd/apply_systemd.go | 24 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 0a2d76bcd4e..5bfa7985e8e 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -1,8 +1,11 @@ package fs import ( + "fmt" + "io" "io/ioutil" "os" + "path" "path/filepath" "strconv" "sync" @@ -75,10 +78,13 @@ type data struct { } func (m *Manager) Apply(pid int) error { + if m.Cgroups == nil { return nil } + var c = m.Cgroups + d, err := getCgroupData(m.Cgroups, pid) if err != nil { return err @@ -108,6 +114,28 @@ func (m *Manager) Apply(pid int) error { } m.Paths = paths + var cpuShares int64 + + fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares")) + if err != nil { + return err + } + + _, err = fmt.Fscanf(fd, "%d", &cpuShares) + if err != nil && err != io.EOF { + return err + } + + fd.Close() + + if c.CpuShares != 0 { + if c.CpuShares > cpuShares { + return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares) + } else if c.CpuShares < cpuShares { + return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares) + } + } + return nil } diff --git a/cgroups/systemd/apply_systemd.go b/cgroups/systemd/apply_systemd.go index 85ee5db06d3..10a32d432fe 100644 --- a/cgroups/systemd/apply_systemd.go +++ b/cgroups/systemd/apply_systemd.go @@ -5,8 +5,10 @@ package systemd import ( "bytes" "fmt" + "io" "io/ioutil" "os" + "path" "path/filepath" "strconv" "strings" @@ -241,6 +243,28 @@ func (m *Manager) Apply(pid int) error { m.Paths = paths + var cpuShares int64 + + fd, err := os.Open(path.Join(m.Paths["cpu"], "cpu.shares")) + if err != nil { + return err + } + + _, err = fmt.Fscanf(fd, "%d", &cpuShares) + if err != nil && err != io.EOF { + return err + } + + fd.Close() + + if c.CpuShares != 0 { + if c.CpuShares > cpuShares { + return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares) + } else if c.CpuShares < cpuShares { + return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares) + } + } + return nil }