2022-04-26 20:50:09 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-11-13 15:44:21 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
2022-04-26 20:50:09 +03:00
|
|
|
"os"
|
2023-06-29 19:10:39 +03:00
|
|
|
"strconv"
|
2024-11-13 15:44:21 +03:00
|
|
|
"time"
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-06-29 19:10:39 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2024-04-15 14:23:48 +03:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/service"
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// signalHandler processes incoming signals and shuts services down.
|
|
|
|
type signalHandler struct {
|
2024-11-13 15:44:21 +03:00
|
|
|
// logger is used for logging the operation of the signal handler.
|
|
|
|
logger *slog.Logger
|
|
|
|
|
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.
|
2024-11-13 15:44:21 +03:00
|
|
|
services []service.Interface
|
|
|
|
|
|
|
|
// shutdownTimeout is the timeout for the shutdown operation.
|
|
|
|
shutdownTimeout time.Duration
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// handle processes OS signals. It blocks until a termination or a
|
|
|
|
// reconfiguration signal is received, after which it either shuts down all
|
|
|
|
// services or reconfigures them. ctx is used for logging and serves as the
|
|
|
|
// base for the shutdown timeout. status is [osutil.ExitCodeSuccess] on success
|
|
|
|
// and [osutil.ExitCodeFailure] on error.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add reconfiguration logic to golibs.
|
|
|
|
func (h *signalHandler) handle(ctx context.Context) (status osutil.ExitCode) {
|
|
|
|
defer slogutil.RecoverAndLog(ctx, h.logger)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.writePID(ctx)
|
2023-06-29 19:10:39 +03:00
|
|
|
|
2022-04-26 20:50:09 +03:00
|
|
|
for sig := range h.signal {
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.InfoContext(ctx, "received", "signal", sig)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
if osutil.IsReconfigureSignal(sig) {
|
|
|
|
err := h.reconfigure(ctx)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.ErrorContext(ctx, "reconfiguration error", slogutil.KeyError, err)
|
|
|
|
|
|
|
|
return osutil.ExitCodeFailure
|
|
|
|
}
|
2024-04-15 14:23:48 +03:00
|
|
|
} else if osutil.IsShutdownSignal(sig) {
|
2024-11-13 15:44:21 +03:00
|
|
|
status = h.shutdown(ctx)
|
2023-06-29 19:10:39 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.removePID(ctx)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
return status
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
}
|
2024-11-13 15:44:21 +03:00
|
|
|
|
|
|
|
// Shouldn't happen, since h.signal is currently never closed.
|
|
|
|
panic("unexpected close of h.signal")
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// writePID writes the PID to the file, if needed. Any errors are reported to
|
|
|
|
// log.
|
|
|
|
func (h *signalHandler) writePID(ctx context.Context) {
|
|
|
|
if h.pidFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := os.Getpid()
|
|
|
|
data := strconv.AppendInt(nil, int64(pid), 10)
|
|
|
|
data = append(data, '\n')
|
|
|
|
|
|
|
|
err := aghos.WriteFile(h.pidFile, data, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.ErrorContext(ctx, "writing pidfile", slogutil.KeyError, err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
return
|
|
|
|
}
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.DebugContext(ctx, "wrote pid", "file", h.pidFile, "pid", pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconfigure rereads the configuration file and updates and restarts services.
|
|
|
|
func (h *signalHandler) reconfigure(ctx context.Context) (err error) {
|
|
|
|
h.logger.InfoContext(ctx, "reconfiguring started")
|
|
|
|
|
|
|
|
status := h.shutdown(ctx)
|
|
|
|
if status != osutil.ExitCodeSuccess {
|
|
|
|
return errors.Error("shutdown failed")
|
2022-10-10 14:05:24 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// 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
|
2022-10-10 14:05:24 +03:00
|
|
|
// currently not the best.
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
var errs []error
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeoutStart)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
confMgr, err := newConfigMgr(ctx, h.confMgrConf)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("configuration manager: %w", err))
|
|
|
|
}
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
web := confMgr.Web()
|
2024-11-13 15:44:21 +03:00
|
|
|
err = web.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("starting web: %w", err))
|
|
|
|
}
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
2024-11-13 15:44:21 +03:00
|
|
|
err = dns.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("starting dns: %w", err))
|
|
|
|
}
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.services = []service.Interface{
|
2022-10-10 14:05:24 +03:00
|
|
|
dns,
|
|
|
|
web,
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.InfoContext(ctx, "reconfiguring finished")
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// shutdown gracefully shuts down all services.
|
2024-11-13 15:44:21 +03:00
|
|
|
func (h *signalHandler) shutdown(ctx context.Context) (status int) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, h.shutdownTimeout)
|
2022-04-26 20:50:09 +03:00
|
|
|
defer cancel()
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
status = osutil.ExitCodeSuccess
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.InfoContext(ctx, "shutting down")
|
|
|
|
for i, svc := range h.services {
|
|
|
|
err := svc.Shutdown(ctx)
|
2022-04-26 20:50:09 +03:00
|
|
|
if err != nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.ErrorContext(ctx, "shutting down service", "idx", i, slogutil.KeyError, err)
|
|
|
|
status = osutil.ExitCodeFailure
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
return status
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// newSignalHandler returns a new signalHandler that shuts down svcs. logger
|
|
|
|
// and confMgrConf must not be nil.
|
2023-06-13 13:41:13 +03:00
|
|
|
func newSignalHandler(
|
2024-11-13 15:44:21 +03:00
|
|
|
logger *slog.Logger,
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgrConf *configmgr.Config,
|
|
|
|
pidFile string,
|
2024-11-13 15:44:21 +03:00
|
|
|
svcs ...service.Interface,
|
2023-06-13 13:41:13 +03:00
|
|
|
) (h *signalHandler) {
|
2022-04-26 20:50:09 +03:00
|
|
|
h = &signalHandler{
|
2024-11-13 15:44:21 +03:00
|
|
|
logger: logger,
|
|
|
|
confMgrConf: confMgrConf,
|
|
|
|
signal: make(chan os.Signal, 1),
|
|
|
|
pidFile: pidFile,
|
|
|
|
services: svcs,
|
|
|
|
shutdownTimeout: defaultTimeoutShutdown,
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-04-15 14:23:48 +03:00
|
|
|
notifier := osutil.DefaultSignalNotifier{}
|
|
|
|
osutil.NotifyShutdownSignal(notifier, h.signal)
|
2024-11-13 15:44:21 +03:00
|
|
|
osutil.NotifyReconfigureSignal(notifier, h.signal)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
2023-06-29 19:10:39 +03:00
|
|
|
|
|
|
|
// removePID removes the PID file, if any.
|
2024-11-13 15:44:21 +03:00
|
|
|
func (h *signalHandler) removePID(ctx context.Context) {
|
2023-06-29 19:10:39 +03:00
|
|
|
if h.pidFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Remove(h.pidFile)
|
|
|
|
if err != nil {
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.ErrorContext(ctx, "removing pidfile", slogutil.KeyError, err)
|
2023-06-29 19:10:39 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
h.logger.DebugContext(ctx, "removed pidfile", "file", h.pidFile)
|
2023-06-29 19:10:39 +03:00
|
|
|
}
|