...

Source file src/github.com/containerd/cgroups/v1.go

Documentation: github.com/containerd/cgroups

     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 cgroups
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  )
    26  
    27  // V1 returns all the groups in the default cgroups mountpoint in a single hierarchy
    28  func V1() ([]Subsystem, error) {
    29  	root, err := v1MountPoint()
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	subsystems, err := defaults(root)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	var enabled []Subsystem
    38  	for _, s := range pathers(subsystems) {
    39  		// check and remove the default groups that do not exist
    40  		if _, err := os.Lstat(s.Path("/")); err == nil {
    41  			enabled = append(enabled, s)
    42  		}
    43  	}
    44  	return enabled, nil
    45  }
    46  
    47  // v1MountPoint returns the mount point where the cgroup
    48  // mountpoints are mounted in a single hiearchy
    49  func v1MountPoint() (string, error) {
    50  	f, err := os.Open("/proc/self/mountinfo")
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	defer f.Close()
    55  	scanner := bufio.NewScanner(f)
    56  	for scanner.Scan() {
    57  		var (
    58  			text      = scanner.Text()
    59  			fields    = strings.Split(text, " ")
    60  			numFields = len(fields)
    61  		)
    62  		if numFields < 10 {
    63  			return "", fmt.Errorf("mountinfo: bad entry %q", text)
    64  		}
    65  		if fields[numFields-3] == "cgroup" {
    66  			return filepath.Dir(fields[4]), nil
    67  		}
    68  	}
    69  	if err := scanner.Err(); err != nil {
    70  		return "", err
    71  	}
    72  	return "", ErrMountPointNotExist
    73  }
    74  

View as plain text