maddy/signal.go
fox.cpp bf188e454f
Move most code from the repo root into subdirectories
The intention is to keep to repo root clean while the list of packages
is slowly growing.

Additionally, a bunch of small (~30 LoC) files in the repo root is
merged into a single maddy.go file, for the same reason.

Most of the internal code is moved into the internal/ directory. Go
toolchain will make it impossible to import these packages from external
applications.

Some packages are renamed and moved into the pkg/ directory in the root.
According to https://github.com/golang-standards/project-layout this is
the de-facto standard to place "library code that's ok to use by
external applications" in.

To clearly define the purpose of top-level directories, README.md files
are added to each.
2019-12-06 01:35:12 +03:00

39 lines
959 B
Go

//+build darwin dragonfly freebsd linux netbsd openbsd solaris
package maddy
import (
"os"
"os/signal"
"syscall"
"github.com/foxcpp/maddy/internal/log"
)
// handleSignals function creates and listens on OS signals channel.
//
// OS-specific signals that correspond to the program termination
// (SIGTERM, SIGHUP, SIGINT) will cause this function to return.
//
// SIGUSR1 will call reinitLogging without returning.
func handleSignals() os.Signal {
sig := make(chan os.Signal, 5)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1)
for {
switch s := <-sig; s {
case syscall.SIGUSR1:
log.Println("SIGUSR1 received, reinitializing logging")
reinitLogging()
default:
go func() {
s := handleSignals()
log.Printf("forced shutdown due to signal (%v)!", s)
os.Exit(1)
}()
log.Printf("signal received (%v), next signal will force immediate shutdown.", s)
return s
}
}
}