-
Notifications
You must be signed in to change notification settings - Fork 1
/
signlhndlr_linux.go
56 lines (47 loc) · 1.3 KB
/
signlhndlr_linux.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
// Copyright © 2020 Circonus, Inc. <[email protected]>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// +build linux
// Signal handling for Linux
// doesn't have SIGINFO, using SIGTRAP instead
package main
import (
"fmt"
"log"
"os"
"os/signal"
"runtime"
"github.com/alecthomas/units"
"golang.org/x/sys/unix"
)
//signalNotifySetup sets up the signals and their channel
func (ac *AgentController) signalNotifySetup() {
signal.Notify(ac.sig, os.Interrupt, unix.SIGTERM, unix.SIGHUP, unix.SIGPIPE, unix.SIGTRAP)
}
//handleSignals handles exiting the program based on different signals
func (ac *AgentController) handleSignals() {
const stackTraceBufferSize = 1 * units.MiB
//pre-allocate a buffer for stacktrace
buf := make([]byte, stackTraceBufferSize)
for {
select {
case sig := <-ac.sig:
log.Printf("signal %s received\n", sig.String())
switch sig {
case os.Interrupt, unix.SIGTERM:
ac.cncl()
return
case unix.SIGPIPE, unix.SIGHUP:
// Noop
case unix.SIGTRAP:
stackLen := runtime.Stack(buf, true)
fmt.Printf("=== received SIGINFO ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stackLen])
default:
log.Printf("signal %s unsupported", sig.String())
}
case <-ac.ctx.Done():
return
}
}
}