2022-04-26 20:50:09 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-06-29 19:10:39 +03:00
|
|
|
"strconv"
|
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"
|
2023-06-29 19:10:39 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
2022-04-26 20:50:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-04-15 14:23:48 +03:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// signalHandler processes incoming signals and shuts services down.
|
|
|
|
type signalHandler struct {
|
2023-06-29 19:10:39 +03:00
|
|
|
// confMgrConf contains the configuration parameters for the configuration
|
|
|
|
// manager.
|
|
|
|
confMgrConf *configmgr.Config
|
|
|
|
|
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
|
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
// pidFile is the path to the file where to store the PID, if any.
|
|
|
|
pidFile string
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
h.writePID()
|
|
|
|
|
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()
|
2024-04-15 14:23:48 +03:00
|
|
|
} else if osutil.IsShutdownSignal(sig) {
|
2022-10-10 14:05:24 +03:00
|
|
|
status := h.shutdown()
|
2023-06-29 19:10:39 +03:00
|
|
|
h.removePID()
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
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 {
|
2023-06-19 12:21:32 +03:00
|
|
|
log.Info("sighdlr: reconfiguring: exiting with status %d", status)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgr, err := newConfigMgr(h.confMgrConf)
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
web := confMgr.Web()
|
|
|
|
err = web.Start()
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
|
|
|
err = dns.Start()
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
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 (
|
2023-06-29 19:10:39 +03:00
|
|
|
statusSuccess = 0
|
|
|
|
statusError = 1
|
|
|
|
statusArgumentError = 2
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2023-06-13 13:41:13 +03:00
|
|
|
func newSignalHandler(
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgrConf *configmgr.Config,
|
|
|
|
pidFile string,
|
2023-06-13 13:41:13 +03:00
|
|
|
svcs ...agh.Service,
|
|
|
|
) (h *signalHandler) {
|
2022-04-26 20:50:09 +03:00
|
|
|
h = &signalHandler{
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgrConf: confMgrConf,
|
|
|
|
signal: make(chan os.Signal, 1),
|
|
|
|
pidFile: pidFile,
|
|
|
|
services: svcs,
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-04-15 14:23:48 +03:00
|
|
|
notifier := osutil.DefaultSignalNotifier{}
|
|
|
|
osutil.NotifyShutdownSignal(notifier, h.signal)
|
2022-10-10 14:05:24 +03:00
|
|
|
aghos.NotifyReconfigureSignal(h.signal)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
2023-06-29 19:10:39 +03:00
|
|
|
|
|
|
|
// writePID writes the PID to the file, if needed. Any errors are reported to
|
|
|
|
// log.
|
|
|
|
func (h *signalHandler) writePID() {
|
|
|
|
if h.pidFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use 8, since most PIDs will fit.
|
|
|
|
data := make([]byte, 0, 8)
|
|
|
|
data = strconv.AppendInt(data, int64(os.Getpid()), 10)
|
|
|
|
data = append(data, '\n')
|
|
|
|
|
2024-10-29 14:28:59 +03:00
|
|
|
err := aghos.WriteFile(h.pidFile, data, 0o644)
|
2023-06-29 19:10:39 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("sighdlr: writing pidfile: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("sighdlr: wrote pid to %q", h.pidFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removePID removes the PID file, if any.
|
|
|
|
func (h *signalHandler) removePID() {
|
|
|
|
if h.pidFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Remove(h.pidFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("sighdlr: removing pidfile: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("sighdlr: removed pid at %q", h.pidFile)
|
|
|
|
}
|