trap.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package trap // import "github.com/docker/docker/cmd/dockerd/trap"
  2. import (
  3. "fmt"
  4. "os"
  5. gosignal "os/signal"
  6. "sync/atomic"
  7. "syscall"
  8. "github.com/docker/docker/pkg/stack"
  9. )
  10. // Trap sets up a simplified signal "trap", appropriate for common
  11. // behavior expected from a vanilla unix command-line tool in general
  12. // (and the Docker engine in particular).
  13. //
  14. // * If SIGINT or SIGTERM are received, `cleanup` is called, then the process is terminated.
  15. // * If SIGINT or SIGTERM are received 3 times before cleanup is complete, then cleanup is
  16. // skipped and the process is terminated immediately (allows force quit of stuck daemon)
  17. // * A SIGQUIT always causes an exit without cleanup, with a goroutine dump preceding exit.
  18. // * Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
  19. // the docker daemon is not restarted and also running under systemd.
  20. // Fixes https://github.com/docker/docker/issues/19728
  21. //
  22. func Trap(cleanup func(), logger interface {
  23. Info(args ...interface{})
  24. }) {
  25. c := make(chan os.Signal, 1)
  26. // we will handle INT, TERM, QUIT, SIGPIPE here
  27. signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE}
  28. gosignal.Notify(c, signals...)
  29. go func() {
  30. interruptCount := uint32(0)
  31. for sig := range c {
  32. if sig == syscall.SIGPIPE {
  33. continue
  34. }
  35. go func(sig os.Signal) {
  36. logger.Info(fmt.Sprintf("Processing signal '%v'", sig))
  37. switch sig {
  38. case os.Interrupt, syscall.SIGTERM:
  39. if atomic.LoadUint32(&interruptCount) < 3 {
  40. // Initiate the cleanup only once
  41. if atomic.AddUint32(&interruptCount, 1) == 1 {
  42. // Call the provided cleanup handler
  43. cleanup()
  44. os.Exit(0)
  45. } else {
  46. return
  47. }
  48. } else {
  49. // 3 SIGTERM/INT signals received; force exit without cleanup
  50. logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
  51. }
  52. case syscall.SIGQUIT:
  53. stack.Dump()
  54. logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
  55. }
  56. // for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
  57. os.Exit(128 + int(sig.(syscall.Signal)))
  58. }(sig)
  59. }
  60. }()
  61. }