...

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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package cgroups
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/containerd/cgroups"
    24  )
    25  
    26  func V1() ([]cgroups.Subsystem, error) {
    27  	subsystems, err := defaults("/host-sys/fs/cgroup")
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	var enabled []cgroups.Subsystem
    32  	for _, s := range pathers(subsystems) {
    33  		// check and remove the default groups that do not exist
    34  		if _, err := os.Lstat(s.Path("/")); err == nil {
    35  			enabled = append(enabled, s)
    36  		}
    37  	}
    38  	return enabled, nil
    39  }
    40  
    41  func PidPath(pid int) cgroups.Path {
    42  	p := fmt.Sprintf("/proc/%d/cgroup", pid)
    43  	paths, err := cgroups.ParseCgroupFile(p)
    44  	if err != nil {
    45  		return func(_ cgroups.Name) (string, error) {
    46  			return "", fmt.Errorf("failed to parse cgroup file %s: %s", p, err.Error())
    47  		}
    48  	}
    49  
    50  	return func(name cgroups.Name) (string, error) {
    51  		root, ok := paths[string(name)]
    52  		if !ok {
    53  			if root, ok = paths["name="+string(name)]; !ok {
    54  				return "", errors.New("controller is not supported")
    55  			}
    56  		}
    57  
    58  		return root, nil
    59  	}
    60  }
    61