From c903f141890869a1a862deae4ad75413c7180107 Mon Sep 17 00:00:00 2001 From: Shannon Wynter Date: Mon, 18 Apr 2016 11:15:15 +1000 Subject: [PATCH 1/3] 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 -} From a4387d0c9224018e208db958e53d387c833430c3 Mon Sep 17 00:00:00 2001 From: Shannon Wynter Date: Mon, 18 Apr 2016 14:38:24 +1000 Subject: [PATCH 2/3] emulate original behaviour --- process/process_linux.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/process/process_linux.go b/process/process_linux.go index 4769047..adc06d3 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -120,6 +120,9 @@ func (p *Process) Parent() (*Process, error) { if err != nil { return nil, err } + if p.parent == 0 { + return nil, fmt.Errorf("wrong number of parents") + } return NewProcess(p.parent) } func (p *Process) Status() (string, error) { From f99d49546222d04b9be8405044b54299c5d89e3e Mon Sep 17 00:00:00 2001 From: Shannon Wynter Date: Mon, 18 Apr 2016 17:28:47 +1000 Subject: [PATCH 3/3] Run goimports before committing... --- process/process_linux.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/process/process_linux.go b/process/process_linux.go index adc06d3..75672a0 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -6,6 +6,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io/ioutil" "os" "path/filepath" @@ -551,7 +552,7 @@ func (p *Process) fillFromStatus() error { p.name = strings.Trim(value, " \t") case "State": p.status = value[0:1] - case "Ppid": + case "PPid", "Ppid": pval, err := strconv.ParseInt(value, 10, 32) if err != nil { return err