sigtrap_posix.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // +build !windows,!plan9,!nacl
  15. package caddy
  16. import (
  17. "log"
  18. "os"
  19. "os/signal"
  20. "syscall"
  21. )
  22. // trapSignalsPosix captures POSIX-only signals.
  23. func trapSignalsPosix() {
  24. go func() {
  25. sigchan := make(chan os.Signal, 1)
  26. signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
  27. for sig := range sigchan {
  28. switch sig {
  29. case syscall.SIGQUIT:
  30. log.Println("[INFO] SIGQUIT: Quitting process immediately")
  31. for _, f := range OnProcessExit {
  32. f() // only perform important cleanup actions
  33. }
  34. os.Exit(0)
  35. case syscall.SIGTERM:
  36. log.Println("[INFO] SIGTERM: Shutting down servers then terminating")
  37. exitCode := executeShutdownCallbacks("SIGTERM")
  38. for _, f := range OnProcessExit {
  39. f() // only perform important cleanup actions
  40. }
  41. err := Stop()
  42. if err != nil {
  43. log.Printf("[ERROR] SIGTERM stop: %v", err)
  44. exitCode = 3
  45. }
  46. os.Exit(exitCode)
  47. case syscall.SIGUSR1:
  48. log.Println("[INFO] SIGUSR1: Reloading")
  49. // Start with the existing Caddyfile
  50. caddyfileToUse, inst, err := getCurrentCaddyfile()
  51. if err != nil {
  52. log.Printf("[ERROR] SIGUSR1: %v", err)
  53. continue
  54. }
  55. if loaderUsed.loader == nil {
  56. // This also should never happen
  57. log.Println("[ERROR] SIGUSR1: no Caddyfile loader with which to reload Caddyfile")
  58. continue
  59. }
  60. // Load the updated Caddyfile
  61. newCaddyfile, err := loaderUsed.loader.Load(inst.serverType)
  62. if err != nil {
  63. log.Printf("[ERROR] SIGUSR1: loading updated Caddyfile: %v", err)
  64. continue
  65. }
  66. if newCaddyfile != nil {
  67. caddyfileToUse = newCaddyfile
  68. }
  69. // Kick off the restart; our work is done
  70. _, err = inst.Restart(caddyfileToUse)
  71. if err != nil {
  72. log.Printf("[ERROR] SIGUSR1: %v", err)
  73. }
  74. case syscall.SIGUSR2:
  75. log.Println("[INFO] SIGUSR2: Upgrading")
  76. if err := Upgrade(); err != nil {
  77. log.Printf("[ERROR] SIGUSR2: upgrading: %v", err)
  78. }
  79. case syscall.SIGHUP:
  80. // ignore; this signal is sometimes sent outside of the user's control
  81. }
  82. }
  83. }()
  84. }