2022-10-10 00:10:34 +04:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
|
|
|
|
|
2022-01-02 21:30:56 +04:00
|
|
|
package main
|
2022-01-02 05:14:21 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-01-02 21:30:56 +04:00
|
|
|
"strings"
|
2022-01-02 05:14:21 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetProcessPIDs(name string) ([]int, error) {
|
|
|
|
var pids []int
|
|
|
|
|
2022-10-10 00:10:34 +04:00
|
|
|
dir, err := os.ReadDir("/proc/")
|
2022-01-02 05:14:21 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range dir {
|
2022-01-06 03:22:38 +04:00
|
|
|
if entry.IsDir() && entry.Name()[0]>>0x4&0xf == 0x3 {
|
|
|
|
cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline")
|
2022-01-03 01:10:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-06 03:22:38 +04:00
|
|
|
if strings.Contains(string(cmdline), name) {
|
2022-10-10 00:10:34 +04:00
|
|
|
pid, err := strconv.Atoi(entry.Name())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-02 05:14:21 +04:00
|
|
|
pids = append(pids, pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pids, nil
|
|
|
|
}
|