gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

paths.go (2769B)


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