...

Source file src/github.com/opencontainers/runc/libcontainer/cgroups/fs2/defaultpath.go

Documentation: github.com/opencontainers/runc/libcontainer/cgroups/fs2

     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 fs2
    18  
    19  import (
    20  	"bufio"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"path/filepath"
    26  	"strings"
    27  
    28  	"github.com/opencontainers/runc/libcontainer/configs"
    29  	"github.com/opencontainers/runc/libcontainer/utils"
    30  )
    31  
    32  const UnifiedMountpoint = "/sys/fs/cgroup"
    33  
    34  func defaultDirPath(c *configs.Cgroup) (string, error) {
    35  	if (c.Name != "" || c.Parent != "") && c.Path != "" {
    36  		return "", fmt.Errorf("cgroup: either Path or Name and Parent should be used, got %+v", c)
    37  	}
    38  
    39  	return _defaultDirPath(UnifiedMountpoint, c.Path, c.Parent, c.Name)
    40  }
    41  
    42  func _defaultDirPath(root, cgPath, cgParent, cgName string) (string, error) {
    43  	if (cgName != "" || cgParent != "") && cgPath != "" {
    44  		return "", errors.New("cgroup: either Path or Name and Parent should be used")
    45  	}
    46  
    47  	// XXX: Do not remove CleanPath. Path safety is important! -- cyphar
    48  	innerPath := utils.CleanPath(cgPath)
    49  	if innerPath == "" {
    50  		cgParent := utils.CleanPath(cgParent)
    51  		cgName := utils.CleanPath(cgName)
    52  		innerPath = filepath.Join(cgParent, cgName)
    53  	}
    54  	if filepath.IsAbs(innerPath) {
    55  		return filepath.Join(root, innerPath), nil
    56  	}
    57  
    58  	ownCgroup, err := parseCgroupFile("/proc/self/cgroup")
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	// The current user scope most probably has tasks in it already,
    63  	// making it impossible to enable controllers for its sub-cgroup.
    64  	// A parent cgroup (with no tasks in it) is what we need.
    65  	ownCgroup = filepath.Dir(ownCgroup)
    66  
    67  	return filepath.Join(root, ownCgroup, innerPath), nil
    68  }
    69  
    70  // parseCgroupFile parses /proc/PID/cgroup file and return string
    71  func parseCgroupFile(path string) (string, error) {
    72  	f, err := os.Open(path)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  	defer f.Close()
    77  	return parseCgroupFromReader(f)
    78  }
    79  
    80  func parseCgroupFromReader(r io.Reader) (string, error) {
    81  	s := bufio.NewScanner(r)
    82  	for s.Scan() {
    83  		var (
    84  			text  = s.Text()
    85  			parts = strings.SplitN(text, ":", 3)
    86  		)
    87  		if len(parts) < 3 {
    88  			return "", fmt.Errorf("invalid cgroup entry: %q", text)
    89  		}
    90  		// text is like "0::/user.slice/user-1001.slice/session-1.scope"
    91  		if parts[0] == "0" && parts[1] == "" {
    92  			return parts[2], nil
    93  		}
    94  	}
    95  	if err := s.Err(); err != nil {
    96  		return "", err
    97  	}
    98  	return "", errors.New("cgroup path not found")
    99  }
   100  

View as plain text