1
0

GetProcessPIDs was renamed to IsProcessRuns and now finds just first occurance of a process.

This commit is contained in:
Alexander Andreev 2024-07-07 17:23:21 +04:00
parent cb8f6b797a
commit ef99d71a1a
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -5,36 +5,28 @@ package main
import ( import (
"os" "os"
"strconv"
"strings" "strings"
) )
// GetProcessPIDs returns a list of PIDs found for a process. // IsProcessRuns returns true if there is a process with a given name in cmdline.
func GetProcessPIDs(name string) (pids []int, err error) { func IsProcessRuns(name string) (bool, error) {
dir, err := os.ReadDir("/proc/") dir, err := os.ReadDir("/proc/")
if err != nil { if err != nil {
return nil, err return false, err
} }
for _, entry := range dir { for _, entry := range dir {
// 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.
if entry.IsDir() && entry.Name()[0]>>0x4&0xf == 0x3 { if entry.IsDir() && entry.Name()[0]>>0x4&0xf == 0x3 {
cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline") cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline")
if err != nil { if err != nil {
return nil, err return false, err
} }
if strings.Contains(string(cmdline), name) { if strings.Contains(string(cmdline), name) {
pid, err := strconv.Atoi(entry.Name()) return true, nil
if err != nil {
continue
}
pids = append(pids, pid)
} }
} }
} }
return pids, nil return false, nil
} }