github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/linux_container/cgroups_manager/cgroup_reader.go (about)

     1  package cgroups_manager
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  type LinuxCgroupReader struct {
    11  	Path     string
    12  	openFile *os.File
    13  }
    14  
    15  func (c *LinuxCgroupReader) CgroupNode(subsystem string) (string, error) {
    16  	if c.openFile == nil {
    17  		var err error
    18  
    19  		c.openFile, err = os.Open(c.Path)
    20  		if err != nil {
    21  			return "", fmt.Errorf("cgroups_manager: opening file: %s", err)
    22  		}
    23  	}
    24  
    25  	_, err := c.openFile.Seek(0, 0)
    26  	if err != nil {
    27  		return "", fmt.Errorf("cgroups_manager: seeking start of file: %s", err)
    28  	}
    29  
    30  	contents, err := ioutil.ReadAll(c.openFile)
    31  	if err != nil {
    32  		return "", fmt.Errorf("cgroups_manager: reading file: %s", err)
    33  	}
    34  
    35  	line := findCgroupEntry(string(contents), subsystem)
    36  	if line == "" {
    37  		return "", fmt.Errorf("cgroups_manager: requested subsystem %s does not exist", subsystem)
    38  	}
    39  
    40  	cgroupEntryColumns := strings.Split(line, ":")
    41  	if len(cgroupEntryColumns) != 3 {
    42  		return "", fmt.Errorf("cgroups_manager: cgroup file malformed: %s", string(contents))
    43  	}
    44  
    45  	return cgroupEntryColumns[2], nil
    46  }
    47  
    48  func findCgroupEntry(contents, subsystem string) string {
    49  	for _, line := range strings.Split(contents, "\n") {
    50  		if strings.Contains(line, fmt.Sprintf("%s:", subsystem)) {
    51  			return line
    52  		}
    53  	}
    54  
    55  	return ""
    56  }