utils.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. "sync"
  24. "time"
  25. units "github.com/docker/go-units"
  26. specs "github.com/opencontainers/runtime-spec/specs-go"
  27. "golang.org/x/sys/unix"
  28. )
  29. var (
  30. isUserNS = runningInUserNS()
  31. checkMode sync.Once
  32. cgMode CGMode
  33. )
  34. const unifiedMountpoint = "/sys/fs/cgroup"
  35. // CGMode is the cgroups mode of the host system
  36. type CGMode int
  37. const (
  38. // Unavailable cgroup mountpoint
  39. Unavailable CGMode = iota
  40. // Legacy cgroups v1
  41. Legacy
  42. // Hybrid with cgroups v1 and v2 controllers mounted
  43. Hybrid
  44. // Unified with only cgroups v2 mounted
  45. Unified
  46. )
  47. // Mode returns the cgroups mode running on the host
  48. func Mode() CGMode {
  49. checkMode.Do(func() {
  50. var st unix.Statfs_t
  51. if err := unix.Statfs(unifiedMountpoint, &st); err != nil {
  52. cgMode = Unavailable
  53. return
  54. }
  55. switch st.Type {
  56. case unix.CGROUP2_SUPER_MAGIC:
  57. cgMode = Unified
  58. default:
  59. cgMode = Legacy
  60. if err := unix.Statfs(filepath.Join(unifiedMountpoint, "unified"), &st); err != nil {
  61. return
  62. }
  63. if st.Type == unix.CGROUP2_SUPER_MAGIC {
  64. cgMode = Hybrid
  65. }
  66. }
  67. })
  68. return cgMode
  69. }
  70. // runningInUserNS detects whether we are currently running in a user namespace.
  71. // Copied from github.com/lxc/lxd/shared/util.go
  72. func runningInUserNS() bool {
  73. file, err := os.Open("/proc/self/uid_map")
  74. if err != nil {
  75. // This kernel-provided file only exists if user namespaces are supported
  76. return false
  77. }
  78. defer file.Close()
  79. buf := bufio.NewReader(file)
  80. l, _, err := buf.ReadLine()
  81. if err != nil {
  82. return false
  83. }
  84. line := string(l)
  85. var a, b, c int64
  86. fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
  87. /*
  88. * We assume we are in the initial user namespace if we have a full
  89. * range - 4294967295 uids starting at uid 0.
  90. */
  91. if a == 0 && b == 0 && c == 4294967295 {
  92. return false
  93. }
  94. return true
  95. }
  96. // defaults returns all known groups
  97. func defaults(root string) ([]Subsystem, error) {
  98. h, err := NewHugetlb(root)
  99. if err != nil && !os.IsNotExist(err) {
  100. return nil, err
  101. }
  102. s := []Subsystem{
  103. NewNamed(root, "systemd"),
  104. NewFreezer(root),
  105. NewPids(root),
  106. NewNetCls(root),
  107. NewNetPrio(root),
  108. NewPerfEvent(root),
  109. NewCputset(root),
  110. NewCpu(root),
  111. NewCpuacct(root),
  112. NewMemory(root),
  113. NewBlkio(root),
  114. NewRdma(root),
  115. }
  116. // only add the devices cgroup if we are not in a user namespace
  117. // because modifications are not allowed
  118. if !isUserNS {
  119. s = append(s, NewDevices(root))
  120. }
  121. // add the hugetlb cgroup if error wasn't due to missing hugetlb
  122. // cgroup support on the host
  123. if err == nil {
  124. s = append(s, h)
  125. }
  126. return s, nil
  127. }
  128. // remove will remove a cgroup path handling EAGAIN and EBUSY errors and
  129. // retrying the remove after a exp timeout
  130. func remove(path string) error {
  131. delay := 10 * time.Millisecond
  132. for i := 0; i < 5; i++ {
  133. if i != 0 {
  134. time.Sleep(delay)
  135. delay *= 2
  136. }
  137. if err := os.RemoveAll(path); err == nil {
  138. return nil
  139. }
  140. }
  141. return fmt.Errorf("cgroups: unable to remove path %q", path)
  142. }
  143. // readPids will read all the pids of processes in a cgroup by the provided path
  144. func readPids(path string, subsystem Name) ([]Process, error) {
  145. f, err := os.Open(filepath.Join(path, cgroupProcs))
  146. if err != nil {
  147. return nil, err
  148. }
  149. defer f.Close()
  150. var (
  151. out []Process
  152. s = bufio.NewScanner(f)
  153. )
  154. for s.Scan() {
  155. if t := s.Text(); t != "" {
  156. pid, err := strconv.Atoi(t)
  157. if err != nil {
  158. return nil, err
  159. }
  160. out = append(out, Process{
  161. Pid: pid,
  162. Subsystem: subsystem,
  163. Path: path,
  164. })
  165. }
  166. }
  167. return out, nil
  168. }
  169. // readTasksPids will read all the pids of tasks in a cgroup by the provided path
  170. func readTasksPids(path string, subsystem Name) ([]Task, error) {
  171. f, err := os.Open(filepath.Join(path, cgroupTasks))
  172. if err != nil {
  173. return nil, err
  174. }
  175. defer f.Close()
  176. var (
  177. out []Task
  178. s = bufio.NewScanner(f)
  179. )
  180. for s.Scan() {
  181. if t := s.Text(); t != "" {
  182. pid, err := strconv.Atoi(t)
  183. if err != nil {
  184. return nil, err
  185. }
  186. out = append(out, Task{
  187. Pid: pid,
  188. Subsystem: subsystem,
  189. Path: path,
  190. })
  191. }
  192. }
  193. return out, nil
  194. }
  195. func hugePageSizes() ([]string, error) {
  196. var (
  197. pageSizes []string
  198. sizeList = []string{"B", "KB", "MB", "GB", "TB", "PB"}
  199. )
  200. files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
  201. if err != nil {
  202. return nil, err
  203. }
  204. for _, st := range files {
  205. nameArray := strings.Split(st.Name(), "-")
  206. pageSize, err := units.RAMInBytes(nameArray[1])
  207. if err != nil {
  208. return nil, err
  209. }
  210. pageSizes = append(pageSizes, units.CustomSize("%g%s", float64(pageSize), 1024.0, sizeList))
  211. }
  212. return pageSizes, nil
  213. }
  214. func readUint(path string) (uint64, error) {
  215. v, err := ioutil.ReadFile(path)
  216. if err != nil {
  217. return 0, err
  218. }
  219. return parseUint(strings.TrimSpace(string(v)), 10, 64)
  220. }
  221. func parseUint(s string, base, bitSize int) (uint64, error) {
  222. v, err := strconv.ParseUint(s, base, bitSize)
  223. if err != nil {
  224. intValue, intErr := strconv.ParseInt(s, base, bitSize)
  225. // 1. Handle negative values greater than MinInt64 (and)
  226. // 2. Handle negative values lesser than MinInt64
  227. if intErr == nil && intValue < 0 {
  228. return 0, nil
  229. } else if intErr != nil &&
  230. intErr.(*strconv.NumError).Err == strconv.ErrRange &&
  231. intValue < 0 {
  232. return 0, nil
  233. }
  234. return 0, err
  235. }
  236. return v, nil
  237. }
  238. func parseKV(raw string) (string, uint64, error) {
  239. parts := strings.Fields(raw)
  240. switch len(parts) {
  241. case 2:
  242. v, err := parseUint(parts[1], 10, 64)
  243. if err != nil {
  244. return "", 0, err
  245. }
  246. return parts[0], v, nil
  247. default:
  248. return "", 0, ErrInvalidFormat
  249. }
  250. }
  251. func parseCgroupFile(path string) (map[string]string, error) {
  252. f, err := os.Open(path)
  253. if err != nil {
  254. return nil, err
  255. }
  256. defer f.Close()
  257. return parseCgroupFromReader(f)
  258. }
  259. func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
  260. var (
  261. cgroups = make(map[string]string)
  262. s = bufio.NewScanner(r)
  263. )
  264. for s.Scan() {
  265. if err := s.Err(); err != nil {
  266. return nil, err
  267. }
  268. var (
  269. text = s.Text()
  270. parts = strings.SplitN(text, ":", 3)
  271. )
  272. if len(parts) < 3 {
  273. return nil, fmt.Errorf("invalid cgroup entry: %q", text)
  274. }
  275. for _, subs := range strings.Split(parts[1], ",") {
  276. if subs != "" {
  277. cgroups[subs] = parts[2]
  278. }
  279. }
  280. }
  281. return cgroups, nil
  282. }
  283. func getCgroupDestination(subsystem string) (string, error) {
  284. f, err := os.Open("/proc/self/mountinfo")
  285. if err != nil {
  286. return "", err
  287. }
  288. defer f.Close()
  289. s := bufio.NewScanner(f)
  290. for s.Scan() {
  291. if err := s.Err(); err != nil {
  292. return "", err
  293. }
  294. fields := strings.Fields(s.Text())
  295. for _, opt := range strings.Split(fields[len(fields)-1], ",") {
  296. if opt == subsystem {
  297. return fields[3], nil
  298. }
  299. }
  300. }
  301. return "", ErrNoCgroupMountDestination
  302. }
  303. func pathers(subystems []Subsystem) []pather {
  304. var out []pather
  305. for _, s := range subystems {
  306. if p, ok := s.(pather); ok {
  307. out = append(out, p)
  308. }
  309. }
  310. return out
  311. }
  312. func initializeSubsystem(s Subsystem, path Path, resources *specs.LinuxResources) error {
  313. if c, ok := s.(creator); ok {
  314. p, err := path(s.Name())
  315. if err != nil {
  316. return err
  317. }
  318. if err := c.Create(p, resources); err != nil {
  319. return err
  320. }
  321. } else if c, ok := s.(pather); ok {
  322. p, err := path(s.Name())
  323. if err != nil {
  324. return err
  325. }
  326. // do the default create if the group does not have a custom one
  327. if err := os.MkdirAll(c.Path(p), defaultDirPerm); err != nil {
  328. return err
  329. }
  330. }
  331. return nil
  332. }
  333. func cleanPath(path string) string {
  334. if path == "" {
  335. return ""
  336. }
  337. path = filepath.Clean(path)
  338. if !filepath.IsAbs(path) {
  339. path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path))
  340. }
  341. return filepath.Clean(path)
  342. }