From 661f4ece48d9e8d4c17847961184f1d93bdfa16f Mon Sep 17 00:00:00 2001
From: Eugene Burkov <e.burkov@adguard.com>
Date: Mon, 27 Dec 2021 19:12:40 +0300
Subject: [PATCH] Pull request: 3868 imp service uninstall

Merge in DNS/adguard-home from 3868-imp-uninstall to master

Closes #3868.
Updates #3457.

Squashed commit of the following:

commit 6f50713407980c27e5b14bef4dc8839e134ec5c8
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Dec 27 19:06:13 2021 +0300

    all: imp openwrt

commit 59f058f8ec7f5ac8cb795bf837c396601652a6ff
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Dec 27 17:26:32 2021 +0300

    all: imp code && docs

commit bab95366b0ffa40d96de5bb8116ec14606e310ed
Merge: 92ebc210 52f36f20
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Dec 27 17:06:25 2021 +0300

    Merge branch 'master' into 3868-imp-uninstall

commit 92ebc210f04d5e02c3eef726017a0d5687f4bc4c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Dec 27 13:18:58 2021 +0300

    home: imp freebsd script & log changes

commit 583ffc256e9f87cf19da2eca8bbefc9e00ea86cc
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 16 14:08:46 2021 +0300

    all: imp service uninstall
---
 CHANGELOG.md                     |  5 ++++-
 internal/home/service.go         | 23 +++++++++++++++--------
 internal/home/service_openbsd.go |  3 +--
 scripts/install.sh               |  1 +
 4 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2cf589c1..f7a8e9f7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,12 +23,14 @@ and this project adheres to
 
 <!--
     TODO(a.garipov): Remove this deprecation, if v0.108.0 is released before
-    that.
+    the Go 1.18 release.
 -->
 - Go 1.17 support.  v0.109.0 will require at least Go 1.18 to build.
 
 ### Fixed
 
+- Service not being stopped before running the `uninstall` service action
+  ([#3868]).
 - Legacy DNS rewrites responding from upstream when a request other than `A` or
   `AAAA` is received ([#4008]).
 - Panic on port availability check during installation ([#3987]).
@@ -38,6 +40,7 @@ and this project adheres to
 - Go 1.16 support.
 
 [#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057
+[#3868]: https://github.com/AdguardTeam/AdGuardHome/issues/3868
 [#3987]: https://github.com/AdguardTeam/AdGuardHome/issues/3987
 [#4008]: https://github.com/AdguardTeam/AdGuardHome/issues/4008
 
diff --git a/internal/home/service.go b/internal/home/service.go
index 4d003584..c2073e6c 100644
--- a/internal/home/service.go
+++ b/internal/home/service.go
@@ -102,9 +102,9 @@ func sendSigReload() {
 		return
 	}
 
-	pidfile := fmt.Sprintf("/var/run/%s.pid", serviceName)
+	pidFile := fmt.Sprintf("/var/run/%s.pid", serviceName)
 	var pid int
-	data, err := os.ReadFile(pidfile)
+	data, err := os.ReadFile(pidFile)
 	if errors.Is(err, os.ErrNotExist) {
 		if pid, err = aghos.PIDByCommand(serviceName, os.Getpid()); err != nil {
 			log.Error("service: finding AdGuardHome process: %s", err)
@@ -112,19 +112,19 @@ func sendSigReload() {
 			return
 		}
 	} else if err != nil {
-		log.Error("service: reading pid file %s: %s", pidfile, err)
+		log.Error("service: reading pid file %s: %s", pidFile, err)
 
 		return
 	} else {
 		parts := strings.SplitN(string(data), "\n", 2)
 		if len(parts) == 0 {
-			log.Error("service: parsing pid file %s: bad value", pidfile)
+			log.Error("service: parsing pid file %s: bad value", pidFile)
 
 			return
 		}
 
 		if pid, err = strconv.Atoi(strings.TrimSpace(parts[0])); err != nil {
-			log.Error("service: parsing pid from file %s: %s", pidfile, err)
+			log.Error("service: parsing pid from file %s: %s", pidFile, err)
 
 			return
 		}
@@ -243,7 +243,7 @@ func handleServiceInstallCommand(s service.Service) {
 
 	if aghos.IsOpenWrt() {
 		// On OpenWrt it is important to run enable after the service
-		// installation Otherwise, the service won't start on the system
+		// installation.  Otherwise, the service won't start on the system
 		// startup.
 		_, err = runInitdCommand("enable")
 		if err != nil {
@@ -279,6 +279,10 @@ func handleServiceUninstallCommand(s service.Service) {
 		}
 	}
 
+	if err := svcAction(s, "stop"); err != nil {
+		log.Debug("service: executing action %q: %s", "stop", err)
+	}
+
 	if err := svcAction(s, "uninstall"); err != nil {
 		log.Fatalf("service: executing action %q: %s", "uninstall", err)
 	}
@@ -341,7 +345,9 @@ func configureService(c *service.Config) {
 // returns command code or error if any
 func runInitdCommand(action string) (int, error) {
 	confPath := "/etc/init.d/" + serviceName
+	// Pass the script and action as a single string argument.
 	code, _, err := aghos.RunCommand("sh", "-c", confPath+" "+action)
+
 	return code, err
 }
 
@@ -579,9 +585,10 @@ const freeBSDScript = `#!/bin/sh
 name="{{.Name}}"
 {{.Name}}_env="IS_DAEMON=1"
 {{.Name}}_user="root"
-pidfile="/var/run/${name}.pid"
+pidfile_child="/var/run/${name}.pid"
+pidfile="/var/run/${name}_daemon.pid"
 command="/usr/sbin/daemon"
-command_args="-p ${pidfile} -f -r {{.WorkingDirectory}}/{{.Name}}"
+command_args="-P ${pidfile} -p ${pidfile_child} -f -r {{.WorkingDirectory}}/{{.Name}}"
 run_rc_command "$1"
 `
 
diff --git a/internal/home/service_openbsd.go b/internal/home/service_openbsd.go
index be715edf..521e6287 100644
--- a/internal/home/service_openbsd.go
+++ b/internal/home/service_openbsd.go
@@ -28,8 +28,7 @@ import (
 //
 // TODO(e.burkov):  Perhaps, file a PR to github.com/kardianos/service.
 
-// sysVersion is the version of local service.System interface
-// implementation.
+// sysVersion is the version of local service.System interface implementation.
 const sysVersion = "openbsd-runcom"
 
 func chooseSystem() {
diff --git a/scripts/install.sh b/scripts/install.sh
index dd0bcdab..078dc34b 100644
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -481,6 +481,7 @@ handle_existing() {
 "to reinstall/uninstall the AdGuard Home using this script specify one of the '-r' or '-u' flags"
 		fi
 
+		# TODO(e.burkov):  Remove the stop once v0.107.1 released.
 		if ( cd "$agh_dir" && ! ./AdGuardHome -s stop || ! ./AdGuardHome -s uninstall )
 		then
 			# It doesn't terminate the script since it is possible