/
main.go
67 lines (60 loc) · 1.46 KB
/
main.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
64
65
66
67
package main
import(
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"
)
// something that does nothing but wait for a signal to kill
// for testing and debugging
func main() {
fmt.Printf("me pid is: %v\nwating for signal to print and SIGINT to quit!\n", os.Getpid())
handleSignals()
}
func handleSignals() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel)
printStack, refuseExit := 0, false
infinite:
for {
select {
case signal := <-signalChannel:
fmt.Printf("got signal %v. ", signal)
switch signal {
case syscall.SIGKILL:
fmt.Println("murderer!")
case syscall.SIGABRT:
if printStack < 2 {
printStack++
fmt.Println("ignoring!")
}
case syscall.SIGTSTP:
fmt.Println("waiting for SIGCONT!")
case syscall.SIGTERM:
fmt.Println("exiting only on SIGINT!")
case syscall.SIGUSR1:
refuseExit = true // too cosy!
fmt.Println("it's nice and cosy here! i think i'll stay!")
case syscall.SIGUSR2:
if refuseExit {
refuseExit = false // not so cosy anymore!
fmt.Println("pretty pretty pretty please don't do what i think you are about to do!")
} else {
fmt.Println("it's been revoked!")
}
case syscall.SIGINT:
if refuseExit { fmt.Println(); continue }
fmt.Println("exiting.")
os.Exit(0)
break infinite
default:
fmt.Println()
}
if printStack == 2 {
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
printStack = 0
}
}
}
}