-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_watcher.go
150 lines (135 loc) · 2.89 KB
/
process_watcher.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package processwatcher
import (
"fmt"
"os"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
type cb_id struct {
idx uint32
val uint32
}
type cn_msg struct {
id cb_id
seq uint32
ack uint32
len uint16
flags uint16
data [1]uint8
}
const (
CN_IDX_PROC = 1
CN_VAL_PROC = 1
)
type Watcher interface {
Start() error
Stop()
Events() <-chan WatchEvent
}
type processWatcher struct {
sock int
events chan WatchEvent
stop chan struct{}
}
func NewProcessWatcher() Watcher {
return &processWatcher{
events: make(chan WatchEvent),
stop: make(chan struct{}),
}
}
func (pw *processWatcher) Stop() {
syscall.Close(pw.sock)
close(pw.stop)
close(pw.events)
}
func (pw *processWatcher) Events() <-chan WatchEvent {
return pw.events
}
func (pw *processWatcher) sendEvents(e WatchEvent) {
select {
case <-pw.stop:
return
default:
}
pw.events <- e
}
type msg_data struct {
cn_msg
cn_mcast uint32
}
type processMonitorMsg struct {
nl_hdr syscall.NlMsghdr
data msg_data
}
func (pw *processWatcher) Start() error {
sock, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, syscall.NETLINK_CONNECTOR)
if err != nil {
return fmt.Errorf("create netlink socket error: %v", err)
}
addr := &syscall.SockaddrNetlink{Family: syscall.AF_NETLINK, Pid: uint32(os.Getpid()), Groups: 1}
if err := syscall.Bind(sock, addr); err != nil {
return fmt.Errorf("bind addr %+v to sock error: %v", addr, err)
}
headerSize := uint64(unsafe.Sizeof(syscall.NlMsghdr{}))
msgSize := uint64(unsafe.Sizeof(cn_msg{}))
mm := processMonitorMsg{
nl_hdr: syscall.NlMsghdr{
Len: uint32(unsafe.Sizeof(processMonitorMsg{})),
Type: syscall.NLMSG_DONE,
Flags: 0,
Pid: uint32(os.Getegid()),
},
data: msg_data{
cn_msg: cn_msg{
id: cb_id{
idx: CN_IDX_PROC,
val: CN_VAL_PROC,
},
len: 4,
},
cn_mcast: 1,
},
}
type slice struct {
p uintptr
len uint64
cap uint64
}
x := slice{
p: uintptr(unsafe.Pointer(&mm)),
len: uint64(unsafe.Sizeof(processMonitorMsg{})),
cap: uint64(unsafe.Sizeof(processMonitorMsg{})),
}
mmm := *(*[]byte)(unsafe.Pointer(&x))
err = unix.Send(sock, mmm, 0)
if err != nil {
return fmt.Errorf("send msg %+v to sock error: %v", mm, err)
}
pw.sock = sock
go func() {
for {
select {
case <-pw.stop:
return
default:
}
bs := make([]byte, 1024)
_, _, err := syscall.Recvfrom(sock, bs, 0)
if err != nil {
pw.sendEvents(WatchEvent{Err: err})
return
}
h := (*syscall.NlMsghdr)(unsafe.Pointer(&bs[0]))
minDataLen := uint32(headerSize) + uint32(msgSize) + uint32(unsafe.Sizeof(ProcEventHeader{}))
if h.Len < minDataLen {
pw.sendEvents(WatchEvent{Err: fmt.Errorf("data len %d is lower than required", h.Len)})
continue
}
msg := (*cn_msg)(unsafe.Pointer(&bs[headerSize]))
pe := ProcEvent{ptr: unsafe.Pointer(&msg.data[0])}
pw.sendEvents(WatchEvent{ProcEvent: pe})
}
}()
return nil
}