From c903f141890869a1a862deae4ad75413c7180107 Mon Sep 17 00:00:00 2001 From: Shannon Wynter Date: Mon, 18 Apr 2016 11:15:15 +1000 Subject: [PATCH] Remove the requirement to use lsof by using the information provided in status to get the parent pid --- process/process.go | 1 + process/process_linux.go | 46 ++++++++-------------------------------------- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/process/process.go b/process/process.go index 2b9d685..4b69224 100644 --- a/process/process.go +++ b/process/process.go @@ -20,6 +20,7 @@ type Process struct { Pid int32 `json:"pid"` name string status string + parent int32 numCtxSwitches *NumCtxSwitchesStat uids []int32 gids []int32 diff --git a/process/process_linux.go b/process/process_linux.go index e79ba36..4769047 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -6,10 +6,8 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -118,18 +116,11 @@ func (p *Process) Cwd() (string, error) { return p.fillFromCwd() } func (p *Process) Parent() (*Process, error) { - r, err := callLsof("R", p.Pid) - if err != nil { - return nil, err - } - if len(r) != 1 { // TODO: pid 1 - return nil, fmt.Errorf("wrong number of parents") - } - v, err := strconv.Atoi(r[0]) + err := p.fillFromStatus() if err != nil { return nil, err } - return NewProcess(int32(v)) + return NewProcess(p.parent) } func (p *Process) Status() (string, error) { err := p.fillFromStatus() @@ -557,6 +548,12 @@ func (p *Process) fillFromStatus() error { p.name = strings.Trim(value, " \t") case "State": p.status = value[0:1] + case "Ppid": + pval, err := strconv.ParseInt(value, 10, 32) + if err != nil { + return err + } + p.parent = int32(pval) case "Uid": p.uids = make([]int32, 0, 4) for _, i := range strings.Split(value, "\t") { @@ -704,30 +701,3 @@ func Pids() ([]int32, error) { return ret, nil } - -func callLsof(arg string, pid int32) ([]string, error) { - var cmd []string - if pid == 0 { // will get from all processes. - cmd = []string{"-F" + arg} - } else { - cmd = []string{"-a", "-F" + arg, "-p", strconv.Itoa(int(pid))} - } - lsof, err := exec.LookPath("lsof") - if err != nil { - return []string{}, err - } - out, err := invoke.Command(lsof, cmd...) - if err != nil { - return []string{}, err - } - lines := strings.Split(string(out), "\n") - - var ret []string - for _, l := range lines[1:] { - if strings.HasPrefix(l, arg) { - ret = append(ret, l[1:]) // delete first char - } - } - - return ret, nil -}