utils.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 of processes 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. // readTasksPids will read all the pids of tasks in a cgroup by the provided path
  128. func readTasksPids(path string, subsystem Name) ([]Task, error) {
  129. f, err := os.Open(filepath.Join(path, cgroupTasks))
  130. if err != nil {
  131. return nil, err
  132. }
  133. defer f.Close()
  134. var (
  135. out []Task
  136. s = bufio.NewScanner(f)
  137. )
  138. for s.Scan() {
  139. if t := s.Text(); t != "" {
  140. pid, err := strconv.Atoi(t)
  141. if err != nil {
  142. return nil, err
  143. }
  144. out = append(out, Task{
  145. Pid: pid,
  146. Subsystem: subsystem,
  147. Path: path,
  148. })
  149. }
  150. }
  151. return out, nil
  152. }
  153. func hugePageSizes() ([]string, error) {
  154. var (
  155. pageSizes []string
  156. sizeList = []string{"B", "kB", "MB", "GB", "TB", "PB"}
  157. )
  158. files, err := ioutil.ReadDir("/sys/kernel/mm/hugepages")
  159. if err != nil {
  160. return nil, err
  161. }
  162. for _, st := range files {
  163. nameArray := strings.Split(st.Name(), "-")
  164. pageSize, err := units.RAMInBytes(nameArray[1])
  165. if err != nil {
  166. return nil, err
  167. }
  168. pageSizes = append(pageSizes, units.CustomSize("%g%s", float64(pageSize), 1024.0, sizeList))
  169. }
  170. return pageSizes, nil
  171. }
  172. func readUint(path string) (uint64, error) {
  173. v, err := ioutil.ReadFile(path)
  174. if err != nil {
  175. return 0, err
  176. }
  177. return parseUint(strings.TrimSpace(string(v)), 10, 64)
  178. }
  179. func parseUint(s string, base, bitSize int) (uint64, error) {
  180. v, err := strconv.ParseUint(s, base, bitSize)
  181. if err != nil {
  182. intValue, intErr := strconv.ParseInt(s, base, bitSize)
  183. // 1. Handle negative values greater than MinInt64 (and)
  184. // 2. Handle negative values lesser than MinInt64
  185. if intErr == nil && intValue < 0 {
  186. return 0, nil
  187. } else if intErr != nil &&
  188. intErr.(*strconv.NumError).Err == strconv.ErrRange &&
  189. intValue < 0 {
  190. return 0, nil
  191. }
  192. return 0, err
  193. }
  194. return v, nil
  195. }
  196. func parseKV(raw string) (string, uint64, error) {
  197. parts := strings.Fields(raw)
  198. switch len(parts) {
  199. case 2:
  200. v, err := parseUint(parts[1], 10, 64)
  201. if err != nil {
  202. return "", 0, err
  203. }
  204. return parts[0], v, nil
  205. default:
  206. return "", 0, ErrInvalidFormat
  207. }
  208. }
  209. func parseCgroupFile(path string) (map[string]string, error) {
  210. f, err := os.Open(path)
  211. if err != nil {
  212. return nil, err
  213. }
  214. defer f.Close()
  215. return parseCgroupFromReader(f)
  216. }
  217. func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
  218. var (
  219. cgroups = make(map[string]string)
  220. s = bufio.NewScanner(r)
  221. )
  222. for s.Scan() {
  223. if err := s.Err(); err != nil {
  224. return nil, err
  225. }
  226. var (
  227. text = s.Text()
  228. parts = strings.SplitN(text, ":", 3)
  229. )
  230. if len(parts) < 3 {
  231. return nil, fmt.Errorf("invalid cgroup entry: %q", text)
  232. }
  233. for _, subs := range strings.Split(parts[1], ",") {
  234. if subs != "" {
  235. cgroups[subs] = parts[2]
  236. }
  237. }
  238. }
  239. return cgroups, nil
  240. }
  241. func getCgroupDestination(subsystem string) (string, error) {
  242. f, err := os.Open("/proc/self/mountinfo")
  243. if err != nil {
  244. return "", err
  245. }
  246. defer f.Close()
  247. s := bufio.NewScanner(f)
  248. for s.Scan() {
  249. if err := s.Err(); err != nil {
  250. return "", err
  251. }
  252. fields := strings.Fields(s.Text())
  253. for _, opt := range strings.Split(fields[len(fields)-1], ",") {
  254. if opt == subsystem {
  255. return fields[3], nil
  256. }
  257. }
  258. }
  259. return "", ErrNoCgroupMountDestination
  260. }
  261. func pathers(subystems []Subsystem) []pather {
  262. var out []pather
  263. for _, s := range subystems {
  264. if p, ok := s.(pather); ok {
  265. out = append(out, p)
  266. }
  267. }
  268. return out
  269. }
  270. func initializeSubsystem(s Subsystem, path Path, resources *specs.LinuxResources) error {
  271. if c, ok := s.(creator); ok {
  272. p, err := path(s.Name())
  273. if err != nil {
  274. return err
  275. }
  276. if err := c.Create(p, resources); err != nil {
  277. return err
  278. }
  279. } else if c, ok := s.(pather); ok {
  280. p, err := path(s.Name())
  281. if err != nil {
  282. return err
  283. }
  284. // do the default create if the group does not have a custom one
  285. if err := os.MkdirAll(c.Path(p), defaultDirPerm); err != nil {
  286. return err
  287. }
  288. }
  289. return nil
  290. }
  291. func cleanPath(path string) string {
  292. if path == "" {
  293. return ""
  294. }
  295. path = filepath.Clean(path)
  296. if !filepath.IsAbs(path) {
  297. path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path))
  298. }
  299. return filepath.Clean(path)
  300. }