2014-08-05 12:32:24 -04:00
|
|
|
package signal
|
|
|
|
|
|
|
|
import (
|
2016-11-01 10:32:55 -04:00
|
|
|
"fmt"
|
2014-08-05 12:32:24 -04:00
|
|
|
"os"
|
|
|
|
gosignal "os/signal"
|
2016-06-28 20:12:31 -04:00
|
|
|
"path/filepath"
|
2015-04-21 00:24:24 -04:00
|
|
|
"runtime"
|
2016-10-12 18:29:47 -04:00
|
|
|
"strings"
|
2014-08-05 12:32:24 -04:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2016-06-28 20:12:31 -04:00
|
|
|
"time"
|
2014-09-25 12:55:53 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-11-01 10:32:55 -04:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
//
|
|
|
|
func Trap(cleanup func()) {
|
|
|
|
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) {
|
2015-04-21 00:24:24 -04:00
|
|
|
logrus.Infof("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
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
|
2014-08-05 12:32:24 -04:00
|
|
|
}
|
|
|
|
case syscall.SIGQUIT:
|
2016-06-28 20:12:31 -04:00
|
|
|
DumpStacks("")
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
|
2014-08-05 12:32:24 -04:00
|
|
|
}
|
2015-04-21 00:24:24 -04: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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2015-04-21 00:24:24 -04:00
|
|
|
|
2016-11-01 10:32:55 -04:00
|
|
|
const stacksLogNameTemplate = "goroutine-stacks-%s.log"
|
|
|
|
|
|
|
|
// DumpStacks appends the runtime stack into file in dir and returns full path
|
|
|
|
// to that file.
|
|
|
|
func DumpStacks(dir string) (string, error) {
|
2015-09-04 13:30:14 -04:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
stackSize int
|
|
|
|
)
|
|
|
|
bufferLen := 16384
|
|
|
|
for stackSize == len(buf) {
|
|
|
|
buf = make([]byte, bufferLen)
|
|
|
|
stackSize = runtime.Stack(buf, true)
|
|
|
|
bufferLen *= 2
|
|
|
|
}
|
|
|
|
buf = buf[:stackSize]
|
2016-10-12 18:29:47 -04:00
|
|
|
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
|
2016-11-01 10:32:55 -04:00
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := f.Write(buf); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to write goroutine stacks")
|
2016-06-28 20:12:31 -04:00
|
|
|
}
|
2016-11-01 10:32:55 -04:00
|
|
|
f.Sync()
|
|
|
|
return path, nil
|
2015-04-21 00:24:24 -04:00
|
|
|
}
|