36b3af2e3d
PR Check / Code Quality: Format (push) Failing after 1s
PR Check / Code Quality: Lint (darwin) (push) Failing after 0s
PR Check / Code Quality: Lint (freebsd) (push) Failing after 1s
PR Check / Code Quality: Lint (windows) (push) Failing after 1s
PR Check / Code Quality: Lint (linux) (push) Failing after 1s
PR Check / Security: Vulnerability Scan (push) Failing after 0s
Update Documentation / update-docs (push) Failing after 2s
PR Check / Code Quality: Vendor (push) Failing after 1s
PR Check / Code Quality: Coverage (push) Failing after 0s
PR Check / Tests: Unit (macos-latest) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04-arm) (push) Has been cancelled
PR Check / Tests: Unit (windows-latest) (push) Has been cancelled
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
//go:build darwin || freebsd
|
|
|
|
package proc
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func buildEnvForPS() []string {
|
|
var env []string
|
|
for _, e := range os.Environ() {
|
|
if !strings.HasPrefix(e, "LC_ALL=") && !strings.HasPrefix(e, "TZ=") {
|
|
env = append(env, e)
|
|
}
|
|
}
|
|
env = append(env, "LC_ALL=C", "TZ=UTC")
|
|
return env
|
|
}
|
|
|
|
// readPIDCommMap returns a pid->comm map produced by a single `ps -axo pid=,comm=`
|
|
// invocation. On macOS comm holds the full executable path (spaces included);
|
|
// on FreeBSD it holds the short command name. Either way the value is the
|
|
// authoritative source for the display name and is parsed by splitting on the
|
|
// first whitespace only, so values containing spaces survive intact.
|
|
func readPIDCommMap() map[int]string {
|
|
cmd := exec.Command("ps", "-axo", "pid=,comm=")
|
|
cmd.Env = buildEnvForPS()
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
m := make(map[int]string)
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
trimmed := strings.TrimLeft(line, " \t")
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
idx := strings.IndexAny(trimmed, " \t")
|
|
if idx < 0 {
|
|
continue
|
|
}
|
|
pid, err := strconv.Atoi(trimmed[:idx])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
comm := strings.TrimSpace(trimmed[idx+1:])
|
|
if comm != "" {
|
|
m[pid] = comm
|
|
}
|
|
}
|
|
return m
|
|
}
|