From 4f7834af2567bd02a5f68e032b7f5630f2b7538f Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:07:28 +0200 Subject: [PATCH 01/11] Added attempt to get exe path from pid --- process/process_darwin.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 8ba1619..1b336ad 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -80,7 +80,22 @@ func (p *Process) Name() (string, error) { return common.IntToString(k.Proc.P_comm[:]), nil } func (p *Process) Exe() (string, error) { - return "", common.ErrNotImplementedError + bin, err := exec.LookPath("lsof") + if err != nil { + return "", err + } + + var cmd []string + cmd := []string{"-p", p.Pid, "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} + + out, err = invoke.Command(bin, cmd...) + if err != nil { + return "", err + } + + ret = strings.TrimSpace(out) + + return ret, nil } // Cmdline returns the command line arguments of the process as a string with From 5481d9398986116a58d23177f749b5418a677eb2 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:10:41 +0200 Subject: [PATCH 02/11] Fixing assignment of vars --- process/process_darwin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 1b336ad..ee4a8b9 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -88,12 +88,12 @@ func (p *Process) Exe() (string, error) { var cmd []string cmd := []string{"-p", p.Pid, "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} - out, err = invoke.Command(bin, cmd...) + out, err := invoke.Command(bin, cmd...) if err != nil { return "", err } - ret = strings.TrimSpace(out) + ret := strings.TrimSpace(out) return ret, nil } From 8bb06aca3db3d46cefe5ffbbe6ed9845d6e52094 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:15:15 +0200 Subject: [PATCH 03/11] Fixed type casting --- process/process_darwin.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index ee4a8b9..79495bb 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -85,15 +85,14 @@ func (p *Process) Exe() (string, error) { return "", err } - var cmd []string - cmd := []string{"-p", p.Pid, "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} + cmd := []string{"-p", string(p.Pid), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} out, err := invoke.Command(bin, cmd...) if err != nil { return "", err } - ret := strings.TrimSpace(out) + ret := strings.TrimSpace(string(out)) return ret, nil } From 9659355f49f049f44025e63d7183cc7c72ddd4b1 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:21:10 +0200 Subject: [PATCH 04/11] Checking content --- process/process_darwin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 79495bb..11c1cd3 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -86,7 +86,9 @@ func (p *Process) Exe() (string, error) { } cmd := []string{"-p", string(p.Pid), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} - + + fmt.Println(strings.Join(cmd, " ")) + out, err := invoke.Command(bin, cmd...) if err != nil { return "", err From 8c3e7bd2553d490cc228ac2a6329c9b34f5e896b Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:26:00 +0200 Subject: [PATCH 05/11] Casting pid properly --- process/process_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 11c1cd3..0672228 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -85,7 +85,7 @@ func (p *Process) Exe() (string, error) { return "", err } - cmd := []string{"-p", string(p.Pid), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} + cmd := []string{"-p", strconv.Itoa(int(pid)), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} fmt.Println(strings.Join(cmd, " ")) From 1534b109ae0d3ea33d1bda46634064b848d036ae Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:27:14 +0200 Subject: [PATCH 06/11] Fixing pid name --- process/process_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 0672228..595abd6 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -85,7 +85,7 @@ func (p *Process) Exe() (string, error) { return "", err } - cmd := []string{"-p", strconv.Itoa(int(pid)), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} + cmd := []string{"-p", strconv.Itoa(int(p.Pid)), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} fmt.Println(strings.Join(cmd, " ")) From dd3cbcc61176866085c7d51736a16222b9e23f04 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 12:36:58 +0200 Subject: [PATCH 07/11] Trying remove single quotes --- process/process_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 595abd6..37d878b 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -85,7 +85,7 @@ func (p *Process) Exe() (string, error) { return "", err } - cmd := []string{"-p", strconv.Itoa(int(p.Pid)), "-Fn", "|", "awk", "'NR==3{print}'", "|", "sed", "'s/n\\//\\//'"} + cmd := []string{"-p", strconv.Itoa(int(p.Pid)), "-Fn", "|", "awk", "NR==3{print}", "|", "sed", "s/n\\//\\//"} fmt.Println(strings.Join(cmd, " ")) From d9b355f75ea694d1681b2319733957ec433cfa5f Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 13:16:22 +0200 Subject: [PATCH 08/11] Added proper piping of commands --- internal/common/common.go | 43 +++++++++++++++++++++++++++++++++++++++ process/process_darwin.go | 14 ++++++++----- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index f0ea6dd..1cc26a1 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -341,3 +341,46 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error { return ErrTimeout } } + +// https://gist.github.com/kylelemons/1525278 +func Pipeline(cmds ...*exec.Cmd) (pipeLineOutput, collectedStandardError []byte, pipeLineError os.Error) { + // Require at least one command + if len(cmds) < 1 { + return nil, nil, nil + } + + // Collect the output from the command(s) + var output bytes.Buffer + var stderr bytes.Buffer + + last := len(cmds) - 1 + for i, cmd := range cmds[:last] { + var err os.Error + // Connect each command's stdin to the previous command's stdout + if cmds[i+1].Stdin, err = cmd.StdoutPipe(); err != nil { + return nil, nil, err + } + // Connect each command's stderr to a buffer + cmd.Stderr = &stderr + } + + // Connect the output and error for the last command + cmds[last].Stdout, cmds[last].Stderr = &output, &stderr + + // Start each command + for _, cmd := range cmds { + if err := cmd.Start(); err != nil { + return output.Bytes(), stderr.Bytes(), err + } + } + + // Wait for each command to complete + for _, cmd := range cmds { + if err := cmd.Wait(); err != nil { + return output.Bytes(), stderr.Bytes(), err + } + } + + // Return the pipeline output and the collected standard error + return output.Bytes(), stderr.Bytes(), nil +} diff --git a/process/process_darwin.go b/process/process_darwin.go index 37d878b..b061969 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -80,21 +80,25 @@ func (p *Process) Name() (string, error) { return common.IntToString(k.Proc.P_comm[:]), nil } func (p *Process) Exe() (string, error) { - bin, err := exec.LookPath("lsof") + lsof_bin, err := exec.LookPath("lsof") if err != nil { return "", err } - cmd := []string{"-p", strconv.Itoa(int(p.Pid)), "-Fn", "|", "awk", "NR==3{print}", "|", "sed", "s/n\\//\\//"} + awk_bin, _ := exec.Lookpath("awk") + sed_bin, _ := exec.LookPath("sed") - fmt.Println(strings.Join(cmd, " ")) + lsof := exec.Command(lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fn") + awk := exec.Command(awk_bin, "NR==3{print}") + sed := exec.Command("s/n\\//\\//") + + output, stderr, err := common.Pipeline(lsof, awk, sed) - out, err := invoke.Command(bin, cmd...) if err != nil { return "", err } - ret := strings.TrimSpace(string(out)) + ret := strings.TrimSpace(string(output)) return ret, nil } From 5c0ac28fedc62a2bd7dbe6d3642a479a6888e1d9 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 13:19:19 +0200 Subject: [PATCH 09/11] Changing a little the return values --- internal/common/common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index 1cc26a1..5be7012 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -343,7 +343,7 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error { } // https://gist.github.com/kylelemons/1525278 -func Pipeline(cmds ...*exec.Cmd) (pipeLineOutput, collectedStandardError []byte, pipeLineError os.Error) { +func Pipeline(cmds ...*exec.Cmd) ([]byte, []byte, error) { // Require at least one command if len(cmds) < 1 { return nil, nil, nil @@ -355,7 +355,7 @@ func Pipeline(cmds ...*exec.Cmd) (pipeLineOutput, collectedStandardError []byte, last := len(cmds) - 1 for i, cmd := range cmds[:last] { - var err os.Error + var err error // Connect each command's stdin to the previous command's stdout if cmds[i+1].Stdin, err = cmd.StdoutPipe(); err != nil { return nil, nil, err From b6a7649aab04eb9b1a8518185d06b937f3e7b595 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 13:20:58 +0200 Subject: [PATCH 10/11] Error handling --- process/process_darwin.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index b061969..8c6a538 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -85,8 +85,15 @@ func (p *Process) Exe() (string, error) { return "", err } - awk_bin, _ := exec.Lookpath("awk") - sed_bin, _ := exec.LookPath("sed") + awk_bin, err := exec.LookPath("awk") + if err != nil { + return "", err + } + + sed_bin, err := exec.LookPath("sed") + if err != nil { + return "", err + } lsof := exec.Command(lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fn") awk := exec.Command(awk_bin, "NR==3{print}") From 189b8e6d12d832884278f2ae6c8eaf067f4fbb9c Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 21 Oct 2016 13:22:09 +0200 Subject: [PATCH 11/11] Fixed missing bin and unused stderr --- process/process_darwin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 8c6a538..b3a6a2e 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -97,9 +97,9 @@ func (p *Process) Exe() (string, error) { lsof := exec.Command(lsof_bin, "-p", strconv.Itoa(int(p.Pid)), "-Fn") awk := exec.Command(awk_bin, "NR==3{print}") - sed := exec.Command("s/n\\//\\//") + sed := exec.Command(sed_bin, "s/n\\//\\//") - output, stderr, err := common.Pipeline(lsof, awk, sed) + output, _, err := common.Pipeline(lsof, awk, sed) if err != nil { return "", err