Debian Codegraph @

psnotify/psnotify_linux.go

raw symbols SCIP
1 // Go interface to the Linux netlink process connector.
2 // See Documentation/connector/connector.txt in the linux kernel source tree.
3 package psnotify
4
5 import (
6 "bytes"
7 "encoding/binary"
8 "os"
9 "syscall"
10 )
11
12 const (
13 // internal flags (from <linux/connector.h>)
14 _CN_IDX_PROC = 0x1
15 _CN_VAL_PROC = 0x1
16
17 // internal flags (from <linux/cn_proc.h>)
18 _PROC_CN_MCAST_LISTEN = 1
19 _PROC_CN_MCAST_IGNORE = 2
20
21 // Flags (from <linux/cn_proc.h>)
22 PROC_EVENT_FORK = 0x00000001 // fork() events
23 PROC_EVENT_EXEC = 0x00000002 // exec() events
24 PROC_EVENT_EXIT = 0x80000000 // exit() events
25
26 // Watch for all process events
27 PROC_EVENT_ALL = PROC_EVENT_FORK | PROC_EVENT_EXEC | PROC_EVENT_EXIT
28 )
29
30 var (
31 byteOrder = binary.LittleEndian
32 )
33
34 // linux/connector.h: struct cb_id
35 type cbId struct {
36 Idx uint32
37 Val uint32
38 }
39
40 // linux/connector.h: struct cb_msg
41 type cnMsg struct {
42 Id cbId
43 Seq uint32
44 Ack uint32
45 Len uint16
46 Flags uint16
47 }
48
49 // linux/cn_proc.h: struct proc_event.{what,cpu,timestamp_ns}
50 type procEventHeader struct {
51 What uint32
52 Cpu uint32
53 Timestamp uint64
54 }
55
56 // linux/cn_proc.h: struct proc_event.fork
57 type forkProcEvent struct {
58 ParentPid uint32
59 ParentTgid uint32
60 ChildPid uint32
61 ChildTgid uint32
62 }
63
64 // linux/cn_proc.h: struct proc_event.exec
65 type execProcEvent struct {
66 ProcessPid uint32
67 ProcessTgid uint32
68 }
69
70 // linux/cn_proc.h: struct proc_event.exit
71 type exitProcEvent struct {
72 ProcessPid uint32
73 ProcessTgid uint32
74 ExitCode uint32
75 ExitSignal uint32
76 }
77
78 // standard netlink header + connector header
79 type netlinkProcMessage struct {
80 Header syscall.NlMsghdr
81 Data cnMsg
82 }
83
84 type netlinkListener struct {
85 addr *syscall.SockaddrNetlink // Netlink socket address
86 sock int // The syscall.Socket() file descriptor
87 seq uint32 // struct cn_msg.seq
88 }
89
90 // Initialize linux implementation of the eventListener interface
91 func createListener() (eventListener, error) {
92 listener := &netlinkListener{}
93 err := listener.bind()
94 return listener, err
95 }
96
97 // noop on linux
98 func (w *Watcher) unregister(pid int) error {
99 return nil
100 }
101
102 // noop on linux
103 func (w *Watcher) register(pid int, flags uint32) error {
104 return nil
105 }
106
107 // Read events from the netlink socket
108 func (w *Watcher) readEvents() {
109 buf := make([]byte, syscall.Getpagesize())
110
111 listener, _ := w.listener.(*netlinkListener)
112
113 for {
114 if w.isDone() {
115 return
116 }
117
118 nr, _, err := syscall.Recvfrom(listener.sock, buf, 0)
119
120 if err != nil {
121 w.Error <- err
122 continue
123 }
124 if nr < syscall.NLMSG_HDRLEN {
125 w.Error <- syscall.EINVAL
126 continue
127 }
128
129 msgs, _ := syscall.ParseNetlinkMessage(buf[:nr])
130
131 for _, m := range msgs {
132 if m.Header.Type == syscall.NLMSG_DONE {
133 w.handleEvent(m.Data)
134 }
135 }
136 }
137 }
138
139 // Internal helper to check if pid && event is being watched
140 func (w *Watcher) isWatching(pid int, event uint32) bool {
141 w.watchesMutex.Lock()
142 defer w.watchesMutex.Unlock()
143
144 if watch, ok := w.watches[pid]; ok {
145 return (watch.flags & event) == event
146 }
147 return false
148 }
149
150 // Dispatch events from the netlink socket to the Event channels.
151 // Unlike bsd kqueue, netlink receives events for all pids,
152 // so we apply filtering based on the watch table via isWatching()
153 func (w *Watcher) handleEvent(data []byte) {
154 buf := bytes.NewBuffer(data)
155 msg := &cnMsg{}
156 hdr := &procEventHeader{}
157
158 binary.Read(buf, byteOrder, msg)
159 binary.Read(buf, byteOrder, hdr)
160
161 switch hdr.What {
162 case PROC_EVENT_FORK:
163 event := &forkProcEvent{}
164 binary.Read(buf, byteOrder, event)
165 ppid := int(event.ParentTgid)
166 pid := int(event.ChildTgid)
167
168 if w.isWatching(ppid, PROC_EVENT_EXEC) {
169 // follow forks
170 watch, _ := w.watches[ppid]
171 w.Watch(pid, watch.flags)
172 }
173
174 if w.isWatching(ppid, PROC_EVENT_FORK) {
175 w.Fork <- &ProcEventFork{ParentPid: ppid, ChildPid: pid}
176 }
177 case PROC_EVENT_EXEC:
178 event := &execProcEvent{}
179 binary.Read(buf, byteOrder, event)
180 pid := int(event.ProcessTgid)
181
182 if w.isWatching(pid, PROC_EVENT_EXEC) {
183 w.Exec <- &ProcEventExec{Pid: pid}
184 }
185 case PROC_EVENT_EXIT:
186 event := &exitProcEvent{}
187 binary.Read(buf, byteOrder, event)
188 pid := int(event.ProcessTgid)
189
190 if w.isWatching(pid, PROC_EVENT_EXIT) {
191 w.RemoveWatch(pid)
192 w.Exit <- &ProcEventExit{Pid: pid}
193 }
194 }
195 }
196
197 // Bind our netlink socket and
198 // send a listen control message to the connector driver.
199 func (listener *netlinkListener) bind() error {
200 sock, err := syscall.Socket(
201 syscall.AF_NETLINK,
202 syscall.SOCK_DGRAM,
203 syscall.NETLINK_CONNECTOR)
204
205 if err != nil {
206 return err
207 }
208
209 listener.sock = sock
210 listener.addr = &syscall.SockaddrNetlink{
211 Family: syscall.AF_NETLINK,
212 Groups: _CN_IDX_PROC,
213 }
214
215 err = syscall.Bind(listener.sock, listener.addr)
216
217 if err != nil {
218 return err
219 }
220
221 return listener.send(_PROC_CN_MCAST_LISTEN)
222 }
223
224 // Send an ignore control message to the connector driver
225 // and close our netlink socket.
226 func (listener *netlinkListener) close() error {
227 err := listener.send(_PROC_CN_MCAST_IGNORE)
228 syscall.Close(listener.sock)
229 return err
230 }
231
232 // Generic method for sending control messages to the connector
233 // driver; where op is one of PROC_CN_MCAST_{LISTEN,IGNORE}
234 func (listener *netlinkListener) send(op uint32) error {
235 listener.seq++
236 pr := &netlinkProcMessage{}
237 plen := binary.Size(pr.Data) + binary.Size(op)
238 pr.Header.Len = syscall.NLMSG_HDRLEN + uint32(plen)
239 pr.Header.Type = uint16(syscall.NLMSG_DONE)
240 pr.Header.Flags = 0
241 pr.Header.Seq = listener.seq
242 pr.Header.Pid = uint32(os.Getpid())
243
244 pr.Data.Id.Idx = _CN_IDX_PROC
245 pr.Data.Id.Val = _CN_VAL_PROC
246
247 pr.Data.Len = uint16(binary.Size(op))
248
249 buf := bytes.NewBuffer(make([]byte, 0, pr.Header.Len))
250 binary.Write(buf, byteOrder, pr)
251 binary.Write(buf, byteOrder, op)
252
253 return syscall.Sendto(listener.sock, buf.Bytes(), 0, listener.addr)
254 }