trap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package signal
  2. import (
  3. "fmt"
  4. "os"
  5. gosignal "os/signal"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "sync/atomic"
  10. "syscall"
  11. "time"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/pkg/errors"
  14. )
  15. // Trap sets up a simplified signal "trap", appropriate for common
  16. // behavior expected from a vanilla unix command-line tool in general
  17. // (and the Docker engine in particular).
  18. //
  19. // * If SIGINT or SIGTERM are received, `cleanup` is called, then the process is terminated.
  20. // * If SIGINT or SIGTERM are received 3 times before cleanup is complete, then cleanup is
  21. // skipped and the process is terminated immediately (allows force quit of stuck daemon)
  22. // * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit.
  23. // * Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
  24. // the docker daemon is not restarted and also running under systemd.
  25. // Fixes https://github.com/docker/docker/issues/19728
  26. //
  27. func Trap(cleanup func()) {
  28. c := make(chan os.Signal, 1)
  29. // we will handle INT, TERM, QUIT, SIGPIPE here
  30. signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE}
  31. gosignal.Notify(c, signals...)
  32. go func() {
  33. interruptCount := uint32(0)
  34. for sig := range c {
  35. if sig == syscall.SIGPIPE {
  36. continue
  37. }
  38. go func(sig os.Signal) {
  39. logrus.Infof("Processing signal '%v'", sig)
  40. switch sig {
  41. case os.Interrupt, syscall.SIGTERM:
  42. if atomic.LoadUint32(&interruptCount) < 3 {
  43. // Initiate the cleanup only once
  44. if atomic.AddUint32(&interruptCount, 1) == 1 {
  45. // Call the provided cleanup handler
  46. cleanup()
  47. os.Exit(0)
  48. } else {
  49. return
  50. }
  51. } else {
  52. // 3 SIGTERM/INT signals received; force exit without cleanup
  53. logrus.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
  54. }
  55. case syscall.SIGQUIT:
  56. DumpStacks("")
  57. logrus.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
  58. }
  59. //for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
  60. os.Exit(128 + int(sig.(syscall.Signal)))
  61. }(sig)
  62. }
  63. }()
  64. }
  65. const stacksLogNameTemplate = "goroutine-stacks-%s.log"
  66. // DumpStacks appends the runtime stack into file in dir and returns full path
  67. // to that file.
  68. func DumpStacks(dir string) (string, error) {
  69. var (
  70. buf []byte
  71. stackSize int
  72. )
  73. bufferLen := 16384
  74. for stackSize == len(buf) {
  75. buf = make([]byte, bufferLen)
  76. stackSize = runtime.Stack(buf, true)
  77. bufferLen *= 2
  78. }
  79. buf = buf[:stackSize]
  80. var f *os.File
  81. if dir != "" {
  82. path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
  83. var err error
  84. f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
  85. if err != nil {
  86. return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
  87. }
  88. defer f.Close()
  89. defer f.Sync()
  90. } else {
  91. f = os.Stderr
  92. }
  93. if _, err := f.Write(buf); err != nil {
  94. return "", errors.Wrap(err, "failed to write goroutine stacks")
  95. }
  96. return f.Name(), nil
  97. }