From cb27ecd6c02caf2a0a76bf8a66970f966ce86489 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Tue, 14 Jun 2022 14:01:51 +0300 Subject: [PATCH] cherry-pick: more sysv Merge in DNS/adguard-home from 4480-sysv-again to master Updates #4480. Squashed commit of the following: commit 263fa05ab19de95b18fb07f6c89e4b9a1b24657b Merge: 360a6468 d3f39b0a Author: Eugene Burkov Date: Tue Jun 14 13:36:15 2022 +0300 Merge branch 'master' into 4480-sysv-again commit 360a646833ca9e0e01cb6d085e70b898a30dc2d0 Author: Eugene Burkov Date: Thu Jun 9 18:15:41 2022 +0300 home: rename linux file commit c3032533b7e00136c25d15a4ad771bb8a9c13e31 Author: Eugene Burkov Date: Thu Jun 9 18:06:25 2022 +0300 home: imp code commit 2381c4a6ab4f6dca88123ff7b0a92f2cf9a420a8 Author: Eugene Burkov Date: Thu Jun 9 17:48:22 2022 +0300 home: wrap sysv service --- internal/home/service_linux.go | 76 +++++++++++++++++++++++++++++++++ internal/home/service_others.go | 6 ++- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 internal/home/service_linux.go diff --git a/internal/home/service_linux.go b/internal/home/service_linux.go new file mode 100644 index 00000000..f27b7717 --- /dev/null +++ b/internal/home/service_linux.go @@ -0,0 +1,76 @@ +//go:build linux +// +build linux + +package home + +import ( + "github.com/AdguardTeam/AdGuardHome/internal/aghos" + "github.com/kardianos/service" +) + +func chooseSystem() { + if sys := service.ChosenSystem(); sys.String() == "unix-systemv" { + 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 +} diff --git a/internal/home/service_others.go b/internal/home/service_others.go index 83aa63ea..6e2afd10 100644 --- a/internal/home/service_others.go +++ b/internal/home/service_others.go @@ -1,6 +1,8 @@ -//go:build !openbsd -// +build !openbsd +//go:build !(openbsd || linux) +// +build !openbsd,!linux package home +// chooseSystem checks the current system detected and substitutes it with local +// implementation if needed. func chooseSystem() {}