2023-12-15 04:05:34 +04:00
|
|
|
//go:build unix
|
|
|
|
// +build unix
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-02-04 20:35:23 +04:00
|
|
|
// GetProcessPIDs returns a list of PIDs found for a process.
|
|
|
|
func GetProcessPIDs(name string) (pids []int, err error) {
|
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 {
|
2023-02-04 20:33:26 +04:00
|
|
|
// This line demands a little clarification. Here we are making sure
|
|
|
|
// that the first character of a directory name is numeric. In ASCII
|
|
|
|
// all numerals are laying within 0x30-0x39 range.
|
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
|
|
|
|
}
|