Throw an error if cgroup tries to set cpu-shares more/less than the maximum/minimum permissible value.

Signed-off-by: Shishir Mahajan <shishir.mahajan@redhat.com>
This commit is contained in:
Shishir Mahajan 2015-03-20 15:29:14 -04:00
parent c851275416
commit 4e65e0e90a
2 changed files with 52 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}