sigtrap.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015 Light Code Labs, LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package caddy
  15. import (
  16. "log"
  17. "os"
  18. "os/signal"
  19. "sync"
  20. "github.com/caddyserver/caddy/telemetry"
  21. )
  22. // TrapSignals create signal handlers for all applicable signals for this
  23. // system. If your Go program uses signals, this is a rather invasive
  24. // function; best to implement them yourself in that case. Signals are not
  25. // required for the caddy package to function properly, but this is a
  26. // convenient way to allow the user to control this part of your program.
  27. func TrapSignals() {
  28. trapSignalsCrossPlatform()
  29. trapSignalsPosix()
  30. }
  31. // trapSignalsCrossPlatform captures SIGINT, which triggers forceful
  32. // shutdown that executes shutdown callbacks first. A second interrupt
  33. // signal will exit the process immediately.
  34. func trapSignalsCrossPlatform() {
  35. go func() {
  36. shutdown := make(chan os.Signal, 1)
  37. signal.Notify(shutdown, os.Interrupt)
  38. for i := 0; true; i++ {
  39. <-shutdown
  40. if i > 0 {
  41. log.Println("[INFO] SIGINT: Force quit")
  42. for _, f := range OnProcessExit {
  43. f() // important cleanup actions only
  44. }
  45. os.Exit(2)
  46. }
  47. log.Println("[INFO] SIGINT: Shutting down")
  48. telemetry.AppendUnique("sigtrap", "SIGINT")
  49. go telemetry.StopEmitting() // not guaranteed to finish in time; that's OK (just don't block!)
  50. // important cleanup actions before shutdown callbacks
  51. for _, f := range OnProcessExit {
  52. f()
  53. }
  54. go func() {
  55. os.Exit(executeShutdownCallbacks("SIGINT"))
  56. }()
  57. }
  58. }()
  59. }
  60. // executeShutdownCallbacks executes the shutdown callbacks as initiated
  61. // by signame. It logs any errors and returns the recommended exit status.
  62. // This function is idempotent; subsequent invocations always return 0.
  63. func executeShutdownCallbacks(signame string) (exitCode int) {
  64. shutdownCallbacksOnce.Do(func() {
  65. // execute third-party shutdown hooks
  66. EmitEvent(ShutdownEvent, signame)
  67. errs := allShutdownCallbacks()
  68. if len(errs) > 0 {
  69. for _, err := range errs {
  70. log.Printf("[ERROR] %s shutdown: %v", signame, err)
  71. }
  72. exitCode = 4
  73. }
  74. })
  75. return
  76. }
  77. // allShutdownCallbacks executes all the shutdown callbacks
  78. // for all the instances, and returns all the errors generated
  79. // during their execution. An error executing one shutdown
  80. // callback does not stop execution of others. Only one shutdown
  81. // callback is executed at a time.
  82. func allShutdownCallbacks() []error {
  83. var errs []error
  84. instancesMu.Lock()
  85. for _, inst := range instances {
  86. errs = append(errs, inst.ShutdownCallbacks()...)
  87. }
  88. instancesMu.Unlock()
  89. return errs
  90. }
  91. // shutdownCallbacksOnce ensures that shutdown callbacks
  92. // for all instances are only executed once.
  93. var shutdownCallbacksOnce sync.Once