v1.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 cgroup1
  14. import (
  15. "bufio"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. )
  21. // Default returns all the groups in the default cgroups mountpoint in a single hierarchy
  22. func Default() ([]Subsystem, error) {
  23. root, err := v1MountPoint()
  24. if err != nil {
  25. return nil, err
  26. }
  27. subsystems, err := defaults(root)
  28. if err != nil {
  29. return nil, err
  30. }
  31. var enabled []Subsystem
  32. for _, s := range pathers(subsystems) {
  33. // check and remove the default groups that do not exist
  34. if _, err := os.Lstat(s.Path("/")); err == nil {
  35. enabled = append(enabled, s)
  36. }
  37. }
  38. return enabled, nil
  39. }
  40. // v1MountPoint returns the mount point where the cgroup
  41. // mountpoints are mounted in a single hierarchy
  42. func v1MountPoint() (string, error) {
  43. f, err := os.Open("/proc/self/mountinfo")
  44. if err != nil {
  45. return "", err
  46. }
  47. defer f.Close()
  48. scanner := bufio.NewScanner(f)
  49. for scanner.Scan() {
  50. var (
  51. text = scanner.Text()
  52. fields = strings.Split(text, " ")
  53. numFields = len(fields)
  54. )
  55. if numFields < 10 {
  56. return "", fmt.Errorf("mountinfo: bad entry %q", text)
  57. }
  58. if fields[numFields-3] == "cgroup" {
  59. return filepath.Dir(fields[4]), nil
  60. }
  61. }
  62. if err := scanner.Err(); err != nil {
  63. return "", err
  64. }
  65. return "", ErrMountPointNotExist
  66. }