paths.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "errors"
  16. "fmt"
  17. "path/filepath"
  18. )
  19. type Path func(subsystem Name) (string, error)
  20. func RootPath(subsystem Name) (string, error) {
  21. return "/", nil
  22. }
  23. // StaticPath returns a static path to use for all cgroups
  24. func StaticPath(path string) Path {
  25. return func(_ Name) (string, error) {
  26. return path, nil
  27. }
  28. }
  29. // NestedPath will nest the cgroups based on the calling processes cgroup
  30. // placing its child processes inside its own path
  31. func NestedPath(suffix string) Path {
  32. paths, err := ParseCgroupFile("/proc/self/cgroup")
  33. if err != nil {
  34. return errorPath(err)
  35. }
  36. return existingPath(paths, suffix)
  37. }
  38. // PidPath will return the correct cgroup paths for an existing process running inside a cgroup
  39. // This is commonly used for the Load function to restore an existing container
  40. func PidPath(pid int) Path {
  41. p := fmt.Sprintf("/proc/%d/cgroup", pid)
  42. paths, err := ParseCgroupFile(p)
  43. if err != nil {
  44. return errorPath(fmt.Errorf("parse cgroup file %s: %w", p, err))
  45. }
  46. return existingPath(paths, "")
  47. }
  48. // ErrControllerNotActive is returned when a controller is not supported or enabled
  49. var ErrControllerNotActive = errors.New("controller is not supported")
  50. func existingPath(paths map[string]string, suffix string) Path {
  51. // localize the paths based on the root mount dest for nested cgroups
  52. for n, p := range paths {
  53. dest, err := getCgroupDestination(n)
  54. if err != nil {
  55. return errorPath(err)
  56. }
  57. rel, err := filepath.Rel(dest, p)
  58. if err != nil {
  59. return errorPath(err)
  60. }
  61. if rel == "." {
  62. rel = dest
  63. }
  64. paths[n] = filepath.Join("/", rel)
  65. }
  66. return func(name Name) (string, error) {
  67. root, ok := paths[string(name)]
  68. if !ok {
  69. if root, ok = paths["name="+string(name)]; !ok {
  70. return "", ErrControllerNotActive
  71. }
  72. }
  73. if suffix != "" {
  74. return filepath.Join(root, suffix), nil
  75. }
  76. return root, nil
  77. }
  78. }
  79. func subPath(path Path, subName string) Path {
  80. return func(name Name) (string, error) {
  81. p, err := path(name)
  82. if err != nil {
  83. return "", err
  84. }
  85. return filepath.Join(p, subName), nil
  86. }
  87. }
  88. func errorPath(err error) Path {
  89. return func(_ Name) (string, error) {
  90. return "", err
  91. }
  92. }