From 82b8111d048d1603e9c1c3e64e8f6d0b2659aee0 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 1 Sep 2018 16:17:19 +0200 Subject: [PATCH 1/2] [process][darwin] Fix #573 use Pids() to get processes in Processes() --- process/process_windows.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/process/process_windows.go b/process/process_windows.go index 199b9b4..82aed37 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -603,21 +603,22 @@ func Processes() ([]*Process, error) { } func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - pids, err := Pids() + out := []*Process{} + + pids, err := PidsWithContext(ctx) if err != nil { - return []*Process{}, fmt.Errorf("could not get Processes %s", err) + return out, fmt.Errorf("could not get Processes %s", err) } - results := []*Process{} for _, pid := range pids { - p, err := NewProcess(int32(pid)) + p, err := NewProcess(pid) if err != nil { continue } - results = append(results, p) + out = append(out, p) } - return results, nil + return out, nil } func getProcInfo(pid int32) (*SystemProcessInformation, error) { From e38ea9f3184ebf945cc456292cb13164c28ae30d Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 1 Sep 2018 16:18:32 +0200 Subject: [PATCH 2/2] [process] Don't lose context in ProcessesWithContext() on Windows and Linux --- process/process_darwin.go | 26 +++++++------------------- process/process_linux.go | 2 +- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index c949154..509ab3b 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -527,34 +527,22 @@ func Processes() ([]*Process, error) { } func ProcessesWithContext(ctx context.Context) ([]*Process, error) { - results := []*Process{} + out := []*Process{} - mib := []int32{CTLKern, KernProc, KernProcAll, 0} - buf, length, err := common.CallSyscall(mib) + pids, err := PidsWithContext(ctx) if err != nil { - return results, err + return out, err } - // get kinfo_proc size - k := KinfoProc{} - procinfoLen := int(unsafe.Sizeof(k)) - count := int(length / uint64(procinfoLen)) - - // parse buf to procs - for i := 0; i < count; i++ { - b := buf[i*procinfoLen : i*procinfoLen+procinfoLen] - k, err := parseKinfoProc(b) - if err != nil { - continue - } - p, err := NewProcess(int32(k.Proc.P_pid)) + for _, pid := range pids { + p, err := NewProcess(pid) if err != nil { continue } - results = append(results, p) + out = append(out, p) } - return results, nil + return out, nil } func parseKinfoProc(buf []byte) (KinfoProc, error) { diff --git a/process/process_linux.go b/process/process_linux.go index 6708173..f844101 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -1220,7 +1220,7 @@ func Processes() ([]*Process, error) { func ProcessesWithContext(ctx context.Context) ([]*Process, error) { out := []*Process{} - pids, err := Pids() + pids, err := PidsWithContext(ctx) if err != nil { return out, err }