2021-07-15 16:08:17 +00:00
|
|
|
package trap // import "github.com/docker/docker/cmd/dockerd/trap"
|
2014-08-05 16:32:24 +00:00
|
|
|
|
|
|
|
import (
|
2016-11-01 14:32:55 +00:00
|
|
|
"fmt"
|
2014-08-05 16:32:24 +00:00
|
|
|
"os"
|
|
|
|
gosignal "os/signal"
|
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2014-09-25 16:55:53 +00:00
|
|
|
|
2021-07-15 15:33:55 +00:00
|
|
|
"github.com/docker/docker/pkg/stack"
|
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.
|
2016-05-05 21:03:28 +00: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 16:32:24 +00:00
|
|
|
//
|
2017-08-01 03:32:57 +00:00
|
|
|
func Trap(cleanup func(), logger interface {
|
|
|
|
Info(args ...interface{})
|
|
|
|
}) {
|
2014-08-05 16:32:24 +00:00
|
|
|
c := make(chan os.Signal, 1)
|
2016-05-05 21:03:28 +00:00
|
|
|
// we will handle INT, TERM, QUIT, SIGPIPE here
|
|
|
|
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE}
|
2014-08-05 16:32:24 +00:00
|
|
|
gosignal.Notify(c, signals...)
|
|
|
|
go func() {
|
|
|
|
interruptCount := uint32(0)
|
|
|
|
for sig := range c {
|
2016-05-05 21:03:28 +00:00
|
|
|
if sig == syscall.SIGPIPE {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:32:24 +00:00
|
|
|
go func(sig os.Signal) {
|
2017-08-01 03:32:57 +00:00
|
|
|
logger.Info(fmt.Sprintf("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
|
2017-08-01 03:32:57 +00:00
|
|
|
logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
|
2014-08-05 16:32:24 +00:00
|
|
|
}
|
|
|
|
case syscall.SIGQUIT:
|
2021-07-15 15:33:55 +00:00
|
|
|
stack.Dump()
|
2017-08-01 03:32:57 +00:00
|
|
|
logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
|
2014-08-05 16:32:24 +00:00
|
|
|
}
|
2019-11-27 14:41:47 +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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|