k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/util/cgroups_linux.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "path/filepath" 21 22 libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups" 23 libcontainerutils "github.com/opencontainers/runc/libcontainer/utils" 24 ) 25 26 const ( 27 // CgroupRoot is the base path where cgroups are mounted 28 CgroupRoot = "/sys/fs/cgroup" 29 ) 30 31 // GetPids gets pids of the desired cgroup 32 // Forked from opencontainers/runc/libcontainer/cgroup/fs.Manager.GetPids() 33 func GetPids(cgroupPath string) ([]int, error) { 34 dir := "" 35 36 if libcontainercgroups.IsCgroup2UnifiedMode() { 37 path, err := filepath.Rel("/", cgroupPath) 38 if err != nil { 39 return nil, err 40 } 41 dir = filepath.Join(CgroupRoot, path) 42 } else { 43 var err error 44 dir, err = getCgroupV1Path(cgroupPath) 45 if err != nil { 46 return nil, err 47 } 48 } 49 return libcontainercgroups.GetPids(dir) 50 } 51 52 // getCgroupV1Path gets the file path to the "devices" subsystem of the desired cgroup. 53 // cgroupPath is the path in the cgroup hierarchy. 54 func getCgroupV1Path(cgroupPath string) (string, error) { 55 cgroupPath = libcontainerutils.CleanPath(cgroupPath) 56 57 mnt, root, err := libcontainercgroups.FindCgroupMountpointAndRoot(cgroupPath, "devices") 58 // If we didn't mount the subsystem, there is no point we make the path. 59 if err != nil { 60 return "", err 61 } 62 63 // If the cgroup name/path is absolute do not look relative to the cgroup of the init process. 64 if filepath.IsAbs(cgroupPath) { 65 // Sometimes subsystems can be mounted together as 'cpu,cpuacct'. 66 return filepath.Join(root, mnt, cgroupPath), nil 67 } 68 69 parentPath, err := getCgroupV1ParentPath(mnt, root) 70 if err != nil { 71 return "", err 72 } 73 74 return filepath.Join(parentPath, cgroupPath), nil 75 } 76 77 // getCgroupV1ParentPath gets the parent filepath to this cgroup, for resolving relative cgroup paths. 78 func getCgroupV1ParentPath(mountpoint, root string) (string, error) { 79 // Use GetThisCgroupDir instead of GetInitCgroupDir, because the creating 80 // process could in container and shared pid namespace with host, and 81 // /proc/1/cgroup could point to whole other world of cgroups. 82 initPath, err := libcontainercgroups.GetOwnCgroup("devices") 83 if err != nil { 84 return "", err 85 } 86 // This is needed for nested containers, because in /proc/self/cgroup we 87 // see paths from host, which don't exist in container. 88 relDir, err := filepath.Rel(root, initPath) 89 if err != nil { 90 return "", err 91 } 92 return filepath.Join(mountpoint, relDir), nil 93 }