From d47acff222cf9b1c0c9a3dc2aaa02b7f60260796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Str=C3=B6mberg?= Date: Mon, 7 Oct 2024 08:13:06 -0400 Subject: [PATCH] process: make non-existent paths non-fatal, sort scan paths (#493) * pip: add known good list * process: make non-existent paths non-fatal, sort scan paths --- pkg/action/process.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/action/process.go b/pkg/action/process.go index a0bd48071..f6b9b1cc4 100644 --- a/pkg/action/process.go +++ b/pkg/action/process.go @@ -2,8 +2,11 @@ package action import ( "context" + "fmt" "os" + "sort" + "github.com/chainguard-dev/clog" "github.com/shirou/gopsutil/v4/process" ) @@ -17,15 +20,18 @@ func GetAllProcessPaths(ctx context.Context) ([]Process, error) { // Retrieve all of the active PIDs procs, err := process.ProcessesWithContext(ctx) if err != nil { - return nil, err + return nil, fmt.Errorf("processes: %w", err) } // Store PIDs and their respective commands (paths) in a map of paths and their Process structs processMap := make(map[string]Process, len(procs)) for _, p := range procs { path, err := p.Exe() + // Executable resolution is non-fatal if err != nil { - return nil, err + name, _ := p.Name() + clog.Errorf("%s[%d]: %v", name, p.Pid, err) + continue } if _, exists := processMap[path]; !exists && path != "" && isValidPath(path) { processMap[path] = Process{ @@ -40,11 +46,15 @@ func GetAllProcessPaths(ctx context.Context) ([]Process, error) { // procMapSlice converts a map of paths and their Process structs to a slice of Processes. func procMapSlice(m map[string]Process) []Process { - result := make([]Process, 0, len(m)) + ps := make([]Process, 0, len(m)) for _, v := range m { - result = append(result, v) + ps = append(ps, v) } - return result + + sort.Slice(ps, func(i, j int) bool { + return ps[i].Path < ps[j].Path + }) + return ps } // isValidPath checks if the given path is valid.