Skip to content

Commit

Permalink
Throw an error if cgroup tries to set cpu-shares more/less than the m…
Browse files Browse the repository at this point in the history
…aximum/minimum permissible value.

Signed-off-by: Shishir Mahajan <shishir.mahajan@redhat.com>
  • Loading branch information
Shishir Mahajan committed Apr 1, 2015
1 parent c851275 commit 4e65e0e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cgroups/fs/apply_raw.go
@@ -1,8 +1,11 @@
package fs

import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"sync"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 24 additions & 0 deletions cgroups/systemd/apply_systemd.go
Expand Up @@ -5,8 +5,10 @@ package systemd
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 4e65e0e

Please sign in to comment.