gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

subsys.go (2882B)


      1 // Copyright (c) 2017 Uber Technologies, Inc.
      2 //
      3 // Permission is hereby granted, free of charge, to any person obtaining a copy
      4 // of this software and associated documentation files (the "Software"), to deal
      5 // in the Software without restriction, including without limitation the rights
      6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      7 // copies of the Software, and to permit persons to whom the Software is
      8 // furnished to do so, subject to the following conditions:
      9 //
     10 // The above copyright notice and this permission notice shall be included in
     11 // all copies or substantial portions of the Software.
     12 //
     13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     19 // THE SOFTWARE.
     20 
     21 //go:build linux
     22 // +build linux
     23 
     24 package cgroups
     25 
     26 import (
     27 	"bufio"
     28 	"os"
     29 	"strconv"
     30 	"strings"
     31 )
     32 
     33 const (
     34 	_cgroupSep       = ":"
     35 	_cgroupSubsysSep = ","
     36 )
     37 
     38 const (
     39 	_csFieldIDID = iota
     40 	_csFieldIDSubsystems
     41 	_csFieldIDName
     42 	_csFieldCount
     43 )
     44 
     45 // CGroupSubsys represents the data structure for entities in
     46 // `/proc/$PID/cgroup`. See also proc(5) for more information.
     47 type CGroupSubsys struct {
     48 	ID         int
     49 	Subsystems []string
     50 	Name       string
     51 }
     52 
     53 // NewCGroupSubsysFromLine returns a new *CGroupSubsys by parsing a string in
     54 // the format of `/proc/$PID/cgroup`
     55 func NewCGroupSubsysFromLine(line string) (*CGroupSubsys, error) {
     56 	fields := strings.SplitN(line, _cgroupSep, _csFieldCount)
     57 
     58 	if len(fields) != _csFieldCount {
     59 		return nil, cgroupSubsysFormatInvalidError{line}
     60 	}
     61 
     62 	id, err := strconv.Atoi(fields[_csFieldIDID])
     63 	if err != nil {
     64 		return nil, err
     65 	}
     66 
     67 	cgroup := &CGroupSubsys{
     68 		ID:         id,
     69 		Subsystems: strings.Split(fields[_csFieldIDSubsystems], _cgroupSubsysSep),
     70 		Name:       fields[_csFieldIDName],
     71 	}
     72 
     73 	return cgroup, nil
     74 }
     75 
     76 // parseCGroupSubsystems parses procPathCGroup (usually at `/proc/$PID/cgroup`)
     77 // and returns a new map[string]*CGroupSubsys.
     78 func parseCGroupSubsystems(procPathCGroup string) (map[string]*CGroupSubsys, error) {
     79 	cgroupFile, err := os.Open(procPathCGroup)
     80 	if err != nil {
     81 		return nil, err
     82 	}
     83 	defer cgroupFile.Close()
     84 
     85 	scanner := bufio.NewScanner(cgroupFile)
     86 	subsystems := make(map[string]*CGroupSubsys)
     87 
     88 	for scanner.Scan() {
     89 		cgroup, err := NewCGroupSubsysFromLine(scanner.Text())
     90 		if err != nil {
     91 			return nil, err
     92 		}
     93 		for _, subsys := range cgroup.Subsystems {
     94 			subsystems[subsys] = cgroup
     95 		}
     96 	}
     97 
     98 	if err := scanner.Err(); err != nil {
     99 		return nil, err
    100 	}
    101 
    102 	return subsystems, nil
    103 }