diff --git a/internal/psscanner/proclist.go b/internal/psscanner/proclist.go index 443e500..d6b97d7 100644 --- a/internal/psscanner/proclist.go +++ b/internal/psscanner/proclist.go @@ -1,6 +1,7 @@ package psscanner import ( + "errors" "fmt" "io/ioutil" "os" @@ -61,18 +62,30 @@ func getPIDs() ([]int, error) { pids := make([]int, 0) for _, f := range proc { - if f.IsDir() { - name := f.Name() - pid, err := strconv.Atoi(name) - if err != nil || pid <= 0 { - continue // not a pid - } - pids = append(pids, pid) + pid, err := file2Pid(f) + if err != nil { + continue } + pids = append(pids, pid) } return pids, nil } +var errNotAPid = errors.New("not a pid") + +func file2Pid(f os.FileInfo) (int, error) { + if !f.IsDir() { + return -1, errNotAPid + } + + pid, err := strconv.Atoi(f.Name()) + if err != nil || pid <= 0 { + return -1, errNotAPid + } + + return pid, nil +} + func getCmd(pid int) (string, error) { cmd, err := cmdLineReader(pid) if err != nil {