github.com/criyle/go-sandbox@v0.10.3/pkg/cgroup/v1controller_linux.go (about) 1 package cgroup 2 3 import ( 4 "errors" 5 "path" 6 "strconv" 7 "strings" 8 ) 9 10 // v1controller is the accessor for single cgroup resource with given path 11 type v1controller struct { 12 path string 13 } 14 15 // ErrNotInitialized returned when trying to read from not initialized cgroup 16 var ErrNotInitialized = errors.New("cgroup was not initialized") 17 18 // newV1Controller creates a cgroup accessor with given path (path needs to be created in advance) 19 func newV1Controller(p string) *v1controller { 20 return &v1controller{path: p} 21 } 22 23 // WriteUint writes uint64 into given file 24 func (c *v1controller) WriteUint(filename string, i uint64) error { 25 if c == nil || c.path == "" { 26 return nil 27 } 28 return c.WriteFile(filename, []byte(strconv.FormatUint(i, 10))) 29 } 30 31 // ReadUint read uint64 from given file 32 func (c *v1controller) ReadUint(filename string) (uint64, error) { 33 if c == nil || c.path == "" { 34 return 0, ErrNotInitialized 35 } 36 b, err := c.ReadFile(filename) 37 if err != nil { 38 return 0, err 39 } 40 s, err := strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64) 41 if err != nil { 42 return 0, err 43 } 44 return s, nil 45 } 46 47 // WriteFile writes cgroup file and handles potential EINTR error while writes to 48 // the slow device (cgroup) 49 func (c *v1controller) WriteFile(name string, content []byte) error { 50 if c == nil || c.path == "" { 51 return ErrNotInitialized 52 } 53 p := path.Join(c.path, name) 54 return writeFile(p, content, filePerm) 55 } 56 57 // ReadFile reads cgroup file and handles potential EINTR error while read to 58 // the slow device (cgroup) 59 func (c *v1controller) ReadFile(name string) ([]byte, error) { 60 if c == nil || c.path == "" { 61 return nil, nil 62 } 63 p := path.Join(c.path, name) 64 return readFile(p) 65 } 66 67 func (c *v1controller) AddProc(pids ...int) error { 68 return AddProcesses(path.Join(c.path, cgroupProcs), pids) 69 }