From 21daedd6b49519b0ce315dff0fa9e44aea364314 Mon Sep 17 00:00:00 2001 From: Robert Weber Date: Thu, 4 Feb 2016 15:06:36 -0700 Subject: [PATCH 1/4] There are two possible error scenarios for CallPgrep. One indicates a broken system (no pgrep command) and one is a normal error state of pgrep meaning no processes found for the criteria given (in this case the parent pid does not exist or the process simply has no children). The later case is quite usefull to know about so I added a static error for this case. --- internal/common/common_unix.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/common/common_unix.go b/internal/common/common_unix.go index 6622eecc2..14016265a 100644 --- a/internal/common/common_unix.go +++ b/internal/common/common_unix.go @@ -3,11 +3,14 @@ package common import ( + "errors" "os/exec" "strconv" "strings" ) +var ErrorNoChildren = errors.New("Process does not have children or does not exist") + func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { var cmd []string if pid == 0 { // will get from all processes. @@ -48,7 +51,7 @@ func CallPgrep(invoke Invoker, pid int32) ([]int32, error) { } out, err := invoke.Command(pgrep, cmd...) if err != nil { - return []int32{}, err + return []int32{}, ErrorNoChildren } lines := strings.Split(string(out), "\n") ret := make([]int32, 0, len(lines)) From 6496b57b3d7e994889fd82fb5c041b428013bc6e Mon Sep 17 00:00:00 2001 From: Robert Weber Date: Thu, 4 Feb 2016 15:14:11 -0700 Subject: [PATCH 2/4] Need to promote this error --- process/process_linux.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/process/process_linux.go b/process/process_linux.go index 6ff2bc1c8..28994a4f9 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -4,6 +4,7 @@ package process import ( "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -18,6 +19,8 @@ import ( "github.com/shirou/gopsutil/net" ) +var ErrorNoChildren = errors.New("Process does not have children or does not exist") + const ( PrioProcess = 0 // linux/resource.h ) @@ -205,6 +208,9 @@ func (p *Process) MemoryPercent() (float32, error) { func (p *Process) Children() ([]*Process, error) { pids, err := common.CallPgrep(invoke, p.Pid) if err != nil { + if err == common.ErrorNoChildren { + return nil, ErrorNoChildren + } return nil, err } ret := make([]*Process, 0, len(pids)) From 4dcb099f3dd61f120af321dc54014b8d4b1a3791 Mon Sep 17 00:00:00 2001 From: Robert Weber Date: Thu, 4 Feb 2016 15:15:29 -0700 Subject: [PATCH 3/4] no or --- process/process_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_linux.go b/process/process_linux.go index 28994a4f9..9e3a87e6c 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -19,7 +19,7 @@ import ( "github.com/shirou/gopsutil/net" ) -var ErrorNoChildren = errors.New("Process does not have children or does not exist") +var ErrorNoChildren = errors.New("Process does not have children") const ( PrioProcess = 0 // linux/resource.h From c804a9e755c3cb29d5b7e181dcd3ac7366524bc4 Mon Sep 17 00:00:00 2001 From: Robert Weber Date: Mon, 8 Feb 2016 09:26:35 -0700 Subject: [PATCH 4/4] Per suggestions --- internal/common/common_unix.go | 5 +---- process/process_linux.go | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/common/common_unix.go b/internal/common/common_unix.go index 14016265a..6622eecc2 100644 --- a/internal/common/common_unix.go +++ b/internal/common/common_unix.go @@ -3,14 +3,11 @@ package common import ( - "errors" "os/exec" "strconv" "strings" ) -var ErrorNoChildren = errors.New("Process does not have children or does not exist") - func CallLsof(invoke Invoker, pid int32, args ...string) ([]string, error) { var cmd []string if pid == 0 { // will get from all processes. @@ -51,7 +48,7 @@ func CallPgrep(invoke Invoker, pid int32) ([]int32, error) { } out, err := invoke.Command(pgrep, cmd...) if err != nil { - return []int32{}, ErrorNoChildren + return []int32{}, err } lines := strings.Split(string(out), "\n") ret := make([]int32, 0, len(lines)) diff --git a/process/process_linux.go b/process/process_linux.go index 9e3a87e6c..deaf285e5 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -19,7 +19,7 @@ import ( "github.com/shirou/gopsutil/net" ) -var ErrorNoChildren = errors.New("Process does not have children") +var ErrorNoChildren = errors.New("process does not have children") const ( PrioProcess = 0 // linux/resource.h @@ -208,7 +208,7 @@ func (p *Process) MemoryPercent() (float32, error) { func (p *Process) Children() ([]*Process, error) { pids, err := common.CallPgrep(invoke, p.Pid) if err != nil { - if err == common.ErrorNoChildren { + if pids == nil || len(pids) == 0 { return nil, ErrorNoChildren } return nil, err