| 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroups |
| 25 | |
| 26 | import ( |
| 27 | "bufio" |
| 28 | "io" |
| 29 | "os" |
| 30 | "path/filepath" |
| 31 | "strconv" |
| 32 | ) |
| 33 | |
| 34 | // CGroup represents the data structure for a Linux control group. |
| 35 | type CGroup struct { |
| 36 | path string |
| 37 | } |
| 38 | |
| 39 | // NewCGroup returns a new *CGroup from a given path. |
| 40 | func NewCGroup(path string) *CGroup { |
| 41 | return &CGroup{path: path} |
| 42 | } |
| 43 | |
| 44 | // Path returns the path of the CGroup*. |
| 45 | func (cg *CGroup) Path() string { |
| 46 | return cg.path |
| 47 | } |
| 48 | |
| 49 | // ParamPath returns the path of the given cgroup param under itself. |
| 50 | func (cg *CGroup) ParamPath(param string) string { |
| 51 | return filepath.Join(cg.path, param) |
| 52 | } |
| 53 | |
| 54 | // readFirstLine reads the first line from a cgroup param file. |
| 55 | func (cg *CGroup) readFirstLine(param string) (string, error) { |
| 56 | paramFile, err := os.Open(cg.ParamPath(param)) |
| 57 | if err != nil { |
| 58 | return "", err |
| 59 | } |
| 60 | defer paramFile.Close() |
| 61 | |
| 62 | scanner := bufio.NewScanner(paramFile) |
| 63 | if scanner.Scan() { |
| 64 | return scanner.Text(), nil |
| 65 | } |
| 66 | if err := scanner.Err(); err != nil { |
| 67 | return "", err |
| 68 | } |
| 69 | return "", io.ErrUnexpectedEOF |
| 70 | } |
| 71 | |
| 72 | // readInt parses the first line from a cgroup param file as int. |
| 73 | func (cg *CGroup) readInt(param string) (int, error) { |
| 74 | text, err := cg.readFirstLine(param) |
| 75 | if err != nil { |
| 76 | return 0, err |
| 77 | } |
| 78 | return strconv.Atoi(text) |
| 79 | } |