Debian Codegraph @

psnotify/psnotify.go

raw symbols SCIP
1 // +build darwin freebsd linux netbsd openbsd
2
3 package psnotify
4
5 import (
6 "errors"
7 "fmt"
8 "sync"
9 )
10
11 type ProcEventFork struct {
12 ParentPid int // Pid of the process that called fork()
13 ChildPid int // Child process pid created by fork()
14 }
15
16 type ProcEventExec struct {
17 Pid int // Pid of the process that called exec()
18 }
19
20 type ProcEventExit struct {
21 Pid int // Pid of the process that called exit()
22 }
23
24 type watch struct {
25 flags uint32 // Saved value of Watch() flags param
26 }
27
28 type eventListener interface {
29 close() error // Watch.Close() closes the OS specific listener
30 }
31
32 type Watcher struct {
33 listener eventListener // OS specifics (kqueue or netlink)
34 watches map[int]*watch // Map of watched process ids
35 watchesMutex *sync.Mutex
36
37 Error chan error // Errors are sent on this channel
38 Fork chan *ProcEventFork // Fork events are sent on this channel
39 Exec chan *ProcEventExec // Exec events are sent on this channel
40 Exit chan *ProcEventExit // Exit events are sent on this channel
41 done chan bool // Used to stop the readEvents() goroutine
42
43 isClosed bool // Set to true when Close() is first called
44 closedMutex *sync.Mutex
45 }
46
47 // Initialize event listener and channels
48 func NewWatcher() (*Watcher, error) {
49 listener, err := createListener()
50
51 if err != nil {
52 return nil, err
53 }
54
55 w := &Watcher{
56 listener: listener,
57 watches: make(map[int]*watch),
58 watchesMutex: &sync.Mutex{},
59 Fork: make(chan *ProcEventFork),
60 Exec: make(chan *ProcEventExec),
61 Exit: make(chan *ProcEventExit),
62 Error: make(chan error),
63 done: make(chan bool, 1),
64 closedMutex: &sync.Mutex{},
65 }
66
67 go w.readEvents()
68 return w, nil
69 }
70
71 // Close event channels when done message is received
72 func (w *Watcher) finish() {
73 close(w.Fork)
74 close(w.Exec)
75 close(w.Exit)
76 close(w.Error)
77 }
78
79 // Closes the OS specific event listener,
80 // removes all watches and closes all event channels.
81 func (w *Watcher) Close() error {
82 w.closedMutex.Lock()
83 defer w.closedMutex.Unlock()
84
85 if w.isClosed {
86 return nil
87 }
88 w.isClosed = true
89
90 w.watchesMutex.Lock()
91 for pid := range w.watches {
92 delete(w.watches, pid)
93 w.unregister(pid)
94 }
95 w.watchesMutex.Unlock()
96
97 w.done <- true
98
99 w.listener.close()
100
101 return nil
102 }
103
104 // Add pid to the watched process set.
105 // The flags param is a bitmask of process events to capture,
106 // must be one or more of: PROC_EVENT_FORK, PROC_EVENT_EXEC, PROC_EVENT_EXIT
107 func (w *Watcher) Watch(pid int, flags uint32) error {
108 w.closedMutex.Lock()
109 closed := w.isClosed
110 w.closedMutex.Unlock()
111
112 if closed {
113 return errors.New("psnotify watcher is closed")
114 }
115
116 watchEntry, found := w.watches[pid]
117
118 if found {
119 watchEntry.flags |= flags
120 } else {
121 if err := w.register(pid, flags); err != nil {
122 return err
123 }
124
125 w.watchesMutex.Lock()
126 w.watches[pid] = &watch{flags: flags}
127 w.watchesMutex.Unlock()
128 }
129
130 return nil
131 }
132
133 // Remove pid from the watched process set.
134 func (w *Watcher) RemoveWatch(pid int) error {
135 w.watchesMutex.Lock()
136 defer w.watchesMutex.Unlock()
137
138 _, ok := w.watches[pid]
139 if !ok {
140 msg := fmt.Sprintf("watch for pid=%d does not exist", pid)
141 return errors.New(msg)
142 }
143 delete(w.watches, pid)
144 return w.unregister(pid)
145 }
146
147 // Internal helper to check if there is a message on the "done" channel.
148 // The "done" message is sent by the Close() method; when received here,
149 // the Watcher.finish method is called to close all channels and return
150 // true - in which case the caller should break from the readEvents loop.
151 func (w *Watcher) isDone() bool {
152 var done bool
153 select {
154 case done = <-w.done:
155 w.finish()
156 default:
157 }
158 return done
159 }