-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_procfs_stat.go
68 lines (58 loc) · 1.74 KB
/
process_procfs_stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2021 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux || netbsd
package ps
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/sys/unix"
)
func newUnixProcess(pid int) (Process, error) {
procDir := filepath.Join("/proc", strconv.Itoa(pid))
var st unix.Stat_t
if err := unix.Lstat(procDir, &st); err == unix.ENOENT {
return nil, fmt.Errorf("no process found with PID %d: %w", pid, err)
} else if err != nil {
return nil, fmt.Errorf("failed to lstat %s: %w", procDir, err)
}
procStat := filepath.Join(procDir, "stat")
b, err := os.ReadFile(procStat)
if err != nil {
return nil, err
}
// see proc(5) section on /proc/[pid]/stat for the description of the
// format
commStart := bytes.IndexByte(b, '(')
commEnd := bytes.LastIndexByte(b[commStart:], ')') + commStart
comm := string(b[commStart+1 : commEnd])
fields := bytes.Fields(b[commEnd+2:]) // +2 for '( '
if len(fields) < 2 {
return nil, fmt.Errorf("invalid process status format in %s", procStat)
}
ppid, err := strconv.Atoi(string(fields[1]))
if err != nil {
return nil, fmt.Errorf("invalid ppid format %s: %w", fields[1], err)
}
p := &unixProcess{
pid: pid,
ppid: ppid,
uid: int(st.Uid),
gid: int(st.Gid),
command: comm,
creationTime: time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec)),
}
p.executablePath, _ = filepath.EvalSymlinks(filepath.Join(procDir, "exe"))
b, err = os.ReadFile(filepath.Join(procDir, "cmdline"))
if err == nil && len(b) > 0 {
p.executableArgs = strings.FieldsFunc(string(b), func(r rune) bool {
return r == '\u0000'
})
}
return p, nil
}