-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_freebsdlike.go
49 lines (40 loc) · 1.08 KB
/
process_freebsdlike.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
// 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
package ps
import (
"bytes"
"time"
"golang.org/x/sys/unix"
)
func (kp *kinfoProc) CreationTime() time.Time {
return time.Unix(kp.Start.Sec, int64(kp.Start.Usec)*1000)
}
func getExePathAndArgs(pid int) (string, []string) {
procArgs, err := unix.SysctlRaw("kern.proc.args", pid)
if err != nil {
return "", nil
}
return parseProcArgs(procArgs)
}
func parseProcArgs(procArgs []byte) (string, []string) {
if len(procArgs) == 0 {
return "", nil
}
if procArgs[len(procArgs)-1] == 0 {
procArgs = procArgs[:len(procArgs)-1]
}
procArgsSlice := bytes.Split(procArgs, []byte{0})
args := make([]string, 0, len(procArgsSlice))
for _, pa := range procArgsSlice {
args = append(args, string(pa))
}
return args[0], args
}
func sysctlProcAll() ([]byte, error) {
return unix.SysctlRaw("kern.proc.all")
}
func sysctlProcPID(pid int) ([]byte, error) {
return unix.SysctlRaw("kern.proc.pid", pid)
}