-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_bsd.go
57 lines (48 loc) · 1.41 KB
/
process_bsd.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
// 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 dragonfly || freebsd || openbsd
package ps
import (
"fmt"
"unsafe"
"golang.org/x/sys/unix"
)
func newUnixProcess(kp *kinfoProc) *unixProcess {
pid := int(kp.Pid)
exePath, exeArgs := getExePathAndArgs(pid)
return &unixProcess{
pid: pid,
ppid: int(kp.Ppid),
uid: int(kp.Uid),
gid: int(kp.Groups[0]),
command: unix.ByteSliceToString(kp.Comm[:]),
executablePath: exePath,
executableArgs: exeArgs,
creationTime: kp.CreationTime(),
}
}
func processes() ([]Process, error) {
b, err := sysctlProcAll()
if err != nil {
return nil, fmt.Errorf("failed to list processes: %w", err)
}
n := len(b) / sizeofKinfoProc
procs := make([]Process, 0, n)
for i := 0; i < n; i++ {
kp := (*kinfoProc)(unsafe.Pointer(&b[i*sizeofKinfoProc : (i+1)*sizeofKinfoProc][0]))
procs = append(procs, newUnixProcess(kp))
}
return procs, nil
}
func findProcess(pid int) (Process, error) {
b, err := sysctlProcPID(pid)
if err != nil {
return nil, fmt.Errorf("no process found with PID %d: %w", pid, err)
}
if len(b) < sizeofKinfoProc {
return nil, fmt.Errorf("failed to get process information for PID %d", pid)
}
kp := (*kinfoProc)(unsafe.Pointer(&b[:sizeofKinfoProc][0]))
return newUnixProcess(kp), nil
}