2018-02-05 16:05:59 -05:00
|
|
|
package signal // import "github.com/docker/docker/pkg/signal"
|
2014-08-05 12:32:24 -04:00
|
|
|
|
|
|
|
import (
|
2016-11-01 10:32:55 -04:00
|
|
|
"fmt"
|
2014-08-05 12:32:24 -04:00
|
|
|
"os"
|
|
|
|
gosignal "os/signal"
|
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2014-09-25 12:55:53 -04:00
|
|
|
|
2021-07-15 11:33:55 -04:00
|
|
|
"github.com/docker/docker/pkg/stack"
|
2014-08-05 12:32:24 -04: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 00:24:24 -04: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.
|
2016-05-05 17:03:28 -04:00
|
|
|
// * Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
|
|
|
|
// the docker daemon is not restarted and also running under systemd.
|
|
|
|
// Fixes https://github.com/docker/docker/issues/19728
|
2014-08-05 12:32:24 -04:00
|
|
|
//
|
2017-07-31 23:32:57 -04:00
|
|
|
func Trap(cleanup func(), logger interface {
|
|
|
|
Info(args ...interface{})
|
|
|
|
}) {
|
2014-08-05 12:32:24 -04:00
|
|
|
c := make(chan os.Signal, 1)
|
2016-05-05 17:03:28 -04:00
|
|
|
// we will handle INT, TERM, QUIT, SIGPIPE here
|
|
|
|
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE}
|
2014-08-05 12:32:24 -04:00
|
|
|
gosignal.Notify(c, signals...)
|
|
|
|
go func() {
|
|
|
|
interruptCount := uint32(0)
|
|
|
|
for sig := range c {
|
2016-05-05 17:03:28 -04:00
|
|
|
if sig == syscall.SIGPIPE {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-05 12:32:24 -04:00
|
|
|
go func(sig os.Signal) {
|
2017-07-31 23:32:57 -04:00
|
|
|
logger.Info(fmt.Sprintf("Processing signal '%v'", sig))
|
2014-08-05 12:32:24 -04:00
|
|
|
switch sig {
|
|
|
|
case os.Interrupt, syscall.SIGTERM:
|
|
|
|
if atomic.LoadUint32(&interruptCount) < 3 {
|
|
|
|
// Initiate the cleanup only once
|
2014-12-06 09:42:32 -05:00
|
|
|
if atomic.AddUint32(&interruptCount, 1) == 1 {
|
2015-04-21 00:24:24 -04:00
|
|
|
// Call the provided cleanup handler
|
2014-08-05 12:32:24 -04:00
|
|
|
cleanup()
|
2014-08-13 15:18:23 -04:00
|
|
|
os.Exit(0)
|
2014-08-05 12:32:24 -04:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-21 00:24:24 -04:00
|
|
|
// 3 SIGTERM/INT signals received; force exit without cleanup
|
2017-07-31 23:32:57 -04:00
|
|
|
logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
|
2014-08-05 12:32:24 -04:00
|
|
|
}
|
|
|
|
case syscall.SIGQUIT:
|
2021-07-15 11:33:55 -04:00
|
|
|
stack.Dump()
|
2017-07-31 23:32:57 -04:00
|
|
|
logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
|
2014-08-05 12:32:24 -04:00
|
|
|
}
|
2019-11-27 09:41:47 -05:00
|
|
|
// for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
|
2014-08-05 12:32:24 -04:00
|
|
|
os.Exit(128 + int(sig.(syscall.Signal)))
|
|
|
|
}(sig)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|