2022-04-26 20:50:09 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-10-10 14:05:24 +03:00
|
|
|
"time"
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
2022-04-26 20:50:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// signalHandler processes incoming signals and shuts services down.
|
|
|
|
type signalHandler struct {
|
2022-10-10 14:05:24 +03:00
|
|
|
// signal is the channel to which OS signals are sent.
|
2022-04-26 20:50:09 +03:00
|
|
|
signal chan os.Signal
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// confFile is the path to the configuration file.
|
|
|
|
confFile string
|
|
|
|
|
|
|
|
// start is the time at which AdGuard Home has been started.
|
|
|
|
start time.Time
|
|
|
|
|
|
|
|
// services are the services that are shut down before application exiting.
|
2022-04-26 20:50:09 +03:00
|
|
|
services []agh.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle processes OS signals.
|
|
|
|
func (h *signalHandler) handle() {
|
2022-04-29 14:39:02 +03:00
|
|
|
defer log.OnPanic("signalHandler.handle")
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
for sig := range h.signal {
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Info("sighdlr: received signal %q", sig)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
if aghos.IsReconfigureSignal(sig) {
|
|
|
|
h.reconfigure()
|
|
|
|
} else if aghos.IsShutdownSignal(sig) {
|
|
|
|
status := h.shutdown()
|
|
|
|
log.Info("sighdlr: exiting with status %d", status)
|
|
|
|
|
|
|
|
os.Exit(status)
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// reconfigure rereads the configuration file and updates and restarts services.
|
|
|
|
func (h *signalHandler) reconfigure() {
|
|
|
|
log.Info("sighdlr: reconfiguring adguard home")
|
|
|
|
|
|
|
|
status := h.shutdown()
|
|
|
|
if status != statusSuccess {
|
|
|
|
log.Info("sighdlr: reconfiruging: exiting with status %d", status)
|
|
|
|
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(a.garipov): This is a very rough way to do it. Some services can be
|
|
|
|
// reconfigured without the full shutdown, and the error handling is
|
|
|
|
// currently not the best.
|
|
|
|
|
|
|
|
confMgr, err := configmgr.New(h.confFile, h.start)
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
web := confMgr.Web()
|
|
|
|
err = web.Start()
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
|
|
|
err = dns.Start()
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
h.services = []agh.Service{
|
|
|
|
dns,
|
|
|
|
web,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("sighdlr: successfully reconfigured adguard home")
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
// Exit status constants.
|
|
|
|
const (
|
|
|
|
statusSuccess = 0
|
|
|
|
statusError = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// shutdown gracefully shuts down all services.
|
2022-10-10 14:05:24 +03:00
|
|
|
func (h *signalHandler) shutdown() (status int) {
|
2022-04-26 20:50:09 +03:00
|
|
|
ctx, cancel := ctxWithDefaultTimeout()
|
|
|
|
defer cancel()
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
status = statusSuccess
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Info("sighdlr: shutting down services")
|
2022-04-26 20:50:09 +03:00
|
|
|
for i, service := range h.services {
|
|
|
|
err := service.Shutdown(ctx)
|
|
|
|
if err != nil {
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Error("sighdlr: shutting down service at index %d: %s", i, err)
|
2022-04-26 20:50:09 +03:00
|
|
|
status = statusError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
return status
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newSignalHandler returns a new signalHandler that shuts down svcs.
|
2022-10-10 14:05:24 +03:00
|
|
|
func newSignalHandler(confFile string, start time.Time, svcs ...agh.Service) (h *signalHandler) {
|
2022-04-26 20:50:09 +03:00
|
|
|
h = &signalHandler{
|
|
|
|
signal: make(chan os.Signal, 1),
|
2022-10-10 14:05:24 +03:00
|
|
|
confFile: confFile,
|
|
|
|
start: start,
|
2022-04-26 20:50:09 +03:00
|
|
|
services: svcs,
|
|
|
|
}
|
|
|
|
|
|
|
|
aghos.NotifyShutdownSignal(h.signal)
|
2022-10-10 14:05:24 +03:00
|
|
|
aghos.NotifyReconfigureSignal(h.signal)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|