github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/utils/cgnetcls/netcls.go (about) 1 // +build !windows 2 3 package cgnetcls 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "go.aporeto.io/enforcerd/trireme-lib/common" 13 "go.aporeto.io/enforcerd/trireme-lib/utils/constants" 14 "go.uber.org/zap" 15 ) 16 17 // receiver definition. 18 type netCls struct { 19 markchan chan uint64 // nolint: structcheck 20 ReleaseAgentPath string 21 TriremePath string 22 } 23 24 var ( 25 cgroupNetClsPath string 26 markval uint64 = constants.Initialmarkval // nolint: varcheck 27 ) 28 29 // ConfigureNetClsPath updates the cgroupNetCls path 30 func ConfigureNetClsPath(path string) { 31 cgroupNetClsPath = path 32 } 33 34 // GetCgroupList geta list of all cgroup names 35 // TODO: only used in autoport detection, and a bad usage as well 36 func GetCgroupList() []string { 37 var cgroupList []string 38 39 // iterate over our different base paths from the different cgroup base paths 40 for _, baseCgroupPath := range []string{common.TriremeCgroupPath, common.TriremeDockerHostNetwork} { 41 filelist, err := ioutil.ReadDir(filepath.Join(cgroupNetClsPath, baseCgroupPath)) 42 if err == nil { 43 for _, file := range filelist { 44 if file.IsDir() { 45 cgroupList = append(cgroupList, filepath.Join(baseCgroupPath, file.Name())) 46 } 47 } 48 } 49 } 50 return cgroupList 51 } 52 53 // ListCgroupProcesses lists the cgroups that trireme has created 54 // TODO: only used in autoport detection, and a bad usage as well 55 func ListCgroupProcesses(cgroupname string) ([]string, error) { 56 57 if _, err := os.Stat(filepath.Join(cgroupNetClsPath, cgroupname)); os.IsNotExist(err) { 58 return []string{}, fmt.Errorf("cgroup %s does not exist: %s", cgroupname, err) 59 } 60 61 data, err := ioutil.ReadFile(filepath.Join(cgroupNetClsPath, cgroupname, "cgroup.procs")) 62 if err != nil { 63 return []string{}, fmt.Errorf("cannot read procs file: %s", err) 64 } 65 66 procs := []string{} 67 68 for _, line := range strings.Split(string(data), "\n") { 69 if len(line) > 0 { 70 procs = append(procs, line) 71 } 72 } 73 74 return procs, nil 75 } 76 77 // GetAssignedMarkVal -- returns the mark val assigned to the group 78 // TODO: looks like dead code 79 func GetAssignedMarkVal(cgroupName string) string { 80 mark, err := ioutil.ReadFile(filepath.Join(cgroupNetClsPath, cgroupName, markFile)) 81 82 if err != nil || len(mark) < 1 { 83 zap.L().Error("Unable to read markval for cgroup", zap.String("Cgroup Name", cgroupName), zap.Error(err)) 84 return "" 85 } 86 return string(mark[:len(mark)-1]) 87 }