mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2026-12-06 15:11:29 +09:00
Squashed commit of the following: commit 59e53b9e5fe5f0ed342c4c4581f847613e3268d5 Merge: b965a7dcda3866c68eAuthor: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 10:11:29 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-use-executil-run commit b965a7dcdbbed2058ec0f05800a2c5925a8b2766 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 10:08:26 2026 +0700 scripts: imp code commit ea8efa085bd9fd5bacd6b899c052f0197f2ea9af Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Apr 2 08:50:17 2026 +0700 ossvc: imp code commit 2ba0269f9cd13298dd1aaa0e6d9dd9e0c9879df2 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Apr 1 08:32:10 2026 +0700 ossvc: imp tests commit 5141a0a86ae17173a502d4a65315917bc36143b0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 12:33:45 2026 +0700 ossvc: imp tests commit 3ec53df0c200cb59ef0d7841b21aeab9f39bf8ad Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 10:53:23 2026 +0700 ossvc: imp tests commit 3030f92f7f8b745e387bb42e6d9af3f0fd8fbb6b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:19:16 2026 +0700 ossvc: imp code commit 4644f3fe20e0f711da91adf98faf4a88dad932a2 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:18:54 2026 +0700 all: imp code commit 282ee76141dabcaf37df555db41db2c2cc82472d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Mar 31 09:08:54 2026 +0700 osvc: imp tests commit 8e4634570ac71ce4f16fe3796cc64d774c7dc257 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Mar 30 16:14:15 2026 +0700 osvc: imp tests commit 3abade03bed5d45e900c2e579c65eb5cc66990a7 Merge: 56ae2eb4f55d0d6ae5Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Mar 30 11:03:35 2026 +0700 Merge remote-tracking branch 'origin/master' into AGDNS-3826-use-executil-run commit 56ae2eb4f8caea427afb87f46e90f21ae8d9fa39 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Mar 27 10:43:07 2026 +0700 aghnet: imp logs commit 7ead0907d39fab66602ab1d94ec17a477338b371 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:47:23 2026 +0700 all: imp code commit afa235d8acfeb8b4286a26b60dddd61545533761 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:38:30 2026 +0700 all: rm aghos.RunCommand commit bd36b286f26b068e145b5ffe2d2d23fb7c5525fa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:38:21 2026 +0700 ossvc: rm aghos.RunCommand commit bba1e37aa4a34f557cec9653abc2e90816e39054 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 12:13:05 2026 +0700 ossvc: pass cmd cons commit 6cc18f7801d669beb2188f79692a4a590fce2885 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 11:38:21 2026 +0700 scripts: rm aghos.RunCommand commit 37d188db6fa5d5da1138299be549dbc9d00fd1b7 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Mar 26 11:02:46 2026 +0700 aghnet: rm aghos.RunCommand
288 lines
7.9 KiB
Go
288 lines
7.9 KiB
Go
//go:build linux
|
|
|
|
package ossvc
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/osutil/executil"
|
|
"github.com/kardianos/service"
|
|
)
|
|
|
|
// chooseSystem checks the current system detected and substitutes it with local
|
|
// implementation if needed. l and cmdCons must not be nil.
|
|
func chooseSystem(ctx context.Context, l *slog.Logger, cmdCons executil.CommandConstructor) {
|
|
sys := service.ChosenSystem()
|
|
switch sys.String() {
|
|
case "unix-systemv":
|
|
// By default, package service uses the SysV system if it cannot detect
|
|
// anything other, but the update-rc.d fix should not be applied on
|
|
// OpenWrt, so exclude it explicitly.
|
|
//
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/4480 and
|
|
// https://github.com/AdguardTeam/AdGuardHome/issues/4677.
|
|
if !aghos.IsOpenWrt() {
|
|
service.ChooseSystem(&sysvSystem{System: sys, cmdCons: cmdCons})
|
|
l.DebugContext(ctx, "using custom SysV system")
|
|
|
|
return
|
|
}
|
|
case "linux-systemd":
|
|
service.ChooseSystem(&systemdSystem{System: sys, cmdCons: cmdCons})
|
|
l.DebugContext(ctx, "using custom systemd system")
|
|
|
|
return
|
|
}
|
|
|
|
l.DebugContext(ctx, "using default system", "system_description", sys.String())
|
|
}
|
|
|
|
// sysvSystem is a wrapper for a [service.System] that returns the custom
|
|
// implementation of the [service.Service] interface.
|
|
//
|
|
// TODO(e.burkov): File a PR to github.com/kardianos/service.
|
|
type sysvSystem struct {
|
|
// System must have an unexported type *service.linuxSystemService.
|
|
service.System
|
|
|
|
// cmdCons is used to run external commands. It must not be nil.
|
|
cmdCons executil.CommandConstructor
|
|
}
|
|
|
|
// type check
|
|
var _ service.System = (*sysvSystem)(nil)
|
|
|
|
// New implements the [service.System] interface for *sysvSystem. i and c must
|
|
// not be nil.
|
|
func (sys *sysvSystem) New(i service.Interface, c *service.Config) (s service.Service, err error) {
|
|
s, err = sys.System.New(i, c)
|
|
if err != nil {
|
|
// Don't wrap the error to keep it as close to the original one as
|
|
// possible.
|
|
return s, err
|
|
}
|
|
|
|
return &sysvService{
|
|
cmdCons: sys.cmdCons,
|
|
Service: s,
|
|
name: c.Name,
|
|
}, nil
|
|
}
|
|
|
|
// sysvService is a wrapper for a SysV [service.Service] that supplements the
|
|
// installation and uninstallation.
|
|
type sysvService struct {
|
|
// cmdCons is used to run external commands. It must not be nil.
|
|
cmdCons executil.CommandConstructor
|
|
|
|
// Service must have an unexported type *service.sysv.
|
|
service.Service
|
|
|
|
// name stores the name of the service to call updating script with it.
|
|
name string
|
|
}
|
|
|
|
// type check
|
|
var _ service.Service = (*sysvService)(nil)
|
|
|
|
// Install implements the [service.Service] interface for *sysvService.
|
|
func (svc *sysvService) Install() (err error) {
|
|
err = svc.Service.Install()
|
|
if err != nil {
|
|
// Don't wrap the error to keep it as close to the original one as
|
|
// possible.
|
|
return err
|
|
}
|
|
|
|
// TODO(s.chzhen): Pass context.
|
|
return executil.RunWithPeek(
|
|
context.TODO(),
|
|
svc.cmdCons,
|
|
aghos.MaxCmdOutputSize,
|
|
"update-rc.d",
|
|
svc.name,
|
|
"defaults",
|
|
)
|
|
}
|
|
|
|
// Uninstall implements the [service.Service] interface for *sysvService.
|
|
func (svc *sysvService) Uninstall() (err error) {
|
|
err = svc.Service.Uninstall()
|
|
if err != nil {
|
|
// Don't wrap the error to keep it as close to the original one as
|
|
// possible.
|
|
return err
|
|
}
|
|
|
|
// TODO(s.chzhen): Pass context.
|
|
return executil.RunWithPeek(
|
|
context.TODO(),
|
|
svc.cmdCons,
|
|
aghos.MaxCmdOutputSize,
|
|
"update-rc.d",
|
|
svc.name,
|
|
"remove",
|
|
)
|
|
}
|
|
|
|
// systemdSystem is a wrapper for a [service.System] that returns the custom
|
|
// implementation of the [service.Service] interface.
|
|
type systemdSystem struct {
|
|
// System must have an unexported type *service.linuxSystemService.
|
|
service.System
|
|
|
|
// cmdCons is used to run external commands. It must not be nil.
|
|
cmdCons executil.CommandConstructor
|
|
}
|
|
|
|
// type check
|
|
var _ service.System = (*systemdSystem)(nil)
|
|
|
|
// New implements the [service.System] interface for *systemdSystem. i and c
|
|
// must not be nil.
|
|
func (sys *systemdSystem) New(i service.Interface, c *service.Config) (s service.Service, err error) {
|
|
s, err = sys.System.New(i, c)
|
|
if err != nil {
|
|
// Don't wrap the error to keep it as close to the original one as
|
|
// possible.
|
|
return s, err
|
|
}
|
|
|
|
return &systemdService{
|
|
cmdCons: sys.cmdCons,
|
|
Service: s,
|
|
unitName: fmt.Sprintf("%s.service", c.Name),
|
|
}, nil
|
|
}
|
|
|
|
// type check
|
|
var _ service.Service = (*systemdService)(nil)
|
|
|
|
// systemdService is a wrapper for a systemd [service.Service] that enriches the
|
|
// service status information.
|
|
type systemdService struct {
|
|
// cmdCons is used to run external commands. It must not be nil.
|
|
cmdCons executil.CommandConstructor
|
|
|
|
// Service is expected to have an unexported type *service.systemd.
|
|
service.Service
|
|
|
|
// unitName stores the name of the systemd daemon.
|
|
unitName string
|
|
}
|
|
|
|
// type check
|
|
var _ service.Service = (*systemdService)(nil)
|
|
|
|
// Status implements the [service.Service] interface for *systemdService.
|
|
func (s *systemdService) Status() (status service.Status, err error) {
|
|
const systemctlCmd = "systemctl"
|
|
|
|
var (
|
|
systemctlArgs = []string{"show", s.unitName}
|
|
systemctlStdout bytes.Buffer
|
|
)
|
|
|
|
// TODO(s.chzhen): Consider streaming the output if needed. Using
|
|
// [io.Pipe] here is unnecessary; it complicates lifecycle management
|
|
// because the output must be read concurrently, and the PipeWriter must be
|
|
// explicitly closed to signal EOF. Since this command's output is small, a
|
|
// bytes.Buffer via executil.Run is sufficient.
|
|
err = executil.Run(
|
|
// TODO(s.chzhen): Pass context.
|
|
context.TODO(),
|
|
s.cmdCons,
|
|
&executil.CommandConfig{
|
|
Path: systemctlCmd,
|
|
Args: systemctlArgs,
|
|
Stdout: &systemctlStdout,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return service.StatusUnknown, fmt.Errorf("executing command: %w", err)
|
|
}
|
|
|
|
status, err = parseSystemctlShow(&systemctlStdout)
|
|
if err != nil {
|
|
return service.StatusUnknown, fmt.Errorf("parsing command output: %w", err)
|
|
}
|
|
|
|
return status, nil
|
|
}
|
|
|
|
// Searched property names. See man systemctl(1).
|
|
const (
|
|
propNameLoadState = "LoadState"
|
|
propNameActiveState = "ActiveState"
|
|
propNameSubState = "SubState"
|
|
)
|
|
|
|
// parseSystemctlShow parses the output of the systemctl show command. It
|
|
// expects the key=value pairs separated by newlines.
|
|
func parseSystemctlShow(output io.Reader) (status service.Status, err error) {
|
|
var loadState, activeState, subState string
|
|
|
|
scanner := bufio.NewScanner(output)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
propName, propValue, ok := strings.Cut(line, "=")
|
|
if !ok {
|
|
return service.StatusUnknown, fmt.Errorf("unexpected line format: %q", line)
|
|
}
|
|
|
|
switch propName {
|
|
case propNameLoadState:
|
|
loadState = propValue
|
|
case propNameActiveState:
|
|
activeState = propValue
|
|
case propNameSubState:
|
|
subState = propValue
|
|
default:
|
|
// Go on.
|
|
}
|
|
}
|
|
if err = scanner.Err(); err != nil {
|
|
return service.StatusUnknown, err
|
|
}
|
|
|
|
return statusFromState(loadState, activeState, subState)
|
|
}
|
|
|
|
// statusFromState returns the service status based on the systemctl state
|
|
// property values.
|
|
func statusFromState(loadState, activeState, subState string) (status service.Status, err error) {
|
|
// Desired property values. See man systemctl(1).
|
|
const (
|
|
propValueLoadStateNotFound = "not-found"
|
|
propValueActiveStateActive = "active"
|
|
propValueActiveStateInactive = "inactive"
|
|
propValueSubStateAutoRestart = "auto-restart"
|
|
)
|
|
|
|
switch {
|
|
case loadState == propValueLoadStateNotFound:
|
|
return service.StatusUnknown, service.ErrNotInstalled
|
|
case activeState == propValueActiveStateActive:
|
|
return service.StatusRunning, nil
|
|
case activeState == propValueActiveStateInactive:
|
|
return service.StatusStopped, nil
|
|
case subState == propValueSubStateAutoRestart:
|
|
return statusRestartOnFail, nil
|
|
default:
|
|
return service.StatusUnknown, fmt.Errorf(
|
|
"unexpected state: %s=%q, %s=%q, %s=%q",
|
|
propNameLoadState, loadState,
|
|
propNameActiveState, activeState,
|
|
propNameSubState, subState,
|
|
)
|
|
}
|
|
}
|