-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_darwin.go
66 lines (52 loc) · 1.03 KB
/
process_darwin.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
package main
import (
"bytes"
"syscall"
"unsafe"
)
// macOS syscalls
const (
procIinfoCallLlistPIDs = 0x01
procListPIDs = 1
procIinfoCallPIDInfo = 0x02
procPIDPathInfo = 11
)
const pidsBufLen = 1024 * 1024
func processes() ([]uint32, error) {
out := make([]uint32, pidsBufLen, pidsBufLen)
_, _, errno := syscall.Syscall6(
syscall.SYS_PROC_INFO,
procIinfoCallLlistPIDs,
procListPIDs,
0,
0,
uintptr(unsafe.Pointer(&out[0])),
pidsBufLen*(unsafe.Sizeof(out[0])))
if errno != 0 {
return nil, errno
}
return out, nil
}
func cString(s []byte) string {
i := bytes.IndexByte(s, 0)
if i != -1 {
return string(s[:i])
}
return string(s)
}
const maxPathSize = 4096
func processPath(pid uint32) (string, error) {
buffer := make([]byte, maxPathSize)
_, _, errno := syscall.Syscall6(
syscall.SYS_PROC_INFO,
procIinfoCallPIDInfo,
uintptr(pid),
procPIDPathInfo,
0,
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(maxPathSize))
if errno != 0 {
return "", errno
}
return cString(buffer), nil
}