Trace all processes in a container.

This commit is contained in:
Tom Wilkie
2015-09-17 10:46:28 +00:00
committed by Tom Wilkie
parent 38e7c7c560
commit 3e4b3ad0eb
5 changed files with 87 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import (
// Tree represents all processes on the machine.
type Tree interface {
GetParent(pid int) (int, error)
GetChildren(pid int) ([]int, error)
}
type tree struct {
@@ -32,3 +33,30 @@ func (pt *tree) GetParent(pid int) (int, error) {
return proc.PPID, nil
}
// GetChildren
func (pt *tree) GetChildren(pid int) ([]int, error) {
_, ok := pt.processes[pid]
if !ok {
return []int{}, fmt.Errorf("PID %d not found", pid)
}
var isChild func(id int) bool
isChild = func(id int) bool {
p, ok := pt.processes[id]
if !ok || p.PPID == 0 {
return false
} else if p.PPID == pid {
return true
}
return isChild(p.PPID)
}
children := []int{pid}
for id, _ := range pt.processes {
if isChild(id) {
children = append(children, id)
}
}
return children, nil
}