/* Copyright The containerd Authors (and Adam Richter, 2019). Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // package cgroups package main import ( "bufio" "fmt" "io" // "io/ioutil" "os" "path/filepath" // "strconv" "strings" // "time" ) // type Path interface { // Path() string // Remove() // } type Path error // var ErrControllerNotActive = "Controller Not Active" var ErrNoCgroupMountDestination = fmt.Errorf("No Cgroup Mount Destination") func parseCgroupFile(path string) (map[string]string, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() return parseCgroupFromReader(f) } func parseCgroupFromReader(r io.Reader) (map[string]string, error) { var ( cgroups = make(map[string]string) s = bufio.NewScanner(r) ) for s.Scan() { if err := s.Err(); err != nil { return nil, err } var ( text = s.Text() parts = strings.SplitN(text, ":", 3) ) if len(parts) < 3 { return nil, fmt.Errorf("invalid cgroup entry: %q", text) } for _, subs := range strings.Split(parts[1], ",") { if subs != "" { cgroups[subs] = parts[2] } } } return cgroups, nil } func getCgroupDestination(subsystem string) (string, error) { f, err := os.Open("/proc/self/mountinfo") if err != nil { return "", err } defer f.Close() s := bufio.NewScanner(f) for s.Scan() { if err := s.Err(); err != nil { return "", err } fields := strings.Fields(s.Text()) for _, opt := range strings.Split(fields[len(fields)-1], ",") { // fmt.Printf("AJR considering %s vs. %s.\n", opt, subsystem); if opt == subsystem { // fmt.Printf("Matched %s.\n", subsystem); return fields[3], nil } } } // fmt.Printf("Failed to match %s.\n", subsystem); fmt.Printf("mount -t cgroup -o %s none /sys/fs/cgroup/%s\n", subsystem, subsystem) // return "", ErrNoCgroupMountDestination return "/", nil } // NestedPath will nest the cgroups based on the calling processes cgroup // placing its child processes inside its own path func NestedPath(suffix string) Path { paths, err := parseCgroupFile("/proc/self/cgroup") if err != nil { return errorPath(err) } return existingPath(paths, suffix) } // PidPath will return the correct cgroup paths for an existing process running inside a cgroup // This is commonly used for the Load function to restore an existing container func PidPath(pid int) Path { p := fmt.Sprintf("/proc/%d/cgroup", pid) paths, err := parseCgroupFile(p) if err != nil { // return errorPath(errors.Wrapf(err, "parse cgroup file %s", p)) return fmt.Errorf("%s: parse cgroup file %s", err, p); } return existingPath(paths, "") } // ErrControllerNotActive is returned when a controller is not supported or enabled // var ErrControllerNotActive = errors.New("controller is not supported") var ErrControllerNotActive = fmt.Errorf("controller is not supported"); func existingPath(paths map[string]string, suffix string) Path { // localize the paths based on the root mount dest for nested cgroups var result = "" for n, p := range paths { dest, err := getCgroupDestination(string(n)) if err != nil { return errorPath(err) } rel, err := filepath.Rel(dest, p) if err != nil { return errorPath(err) } if rel == "." { rel = dest } paths[n] = filepath.Join("/", rel) // result = fmt.Sprintf("%s %s", result, paths[n]) } return fmt.Errorf("%s", result); } func errorPath(err error) Path { return err; // return func(_ Name) (string, error) { // return "", err // } } func main() { // fmt.Printf("%s\n", NestedPath("")); NestedPath(""); }