2018-02-27 19:46:50 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrStop should be returned signal handler function
|
|
|
|
// for termination of handling signals.
|
|
|
|
var ErrStop = errors.New("stop serve signals")
|
|
|
|
|
|
|
|
// SignalHandlerFunc is the interface for signal handler functions.
|
|
|
|
type SignalHandlerFunc func(sig os.Signal) (err error)
|
|
|
|
|
2018-03-19 15:51:38 +00:00
|
|
|
// SetSigHandler sets handler for the given signals.
|
2018-02-27 19:46:50 +00:00
|
|
|
// SIGTERM has the default handler, he returns ErrStop.
|
|
|
|
func SetSigHandler(handler SignalHandlerFunc, signals ...os.Signal) {
|
|
|
|
for _, sig := range signals {
|
|
|
|
handlers[sig] = handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 15:51:38 +00:00
|
|
|
// ServeSignals calls handlers for system signals.
|
2018-02-27 19:46:50 +00:00
|
|
|
func ServeSignals() (err error) {
|
|
|
|
signals := make([]os.Signal, 0, len(handlers))
|
2018-03-19 15:51:38 +00:00
|
|
|
for sig := range handlers {
|
2018-02-27 19:46:50 +00:00
|
|
|
signals = append(signals, sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan os.Signal, 8)
|
|
|
|
signal.Notify(ch, signals...)
|
|
|
|
|
|
|
|
for sig := range ch {
|
|
|
|
err = handlers[sig](sig)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.Stop(ch)
|
|
|
|
|
|
|
|
if err == ErrStop {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var handlers = make(map[os.Signal]SignalHandlerFunc)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
handlers[syscall.SIGTERM] = sigtermDefaultHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func sigtermDefaultHandler(sig os.Signal) error {
|
|
|
|
return ErrStop
|
|
|
|
}
|