forked from mitchellh/go-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_darwin.go
72 lines (56 loc) · 1.13 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
67
68
69
70
71
72
// +build darwin
package ps
// #include "process_darwin.h"
import "C"
import (
"sync"
)
// This lock is what verifies that C calling back into Go is only
// modifying data once at a time.
var darwinLock sync.Mutex
var darwinProcs []Process
type DarwinProcess struct {
pid int
ppid int
binary string
}
func (p *DarwinProcess) Pid() int {
return p.pid
}
func (p *DarwinProcess) PPid() int {
return p.ppid
}
func (p *DarwinProcess) Executable() string {
return p.binary
}
//export go_darwin_append_proc
func go_darwin_append_proc(pid C.pid_t, ppid C.pid_t, comm *C.char) {
proc := &DarwinProcess{
pid: int(pid),
ppid: int(ppid),
binary: C.GoString(comm),
}
darwinProcs = append(darwinProcs, proc)
}
func findProcess(pid int) (Process, error) {
ps, err := processes()
if err != nil {
return nil, err
}
for _, p := range ps {
if p.Pid() == pid {
return p, nil
}
}
return nil, nil
}
func processes() ([]Process, error) {
darwinLock.Lock()
defer darwinLock.Unlock()
darwinProcs = make([]Process, 0, 50)
_, err := C.darwinProcesses()
if err != nil {
return nil, err
}
return darwinProcs, nil
}