From 8c186ae593caed7822791080456c1eb959d723d5 Mon Sep 17 00:00:00 2001 From: Chris Gilling Date: Sun, 28 Feb 2016 19:49:50 -0800 Subject: [PATCH] process: add CmdlineSlice for darwin + windows darwin will not perform correctly if there are spaces in the actual arguments, in which case a single argument will be reported as multple. Some CGO would be needed to get around this I think. I couldn't find any good documentation on how windows handles command line arguments with spaces inside the actual arguments, so this implementation merely just splits on spaces. --- process/process_darwin.go | 15 ++++++++++++++- process/process_windows.go | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index f2bd0a8..c728bcd 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -81,6 +81,9 @@ func (p *Process) Name() (string, error) { func (p *Process) Exe() (string, error) { return "", common.NotImplementedError } + +// Cmdline returns the command line arguments of the process as a string with +// each argument separated by 0x20 ascii character. func (p *Process) Cmdline() (string, error) { r, err := callPs("command", p.Pid, false) if err != nil { @@ -88,8 +91,18 @@ func (p *Process) Cmdline() (string, error) { } return strings.Join(r[0], " "), err } + +// CmdlineSlice returns the command line arguments of the process as a slice with each +// element being an argument. Because of current deficiencies in the way that the command +// line arguments are found, single arguments that have spaces in the will actually be +// reported as two separate items. In order to do something better CGO would be needed +// to use the native darwin functions. func (p *Process) CmdlineSlice() ([]string, error) { - return nil, common.NotImplementedError + r, err := callPs("command", p.Pid, false) + if err != nil { + return nil, err + } + return r[0], err } func (p *Process) CreateTime() (int64, error) { return 0, common.NotImplementedError diff --git a/process/process_windows.go b/process/process_windows.go index 6fd972e..f4fe119 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -5,6 +5,7 @@ package process import ( "errors" "fmt" + "strings" "syscall" "time" "unsafe" @@ -144,9 +145,18 @@ func (p *Process) Cmdline() (string, error) { } return *dst[0].CommandLine, nil } + +// CmdlineSlice returns the command line arguments of the process as a slice with each +// element being an argument. This merely returns the CommandLine informations passed +// to the process split on the 0x20 ASCII character. func (p *Process) CmdlineSlice() ([]string, error) { - return nil, common.NotImplementedError + cmdline, err := p.Cmdline() + if err != nil { + return nil, err + } + return strings.Split(cmdline, " "), nil } + func (p *Process) CreateTime() (int64, error) { dst, err := GetWin32Proc(p.Pid) if err != nil {