Files
at-container-registry/cmd/credential-helper/process_linux.go

43 lines
973 B
Go

package main
import (
"fmt"
"os"
"strconv"
"strings"
)
// getProcessArgs reads /proc/<pid>/cmdline to get process arguments.
func getProcessArgs(pid int) ([]string, error) {
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return nil, fmt.Errorf("reading /proc/%d/cmdline: %w", pid, err)
}
s := strings.TrimRight(string(data), "\x00")
if s == "" {
return nil, fmt.Errorf("empty cmdline for pid %d", pid)
}
return strings.Split(s, "\x00"), nil
}
// getParentPID reads /proc/<pid>/status to find the parent PID.
func getParentPID(pid int) (int, error) {
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return 0, err
}
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "PPid:") {
fields := strings.Fields(line)
if len(fields) >= 2 {
return strconv.Atoi(fields[1])
}
}
}
return 0, fmt.Errorf("PPid not found in /proc/%d/status", pid)
}