sigtrap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. )
  21. // TrapSignals create signal handlers for all applicable signals for this
  22. // system. If your Go program uses signals, this is a rather invasive
  23. // function; best to implement them yourself in that case. Signals are not
  24. // required for the caddy package to function properly, but this is a
  25. // convenient way to allow the user to control this part of your program.
  26. func TrapSignals() {
  27. trapSignalsCrossPlatform()
  28. trapSignalsPosix()
  29. }
  30. // trapSignalsCrossPlatform captures SIGINT, which triggers forceful
  31. // shutdown that executes shutdown callbacks first. A second interrupt
  32. // signal will exit the process immediately.
  33. func trapSignalsCrossPlatform() {
  34. go func() {
  35. shutdown := make(chan os.Signal, 1)
  36. signal.Notify(shutdown, os.Interrupt)
  37. for i := 0; true; i++ {
  38. <-shutdown
  39. if i > 0 {
  40. log.Println("[INFO] SIGINT: Force quit")
  41. for _, f := range OnProcessExit {
  42. f() // important cleanup actions only
  43. }
  44. os.Exit(2)
  45. }
  46. log.Println("[INFO] SIGINT: Shutting down")
  47. // important cleanup actions before shutdown callbacks
  48. for _, f := range OnProcessExit {
  49. f()
  50. }
  51. go func() {
  52. os.Exit(executeShutdownCallbacks("SIGINT"))
  53. }()
  54. }
  55. }()
  56. }
  57. // executeShutdownCallbacks executes the shutdown callbacks as initiated
  58. // by signame. It logs any errors and returns the recommended exit status.
  59. // This function is idempotent; subsequent invocations always return 0.
  60. func executeShutdownCallbacks(signame string) (exitCode int) {
  61. shutdownCallbacksOnce.Do(func() {
  62. // execute third-party shutdown hooks
  63. EmitEvent(ShutdownEvent, signame)
  64. errs := allShutdownCallbacks()
  65. if len(errs) > 0 {
  66. for _, err := range errs {
  67. log.Printf("[ERROR] %s shutdown: %v", signame, err)
  68. }
  69. exitCode = 4
  70. }
  71. })
  72. return
  73. }
  74. // allShutdownCallbacks executes all the shutdown callbacks
  75. // for all the instances, and returns all the errors generated
  76. // during their execution. An error executing one shutdown
  77. // callback does not stop execution of others. Only one shutdown
  78. // callback is executed at a time.
  79. func allShutdownCallbacks() []error {
  80. var errs []error
  81. instancesMu.Lock()
  82. for _, inst := range instances {
  83. errs = append(errs, inst.ShutdownCallbacks()...)
  84. }
  85. instancesMu.Unlock()
  86. return errs
  87. }
  88. // shutdownCallbacksOnce ensures that shutdown callbacks
  89. // for all instances are only executed once.
  90. var shutdownCallbacksOnce sync.Once