2015-10-26 23:49:05 +01:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package caddy
|
|
|
|
|
|
|
|
import (
|
2015-10-27 00:57:32 +01:00
|
|
|
"io/ioutil"
|
2015-10-26 23:49:05 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2015-11-15 05:59:43 +01:00
|
|
|
// trapSignalsPosix captures POSIX-only signals.
|
|
|
|
func trapSignalsPosix() {
|
2015-10-26 23:49:05 +01:00
|
|
|
go func() {
|
2015-11-15 04:56:34 +01:00
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1)
|
2015-10-26 23:49:05 +01:00
|
|
|
|
2015-11-15 04:56:34 +01:00
|
|
|
for sig := range sigchan {
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGTERM:
|
|
|
|
log.Println("[INFO] SIGTERM: Terminating process")
|
2016-01-10 05:48:07 +01:00
|
|
|
if PidFile != "" {
|
|
|
|
os.Remove(PidFile)
|
|
|
|
}
|
2015-11-15 04:56:34 +01:00
|
|
|
os.Exit(0)
|
2015-10-27 00:57:32 +01:00
|
|
|
|
2015-11-15 04:56:34 +01:00
|
|
|
case syscall.SIGQUIT:
|
|
|
|
log.Println("[INFO] SIGQUIT: Shutting down")
|
|
|
|
exitCode := executeShutdownCallbacks("SIGQUIT")
|
|
|
|
err := Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] SIGQUIT stop: %v", err)
|
|
|
|
exitCode = 1
|
|
|
|
}
|
2016-01-10 05:48:07 +01:00
|
|
|
if PidFile != "" {
|
|
|
|
os.Remove(PidFile)
|
|
|
|
}
|
2015-11-15 04:56:34 +01:00
|
|
|
os.Exit(exitCode)
|
2015-11-13 07:54:42 +01:00
|
|
|
|
2015-11-15 04:56:34 +01:00
|
|
|
case syscall.SIGHUP:
|
|
|
|
log.Println("[INFO] SIGHUP: Hanging up")
|
|
|
|
err := Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] SIGHUP stop: %v", err)
|
|
|
|
}
|
2015-10-27 00:57:32 +01:00
|
|
|
|
2015-11-15 04:56:34 +01:00
|
|
|
case syscall.SIGUSR1:
|
|
|
|
log.Println("[INFO] SIGUSR1: Reloading")
|
|
|
|
|
|
|
|
var updatedCaddyfile Input
|
|
|
|
|
|
|
|
caddyfileMu.Lock()
|
|
|
|
if caddyfile == nil {
|
|
|
|
// Hmm, did spawing process forget to close stdin? Anyhow, this is unusual.
|
|
|
|
log.Println("[ERROR] SIGUSR1: no Caddyfile to reload (was stdin left open?)")
|
|
|
|
caddyfileMu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if caddyfile.IsFile() {
|
|
|
|
body, err := ioutil.ReadFile(caddyfile.Path())
|
|
|
|
if err == nil {
|
|
|
|
updatedCaddyfile = CaddyfileInput{
|
|
|
|
Filepath: caddyfile.Path(),
|
|
|
|
Contents: body,
|
|
|
|
RealFile: true,
|
|
|
|
}
|
2015-10-27 00:57:32 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-15 04:56:34 +01:00
|
|
|
caddyfileMu.Unlock()
|
2015-10-27 00:57:32 +01:00
|
|
|
|
2015-11-15 04:56:34 +01:00
|
|
|
err := Restart(updatedCaddyfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] SIGUSR1: %v", err)
|
|
|
|
}
|
2015-10-26 23:49:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|