mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
refactor get pid
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package psscanner
|
package psscanner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@@ -61,18 +62,30 @@ func getPIDs() ([]int, error) {
|
|||||||
|
|
||||||
pids := make([]int, 0)
|
pids := make([]int, 0)
|
||||||
for _, f := range proc {
|
for _, f := range proc {
|
||||||
if f.IsDir() {
|
pid, err := file2Pid(f)
|
||||||
name := f.Name()
|
if err != nil {
|
||||||
pid, err := strconv.Atoi(name)
|
continue
|
||||||
if err != nil || pid <= 0 {
|
|
||||||
continue // not a pid
|
|
||||||
}
|
|
||||||
pids = append(pids, pid)
|
|
||||||
}
|
}
|
||||||
|
pids = append(pids, pid)
|
||||||
}
|
}
|
||||||
return pids, nil
|
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) {
|
func getCmd(pid int) (string, error) {
|
||||||
cmd, err := cmdLineReader(pid)
|
cmd, err := cmdLineReader(pid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user