cgroups_linux.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2016 The Kubernetes 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 util
  14. import (
  15. "path/filepath"
  16. libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups"
  17. libcontainerutils "github.com/opencontainers/runc/libcontainer/utils"
  18. )
  19. // GetPids gets pids of the desired cgroup
  20. // Forked from opencontainers/runc/libcontainer/cgroup/fs.Manager.GetPids()
  21. func GetPids(cgroupPath string) ([]int, error) {
  22. dir, err := getCgroupPath(cgroupPath)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return libcontainercgroups.GetPids(dir)
  27. }
  28. // getCgroupPath gets the file path to the "devices" subsystem of the desired cgroup.
  29. // cgroupPath is the path in the cgroup hierarchy.
  30. func getCgroupPath(cgroupPath string) (string, error) {
  31. cgroupPath = libcontainerutils.CleanPath(cgroupPath)
  32. mnt, root, err := libcontainercgroups.FindCgroupMountpointAndRoot(cgroupPath, "devices")
  33. // If we didn't mount the subsystem, there is no point we make the path.
  34. if err != nil {
  35. return "", err
  36. }
  37. // If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
  38. if filepath.IsAbs(cgroupPath) {
  39. // Sometimes subsystems can be mounted together as 'cpu,cpuacct'.
  40. return filepath.Join(root, mnt, cgroupPath), nil
  41. }
  42. parentPath, err := getCgroupParentPath(mnt, root)
  43. if err != nil {
  44. return "", err
  45. }
  46. return filepath.Join(parentPath, cgroupPath), nil
  47. }
  48. // getCgroupParentPath gets the parent filepath to this cgroup, for resolving relative cgroup paths.
  49. func getCgroupParentPath(mountpoint, root string) (string, error) {
  50. // Use GetThisCgroupDir instead of GetInitCgroupDir, because the creating
  51. // process could in container and shared pid namespace with host, and
  52. // /proc/1/cgroup could point to whole other world of cgroups.
  53. initPath, err := libcontainercgroups.GetOwnCgroup("devices")
  54. if err != nil {
  55. return "", err
  56. }
  57. // This is needed for nested containers, because in /proc/self/cgroup we
  58. // see paths from host, which don't exist in container.
  59. relDir, err := filepath.Rel(root, initPath)
  60. if err != nil {
  61. return "", err
  62. }
  63. return filepath.Join(mountpoint, relDir), nil
  64. }