2014-08-05 16:32:24 +00:00
|
|
|
package signal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
gosignal "os/signal"
|
2015-04-21 04:24:24 +00:00
|
|
|
"runtime"
|
2014-08-05 16:32:24 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2014-09-25 16:55:53 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-05 16:32:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Trap sets up a simplified signal "trap", appropriate for common
|
|
|
|
// behavior expected from a vanilla unix command-line tool in general
|
|
|
|
// (and the Docker engine in particular).
|
|
|
|
//
|
|
|
|
// * If SIGINT or SIGTERM are received, `cleanup` is called, then the process is terminated.
|
2015-04-21 04:24:24 +00:00
|
|
|
// * If SIGINT or SIGTERM are received 3 times before cleanup is complete, then cleanup is
|
|
|
|
// skipped and the process is terminated immediately (allows force quit of stuck daemon)
|
|
|
|
// * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit.
|
2014-08-05 16:32:24 +00:00
|
|
|
//
|
|
|
|
func Trap(cleanup func()) {
|
|
|
|
c := make(chan os.Signal, 1)
|
2015-04-21 04:24:24 +00:00
|
|
|
// we will handle INT, TERM, QUIT here
|
|
|
|
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT}
|
2014-08-05 16:32:24 +00:00
|
|
|
gosignal.Notify(c, signals...)
|
|
|
|
go func() {
|
|
|
|
interruptCount := uint32(0)
|
|
|
|
for sig := range c {
|
|
|
|
go func(sig os.Signal) {
|
2015-04-21 04:24:24 +00:00
|
|
|
logrus.Infof("Processing signal '%v'", sig)
|
2014-08-05 16:32:24 +00:00
|
|
|
switch sig {
|
|
|
|
case os.Interrupt, syscall.SIGTERM:
|
|
|
|
if atomic.LoadUint32(&interruptCount) < 3 {
|
|
|
|
// Initiate the cleanup only once
|
2014-12-06 14:42:32 +00:00
|
|
|
if atomic.AddUint32(&interruptCount, 1) == 1 {
|
2015-04-21 04:24:24 +00:00
|
|
|
// Call the provided cleanup handler
|
2014-08-05 16:32:24 +00:00
|
|
|
cleanup()
|
2014-08-13 19:18:23 +00:00
|
|
|
os.Exit(0)
|
2014-08-05 16:32:24 +00:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-21 04:24:24 +00:00
|
|
|
// 3 SIGTERM/INT signals received; force exit without cleanup
|
|
|
|
logrus.Infof("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
|
2014-08-05 16:32:24 +00:00
|
|
|
}
|
|
|
|
case syscall.SIGQUIT:
|
2015-04-21 04:24:24 +00:00
|
|
|
DumpStacks()
|
|
|
|
logrus.Infof("Forcing docker daemon shutdown without cleanup on SIGQUIT")
|
2014-08-05 16:32:24 +00:00
|
|
|
}
|
2015-04-21 04:24:24 +00:00
|
|
|
//for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
|
2014-08-05 16:32:24 +00:00
|
|
|
os.Exit(128 + int(sig.(syscall.Signal)))
|
|
|
|
}(sig)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2015-04-21 04:24:24 +00:00
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// DumpStacks dumps the runtime stack.
|
2015-04-21 04:24:24 +00:00
|
|
|
func DumpStacks() {
|
|
|
|
buf := make([]byte, 16384)
|
|
|
|
buf = buf[:runtime.Stack(buf, true)]
|
|
|
|
// Note that if the daemon is started with a less-verbose log-level than "info" (the default), the goroutine
|
|
|
|
// traces won't show up in the log.
|
|
|
|
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
|
|
|
|
}
|