forked from awnumar/memguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.go
63 lines (51 loc) · 1.56 KB
/
signals.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
package memguard
import (
"os"
"os/signal"
"sync"
"github.com/awnumar/memguard/core"
)
var (
// Ensure we only start a single signal handling instance
create sync.Once
// Channel for updating the signal handler
sigfunc = make(chan func(os.Signal), 1)
// Channel that caught signals are sent to by the runtime
listener = make(chan os.Signal, 4)
)
/*
CatchSignal assigns a given function to be run in the event of a signal being received by the process. If no signals are provided all signals will be caught.
1. Signal is caught by the process
2. Interrupt handler is executed
3. Secure session state is wiped
4. Process terminates with exit code 1
This function can be called multiple times with the effect that only the last call will have any effect.
*/
func CatchSignal(f func(os.Signal), signals ...os.Signal) {
create.Do(func() {
// Start a goroutine to listen on the channels.
go func() {
var handler func(os.Signal)
for {
select {
case signal := <-listener:
handler(signal)
core.Exit(1)
case handler = <-sigfunc:
}
}
}()
})
// Update the handler function.
sigfunc <- f
// Notify the channel if we receive a signal.
signal.Reset()
signal.Notify(listener, signals...)
}
/*
CatchInterrupt is a wrapper around CatchSignal that makes it easy to safely handle receiving interrupt signals. If an interrupt is received, the process will wipe sensitive data in memory before terminating.
A subsequent call to CatchSignal will override this call.
*/
func CatchInterrupt() {
CatchSignal(func(_ os.Signal) {}, os.Interrupt)
}