From ef99d71a1a01e06555f21f5be0856f3c6710751f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 7 Jul 2024 17:23:21 +0400 Subject: [PATCH] GetProcessPIDs was renamed to IsProcessRuns and now finds just first occurance of a process. --- process_unix.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/process_unix.go b/process_unix.go index e140022..687afdb 100644 --- a/process_unix.go +++ b/process_unix.go @@ -5,36 +5,28 @@ package main import ( "os" - "strconv" "strings" ) -// GetProcessPIDs returns a list of PIDs found for a process. -func GetProcessPIDs(name string) (pids []int, err error) { +// IsProcessRuns returns true if there is a process with a given name in cmdline. +func IsProcessRuns(name string) (bool, error) { dir, err := os.ReadDir("/proc/") if err != nil { - return nil, err + return false, err } 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 { cmdline, err := os.ReadFile("/proc/" + entry.Name() + "/cmdline") if err != nil { - return nil, err + return false, err } if strings.Contains(string(cmdline), name) { - pid, err := strconv.Atoi(entry.Name()) - if err != nil { - continue - } - pids = append(pids, pid) + return true, nil } } } - return pids, nil + return false, nil }