AdGuardHome/internal/home/service_linux.go
Eugene Burkov b74b92fc27 Pull request: Improve build tags
Merge in DNS/adguard-home from imp-build-tags to master

Squashed commit of the following:

commit c15793e04c08097835692568a598b8a8d15f57f4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 13 19:25:20 2022 +0300

    home: imp build tags

commit 2b9b68e9fe6942422951f50d90c70143a3509401
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 13 19:23:56 2022 +0300

    version: imp build tags

commit c0ade3d6ae8885c596fc31312360b25fe992d1e4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 13 19:20:48 2022 +0300

    dhcpd: imp build tags

commit 0ca2a73b7c3b721400a0cc6383cc9e60f4961f22
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 13 19:17:22 2022 +0300

    aghos: imp build tags

commit 733a685b24b56153b96d59cb97c174ad322ff841
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 13 19:13:11 2022 +0300

    aghnet: imp build tags
2022-09-13 20:06:23 +03:00

83 lines
2.3 KiB
Go

//go:build linux
package home
import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/kardianos/service"
)
func chooseSystem() {
sys := service.ChosenSystem()
// 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 sys.String() == "unix-systemv" && !aghos.IsOpenWrt() {
service.ChooseSystem(sysvSystem{System: sys})
}
}
// sysvSystem is a wrapper for service.System that wraps the service.Service
// while creating a new one.
//
// TODO(e.burkov): File a PR to github.com/kardianos/service.
type sysvSystem struct {
// System is expected to have an unexported type
// *service.linuxSystemService.
service.System
}
// New returns a wrapped service.Service.
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 {
return s, err
}
return sysvService{
Service: s,
name: c.Name,
}, nil
}
// sysvService is a wrapper for a service.Service that also calls update-rc.d in
// a proper way on installing and uninstalling.
type sysvService struct {
// Service is expected to have an unexported type *service.sysv.
service.Service
// name stores the name of the service to call updating script with it.
name string
}
// Install wraps service.Service.Install call with calling the updating script.
func (svc sysvService) Install() (err error) {
err = svc.Service.Install()
if err != nil {
// Don't wrap an error since it's informative enough as is.
return err
}
_, _, err = aghos.RunCommand("update-rc.d", svc.name, "defaults")
// Don't wrap an error since it's informative enough as is.
return err
}
// Uninstall wraps service.Service.Uninstall call with calling the updating
// script.
func (svc sysvService) Uninstall() (err error) {
err = svc.Service.Uninstall()
if err != nil {
// Don't wrap an error since it's informative enough as is.
return err
}
_, _, err = aghos.RunCommand("update-rc.d", svc.name, "remove")
// Don't wrap an error since it's informative enough as is.
return err
}