| 1 |
|
| 2 |
|
| 3 |
package psnotify |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"sync" |
| 9 |
) |
| 10 |
|
| 11 |
type ProcEventFork struct { |
| 12 |
ParentPid int |
| 13 |
ChildPid int |
| 14 |
} |
| 15 |
|
| 16 |
type ProcEventExec struct { |
| 17 |
Pid int |
| 18 |
} |
| 19 |
|
| 20 |
type ProcEventExit struct { |
| 21 |
Pid int |
| 22 |
} |
| 23 |
|
| 24 |
type watch struct { |
| 25 |
flags uint32 |
| 26 |
} |
| 27 |
|
| 28 |
type eventListener interface { |
| 29 |
close() error |
| 30 |
} |
| 31 |
|
| 32 |
type Watcher struct { |
| 33 |
listener eventListener |
| 34 |
watches map[int]*watch |
| 35 |
watchesMutex *sync.Mutex |
| 36 |
|
| 37 |
Error chan error |
| 38 |
Fork chan *ProcEventFork |
| 39 |
Exec chan *ProcEventExec |
| 40 |
Exit chan *ProcEventExit |
| 41 |
done chan bool |
| 42 |
|
| 43 |
isClosed bool |
| 44 |
closedMutex *sync.Mutex |
| 45 |
} |
| 46 |
|
| 47 |
|
| 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 |
|
| 72 |
func (w *Watcher) finish() { |
| 73 |
close(w.Fork) |
| 74 |
close(w.Exec) |
| 75 |
close(w.Exit) |
| 76 |
close(w.Error) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
} |