defaultpath.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 fs2
  14. import (
  15. "bufio"
  16. "io"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "github.com/opencontainers/runc/libcontainer/configs"
  21. libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
  22. "github.com/pkg/errors"
  23. )
  24. const UnifiedMountpoint = "/sys/fs/cgroup"
  25. func defaultDirPath(c *configs.Cgroup) (string, error) {
  26. if (c.Name != "" || c.Parent != "") && c.Path != "" {
  27. return "", errors.Errorf("cgroup: either Path or Name and Parent should be used, got %+v", c)
  28. }
  29. if len(c.Paths) != 0 {
  30. // never set by specconv
  31. return "", errors.Errorf("cgroup: Paths is unsupported, use Path, got %+v", c)
  32. }
  33. // XXX: Do not remove this code. Path safety is important! -- cyphar
  34. cgPath := libcontainerUtils.CleanPath(c.Path)
  35. cgParent := libcontainerUtils.CleanPath(c.Parent)
  36. cgName := libcontainerUtils.CleanPath(c.Name)
  37. ownCgroup, err := parseCgroupFile("/proc/self/cgroup")
  38. if err != nil {
  39. return "", err
  40. }
  41. return _defaultDirPath(UnifiedMountpoint, cgPath, cgParent, cgName, ownCgroup)
  42. }
  43. func _defaultDirPath(root, cgPath, cgParent, cgName, ownCgroup string) (string, error) {
  44. if (cgName != "" || cgParent != "") && cgPath != "" {
  45. return "", errors.New("cgroup: either Path or Name and Parent should be used")
  46. }
  47. innerPath := cgPath
  48. if innerPath == "" {
  49. innerPath = filepath.Join(cgParent, cgName)
  50. }
  51. if filepath.IsAbs(innerPath) {
  52. return filepath.Join(root, innerPath), nil
  53. }
  54. return filepath.Join(root, ownCgroup, innerPath), nil
  55. }
  56. // parseCgroupFile parses /proc/PID/cgroup file and return string
  57. func parseCgroupFile(path string) (string, error) {
  58. f, err := os.Open(path)
  59. if err != nil {
  60. return "", err
  61. }
  62. defer f.Close()
  63. return parseCgroupFromReader(f)
  64. }
  65. func parseCgroupFromReader(r io.Reader) (string, error) {
  66. var (
  67. s = bufio.NewScanner(r)
  68. )
  69. for s.Scan() {
  70. if err := s.Err(); err != nil {
  71. return "", err
  72. }
  73. var (
  74. text = s.Text()
  75. parts = strings.SplitN(text, ":", 3)
  76. )
  77. if len(parts) < 3 {
  78. return "", errors.Errorf("invalid cgroup entry: %q", text)
  79. }
  80. // text is like "0::/user.slice/user-1001.slice/session-1.scope"
  81. if parts[0] == "0" && parts[1] == "" {
  82. return parts[2], nil
  83. }
  84. }
  85. return "", errors.New("cgroup path not found")
  86. }