1
0
httpprocprobed/util.go

34 lines
536 B
Go
Raw Normal View History

package main
import (
"io/ioutil"
"os"
"strconv"
"strings"
)
func GetProcessPIDs(name string) ([]int, error) {
var pids []int
dir, err := ioutil.ReadDir("/proc/")
if err != nil {
return nil, err
}
for _, entry := range dir {
pid, err := strconv.Atoi(entry.Name())
if entry.IsDir() && err == nil {
f, err := os.ReadFile("/proc/" + entry.Name() + "/comm")
if err != nil {
return nil, err
}
if strings.Contains(string(f[:len(f)-1]), name) {
pids = append(pids, pid)
}
}
}
return pids, nil
}