utils.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroups
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "time"
  24. units "github.com/docker/go-units"
  25. specs "github.com/opencontainers/runtime-spec/specs-go"
  26. )
  27. var isUserNS = runningInUserNS()
  28. // runningInUserNS detects whether we are currently running in a user namespace.
  29. // Copied from github.com/lxc/lxd/shared/util.go
  30. func runningInUserNS() bool {
  31. file, err := os.Open("/proc/self/uid_map")
  32. if err != nil {
  33. // This kernel-provided file only exists if user namespaces are supported
  34. return false
  35. }
  36. defer file.Close()
  37. buf := bufio.NewReader(file)
  38. l, _, err := buf.ReadLine()
  39. if err != nil {
  40. return false
  41. }
  42. line := string(l)
  43. var a, b, c int64
  44. fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
  45. /*
  46. * We assume we are in the initial user namespace if we have a full
  47. * range - 4294967295 uids starting at uid 0.
  48. */
  49. if a == 0 && b == 0 && c == 4294967295 {
  50. return false
  51. }
  52. return true
  53. }
  54. // defaults returns all known groups
  55. func defaults(root string) ([]Subsystem, error) {
  56. h, err := NewHugetlb(root)
  57. if err != nil && !os.IsNotExist(err) {
  58. return nil, err
  59. }
  60. s := []Subsystem{
  61. NewNamed(root, "systemd"),
  62. NewFreezer(root),
  63. NewPids(root),
  64. NewNetCls(root),
  65. NewNetPrio(root),
  66. NewPerfEvent(root),
  67. NewCputset(root),
  68. NewCpu(root),
  69. NewCpuacct(root),
  70. NewMemory(root),
  71. NewBlkio(root),
  72. NewRdma(root),
  73. }
  74. // only add the devices cgroup if we are not in a user namespace
  75. // because modifications are not allowed
  76. if !isUserNS {
  77. s = append(s, NewDevices(root))
  78. }
  79. // add the hugetlb cgroup if error wasn't due to missing hugetlb
  80. // cgroup support on the host
  81. if err == nil {
  82. s = append(s, h)
  83. }
  84. return s, nil
  85. }
  86. // remove will remove a cgroup path handling EAGAIN and EBUSY errors and
  87. // retrying the remove after a exp timeout
  88. func remove(path string) error {
  89. delay := 10 * time.Millisecond
  90. for i := 0; i < 5; i++ {
  91. if i != 0 {
  92. time.Sleep(delay)
  93. delay *= 2
  94. }
  95. if err := os.RemoveAll(path); err == nil {
  96. return nil
  97. }
  98. }
  99. return fmt.Errorf("cgroups: unable to remove path %q", path)
  100. }
  101. // readPids will read all the pids in a cgroup by the provided path
  102. func readPids(path string, subsystem Name) ([]Process, error) {
  103. f, err := os.Open(filepath.Join(path, cgroupProcs))
  104. if err != nil {
  105. return nil, err
  106. }
  107. defer f.Close()
  108. var (
  109. out []Process
  110. s = bufio.NewScanner(f)
  111. )
  112. for s.Scan() {
  113. if t := s.Text(); t != "" {
  114. pid, err := strconv.Atoi(t)
  115. if err != nil {
  116. return nil, err
  117. }
  118. out = append(out, Process{
  119. Pid: pid,
  120. Subsystem: subsystem,
  121. Path: path,
  122. })
  123. }
  124. }
  125. return out, nil
  126. }
  127. func hugePageSizes() ([]string, error) {
  128. var (
  129. pageSizes []string
  130. sizeList = []string{"B", "kB", "MB", "GB", "TB", "PB"}
  131. )
  132. files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
  133. if err != nil {
  134. return nil, err
  135. }
  136. for _, st := range files {
  137. nameArray := strings.Split(st.Name(), "-")
  138. pageSize, err := units.RAMInBytes(nameArray[1])
  139. if err != nil {
  140. return nil, err
  141. }
  142. pageSizes = append(pageSizes, units.CustomSize("%g%s", float64(pageSize), 1024.0, sizeList))
  143. }
  144. return pageSizes, nil
  145. }
  146. func readUint(path string) (uint64, error) {
  147. v, err := ioutil.ReadFile(path)
  148. if err != nil {
  149. return 0, err
  150. }
  151. return parseUint(strings.TrimSpace(string(v)), 10, 64)
  152. }
  153. func parseUint(s string, base, bitSize int) (uint64, error) {
  154. v, err := strconv.ParseUint(s, base, bitSize)
  155. if err != nil {
  156. intValue, intErr := strconv.ParseInt(s, base, bitSize)
  157. // 1. Handle negative values greater than MinInt64 (and)
  158. // 2. Handle negative values lesser than MinInt64
  159. if intErr == nil && intValue < 0 {
  160. return 0, nil
  161. } else if intErr != nil &&
  162. intErr.(*strconv.NumError).Err == strconv.ErrRange &&
  163. intValue < 0 {
  164. return 0, nil
  165. }
  166. return 0, err
  167. }
  168. return v, nil
  169. }
  170. func parseKV(raw string) (string, uint64, error) {
  171. parts := strings.Fields(raw)
  172. switch len(parts) {
  173. case 2:
  174. v, err := parseUint(parts[1], 10, 64)
  175. if err != nil {
  176. return "", 0, err
  177. }
  178. return parts[0], v, nil
  179. default:
  180. return "", 0, ErrInvalidFormat
  181. }
  182. }
  183. func parseCgroupFile(path string) (map[string]string, error) {
  184. f, err := os.Open(path)
  185. if err != nil {
  186. return nil, err
  187. }
  188. defer f.Close()
  189. return parseCgroupFromReader(f)
  190. }
  191. func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
  192. var (
  193. cgroups = make(map[string]string)
  194. s = bufio.NewScanner(r)
  195. )
  196. for s.Scan() {
  197. if err := s.Err(); err != nil {
  198. return nil, err
  199. }
  200. var (
  201. text = s.Text()
  202. parts = strings.SplitN(text, ":", 3)
  203. )
  204. if len(parts) < 3 {
  205. return nil, fmt.Errorf("invalid cgroup entry: %q", text)
  206. }
  207. for _, subs := range strings.Split(parts[1], ",") {
  208. if subs != "" {
  209. cgroups[subs] = parts[2]
  210. }
  211. }
  212. }
  213. return cgroups, nil
  214. }
  215. func getCgroupDestination(subsystem string) (string, error) {
  216. f, err := os.Open("/proc/self/mountinfo")
  217. if err != nil {
  218. return "", err
  219. }
  220. defer f.Close()
  221. s := bufio.NewScanner(f)
  222. for s.Scan() {
  223. if err := s.Err(); err != nil {
  224. return "", err
  225. }
  226. fields := strings.Fields(s.Text())
  227. for _, opt := range strings.Split(fields[len(fields)-1], ",") {
  228. if opt == subsystem {
  229. return fields[3], nil
  230. }
  231. }
  232. }
  233. return "", ErrNoCgroupMountDestination
  234. }
  235. func pathers(subystems []Subsystem) []pather {
  236. var out []pather
  237. for _, s := range subystems {
  238. if p, ok := s.(pather); ok {
  239. out = append(out, p)
  240. }
  241. }
  242. return out
  243. }
  244. func initializeSubsystem(s Subsystem, path Path, resources *specs.LinuxResources) error {
  245. if c, ok := s.(creator); ok {
  246. p, err := path(s.Name())
  247. if err != nil {
  248. return err
  249. }
  250. if err := c.Create(p, resources); err != nil {
  251. return err
  252. }
  253. } else if c, ok := s.(pather); ok {
  254. p, err := path(s.Name())
  255. if err != nil {
  256. return err
  257. }
  258. // do the default create if the group does not have a custom one
  259. if err := os.MkdirAll(c.Path(p), defaultDirPerm); err != nil {
  260. return err
  261. }
  262. }
  263. return nil
  264. }
  265. func cleanPath(path string) string {
  266. if path == "" {
  267. return ""
  268. }
  269. path = filepath.Clean(path)
  270. if !filepath.IsAbs(path) {
  271. path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path))
  272. }
  273. return filepath.Clean(path)
  274. }