...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/cgroups/pidpath.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/cgroups

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cgroups
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/containerd/cgroups"
    22  )
    23  
    24  func V1() ([]cgroups.Subsystem, error) {
    25  	subsystems, err := defaults("/host-sys/fs/cgroup")
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	var enabled []cgroups.Subsystem
    30  	for _, s := range pathers(subsystems) {
    31  		// check and remove the default groups that do not exist
    32  		if _, err := os.Lstat(s.Path("/")); err == nil {
    33  			enabled = append(enabled, s)
    34  		}
    35  	}
    36  	return enabled, nil
    37  }
    38  
    39  func PidPath(pid int) cgroups.Path {
    40  	p := fmt.Sprintf("/proc/%d/cgroup", pid)
    41  	paths, err := cgroups.ParseCgroupFile(p)
    42  	if err != nil {
    43  		return func(_ cgroups.Name) (string, error) {
    44  			return "", fmt.Errorf("failed to parse cgroup file %s: %s", p, err.Error())
    45  		}
    46  	}
    47  
    48  	return func(name cgroups.Name) (string, error) {
    49  		root, ok := paths[string(name)]
    50  		if !ok {
    51  			if root, ok = paths["name="+string(name)]; !ok {
    52  				return "", errors.New("controller is not supported")
    53  			}
    54  		}
    55  
    56  		return root, nil
    57  	}
    58  }
    59