| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package proc
- import (
- "bytes"
- "fmt"
- "os"
- "path"
- "strconv"
- "strings"
- "github.com/coroot/coroot-node-agent/cgroup"
- )
- var root = "/proc"
- func Path(pid uint32, subpath ...string) string {
- return path.Join(append([]string{root, strconv.Itoa(int(pid))}, subpath...)...)
- }
- func HostPath(p string) string {
- return Path(1, "root", p)
- }
- func GetCmdline(pid uint32) []byte {
- cmdline, err := os.ReadFile(Path(pid, "cmdline"))
- if err != nil {
- return nil
- }
- return bytes.TrimSuffix(cmdline, []byte{0})
- }
- func GetRealCmdline(pid uint32) []byte {
- cmdline, err := os.ReadFile(Path(pid, "cmdline"))
- if err != nil {
- return nil
- }
- return bytes.ReplaceAll(cmdline, []byte("\x00"), []byte(" "))
- }
- func GetExe(pid uint32) []byte {
- exe, err := os.ReadFile(Path(pid, "exe"))
- if err != nil {
- fmt.Printf("Error reading executable path for PID %d: %v\n", pid, err)
- return nil
- }
- return bytes.TrimSuffix(exe, []byte{0})
- }
- func GetNsPid(pid uint32) uint32 {
- data, err := os.ReadFile(Path(pid, "status"))
- if err != nil {
- return pid
- }
- for _, line := range strings.Split(string(data), "\n") {
- fields := strings.Fields(line)
- if len(fields) < 2 {
- continue
- }
- if fields[0] == "NSpid:" {
- if len(fields) == 3 {
- if nsPid, err := strconv.ParseUint(fields[2], 10, 32); err == nil {
- return uint32(nsPid)
- }
- }
- return pid
- }
- }
- return pid
- }
- func ReadCgroup(pid uint32) (*cgroup.Cgroup, error) {
- return cgroup.NewFromProcessCgroupFile(Path(pid, "cgroup"))
- }
- func ListPids() ([]uint32, error) {
- root, err := os.Open(root)
- if err != nil {
- return nil, err
- }
- defer root.Close()
- dirs, err := root.Readdirnames(0)
- if err != nil {
- return nil, err
- }
- res := make([]uint32, 0, len(dirs))
- for _, dir := range dirs {
- pid64, err := strconv.ParseUint(dir, 10, 32)
- if err != nil {
- continue
- }
- res = append(res, uint32(pid64))
- }
- return res, nil
- }
- func GetProcName(pid uint32) string {
- comm, err := os.ReadFile(Path(pid, "comm"))
- if err != nil {
- return ""
- }
- return string(bytes.TrimRight(comm, "\n"))
- }
|